util.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util.c
  7. * \brief Common functions for strings, IO, network, data structures,
  8. * process control.
  9. **/
  10. #include "orconfig.h"
  11. #ifdef HAVE_FCNTL_H
  12. #include <fcntl.h>
  13. #endif
  14. #define UTIL_PRIVATE
  15. #include "common/util.h"
  16. #include "lib/log/torlog.h"
  17. #include "lib/crypt_ops/crypto_digest.h"
  18. #include "lib/cc/torint.h"
  19. #include "lib/container/smartlist.h"
  20. #include "lib/fdio/fdio.h"
  21. #include "lib/net/address.h"
  22. #include "lib/sandbox/sandbox.h"
  23. #include "lib/err/backtrace.h"
  24. #include "common/util_process.h"
  25. #include "common/util_format.h"
  26. #ifdef _WIN32
  27. #include <io.h>
  28. #include <direct.h>
  29. #include <process.h>
  30. #include <tchar.h>
  31. #include <winbase.h>
  32. #else /* !(defined(_WIN32)) */
  33. #include <dirent.h>
  34. #include <pwd.h>
  35. #include <grp.h>
  36. #endif /* defined(_WIN32) */
  37. /* math.h needs this on Linux */
  38. #ifndef _USE_ISOC99_
  39. #define _USE_ISOC99_ 1
  40. #endif
  41. #include <math.h>
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <signal.h>
  46. #ifdef HAVE_NETINET_IN_H
  47. #include <netinet/in.h>
  48. #endif
  49. #ifdef HAVE_ARPA_INET_H
  50. #include <arpa/inet.h>
  51. #endif
  52. #ifdef HAVE_ERRNO_H
  53. #include <errno.h>
  54. #endif
  55. #ifdef HAVE_SYS_SOCKET_H
  56. #include <sys/socket.h>
  57. #endif
  58. #ifdef HAVE_SYS_TIME_H
  59. #include <sys/time.h>
  60. #endif
  61. #ifdef HAVE_UNISTD_H
  62. #include <unistd.h>
  63. #endif
  64. #ifdef HAVE_SYS_STAT_H
  65. #include <sys/stat.h>
  66. #endif
  67. #ifdef HAVE_SYS_FCNTL_H
  68. #include <sys/fcntl.h>
  69. #endif
  70. #ifdef HAVE_TIME_H
  71. #include <time.h>
  72. #endif
  73. #ifdef HAVE_MALLOC_MALLOC_H
  74. #include <malloc/malloc.h>
  75. #endif
  76. #ifdef HAVE_MALLOC_H
  77. #if !defined(OpenBSD) && !defined(__FreeBSD__)
  78. /* OpenBSD has a malloc.h, but for our purposes, it only exists in order to
  79. * scold us for being so stupid as to autodetect its presence. To be fair,
  80. * they've done this since 1996, when autoconf was only 5 years old. */
  81. #include <malloc.h>
  82. #endif /* !defined(OpenBSD) && !defined(__FreeBSD__) */
  83. #endif /* defined(HAVE_MALLOC_H) */
  84. #ifdef HAVE_MALLOC_NP_H
  85. #include <malloc_np.h>
  86. #endif
  87. #ifdef HAVE_SYS_WAIT_H
  88. #include <sys/wait.h>
  89. #endif
  90. #if defined(HAVE_SYS_PRCTL_H) && defined(__linux__)
  91. #include <sys/prctl.h>
  92. #endif
  93. /* =====
  94. * Memory management
  95. * ===== */
  96. DISABLE_GCC_WARNING(aggregate-return)
  97. /** Call the platform malloc info function, and dump the results to the log at
  98. * level <b>severity</b>. If no such function exists, do nothing. */
  99. void
  100. tor_log_mallinfo(int severity)
  101. {
  102. #ifdef HAVE_MALLINFO
  103. struct mallinfo mi;
  104. memset(&mi, 0, sizeof(mi));
  105. mi = mallinfo();
  106. tor_log(severity, LD_MM,
  107. "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
  108. "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
  109. "keepcost=%d",
  110. mi.arena, mi.ordblks, mi.smblks, mi.hblks,
  111. mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
  112. mi.keepcost);
  113. #else /* !(defined(HAVE_MALLINFO)) */
  114. (void)severity;
  115. #endif /* defined(HAVE_MALLINFO) */
  116. }
  117. ENABLE_GCC_WARNING(aggregate-return)
  118. /* =====
  119. * Math
  120. * ===== */
  121. /**
  122. * Returns the natural logarithm of d base e. We defined this wrapper here so
  123. * to avoid conflicts with old versions of tor_log(), which were named log().
  124. */
  125. double
  126. tor_mathlog(double d)
  127. {
  128. return log(d);
  129. }
  130. /** Return the long integer closest to <b>d</b>. We define this wrapper
  131. * here so that not all users of math.h need to use the right incantations
  132. * to get the c99 functions. */
  133. long
  134. tor_lround(double d)
  135. {
  136. #if defined(HAVE_LROUND)
  137. return lround(d);
  138. #elif defined(HAVE_RINT)
  139. return (long)rint(d);
  140. #else
  141. return (long)(d > 0 ? d + 0.5 : ceil(d - 0.5));
  142. #endif /* defined(HAVE_LROUND) || ... */
  143. }
  144. /** Return the 64-bit integer closest to d. We define this wrapper here so
  145. * that not all users of math.h need to use the right incantations to get the
  146. * c99 functions. */
  147. int64_t
  148. tor_llround(double d)
  149. {
  150. #if defined(HAVE_LLROUND)
  151. return (int64_t)llround(d);
  152. #elif defined(HAVE_RINT)
  153. return (int64_t)rint(d);
  154. #else
  155. return (int64_t)(d > 0 ? d + 0.5 : ceil(d - 0.5));
  156. #endif /* defined(HAVE_LLROUND) || ... */
  157. }
  158. /** Transform a random value <b>p</b> from the uniform distribution in
  159. * [0.0, 1.0[ into a Laplace distributed value with location parameter
  160. * <b>mu</b> and scale parameter <b>b</b>. Truncate the final result
  161. * to be an integer in [INT64_MIN, INT64_MAX]. */
  162. int64_t
  163. sample_laplace_distribution(double mu, double b, double p)
  164. {
  165. double result;
  166. tor_assert(p >= 0.0 && p < 1.0);
  167. /* This is the "inverse cumulative distribution function" from:
  168. * http://en.wikipedia.org/wiki/Laplace_distribution */
  169. if (p <= 0.0) {
  170. /* Avoid taking log(0.0) == -INFINITY, as some processors or compiler
  171. * options can cause the program to trap. */
  172. return INT64_MIN;
  173. }
  174. result = mu - b * (p > 0.5 ? 1.0 : -1.0)
  175. * tor_mathlog(1.0 - 2.0 * fabs(p - 0.5));
  176. return clamp_double_to_int64(result);
  177. }
  178. /** Add random noise between INT64_MIN and INT64_MAX coming from a Laplace
  179. * distribution with mu = 0 and b = <b>delta_f</b>/<b>epsilon</b> to
  180. * <b>signal</b> based on the provided <b>random</b> value in [0.0, 1.0[.
  181. * The epsilon value must be between ]0.0, 1.0]. delta_f must be greater
  182. * than 0. */
  183. int64_t
  184. add_laplace_noise(int64_t signal_, double random_, double delta_f,
  185. double epsilon)
  186. {
  187. int64_t noise;
  188. /* epsilon MUST be between ]0.0, 1.0] */
  189. tor_assert(epsilon > 0.0 && epsilon <= 1.0);
  190. /* delta_f MUST be greater than 0. */
  191. tor_assert(delta_f > 0.0);
  192. /* Just add noise, no further signal */
  193. noise = sample_laplace_distribution(0.0,
  194. delta_f / epsilon,
  195. random_);
  196. /* Clip (signal + noise) to [INT64_MIN, INT64_MAX] */
  197. if (noise > 0 && INT64_MAX - noise < signal_)
  198. return INT64_MAX;
  199. else if (noise < 0 && INT64_MIN - noise > signal_)
  200. return INT64_MIN;
  201. else
  202. return signal_ + noise;
  203. }
  204. /* =====
  205. * String manipulation
  206. * ===== */
  207. /** Return a pointer to a NUL-terminated hexadecimal string encoding
  208. * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
  209. * result does not need to be deallocated, but repeated calls to
  210. * hex_str will trash old results.
  211. */
  212. const char *
  213. hex_str(const char *from, size_t fromlen)
  214. {
  215. static char buf[65];
  216. if (fromlen>(sizeof(buf)-1)/2)
  217. fromlen = (sizeof(buf)-1)/2;
  218. base16_encode(buf,sizeof(buf),from,fromlen);
  219. return buf;
  220. }
  221. /** Compare the value of the string <b>prefix</b> with the start of the
  222. * <b>memlen</b>-byte memory chunk at <b>mem</b>. Return as for strcmp.
  223. *
  224. * [As fast_memcmp(mem, prefix, strlen(prefix)) but returns -1 if memlen is
  225. * less than strlen(prefix).]
  226. */
  227. int
  228. fast_memcmpstart(const void *mem, size_t memlen,
  229. const char *prefix)
  230. {
  231. size_t plen = strlen(prefix);
  232. if (memlen < plen)
  233. return -1;
  234. return fast_memcmp(mem, prefix, plen);
  235. }
  236. /** Return true iff the 'len' bytes at 'mem' are all zero. */
  237. int
  238. tor_mem_is_zero(const char *mem, size_t len)
  239. {
  240. static const char ZERO[] = {
  241. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
  242. };
  243. while (len >= sizeof(ZERO)) {
  244. /* It's safe to use fast_memcmp here, since the very worst thing an
  245. * attacker could learn is how many initial bytes of a secret were zero */
  246. if (fast_memcmp(mem, ZERO, sizeof(ZERO)))
  247. return 0;
  248. len -= sizeof(ZERO);
  249. mem += sizeof(ZERO);
  250. }
  251. /* Deal with leftover bytes. */
  252. if (len)
  253. return fast_memeq(mem, ZERO, len);
  254. return 1;
  255. }
  256. /** Return true iff the DIGEST_LEN bytes in digest are all zero. */
  257. int
  258. tor_digest_is_zero(const char *digest)
  259. {
  260. static const uint8_t ZERO_DIGEST[] = {
  261. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
  262. };
  263. return tor_memeq(digest, ZERO_DIGEST, DIGEST_LEN);
  264. }
  265. /** Return true if <b>string</b> is a valid 'key=[value]' string.
  266. * "value" is optional, to indicate the empty string. Log at logging
  267. * <b>severity</b> if something ugly happens. */
  268. int
  269. string_is_key_value(int severity, const char *string)
  270. {
  271. /* position of equal sign in string */
  272. const char *equal_sign_pos = NULL;
  273. tor_assert(string);
  274. if (strlen(string) < 2) { /* "x=" is shortest args string */
  275. tor_log(severity, LD_GENERAL, "'%s' is too short to be a k=v value.",
  276. escaped(string));
  277. return 0;
  278. }
  279. equal_sign_pos = strchr(string, '=');
  280. if (!equal_sign_pos) {
  281. tor_log(severity, LD_GENERAL, "'%s' is not a k=v value.", escaped(string));
  282. return 0;
  283. }
  284. /* validate that the '=' is not in the beginning of the string. */
  285. if (equal_sign_pos == string) {
  286. tor_log(severity, LD_GENERAL, "'%s' is not a valid k=v value.",
  287. escaped(string));
  288. return 0;
  289. }
  290. return 1;
  291. }
  292. /** Return true if <b>string</b> represents a valid IPv4 adddress in
  293. * 'a.b.c.d' form.
  294. */
  295. int
  296. string_is_valid_ipv4_address(const char *string)
  297. {
  298. struct in_addr addr;
  299. return (tor_inet_pton(AF_INET,string,&addr) == 1);
  300. }
  301. /** Return true if <b>string</b> represents a valid IPv6 address in
  302. * a form that inet_pton() can parse.
  303. */
  304. int
  305. string_is_valid_ipv6_address(const char *string)
  306. {
  307. struct in6_addr addr;
  308. return (tor_inet_pton(AF_INET6,string,&addr) == 1);
  309. }
  310. /** Return true iff <b>string</b> is a valid destination address,
  311. * i.e. either a DNS hostname or IPv4/IPv6 address string.
  312. */
  313. int
  314. string_is_valid_dest(const char *string)
  315. {
  316. char *tmp = NULL;
  317. int retval;
  318. size_t len;
  319. if (string == NULL)
  320. return 0;
  321. len = strlen(string);
  322. if (len == 0)
  323. return 0;
  324. if (string[0] == '[' && string[len - 1] == ']')
  325. string = tmp = tor_strndup(string + 1, len - 2);
  326. retval = string_is_valid_ipv4_address(string) ||
  327. string_is_valid_ipv6_address(string) ||
  328. string_is_valid_nonrfc_hostname(string);
  329. tor_free(tmp);
  330. return retval;
  331. }
  332. /** Return true iff <b>string</b> matches a pattern of DNS names
  333. * that we allow Tor clients to connect to.
  334. *
  335. * Note: This allows certain technically invalid characters ('_') to cope
  336. * with misconfigured zones that have been encountered in the wild.
  337. */
  338. int
  339. string_is_valid_nonrfc_hostname(const char *string)
  340. {
  341. int result = 1;
  342. int has_trailing_dot;
  343. char *last_label;
  344. smartlist_t *components;
  345. if (!string || strlen(string) == 0)
  346. return 0;
  347. if (string_is_valid_ipv4_address(string))
  348. return 0;
  349. components = smartlist_new();
  350. smartlist_split_string(components,string,".",0,0);
  351. if (BUG(smartlist_len(components) == 0))
  352. return 0; // LCOV_EXCL_LINE should be impossible given the earlier checks.
  353. /* Allow a single terminating '.' used rarely to indicate domains
  354. * are FQDNs rather than relative. */
  355. last_label = (char *)smartlist_get(components,
  356. smartlist_len(components) - 1);
  357. has_trailing_dot = (last_label[0] == '\0');
  358. if (has_trailing_dot) {
  359. smartlist_pop_last(components);
  360. tor_free(last_label);
  361. last_label = NULL;
  362. }
  363. SMARTLIST_FOREACH_BEGIN(components, char *, c) {
  364. if ((c[0] == '-') || (*c == '_')) {
  365. result = 0;
  366. break;
  367. }
  368. do {
  369. result = (TOR_ISALNUM(*c) || (*c == '-') || (*c == '_'));
  370. c++;
  371. } while (result && *c);
  372. if (result == 0) {
  373. break;
  374. }
  375. } SMARTLIST_FOREACH_END(c);
  376. SMARTLIST_FOREACH_BEGIN(components, char *, c) {
  377. tor_free(c);
  378. } SMARTLIST_FOREACH_END(c);
  379. smartlist_free(components);
  380. return result;
  381. }
  382. /** Return true iff the DIGEST256_LEN bytes in digest are all zero. */
  383. int
  384. tor_digest256_is_zero(const char *digest)
  385. {
  386. return tor_mem_is_zero(digest, DIGEST256_LEN);
  387. }
  388. /** Return a newly allocated string equal to <b>string</b>, except that every
  389. * character in <b>chars_to_escape</b> is preceded by a backslash. */
  390. char *
  391. tor_escape_str_for_pt_args(const char *string, const char *chars_to_escape)
  392. {
  393. char *new_string = NULL;
  394. char *new_cp = NULL;
  395. size_t length, new_length;
  396. tor_assert(string);
  397. length = strlen(string);
  398. if (!length) /* If we were given the empty string, return the same. */
  399. return tor_strdup("");
  400. /* (new_length > SIZE_MAX) => ((length * 2) + 1 > SIZE_MAX) =>
  401. (length*2 > SIZE_MAX - 1) => (length > (SIZE_MAX - 1)/2) */
  402. if (length > (SIZE_MAX - 1)/2) /* check for overflow */
  403. return NULL;
  404. /* this should be enough even if all characters must be escaped */
  405. new_length = (length * 2) + 1;
  406. new_string = new_cp = tor_malloc(new_length);
  407. while (*string) {
  408. if (strchr(chars_to_escape, *string))
  409. *new_cp++ = '\\';
  410. *new_cp++ = *string++;
  411. }
  412. *new_cp = '\0'; /* NUL-terminate the new string */
  413. return new_string;
  414. }
  415. /* =====
  416. * Time
  417. * ===== */
  418. #define TOR_USEC_PER_SEC 1000000
  419. /** Return the difference between start->tv_sec and end->tv_sec.
  420. * Returns INT64_MAX on overflow and underflow.
  421. */
  422. static int64_t
  423. tv_secdiff_impl(const struct timeval *start, const struct timeval *end)
  424. {
  425. const int64_t s = (int64_t)start->tv_sec;
  426. const int64_t e = (int64_t)end->tv_sec;
  427. /* This may not be the most efficient way of implemeting this check,
  428. * but it's easy to see that it's correct and doesn't overflow */
  429. if (s > 0 && e < INT64_MIN + s) {
  430. /* s is positive: equivalent to e - s < INT64_MIN, but without any
  431. * overflow */
  432. return INT64_MAX;
  433. } else if (s < 0 && e > INT64_MAX + s) {
  434. /* s is negative: equivalent to e - s > INT64_MAX, but without any
  435. * overflow */
  436. return INT64_MAX;
  437. }
  438. return e - s;
  439. }
  440. /** Return the number of microseconds elapsed between *start and *end.
  441. * Returns LONG_MAX on overflow and underflow.
  442. */
  443. long
  444. tv_udiff(const struct timeval *start, const struct timeval *end)
  445. {
  446. /* Sanity check tv_usec */
  447. if (start->tv_usec > TOR_USEC_PER_SEC || start->tv_usec < 0) {
  448. log_warn(LD_GENERAL, "comparing times on microsecond detail with bad "
  449. "start tv_usec: " I64_FORMAT " microseconds",
  450. I64_PRINTF_ARG(start->tv_usec));
  451. return LONG_MAX;
  452. }
  453. if (end->tv_usec > TOR_USEC_PER_SEC || end->tv_usec < 0) {
  454. log_warn(LD_GENERAL, "comparing times on microsecond detail with bad "
  455. "end tv_usec: " I64_FORMAT " microseconds",
  456. I64_PRINTF_ARG(end->tv_usec));
  457. return LONG_MAX;
  458. }
  459. /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
  460. */
  461. int64_t udiff;
  462. const int64_t secdiff = tv_secdiff_impl(start, end);
  463. /* end->tv_usec - start->tv_usec can be up to 1 second either way */
  464. if (secdiff > (int64_t)(LONG_MAX/1000000 - 1) ||
  465. secdiff < (int64_t)(LONG_MIN/1000000 + 1)) {
  466. log_warn(LD_GENERAL, "comparing times on microsecond detail too far "
  467. "apart: " I64_FORMAT " seconds", I64_PRINTF_ARG(secdiff));
  468. return LONG_MAX;
  469. }
  470. /* we'll never get an overflow here, because we check that both usecs are
  471. * between 0 and TV_USEC_PER_SEC. */
  472. udiff = secdiff*1000000 + ((int64_t)end->tv_usec - (int64_t)start->tv_usec);
  473. /* Some compilers are smart enough to work out this is a no-op on L64 */
  474. #if SIZEOF_LONG < 8
  475. if (udiff > (int64_t)LONG_MAX || udiff < (int64_t)LONG_MIN) {
  476. return LONG_MAX;
  477. }
  478. #endif
  479. return (long)udiff;
  480. }
  481. /** Return the number of milliseconds elapsed between *start and *end.
  482. * If the tv_usec difference is 500, rounds away from zero.
  483. * Returns LONG_MAX on overflow and underflow.
  484. */
  485. long
  486. tv_mdiff(const struct timeval *start, const struct timeval *end)
  487. {
  488. /* Sanity check tv_usec */
  489. if (start->tv_usec > TOR_USEC_PER_SEC || start->tv_usec < 0) {
  490. log_warn(LD_GENERAL, "comparing times on millisecond detail with bad "
  491. "start tv_usec: " I64_FORMAT " microseconds",
  492. I64_PRINTF_ARG(start->tv_usec));
  493. return LONG_MAX;
  494. }
  495. if (end->tv_usec > TOR_USEC_PER_SEC || end->tv_usec < 0) {
  496. log_warn(LD_GENERAL, "comparing times on millisecond detail with bad "
  497. "end tv_usec: " I64_FORMAT " microseconds",
  498. I64_PRINTF_ARG(end->tv_usec));
  499. return LONG_MAX;
  500. }
  501. /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
  502. */
  503. int64_t mdiff;
  504. const int64_t secdiff = tv_secdiff_impl(start, end);
  505. /* end->tv_usec - start->tv_usec can be up to 1 second either way, but the
  506. * mdiff calculation may add another temporary second for rounding.
  507. * Whether this actually causes overflow depends on the compiler's constant
  508. * folding and order of operations. */
  509. if (secdiff > (int64_t)(LONG_MAX/1000 - 2) ||
  510. secdiff < (int64_t)(LONG_MIN/1000 + 1)) {
  511. log_warn(LD_GENERAL, "comparing times on millisecond detail too far "
  512. "apart: " I64_FORMAT " seconds", I64_PRINTF_ARG(secdiff));
  513. return LONG_MAX;
  514. }
  515. /* Subtract and round */
  516. mdiff = secdiff*1000 +
  517. /* We add a million usec here to ensure that the result is positive,
  518. * so that the round-towards-zero behavior of the division will give
  519. * the right result for rounding to the nearest msec. Later we subtract
  520. * 1000 in order to get the correct result.
  521. * We'll never get an overflow here, because we check that both usecs are
  522. * between 0 and TV_USEC_PER_SEC. */
  523. ((int64_t)end->tv_usec - (int64_t)start->tv_usec + 500 + 1000000) / 1000
  524. - 1000;
  525. /* Some compilers are smart enough to work out this is a no-op on L64 */
  526. #if SIZEOF_LONG < 8
  527. if (mdiff > (int64_t)LONG_MAX || mdiff < (int64_t)LONG_MIN) {
  528. return LONG_MAX;
  529. }
  530. #endif
  531. return (long)mdiff;
  532. }
  533. /**
  534. * Converts timeval to milliseconds.
  535. */
  536. int64_t
  537. tv_to_msec(const struct timeval *tv)
  538. {
  539. int64_t conv = ((int64_t)tv->tv_sec)*1000L;
  540. /* Round ghetto-style */
  541. conv += ((int64_t)tv->tv_usec+500)/1000L;
  542. return conv;
  543. }
  544. /** Yield true iff <b>y</b> is a leap-year. */
  545. #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
  546. /** Helper: Return the number of leap-days between Jan 1, y1 and Jan 1, y2. */
  547. static int
  548. n_leapdays(int year1, int year2)
  549. {
  550. --year1;
  551. --year2;
  552. return (year2/4 - year1/4) - (year2/100 - year1/100)
  553. + (year2/400 - year1/400);
  554. }
  555. /** Number of days per month in non-leap year; used by tor_timegm and
  556. * parse_rfc1123_time. */
  557. static const int days_per_month[] =
  558. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  559. /** Compute a time_t given a struct tm. The result is given in UTC, and
  560. * does not account for leap seconds. Return 0 on success, -1 on failure.
  561. */
  562. int
  563. tor_timegm(const struct tm *tm, time_t *time_out)
  564. {
  565. /* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
  566. * It's way more brute-force than fiddling with tzset().
  567. *
  568. * We use int64_t rather than time_t to avoid overflow on multiplication on
  569. * platforms with 32-bit time_t. Since year is clipped to INT32_MAX, and
  570. * since 365 * 24 * 60 * 60 is approximately 31 million, it's not possible
  571. * for INT32_MAX years to overflow int64_t when converted to seconds. */
  572. int64_t year, days, hours, minutes, seconds;
  573. int i, invalid_year, dpm;
  574. /* Initialize time_out to 0 for now, to avoid bad usage in case this function
  575. fails and the caller ignores the return value. */
  576. tor_assert(time_out);
  577. *time_out = 0;
  578. /* avoid int overflow on addition */
  579. if (tm->tm_year < INT32_MAX-1900) {
  580. year = tm->tm_year + 1900;
  581. } else {
  582. /* clamp year */
  583. year = INT32_MAX;
  584. }
  585. invalid_year = (year < 1970 || tm->tm_year >= INT32_MAX-1900);
  586. if (tm->tm_mon >= 0 && tm->tm_mon <= 11) {
  587. dpm = days_per_month[tm->tm_mon];
  588. if (tm->tm_mon == 1 && !invalid_year && IS_LEAPYEAR(tm->tm_year)) {
  589. dpm = 29;
  590. }
  591. } else {
  592. /* invalid month - default to 0 days per month */
  593. dpm = 0;
  594. }
  595. if (invalid_year ||
  596. tm->tm_mon < 0 || tm->tm_mon > 11 ||
  597. tm->tm_mday < 1 || tm->tm_mday > dpm ||
  598. tm->tm_hour < 0 || tm->tm_hour > 23 ||
  599. tm->tm_min < 0 || tm->tm_min > 59 ||
  600. tm->tm_sec < 0 || tm->tm_sec > 60) {
  601. log_warn(LD_BUG, "Out-of-range argument to tor_timegm");
  602. return -1;
  603. }
  604. days = 365 * (year-1970) + n_leapdays(1970,(int)year);
  605. for (i = 0; i < tm->tm_mon; ++i)
  606. days += days_per_month[i];
  607. if (tm->tm_mon > 1 && IS_LEAPYEAR(year))
  608. ++days;
  609. days += tm->tm_mday - 1;
  610. hours = days*24 + tm->tm_hour;
  611. minutes = hours*60 + tm->tm_min;
  612. seconds = minutes*60 + tm->tm_sec;
  613. /* Check that "seconds" will fit in a time_t. On platforms where time_t is
  614. * 32-bit, this check will fail for dates in and after 2038.
  615. *
  616. * We already know that "seconds" can't be negative because "year" >= 1970 */
  617. #if SIZEOF_TIME_T < 8
  618. if (seconds < TIME_MIN || seconds > TIME_MAX) {
  619. log_warn(LD_BUG, "Result does not fit in tor_timegm");
  620. return -1;
  621. }
  622. #endif /* SIZEOF_TIME_T < 8 */
  623. *time_out = (time_t)seconds;
  624. return 0;
  625. }
  626. /* strftime is locale-specific, so we need to replace those parts */
  627. /** A c-locale array of 3-letter names of weekdays, starting with Sun. */
  628. static const char *WEEKDAY_NAMES[] =
  629. { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  630. /** A c-locale array of 3-letter names of months, starting with Jan. */
  631. static const char *MONTH_NAMES[] =
  632. { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  633. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  634. /** Set <b>buf</b> to the RFC1123 encoding of the UTC value of <b>t</b>.
  635. * The buffer must be at least RFC1123_TIME_LEN+1 bytes long.
  636. *
  637. * (RFC1123 format is "Fri, 29 Sep 2006 15:54:20 GMT". Note the "GMT"
  638. * rather than "UTC".)
  639. */
  640. void
  641. format_rfc1123_time(char *buf, time_t t)
  642. {
  643. struct tm tm;
  644. tor_gmtime_r(&t, &tm);
  645. strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", &tm);
  646. tor_assert(tm.tm_wday >= 0);
  647. tor_assert(tm.tm_wday <= 6);
  648. memcpy(buf, WEEKDAY_NAMES[tm.tm_wday], 3);
  649. tor_assert(tm.tm_mon >= 0);
  650. tor_assert(tm.tm_mon <= 11);
  651. memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
  652. }
  653. /** Parse the (a subset of) the RFC1123 encoding of some time (in UTC) from
  654. * <b>buf</b>, and store the result in *<b>t</b>.
  655. *
  656. * Note that we only accept the subset generated by format_rfc1123_time above,
  657. * not the full range of formats suggested by RFC 1123.
  658. *
  659. * Return 0 on success, -1 on failure.
  660. */
  661. int
  662. parse_rfc1123_time(const char *buf, time_t *t)
  663. {
  664. struct tm tm;
  665. char month[4];
  666. char weekday[4];
  667. int i, m, invalid_year;
  668. unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
  669. unsigned dpm;
  670. if (strlen(buf) != RFC1123_TIME_LEN)
  671. return -1;
  672. memset(&tm, 0, sizeof(tm));
  673. if (tor_sscanf(buf, "%3s, %2u %3s %u %2u:%2u:%2u GMT", weekday,
  674. &tm_mday, month, &tm_year, &tm_hour,
  675. &tm_min, &tm_sec) < 7) {
  676. char *esc = esc_for_log(buf);
  677. log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
  678. tor_free(esc);
  679. return -1;
  680. }
  681. m = -1;
  682. for (i = 0; i < 12; ++i) {
  683. if (!strcmp(month, MONTH_NAMES[i])) {
  684. m = i;
  685. break;
  686. }
  687. }
  688. if (m<0) {
  689. char *esc = esc_for_log(buf);
  690. log_warn(LD_GENERAL, "Got invalid RFC1123 time %s: No such month", esc);
  691. tor_free(esc);
  692. return -1;
  693. }
  694. tm.tm_mon = m;
  695. invalid_year = (tm_year >= INT32_MAX || tm_year < 1970);
  696. tor_assert(m >= 0 && m <= 11);
  697. dpm = days_per_month[m];
  698. if (m == 1 && !invalid_year && IS_LEAPYEAR(tm_year)) {
  699. dpm = 29;
  700. }
  701. if (invalid_year || tm_mday < 1 || tm_mday > dpm ||
  702. tm_hour > 23 || tm_min > 59 || tm_sec > 60) {
  703. char *esc = esc_for_log(buf);
  704. log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
  705. tor_free(esc);
  706. return -1;
  707. }
  708. tm.tm_mday = (int)tm_mday;
  709. tm.tm_year = (int)tm_year;
  710. tm.tm_hour = (int)tm_hour;
  711. tm.tm_min = (int)tm_min;
  712. tm.tm_sec = (int)tm_sec;
  713. if (tm.tm_year < 1970) {
  714. /* LCOV_EXCL_START
  715. * XXXX I think this is dead code; we already checked for
  716. * invalid_year above. */
  717. tor_assert_nonfatal_unreached();
  718. char *esc = esc_for_log(buf);
  719. log_warn(LD_GENERAL,
  720. "Got invalid RFC1123 time %s. (Before 1970)", esc);
  721. tor_free(esc);
  722. return -1;
  723. /* LCOV_EXCL_STOP */
  724. }
  725. tm.tm_year -= 1900;
  726. return tor_timegm(&tm, t);
  727. }
  728. /** Set <b>buf</b> to the ISO8601 encoding of the local value of <b>t</b>.
  729. * The buffer must be at least ISO_TIME_LEN+1 bytes long.
  730. *
  731. * (ISO8601 format is 2006-10-29 10:57:20)
  732. */
  733. void
  734. format_local_iso_time(char *buf, time_t t)
  735. {
  736. struct tm tm;
  737. strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t, &tm));
  738. }
  739. /** Set <b>buf</b> to the ISO8601 encoding of the GMT value of <b>t</b>.
  740. * The buffer must be at least ISO_TIME_LEN+1 bytes long.
  741. */
  742. void
  743. format_iso_time(char *buf, time_t t)
  744. {
  745. struct tm tm;
  746. strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
  747. }
  748. /** As format_local_iso_time, but use the yyyy-mm-ddThh:mm:ss format to avoid
  749. * embedding an internal space. */
  750. void
  751. format_local_iso_time_nospace(char *buf, time_t t)
  752. {
  753. format_local_iso_time(buf, t);
  754. buf[10] = 'T';
  755. }
  756. /** As format_iso_time, but use the yyyy-mm-ddThh:mm:ss format to avoid
  757. * embedding an internal space. */
  758. void
  759. format_iso_time_nospace(char *buf, time_t t)
  760. {
  761. format_iso_time(buf, t);
  762. buf[10] = 'T';
  763. }
  764. /** As format_iso_time_nospace, but include microseconds in decimal
  765. * fixed-point format. Requires that buf be at least ISO_TIME_USEC_LEN+1
  766. * bytes long. */
  767. void
  768. format_iso_time_nospace_usec(char *buf, const struct timeval *tv)
  769. {
  770. tor_assert(tv);
  771. format_iso_time_nospace(buf, (time_t)tv->tv_sec);
  772. tor_snprintf(buf+ISO_TIME_LEN, 8, ".%06d", (int)tv->tv_usec);
  773. }
  774. /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>,
  775. * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on
  776. * failure. Ignore extraneous stuff in <b>cp</b> after the end of the time
  777. * string, unless <b>strict</b> is set. If <b>nospace</b> is set,
  778. * expect the YYYY-MM-DDTHH:MM:SS format. */
  779. int
  780. parse_iso_time_(const char *cp, time_t *t, int strict, int nospace)
  781. {
  782. struct tm st_tm;
  783. unsigned int year=0, month=0, day=0, hour=0, minute=0, second=0;
  784. int n_fields;
  785. char extra_char, separator_char;
  786. n_fields = tor_sscanf(cp, "%u-%2u-%2u%c%2u:%2u:%2u%c",
  787. &year, &month, &day,
  788. &separator_char,
  789. &hour, &minute, &second, &extra_char);
  790. if (strict ? (n_fields != 7) : (n_fields < 7)) {
  791. char *esc = esc_for_log(cp);
  792. log_warn(LD_GENERAL, "ISO time %s was unparseable", esc);
  793. tor_free(esc);
  794. return -1;
  795. }
  796. if (separator_char != (nospace ? 'T' : ' ')) {
  797. char *esc = esc_for_log(cp);
  798. log_warn(LD_GENERAL, "ISO time %s was unparseable", esc);
  799. tor_free(esc);
  800. return -1;
  801. }
  802. if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
  803. hour > 23 || minute > 59 || second > 60 || year >= INT32_MAX) {
  804. char *esc = esc_for_log(cp);
  805. log_warn(LD_GENERAL, "ISO time %s was nonsensical", esc);
  806. tor_free(esc);
  807. return -1;
  808. }
  809. st_tm.tm_year = (int)year-1900;
  810. st_tm.tm_mon = month-1;
  811. st_tm.tm_mday = day;
  812. st_tm.tm_hour = hour;
  813. st_tm.tm_min = minute;
  814. st_tm.tm_sec = second;
  815. st_tm.tm_wday = 0; /* Should be ignored. */
  816. if (st_tm.tm_year < 70) {
  817. /* LCOV_EXCL_START
  818. * XXXX I think this is dead code; we already checked for
  819. * year < 1970 above. */
  820. tor_assert_nonfatal_unreached();
  821. char *esc = esc_for_log(cp);
  822. log_warn(LD_GENERAL, "Got invalid ISO time %s. (Before 1970)", esc);
  823. tor_free(esc);
  824. return -1;
  825. /* LCOV_EXCL_STOP */
  826. }
  827. return tor_timegm(&st_tm, t);
  828. }
  829. /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>,
  830. * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on
  831. * failure. Reject the string if any characters are present after the time.
  832. */
  833. int
  834. parse_iso_time(const char *cp, time_t *t)
  835. {
  836. return parse_iso_time_(cp, t, 1, 0);
  837. }
  838. /**
  839. * As parse_iso_time, but parses a time encoded by format_iso_time_nospace().
  840. */
  841. int
  842. parse_iso_time_nospace(const char *cp, time_t *t)
  843. {
  844. return parse_iso_time_(cp, t, 1, 1);
  845. }
  846. /** Given a <b>date</b> in one of the three formats allowed by HTTP (ugh),
  847. * parse it into <b>tm</b>. Return 0 on success, negative on failure. */
  848. int
  849. parse_http_time(const char *date, struct tm *tm)
  850. {
  851. const char *cp;
  852. char month[4];
  853. char wkday[4];
  854. int i;
  855. unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
  856. tor_assert(tm);
  857. memset(tm, 0, sizeof(*tm));
  858. /* First, try RFC1123 or RFC850 format: skip the weekday. */
  859. if ((cp = strchr(date, ','))) {
  860. ++cp;
  861. if (*cp != ' ')
  862. return -1;
  863. ++cp;
  864. if (tor_sscanf(cp, "%2u %3s %4u %2u:%2u:%2u GMT",
  865. &tm_mday, month, &tm_year,
  866. &tm_hour, &tm_min, &tm_sec) == 6) {
  867. /* rfc1123-date */
  868. tm_year -= 1900;
  869. } else if (tor_sscanf(cp, "%2u-%3s-%2u %2u:%2u:%2u GMT",
  870. &tm_mday, month, &tm_year,
  871. &tm_hour, &tm_min, &tm_sec) == 6) {
  872. /* rfc850-date */
  873. } else {
  874. return -1;
  875. }
  876. } else {
  877. /* No comma; possibly asctime() format. */
  878. if (tor_sscanf(date, "%3s %3s %2u %2u:%2u:%2u %4u",
  879. wkday, month, &tm_mday,
  880. &tm_hour, &tm_min, &tm_sec, &tm_year) == 7) {
  881. tm_year -= 1900;
  882. } else {
  883. return -1;
  884. }
  885. }
  886. tm->tm_mday = (int)tm_mday;
  887. tm->tm_year = (int)tm_year;
  888. tm->tm_hour = (int)tm_hour;
  889. tm->tm_min = (int)tm_min;
  890. tm->tm_sec = (int)tm_sec;
  891. tm->tm_wday = 0; /* Leave this unset. */
  892. month[3] = '\0';
  893. /* Okay, now decode the month. */
  894. /* set tm->tm_mon to dummy value so the check below fails. */
  895. tm->tm_mon = -1;
  896. for (i = 0; i < 12; ++i) {
  897. if (!strcasecmp(MONTH_NAMES[i], month)) {
  898. tm->tm_mon = i;
  899. }
  900. }
  901. if (tm->tm_year < 0 ||
  902. tm->tm_mon < 0 || tm->tm_mon > 11 ||
  903. tm->tm_mday < 1 || tm->tm_mday > 31 ||
  904. tm->tm_hour < 0 || tm->tm_hour > 23 ||
  905. tm->tm_min < 0 || tm->tm_min > 59 ||
  906. tm->tm_sec < 0 || tm->tm_sec > 60)
  907. return -1; /* Out of range, or bad month. */
  908. return 0;
  909. }
  910. /** Given an <b>interval</b> in seconds, try to write it to the
  911. * <b>out_len</b>-byte buffer in <b>out</b> in a human-readable form.
  912. * Returns a non-negative integer on success, -1 on failure.
  913. */
  914. int
  915. format_time_interval(char *out, size_t out_len, long interval)
  916. {
  917. /* We only report seconds if there's no hours. */
  918. long sec = 0, min = 0, hour = 0, day = 0;
  919. /* -LONG_MIN is LONG_MAX + 1, which causes signed overflow */
  920. if (interval < -LONG_MAX)
  921. interval = LONG_MAX;
  922. else if (interval < 0)
  923. interval = -interval;
  924. if (interval >= 86400) {
  925. day = interval / 86400;
  926. interval %= 86400;
  927. }
  928. if (interval >= 3600) {
  929. hour = interval / 3600;
  930. interval %= 3600;
  931. }
  932. if (interval >= 60) {
  933. min = interval / 60;
  934. interval %= 60;
  935. }
  936. sec = interval;
  937. if (day) {
  938. return tor_snprintf(out, out_len, "%ld days, %ld hours, %ld minutes",
  939. day, hour, min);
  940. } else if (hour) {
  941. return tor_snprintf(out, out_len, "%ld hours, %ld minutes", hour, min);
  942. } else if (min) {
  943. return tor_snprintf(out, out_len, "%ld minutes, %ld seconds", min, sec);
  944. } else {
  945. return tor_snprintf(out, out_len, "%ld seconds", sec);
  946. }
  947. }
  948. /* =====
  949. * File helpers
  950. * ===== */
  951. /*
  952. * Filesystem operations.
  953. */
  954. #define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
  955. /** Given a c-style double-quoted escaped string in <b>s</b>, extract and
  956. * decode its contents into a newly allocated string. On success, assign this
  957. * string to *<b>result</b>, assign its length to <b>size_out</b> (if
  958. * provided), and return a pointer to the position in <b>s</b> immediately
  959. * after the string. On failure, return NULL.
  960. */
  961. const char *
  962. unescape_string(const char *s, char **result, size_t *size_out)
  963. {
  964. const char *cp;
  965. char *out;
  966. if (s[0] != '\"')
  967. return NULL;
  968. cp = s+1;
  969. while (1) {
  970. switch (*cp) {
  971. case '\0':
  972. case '\n':
  973. return NULL;
  974. case '\"':
  975. goto end_of_loop;
  976. case '\\':
  977. if (cp[1] == 'x' || cp[1] == 'X') {
  978. if (!(TOR_ISXDIGIT(cp[2]) && TOR_ISXDIGIT(cp[3])))
  979. return NULL;
  980. cp += 4;
  981. } else if (TOR_ISODIGIT(cp[1])) {
  982. cp += 2;
  983. if (TOR_ISODIGIT(*cp)) ++cp;
  984. if (TOR_ISODIGIT(*cp)) ++cp;
  985. } else if (cp[1] == 'n' || cp[1] == 'r' || cp[1] == 't' || cp[1] == '"'
  986. || cp[1] == '\\' || cp[1] == '\'') {
  987. cp += 2;
  988. } else {
  989. return NULL;
  990. }
  991. break;
  992. default:
  993. ++cp;
  994. break;
  995. }
  996. }
  997. end_of_loop:
  998. out = *result = tor_malloc(cp-s + 1);
  999. cp = s+1;
  1000. while (1) {
  1001. switch (*cp)
  1002. {
  1003. case '\"':
  1004. *out = '\0';
  1005. if (size_out) *size_out = out - *result;
  1006. return cp+1;
  1007. /* LCOV_EXCL_START -- we caught this in parse_config_from_line. */
  1008. case '\0':
  1009. tor_fragile_assert();
  1010. tor_free(*result);
  1011. return NULL;
  1012. /* LCOV_EXCL_STOP */
  1013. case '\\':
  1014. switch (cp[1])
  1015. {
  1016. case 'n': *out++ = '\n'; cp += 2; break;
  1017. case 'r': *out++ = '\r'; cp += 2; break;
  1018. case 't': *out++ = '\t'; cp += 2; break;
  1019. case 'x': case 'X':
  1020. {
  1021. int x1, x2;
  1022. x1 = hex_decode_digit(cp[2]);
  1023. x2 = hex_decode_digit(cp[3]);
  1024. if (x1 == -1 || x2 == -1) {
  1025. /* LCOV_EXCL_START */
  1026. /* we caught this above in the initial loop. */
  1027. tor_assert_nonfatal_unreached();
  1028. tor_free(*result);
  1029. return NULL;
  1030. /* LCOV_EXCL_STOP */
  1031. }
  1032. *out++ = ((x1<<4) + x2);
  1033. cp += 4;
  1034. }
  1035. break;
  1036. case '0': case '1': case '2': case '3': case '4': case '5':
  1037. case '6': case '7':
  1038. {
  1039. int n = cp[1]-'0';
  1040. cp += 2;
  1041. if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
  1042. if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
  1043. if (n > 255) { tor_free(*result); return NULL; }
  1044. *out++ = (char)n;
  1045. }
  1046. break;
  1047. case '\'':
  1048. case '\"':
  1049. case '\\':
  1050. case '\?':
  1051. *out++ = cp[1];
  1052. cp += 2;
  1053. break;
  1054. /* LCOV_EXCL_START */
  1055. default:
  1056. /* we caught this above in the initial loop. */
  1057. tor_assert_nonfatal_unreached();
  1058. tor_free(*result); return NULL;
  1059. /* LCOV_EXCL_STOP */
  1060. }
  1061. break;
  1062. default:
  1063. *out++ = *cp++;
  1064. }
  1065. }
  1066. }
  1067. /* =====
  1068. * Process helpers
  1069. * ===== */
  1070. #ifndef _WIN32
  1071. /* Based on code contributed by christian grothoff */
  1072. /** True iff we've called start_daemon(). */
  1073. static int start_daemon_called = 0;
  1074. /** True iff we've called finish_daemon(). */
  1075. static int finish_daemon_called = 0;
  1076. /** Socketpair used to communicate between parent and child process while
  1077. * daemonizing. */
  1078. static int daemon_filedes[2];
  1079. /** Start putting the process into daemon mode: fork and drop all resources
  1080. * except standard fds. The parent process never returns, but stays around
  1081. * until finish_daemon is called. (Note: it's safe to call this more
  1082. * than once: calls after the first are ignored.)
  1083. */
  1084. void
  1085. start_daemon(void)
  1086. {
  1087. pid_t pid;
  1088. if (start_daemon_called)
  1089. return;
  1090. start_daemon_called = 1;
  1091. if (pipe(daemon_filedes)) {
  1092. /* LCOV_EXCL_START */
  1093. log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno));
  1094. exit(1); // exit ok: during daemonize, pipe failed.
  1095. /* LCOV_EXCL_STOP */
  1096. }
  1097. pid = fork();
  1098. if (pid < 0) {
  1099. /* LCOV_EXCL_START */
  1100. log_err(LD_GENERAL,"fork failed. Exiting.");
  1101. exit(1); // exit ok: during daemonize, fork failed
  1102. /* LCOV_EXCL_STOP */
  1103. }
  1104. if (pid) { /* Parent */
  1105. int ok;
  1106. char c;
  1107. close(daemon_filedes[1]); /* we only read */
  1108. ok = -1;
  1109. while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
  1110. if (c == '.')
  1111. ok = 1;
  1112. }
  1113. fflush(stdout);
  1114. if (ok == 1)
  1115. exit(0); // exit ok: during daemonize, daemonizing.
  1116. else
  1117. exit(1); /* child reported error. exit ok: daemonize failed. */
  1118. } else { /* Child */
  1119. close(daemon_filedes[0]); /* we only write */
  1120. (void) setsid(); /* Detach from controlling terminal */
  1121. /*
  1122. * Fork one more time, so the parent (the session group leader) can exit.
  1123. * This means that we, as a non-session group leader, can never regain a
  1124. * controlling terminal. This part is recommended by Stevens's
  1125. * _Advanced Programming in the Unix Environment_.
  1126. */
  1127. if (fork() != 0) {
  1128. exit(0); // exit ok: during daemonize, fork failed (2)
  1129. }
  1130. set_main_thread(); /* We are now the main thread. */
  1131. return;
  1132. }
  1133. }
  1134. /** Finish putting the process into daemon mode: drop standard fds, and tell
  1135. * the parent process to exit. (Note: it's safe to call this more than once:
  1136. * calls after the first are ignored. Calls start_daemon first if it hasn't
  1137. * been called already.)
  1138. */
  1139. void
  1140. finish_daemon(const char *desired_cwd)
  1141. {
  1142. int nullfd;
  1143. char c = '.';
  1144. if (finish_daemon_called)
  1145. return;
  1146. if (!start_daemon_called)
  1147. start_daemon();
  1148. finish_daemon_called = 1;
  1149. if (!desired_cwd)
  1150. desired_cwd = "/";
  1151. /* Don't hold the wrong FS mounted */
  1152. if (chdir(desired_cwd) < 0) {
  1153. log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
  1154. exit(1); // exit ok: during daemonize, chdir failed.
  1155. }
  1156. nullfd = tor_open_cloexec("/dev/null", O_RDWR, 0);
  1157. if (nullfd < 0) {
  1158. /* LCOV_EXCL_START */
  1159. log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
  1160. exit(1); // exit ok: during daemonize, couldn't open /dev/null
  1161. /* LCOV_EXCL_STOP */
  1162. }
  1163. /* close fds linking to invoking terminal, but
  1164. * close usual incoming fds, but redirect them somewhere
  1165. * useful so the fds don't get reallocated elsewhere.
  1166. */
  1167. if (dup2(nullfd,0) < 0 ||
  1168. dup2(nullfd,1) < 0 ||
  1169. dup2(nullfd,2) < 0) {
  1170. /* LCOV_EXCL_START */
  1171. log_err(LD_GENERAL,"dup2 failed. Exiting.");
  1172. exit(1); // exit ok: during daemonize, dup2 failed.
  1173. /* LCOV_EXCL_STOP */
  1174. }
  1175. if (nullfd > 2)
  1176. close(nullfd);
  1177. /* signal success */
  1178. if (write(daemon_filedes[1], &c, sizeof(char)) != sizeof(char)) {
  1179. log_err(LD_GENERAL,"write failed. Exiting.");
  1180. }
  1181. close(daemon_filedes[1]);
  1182. }
  1183. #else /* !(!defined(_WIN32)) */
  1184. /* defined(_WIN32) */
  1185. void
  1186. start_daemon(void)
  1187. {
  1188. }
  1189. void
  1190. finish_daemon(const char *cp)
  1191. {
  1192. (void)cp;
  1193. }
  1194. #endif /* !defined(_WIN32) */
  1195. /** Write the current process ID, followed by NL, into <b>filename</b>.
  1196. * Return 0 on success, -1 on failure.
  1197. */
  1198. int
  1199. write_pidfile(const char *filename)
  1200. {
  1201. FILE *pidfile;
  1202. if ((pidfile = fopen(filename, "w")) == NULL) {
  1203. log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
  1204. strerror(errno));
  1205. return -1;
  1206. } else {
  1207. #ifdef _WIN32
  1208. int pid = (int)_getpid();
  1209. #else
  1210. int pid = (int)getpid();
  1211. #endif
  1212. int rv = 0;
  1213. if (fprintf(pidfile, "%d\n", pid) < 0)
  1214. rv = -1;
  1215. if (fclose(pidfile) < 0)
  1216. rv = -1;
  1217. return rv;
  1218. }
  1219. }
  1220. #ifdef _WIN32
  1221. HANDLE
  1222. load_windows_system_library(const TCHAR *library_name)
  1223. {
  1224. TCHAR path[MAX_PATH];
  1225. unsigned n;
  1226. n = GetSystemDirectory(path, MAX_PATH);
  1227. if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH)
  1228. return 0;
  1229. _tcscat(path, TEXT("\\"));
  1230. _tcscat(path, library_name);
  1231. return LoadLibrary(path);
  1232. }
  1233. #endif /* defined(_WIN32) */
  1234. /** Format a single argument for being put on a Windows command line.
  1235. * Returns a newly allocated string */
  1236. static char *
  1237. format_win_cmdline_argument(const char *arg)
  1238. {
  1239. char *formatted_arg;
  1240. char need_quotes;
  1241. const char *c;
  1242. int i;
  1243. int bs_counter = 0;
  1244. /* Backslash we can point to when one is inserted into the string */
  1245. const char backslash = '\\';
  1246. /* Smartlist of *char */
  1247. smartlist_t *arg_chars;
  1248. arg_chars = smartlist_new();
  1249. /* Quote string if it contains whitespace or is empty */
  1250. need_quotes = (strchr(arg, ' ') || strchr(arg, '\t') || '\0' == arg[0]);
  1251. /* Build up smartlist of *chars */
  1252. for (c=arg; *c != '\0'; c++) {
  1253. if ('"' == *c) {
  1254. /* Double up backslashes preceding a quote */
  1255. for (i=0; i<(bs_counter*2); i++)
  1256. smartlist_add(arg_chars, (void*)&backslash);
  1257. bs_counter = 0;
  1258. /* Escape the quote */
  1259. smartlist_add(arg_chars, (void*)&backslash);
  1260. smartlist_add(arg_chars, (void*)c);
  1261. } else if ('\\' == *c) {
  1262. /* Count backslashes until we know whether to double up */
  1263. bs_counter++;
  1264. } else {
  1265. /* Don't double up slashes preceding a non-quote */
  1266. for (i=0; i<bs_counter; i++)
  1267. smartlist_add(arg_chars, (void*)&backslash);
  1268. bs_counter = 0;
  1269. smartlist_add(arg_chars, (void*)c);
  1270. }
  1271. }
  1272. /* Don't double up trailing backslashes */
  1273. for (i=0; i<bs_counter; i++)
  1274. smartlist_add(arg_chars, (void*)&backslash);
  1275. /* Allocate space for argument, quotes (if needed), and terminator */
  1276. const size_t formatted_arg_len = smartlist_len(arg_chars) +
  1277. (need_quotes ? 2 : 0) + 1;
  1278. formatted_arg = tor_malloc_zero(formatted_arg_len);
  1279. /* Add leading quote */
  1280. i=0;
  1281. if (need_quotes)
  1282. formatted_arg[i++] = '"';
  1283. /* Add characters */
  1284. SMARTLIST_FOREACH(arg_chars, char*, ch,
  1285. {
  1286. formatted_arg[i++] = *ch;
  1287. });
  1288. /* Add trailing quote */
  1289. if (need_quotes)
  1290. formatted_arg[i++] = '"';
  1291. formatted_arg[i] = '\0';
  1292. smartlist_free(arg_chars);
  1293. return formatted_arg;
  1294. }
  1295. /** Format a command line for use on Windows, which takes the command as a
  1296. * string rather than string array. Follows the rules from "Parsing C++
  1297. * Command-Line Arguments" in MSDN. Algorithm based on list2cmdline in the
  1298. * Python subprocess module. Returns a newly allocated string */
  1299. char *
  1300. tor_join_win_cmdline(const char *argv[])
  1301. {
  1302. smartlist_t *argv_list;
  1303. char *joined_argv;
  1304. int i;
  1305. /* Format each argument and put the result in a smartlist */
  1306. argv_list = smartlist_new();
  1307. for (i=0; argv[i] != NULL; i++) {
  1308. smartlist_add(argv_list, (void *)format_win_cmdline_argument(argv[i]));
  1309. }
  1310. /* Join the arguments with whitespace */
  1311. joined_argv = smartlist_join_strings(argv_list, " ", 0, NULL);
  1312. /* Free the newly allocated arguments, and the smartlist */
  1313. SMARTLIST_FOREACH(argv_list, char *, arg,
  1314. {
  1315. tor_free(arg);
  1316. });
  1317. smartlist_free(argv_list);
  1318. return joined_argv;
  1319. }
  1320. #ifndef _WIN32
  1321. /** Format <b>child_state</b> and <b>saved_errno</b> as a hex string placed in
  1322. * <b>hex_errno</b>. Called between fork and _exit, so must be signal-handler
  1323. * safe.
  1324. *
  1325. * <b>hex_errno</b> must have at least HEX_ERRNO_SIZE+1 bytes available.
  1326. *
  1327. * The format of <b>hex_errno</b> is: "CHILD_STATE/ERRNO\n", left-padded
  1328. * with spaces. CHILD_STATE indicates where
  1329. * in the process of starting the child process did the failure occur (see
  1330. * CHILD_STATE_* macros for definition), and SAVED_ERRNO is the value of
  1331. * errno when the failure occurred.
  1332. *
  1333. * On success return the number of characters added to hex_errno, not counting
  1334. * the terminating NUL; return -1 on error.
  1335. */
  1336. STATIC int
  1337. format_helper_exit_status(unsigned char child_state, int saved_errno,
  1338. char *hex_errno)
  1339. {
  1340. unsigned int unsigned_errno;
  1341. int written, left;
  1342. char *cur;
  1343. size_t i;
  1344. int res = -1;
  1345. /* Fill hex_errno with spaces, and a trailing newline (memset may
  1346. not be signal handler safe, so we can't use it) */
  1347. for (i = 0; i < (HEX_ERRNO_SIZE - 1); i++)
  1348. hex_errno[i] = ' ';
  1349. hex_errno[HEX_ERRNO_SIZE - 1] = '\n';
  1350. /* Convert errno to be unsigned for hex conversion */
  1351. if (saved_errno < 0) {
  1352. // Avoid overflow on the cast to unsigned int when result is INT_MIN
  1353. // by adding 1 to the signed int negative value,
  1354. // then, after it has been negated and cast to unsigned,
  1355. // adding the original 1 back (the double-addition is intentional).
  1356. // Otherwise, the cast to signed could cause a temporary int
  1357. // to equal INT_MAX + 1, which is undefined.
  1358. unsigned_errno = ((unsigned int) -(saved_errno + 1)) + 1;
  1359. } else {
  1360. unsigned_errno = (unsigned int) saved_errno;
  1361. }
  1362. /*
  1363. * Count how many chars of space we have left, and keep a pointer into the
  1364. * current point in the buffer.
  1365. */
  1366. left = HEX_ERRNO_SIZE+1;
  1367. cur = hex_errno;
  1368. /* Emit child_state */
  1369. written = format_hex_number_sigsafe(child_state, cur, left);
  1370. if (written <= 0)
  1371. goto err;
  1372. /* Adjust left and cur */
  1373. left -= written;
  1374. cur += written;
  1375. if (left <= 0)
  1376. goto err;
  1377. /* Now the '/' */
  1378. *cur = '/';
  1379. /* Adjust left and cur */
  1380. ++cur;
  1381. --left;
  1382. if (left <= 0)
  1383. goto err;
  1384. /* Need minus? */
  1385. if (saved_errno < 0) {
  1386. *cur = '-';
  1387. ++cur;
  1388. --left;
  1389. if (left <= 0)
  1390. goto err;
  1391. }
  1392. /* Emit unsigned_errno */
  1393. written = format_hex_number_sigsafe(unsigned_errno, cur, left);
  1394. if (written <= 0)
  1395. goto err;
  1396. /* Adjust left and cur */
  1397. left -= written;
  1398. cur += written;
  1399. /* Check that we have enough space left for a newline and a NUL */
  1400. if (left <= 1)
  1401. goto err;
  1402. /* Emit the newline and NUL */
  1403. *cur++ = '\n';
  1404. *cur++ = '\0';
  1405. res = (int)(cur - hex_errno - 1);
  1406. goto done;
  1407. err:
  1408. /*
  1409. * In error exit, just write a '\0' in the first char so whatever called
  1410. * this at least won't fall off the end.
  1411. */
  1412. *hex_errno = '\0';
  1413. done:
  1414. return res;
  1415. }
  1416. #endif /* !defined(_WIN32) */
  1417. /* Maximum number of file descriptors, if we cannot get it via sysconf() */
  1418. #define DEFAULT_MAX_FD 256
  1419. /** Terminate the process of <b>process_handle</b>, if that process has not
  1420. * already exited.
  1421. *
  1422. * Return 0 if we succeeded in terminating the process (or if the process
  1423. * already exited), and -1 if we tried to kill the process but failed.
  1424. *
  1425. * Based on code originally borrowed from Python's os.kill. */
  1426. int
  1427. tor_terminate_process(process_handle_t *process_handle)
  1428. {
  1429. #ifdef _WIN32
  1430. if (tor_get_exit_code(process_handle, 0, NULL) == PROCESS_EXIT_RUNNING) {
  1431. HANDLE handle = process_handle->pid.hProcess;
  1432. if (!TerminateProcess(handle, 0))
  1433. return -1;
  1434. else
  1435. return 0;
  1436. }
  1437. #else /* !(defined(_WIN32)) */
  1438. if (process_handle->waitpid_cb) {
  1439. /* We haven't got a waitpid yet, so we can just kill off the process. */
  1440. return kill(process_handle->pid, SIGTERM);
  1441. }
  1442. #endif /* defined(_WIN32) */
  1443. return 0; /* We didn't need to kill the process, so report success */
  1444. }
  1445. /** Return the Process ID of <b>process_handle</b>. */
  1446. int
  1447. tor_process_get_pid(process_handle_t *process_handle)
  1448. {
  1449. #ifdef _WIN32
  1450. return (int) process_handle->pid.dwProcessId;
  1451. #else
  1452. return (int) process_handle->pid;
  1453. #endif
  1454. }
  1455. #ifdef _WIN32
  1456. HANDLE
  1457. tor_process_get_stdout_pipe(process_handle_t *process_handle)
  1458. {
  1459. return process_handle->stdout_pipe;
  1460. }
  1461. #else /* !(defined(_WIN32)) */
  1462. /* DOCDOC tor_process_get_stdout_pipe */
  1463. int
  1464. tor_process_get_stdout_pipe(process_handle_t *process_handle)
  1465. {
  1466. return process_handle->stdout_pipe;
  1467. }
  1468. #endif /* defined(_WIN32) */
  1469. /* DOCDOC process_handle_new */
  1470. static process_handle_t *
  1471. process_handle_new(void)
  1472. {
  1473. process_handle_t *out = tor_malloc_zero(sizeof(process_handle_t));
  1474. #ifdef _WIN32
  1475. out->stdin_pipe = INVALID_HANDLE_VALUE;
  1476. out->stdout_pipe = INVALID_HANDLE_VALUE;
  1477. out->stderr_pipe = INVALID_HANDLE_VALUE;
  1478. #else
  1479. out->stdin_pipe = -1;
  1480. out->stdout_pipe = -1;
  1481. out->stderr_pipe = -1;
  1482. #endif /* defined(_WIN32) */
  1483. return out;
  1484. }
  1485. #ifndef _WIN32
  1486. /** Invoked when a process that we've launched via tor_spawn_background() has
  1487. * been found to have terminated.
  1488. */
  1489. static void
  1490. process_handle_waitpid_cb(int status, void *arg)
  1491. {
  1492. process_handle_t *process_handle = arg;
  1493. process_handle->waitpid_exit_status = status;
  1494. clear_waitpid_callback(process_handle->waitpid_cb);
  1495. if (process_handle->status == PROCESS_STATUS_RUNNING)
  1496. process_handle->status = PROCESS_STATUS_NOTRUNNING;
  1497. process_handle->waitpid_cb = 0;
  1498. }
  1499. #endif /* !defined(_WIN32) */
  1500. /**
  1501. * @name child-process states
  1502. *
  1503. * Each of these values represents a possible state that a child process can
  1504. * be in. They're used to determine what to say when telling the parent how
  1505. * far along we were before failure.
  1506. *
  1507. * @{
  1508. */
  1509. #define CHILD_STATE_INIT 0
  1510. #define CHILD_STATE_PIPE 1
  1511. #define CHILD_STATE_MAXFD 2
  1512. #define CHILD_STATE_FORK 3
  1513. #define CHILD_STATE_DUPOUT 4
  1514. #define CHILD_STATE_DUPERR 5
  1515. #define CHILD_STATE_DUPIN 6
  1516. #define CHILD_STATE_CLOSEFD 7
  1517. #define CHILD_STATE_EXEC 8
  1518. #define CHILD_STATE_FAILEXEC 9
  1519. /** @} */
  1520. /**
  1521. * Boolean. If true, then Tor may call execve or CreateProcess via
  1522. * tor_spawn_background.
  1523. **/
  1524. static int may_spawn_background_process = 1;
  1525. /**
  1526. * Turn off may_spawn_background_process, so that all future calls to
  1527. * tor_spawn_background are guaranteed to fail.
  1528. **/
  1529. void
  1530. tor_disable_spawning_background_processes(void)
  1531. {
  1532. may_spawn_background_process = 0;
  1533. }
  1534. /** Start a program in the background. If <b>filename</b> contains a '/', then
  1535. * it will be treated as an absolute or relative path. Otherwise, on
  1536. * non-Windows systems, the system path will be searched for <b>filename</b>.
  1537. * On Windows, only the current directory will be searched. Here, to search the
  1538. * system path (as well as the application directory, current working
  1539. * directory, and system directories), set filename to NULL.
  1540. *
  1541. * The strings in <b>argv</b> will be passed as the command line arguments of
  1542. * the child program (following convention, argv[0] should normally be the
  1543. * filename of the executable, and this must be the case if <b>filename</b> is
  1544. * NULL). The last element of argv must be NULL. A handle to the child process
  1545. * will be returned in process_handle (which must be non-NULL). Read
  1546. * process_handle.status to find out if the process was successfully launched.
  1547. * For convenience, process_handle.status is returned by this function.
  1548. *
  1549. * Some parts of this code are based on the POSIX subprocess module from
  1550. * Python, and example code from
  1551. * http://msdn.microsoft.com/en-us/library/ms682499%28v=vs.85%29.aspx.
  1552. */
  1553. int
  1554. tor_spawn_background(const char *const filename, const char **argv,
  1555. process_environment_t *env,
  1556. process_handle_t **process_handle_out)
  1557. {
  1558. if (BUG(may_spawn_background_process == 0)) {
  1559. /* We should never reach this point if we're forbidden to spawn
  1560. * processes. Instead we should have caught the attempt earlier. */
  1561. return PROCESS_STATUS_ERROR;
  1562. }
  1563. #ifdef _WIN32
  1564. HANDLE stdout_pipe_read = NULL;
  1565. HANDLE stdout_pipe_write = NULL;
  1566. HANDLE stderr_pipe_read = NULL;
  1567. HANDLE stderr_pipe_write = NULL;
  1568. HANDLE stdin_pipe_read = NULL;
  1569. HANDLE stdin_pipe_write = NULL;
  1570. process_handle_t *process_handle;
  1571. int status;
  1572. STARTUPINFOA siStartInfo;
  1573. BOOL retval = FALSE;
  1574. SECURITY_ATTRIBUTES saAttr;
  1575. char *joined_argv;
  1576. saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
  1577. saAttr.bInheritHandle = TRUE;
  1578. /* TODO: should we set explicit security attributes? (#2046, comment 5) */
  1579. saAttr.lpSecurityDescriptor = NULL;
  1580. /* Assume failure to start process */
  1581. status = PROCESS_STATUS_ERROR;
  1582. /* Set up pipe for stdout */
  1583. if (!CreatePipe(&stdout_pipe_read, &stdout_pipe_write, &saAttr, 0)) {
  1584. log_warn(LD_GENERAL,
  1585. "Failed to create pipe for stdout communication with child process: %s",
  1586. format_win32_error(GetLastError()));
  1587. return status;
  1588. }
  1589. if (!SetHandleInformation(stdout_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
  1590. log_warn(LD_GENERAL,
  1591. "Failed to configure pipe for stdout communication with child "
  1592. "process: %s", format_win32_error(GetLastError()));
  1593. return status;
  1594. }
  1595. /* Set up pipe for stderr */
  1596. if (!CreatePipe(&stderr_pipe_read, &stderr_pipe_write, &saAttr, 0)) {
  1597. log_warn(LD_GENERAL,
  1598. "Failed to create pipe for stderr communication with child process: %s",
  1599. format_win32_error(GetLastError()));
  1600. return status;
  1601. }
  1602. if (!SetHandleInformation(stderr_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
  1603. log_warn(LD_GENERAL,
  1604. "Failed to configure pipe for stderr communication with child "
  1605. "process: %s", format_win32_error(GetLastError()));
  1606. return status;
  1607. }
  1608. /* Set up pipe for stdin */
  1609. if (!CreatePipe(&stdin_pipe_read, &stdin_pipe_write, &saAttr, 0)) {
  1610. log_warn(LD_GENERAL,
  1611. "Failed to create pipe for stdin communication with child process: %s",
  1612. format_win32_error(GetLastError()));
  1613. return status;
  1614. }
  1615. if (!SetHandleInformation(stdin_pipe_write, HANDLE_FLAG_INHERIT, 0)) {
  1616. log_warn(LD_GENERAL,
  1617. "Failed to configure pipe for stdin communication with child "
  1618. "process: %s", format_win32_error(GetLastError()));
  1619. return status;
  1620. }
  1621. /* Create the child process */
  1622. /* Windows expects argv to be a whitespace delimited string, so join argv up
  1623. */
  1624. joined_argv = tor_join_win_cmdline(argv);
  1625. process_handle = process_handle_new();
  1626. process_handle->status = status;
  1627. ZeroMemory(&(process_handle->pid), sizeof(PROCESS_INFORMATION));
  1628. ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
  1629. siStartInfo.cb = sizeof(STARTUPINFO);
  1630. siStartInfo.hStdError = stderr_pipe_write;
  1631. siStartInfo.hStdOutput = stdout_pipe_write;
  1632. siStartInfo.hStdInput = stdin_pipe_read;
  1633. siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
  1634. /* Create the child process */
  1635. retval = CreateProcessA(filename, // module name
  1636. joined_argv, // command line
  1637. /* TODO: should we set explicit security attributes? (#2046, comment 5) */
  1638. NULL, // process security attributes
  1639. NULL, // primary thread security attributes
  1640. TRUE, // handles are inherited
  1641. /*(TODO: set CREATE_NEW CONSOLE/PROCESS_GROUP to make GetExitCodeProcess()
  1642. * work?) */
  1643. CREATE_NO_WINDOW, // creation flags
  1644. (env==NULL) ? NULL : env->windows_environment_block,
  1645. NULL, // use parent's current directory
  1646. &siStartInfo, // STARTUPINFO pointer
  1647. &(process_handle->pid)); // receives PROCESS_INFORMATION
  1648. tor_free(joined_argv);
  1649. if (!retval) {
  1650. log_warn(LD_GENERAL,
  1651. "Failed to create child process %s: %s", filename?filename:argv[0],
  1652. format_win32_error(GetLastError()));
  1653. tor_free(process_handle);
  1654. } else {
  1655. /* TODO: Close hProcess and hThread in process_handle->pid? */
  1656. process_handle->stdout_pipe = stdout_pipe_read;
  1657. process_handle->stderr_pipe = stderr_pipe_read;
  1658. process_handle->stdin_pipe = stdin_pipe_write;
  1659. status = process_handle->status = PROCESS_STATUS_RUNNING;
  1660. }
  1661. /* TODO: Close pipes on exit */
  1662. *process_handle_out = process_handle;
  1663. return status;
  1664. #else /* !(defined(_WIN32)) */
  1665. pid_t pid;
  1666. int stdout_pipe[2];
  1667. int stderr_pipe[2];
  1668. int stdin_pipe[2];
  1669. int fd, retval;
  1670. process_handle_t *process_handle;
  1671. int status;
  1672. const char *error_message = SPAWN_ERROR_MESSAGE;
  1673. size_t error_message_length;
  1674. /* Represents where in the process of spawning the program is;
  1675. this is used for printing out the error message */
  1676. unsigned char child_state = CHILD_STATE_INIT;
  1677. char hex_errno[HEX_ERRNO_SIZE + 2]; /* + 1 should be sufficient actually */
  1678. static int max_fd = -1;
  1679. status = PROCESS_STATUS_ERROR;
  1680. /* We do the strlen here because strlen() is not signal handler safe,
  1681. and we are not allowed to use unsafe functions between fork and exec */
  1682. error_message_length = strlen(error_message);
  1683. // child_state = CHILD_STATE_PIPE;
  1684. /* Set up pipe for redirecting stdout, stderr, and stdin of child */
  1685. retval = pipe(stdout_pipe);
  1686. if (-1 == retval) {
  1687. log_warn(LD_GENERAL,
  1688. "Failed to set up pipe for stdout communication with child process: %s",
  1689. strerror(errno));
  1690. return status;
  1691. }
  1692. retval = pipe(stderr_pipe);
  1693. if (-1 == retval) {
  1694. log_warn(LD_GENERAL,
  1695. "Failed to set up pipe for stderr communication with child process: %s",
  1696. strerror(errno));
  1697. close(stdout_pipe[0]);
  1698. close(stdout_pipe[1]);
  1699. return status;
  1700. }
  1701. retval = pipe(stdin_pipe);
  1702. if (-1 == retval) {
  1703. log_warn(LD_GENERAL,
  1704. "Failed to set up pipe for stdin communication with child process: %s",
  1705. strerror(errno));
  1706. close(stdout_pipe[0]);
  1707. close(stdout_pipe[1]);
  1708. close(stderr_pipe[0]);
  1709. close(stderr_pipe[1]);
  1710. return status;
  1711. }
  1712. // child_state = CHILD_STATE_MAXFD;
  1713. #ifdef _SC_OPEN_MAX
  1714. if (-1 == max_fd) {
  1715. max_fd = (int) sysconf(_SC_OPEN_MAX);
  1716. if (max_fd == -1) {
  1717. max_fd = DEFAULT_MAX_FD;
  1718. log_warn(LD_GENERAL,
  1719. "Cannot find maximum file descriptor, assuming %d", max_fd);
  1720. }
  1721. }
  1722. #else /* !(defined(_SC_OPEN_MAX)) */
  1723. max_fd = DEFAULT_MAX_FD;
  1724. #endif /* defined(_SC_OPEN_MAX) */
  1725. // child_state = CHILD_STATE_FORK;
  1726. pid = fork();
  1727. if (0 == pid) {
  1728. /* In child */
  1729. #if defined(HAVE_SYS_PRCTL_H) && defined(__linux__)
  1730. /* Attempt to have the kernel issue a SIGTERM if the parent
  1731. * goes away. Certain attributes of the binary being execve()ed
  1732. * will clear this during the execve() call, but it's better
  1733. * than nothing.
  1734. */
  1735. prctl(PR_SET_PDEATHSIG, SIGTERM);
  1736. #endif /* defined(HAVE_SYS_PRCTL_H) && defined(__linux__) */
  1737. child_state = CHILD_STATE_DUPOUT;
  1738. /* Link child stdout to the write end of the pipe */
  1739. retval = dup2(stdout_pipe[1], STDOUT_FILENO);
  1740. if (-1 == retval)
  1741. goto error;
  1742. child_state = CHILD_STATE_DUPERR;
  1743. /* Link child stderr to the write end of the pipe */
  1744. retval = dup2(stderr_pipe[1], STDERR_FILENO);
  1745. if (-1 == retval)
  1746. goto error;
  1747. child_state = CHILD_STATE_DUPIN;
  1748. /* Link child stdin to the read end of the pipe */
  1749. retval = dup2(stdin_pipe[0], STDIN_FILENO);
  1750. if (-1 == retval)
  1751. goto error;
  1752. // child_state = CHILD_STATE_CLOSEFD;
  1753. close(stderr_pipe[0]);
  1754. close(stderr_pipe[1]);
  1755. close(stdout_pipe[0]);
  1756. close(stdout_pipe[1]);
  1757. close(stdin_pipe[0]);
  1758. close(stdin_pipe[1]);
  1759. /* Close all other fds, including the read end of the pipe */
  1760. /* XXX: We should now be doing enough FD_CLOEXEC setting to make
  1761. * this needless. */
  1762. for (fd = STDERR_FILENO + 1; fd < max_fd; fd++) {
  1763. close(fd);
  1764. }
  1765. // child_state = CHILD_STATE_EXEC;
  1766. /* Call the requested program. We need the cast because
  1767. execvp doesn't define argv as const, even though it
  1768. does not modify the arguments */
  1769. if (env)
  1770. execve(filename, (char *const *) argv, env->unixoid_environment_block);
  1771. else {
  1772. static char *new_env[] = { NULL };
  1773. execve(filename, (char *const *) argv, new_env);
  1774. }
  1775. /* If we got here, the exec or open(/dev/null) failed */
  1776. child_state = CHILD_STATE_FAILEXEC;
  1777. error:
  1778. {
  1779. /* XXX: are we leaking fds from the pipe? */
  1780. int n, err=0;
  1781. ssize_t nbytes;
  1782. n = format_helper_exit_status(child_state, errno, hex_errno);
  1783. if (n >= 0) {
  1784. /* Write the error message. GCC requires that we check the return
  1785. value, but there is nothing we can do if it fails */
  1786. /* TODO: Don't use STDOUT, use a pipe set up just for this purpose */
  1787. nbytes = write(STDOUT_FILENO, error_message, error_message_length);
  1788. err = (nbytes < 0);
  1789. nbytes = write(STDOUT_FILENO, hex_errno, n);
  1790. err += (nbytes < 0);
  1791. }
  1792. _exit(err?254:255); // exit ok: in child.
  1793. }
  1794. /* Never reached, but avoids compiler warning */
  1795. return status; // LCOV_EXCL_LINE
  1796. }
  1797. /* In parent */
  1798. if (-1 == pid) {
  1799. log_warn(LD_GENERAL, "Failed to fork child process: %s", strerror(errno));
  1800. close(stdin_pipe[0]);
  1801. close(stdin_pipe[1]);
  1802. close(stdout_pipe[0]);
  1803. close(stdout_pipe[1]);
  1804. close(stderr_pipe[0]);
  1805. close(stderr_pipe[1]);
  1806. return status;
  1807. }
  1808. process_handle = process_handle_new();
  1809. process_handle->status = status;
  1810. process_handle->pid = pid;
  1811. /* TODO: If the child process forked but failed to exec, waitpid it */
  1812. /* Return read end of the pipes to caller, and close write end */
  1813. process_handle->stdout_pipe = stdout_pipe[0];
  1814. retval = close(stdout_pipe[1]);
  1815. if (-1 == retval) {
  1816. log_warn(LD_GENERAL,
  1817. "Failed to close write end of stdout pipe in parent process: %s",
  1818. strerror(errno));
  1819. }
  1820. process_handle->waitpid_cb = set_waitpid_callback(pid,
  1821. process_handle_waitpid_cb,
  1822. process_handle);
  1823. process_handle->stderr_pipe = stderr_pipe[0];
  1824. retval = close(stderr_pipe[1]);
  1825. if (-1 == retval) {
  1826. log_warn(LD_GENERAL,
  1827. "Failed to close write end of stderr pipe in parent process: %s",
  1828. strerror(errno));
  1829. }
  1830. /* Return write end of the stdin pipe to caller, and close the read end */
  1831. process_handle->stdin_pipe = stdin_pipe[1];
  1832. retval = close(stdin_pipe[0]);
  1833. if (-1 == retval) {
  1834. log_warn(LD_GENERAL,
  1835. "Failed to close read end of stdin pipe in parent process: %s",
  1836. strerror(errno));
  1837. }
  1838. status = process_handle->status = PROCESS_STATUS_RUNNING;
  1839. /* Set stdin/stdout/stderr pipes to be non-blocking */
  1840. if (fcntl(process_handle->stdout_pipe, F_SETFL, O_NONBLOCK) < 0 ||
  1841. fcntl(process_handle->stderr_pipe, F_SETFL, O_NONBLOCK) < 0 ||
  1842. fcntl(process_handle->stdin_pipe, F_SETFL, O_NONBLOCK) < 0) {
  1843. log_warn(LD_GENERAL, "Failed to set stderror/stdout/stdin pipes "
  1844. "nonblocking in parent process: %s", strerror(errno));
  1845. }
  1846. *process_handle_out = process_handle;
  1847. return status;
  1848. #endif /* defined(_WIN32) */
  1849. }
  1850. /** Destroy all resources allocated by the process handle in
  1851. * <b>process_handle</b>.
  1852. * If <b>also_terminate_process</b> is true, also terminate the
  1853. * process of the process handle. */
  1854. MOCK_IMPL(void,
  1855. tor_process_handle_destroy,(process_handle_t *process_handle,
  1856. int also_terminate_process))
  1857. {
  1858. if (!process_handle)
  1859. return;
  1860. if (also_terminate_process) {
  1861. if (tor_terminate_process(process_handle) < 0) {
  1862. const char *errstr =
  1863. #ifdef _WIN32
  1864. format_win32_error(GetLastError());
  1865. #else
  1866. strerror(errno);
  1867. #endif
  1868. log_notice(LD_GENERAL, "Failed to terminate process with "
  1869. "PID '%d' ('%s').", tor_process_get_pid(process_handle),
  1870. errstr);
  1871. } else {
  1872. log_info(LD_GENERAL, "Terminated process with PID '%d'.",
  1873. tor_process_get_pid(process_handle));
  1874. }
  1875. }
  1876. process_handle->status = PROCESS_STATUS_NOTRUNNING;
  1877. #ifdef _WIN32
  1878. if (process_handle->stdout_pipe)
  1879. CloseHandle(process_handle->stdout_pipe);
  1880. if (process_handle->stderr_pipe)
  1881. CloseHandle(process_handle->stderr_pipe);
  1882. if (process_handle->stdin_pipe)
  1883. CloseHandle(process_handle->stdin_pipe);
  1884. #else /* !(defined(_WIN32)) */
  1885. close(process_handle->stdout_pipe);
  1886. close(process_handle->stderr_pipe);
  1887. close(process_handle->stdin_pipe);
  1888. clear_waitpid_callback(process_handle->waitpid_cb);
  1889. #endif /* defined(_WIN32) */
  1890. memset(process_handle, 0x0f, sizeof(process_handle_t));
  1891. tor_free(process_handle);
  1892. }
  1893. /** Get the exit code of a process specified by <b>process_handle</b> and store
  1894. * it in <b>exit_code</b>, if set to a non-NULL value. If <b>block</b> is set
  1895. * to true, the call will block until the process has exited. Otherwise if
  1896. * the process is still running, the function will return
  1897. * PROCESS_EXIT_RUNNING, and exit_code will be left unchanged. Returns
  1898. * PROCESS_EXIT_EXITED if the process did exit. If there is a failure,
  1899. * PROCESS_EXIT_ERROR will be returned and the contents of exit_code (if
  1900. * non-NULL) will be undefined. N.B. Under *nix operating systems, this will
  1901. * probably not work in Tor, because waitpid() is called in main.c to reap any
  1902. * terminated child processes.*/
  1903. int
  1904. tor_get_exit_code(process_handle_t *process_handle,
  1905. int block, int *exit_code)
  1906. {
  1907. #ifdef _WIN32
  1908. DWORD retval;
  1909. BOOL success;
  1910. if (block) {
  1911. /* Wait for the process to exit */
  1912. retval = WaitForSingleObject(process_handle->pid.hProcess, INFINITE);
  1913. if (retval != WAIT_OBJECT_0) {
  1914. log_warn(LD_GENERAL, "WaitForSingleObject() failed (%d): %s",
  1915. (int)retval, format_win32_error(GetLastError()));
  1916. return PROCESS_EXIT_ERROR;
  1917. }
  1918. } else {
  1919. retval = WaitForSingleObject(process_handle->pid.hProcess, 0);
  1920. if (WAIT_TIMEOUT == retval) {
  1921. /* Process has not exited */
  1922. return PROCESS_EXIT_RUNNING;
  1923. } else if (retval != WAIT_OBJECT_0) {
  1924. log_warn(LD_GENERAL, "WaitForSingleObject() failed (%d): %s",
  1925. (int)retval, format_win32_error(GetLastError()));
  1926. return PROCESS_EXIT_ERROR;
  1927. }
  1928. }
  1929. if (exit_code != NULL) {
  1930. success = GetExitCodeProcess(process_handle->pid.hProcess,
  1931. (PDWORD)exit_code);
  1932. if (!success) {
  1933. log_warn(LD_GENERAL, "GetExitCodeProcess() failed: %s",
  1934. format_win32_error(GetLastError()));
  1935. return PROCESS_EXIT_ERROR;
  1936. }
  1937. }
  1938. #else /* !(defined(_WIN32)) */
  1939. int stat_loc;
  1940. int retval;
  1941. if (process_handle->waitpid_cb) {
  1942. /* We haven't processed a SIGCHLD yet. */
  1943. retval = waitpid(process_handle->pid, &stat_loc, block?0:WNOHANG);
  1944. if (retval == process_handle->pid) {
  1945. clear_waitpid_callback(process_handle->waitpid_cb);
  1946. process_handle->waitpid_cb = NULL;
  1947. process_handle->waitpid_exit_status = stat_loc;
  1948. }
  1949. } else {
  1950. /* We already got a SIGCHLD for this process, and handled it. */
  1951. retval = process_handle->pid;
  1952. stat_loc = process_handle->waitpid_exit_status;
  1953. }
  1954. if (!block && 0 == retval) {
  1955. /* Process has not exited */
  1956. return PROCESS_EXIT_RUNNING;
  1957. } else if (retval != process_handle->pid) {
  1958. log_warn(LD_GENERAL, "waitpid() failed for PID %d: %s",
  1959. (int)process_handle->pid, strerror(errno));
  1960. return PROCESS_EXIT_ERROR;
  1961. }
  1962. if (!WIFEXITED(stat_loc)) {
  1963. log_warn(LD_GENERAL, "Process %d did not exit normally",
  1964. (int)process_handle->pid);
  1965. return PROCESS_EXIT_ERROR;
  1966. }
  1967. if (exit_code != NULL)
  1968. *exit_code = WEXITSTATUS(stat_loc);
  1969. #endif /* defined(_WIN32) */
  1970. return PROCESS_EXIT_EXITED;
  1971. }
  1972. /** Helper: return the number of characters in <b>s</b> preceding the first
  1973. * occurrence of <b>ch</b>. If <b>ch</b> does not occur in <b>s</b>, return
  1974. * the length of <b>s</b>. Should be equivalent to strspn(s, "ch"). */
  1975. static inline size_t
  1976. str_num_before(const char *s, char ch)
  1977. {
  1978. const char *cp = strchr(s, ch);
  1979. if (cp)
  1980. return cp - s;
  1981. else
  1982. return strlen(s);
  1983. }
  1984. /** Return non-zero iff getenv would consider <b>s1</b> and <b>s2</b>
  1985. * to have the same name as strings in a process's environment. */
  1986. int
  1987. environment_variable_names_equal(const char *s1, const char *s2)
  1988. {
  1989. size_t s1_name_len = str_num_before(s1, '=');
  1990. size_t s2_name_len = str_num_before(s2, '=');
  1991. return (s1_name_len == s2_name_len &&
  1992. tor_memeq(s1, s2, s1_name_len));
  1993. }
  1994. /** Free <b>env</b> (assuming it was produced by
  1995. * process_environment_make). */
  1996. void
  1997. process_environment_free_(process_environment_t *env)
  1998. {
  1999. if (env == NULL) return;
  2000. /* As both an optimization hack to reduce consing on Unixoid systems
  2001. * and a nice way to ensure that some otherwise-Windows-specific
  2002. * code will always get tested before changes to it get merged, the
  2003. * strings which env->unixoid_environment_block points to are packed
  2004. * into env->windows_environment_block. */
  2005. tor_free(env->unixoid_environment_block);
  2006. tor_free(env->windows_environment_block);
  2007. tor_free(env);
  2008. }
  2009. /** Make a process_environment_t containing the environment variables
  2010. * specified in <b>env_vars</b> (as C strings of the form
  2011. * "NAME=VALUE"). */
  2012. process_environment_t *
  2013. process_environment_make(struct smartlist_t *env_vars)
  2014. {
  2015. process_environment_t *env = tor_malloc_zero(sizeof(process_environment_t));
  2016. int n_env_vars = smartlist_len(env_vars);
  2017. int i;
  2018. size_t total_env_length;
  2019. smartlist_t *env_vars_sorted;
  2020. tor_assert(n_env_vars + 1 != 0);
  2021. env->unixoid_environment_block = tor_calloc(n_env_vars + 1, sizeof(char *));
  2022. /* env->unixoid_environment_block is already NULL-terminated,
  2023. * because we assume that NULL == 0 (and check that during compilation). */
  2024. total_env_length = 1; /* terminating NUL of terminating empty string */
  2025. for (i = 0; i < n_env_vars; ++i) {
  2026. const char *s = smartlist_get(env_vars, (int)i);
  2027. size_t slen = strlen(s);
  2028. tor_assert(slen + 1 != 0);
  2029. tor_assert(slen + 1 < SIZE_MAX - total_env_length);
  2030. total_env_length += slen + 1;
  2031. }
  2032. env->windows_environment_block = tor_malloc_zero(total_env_length);
  2033. /* env->windows_environment_block is already
  2034. * (NUL-terminated-empty-string)-terminated. */
  2035. /* Some versions of Windows supposedly require that environment
  2036. * blocks be sorted. Or maybe some Windows programs (or their
  2037. * runtime libraries) fail to look up strings in non-sorted
  2038. * environment blocks.
  2039. *
  2040. * Also, sorting strings makes it easy to find duplicate environment
  2041. * variables and environment-variable strings without an '=' on all
  2042. * OSes, and they can cause badness. Let's complain about those. */
  2043. env_vars_sorted = smartlist_new();
  2044. smartlist_add_all(env_vars_sorted, env_vars);
  2045. smartlist_sort_strings(env_vars_sorted);
  2046. /* Now copy the strings into the environment blocks. */
  2047. {
  2048. char *cp = env->windows_environment_block;
  2049. const char *prev_env_var = NULL;
  2050. for (i = 0; i < n_env_vars; ++i) {
  2051. const char *s = smartlist_get(env_vars_sorted, (int)i);
  2052. size_t slen = strlen(s);
  2053. size_t s_name_len = str_num_before(s, '=');
  2054. if (s_name_len == slen) {
  2055. log_warn(LD_GENERAL,
  2056. "Preparing an environment containing a variable "
  2057. "without a value: %s",
  2058. s);
  2059. }
  2060. if (prev_env_var != NULL &&
  2061. environment_variable_names_equal(s, prev_env_var)) {
  2062. log_warn(LD_GENERAL,
  2063. "Preparing an environment containing two variables "
  2064. "with the same name: %s and %s",
  2065. prev_env_var, s);
  2066. }
  2067. prev_env_var = s;
  2068. /* Actually copy the string into the environment. */
  2069. memcpy(cp, s, slen+1);
  2070. env->unixoid_environment_block[i] = cp;
  2071. cp += slen+1;
  2072. }
  2073. tor_assert(cp == env->windows_environment_block + total_env_length - 1);
  2074. }
  2075. smartlist_free(env_vars_sorted);
  2076. return env;
  2077. }
  2078. /** Return a newly allocated smartlist containing every variable in
  2079. * this process's environment, as a NUL-terminated string of the form
  2080. * "NAME=VALUE". Note that on some/many/most/all OSes, the parent
  2081. * process can put strings not of that form in our environment;
  2082. * callers should try to not get crashed by that.
  2083. *
  2084. * The returned strings are heap-allocated, and must be freed by the
  2085. * caller. */
  2086. struct smartlist_t *
  2087. get_current_process_environment_variables(void)
  2088. {
  2089. smartlist_t *sl = smartlist_new();
  2090. char **environ_tmp; /* Not const char ** ? Really? */
  2091. for (environ_tmp = get_environment(); *environ_tmp; ++environ_tmp) {
  2092. smartlist_add_strdup(sl, *environ_tmp);
  2093. }
  2094. return sl;
  2095. }
  2096. /** For each string s in <b>env_vars</b> such that
  2097. * environment_variable_names_equal(s, <b>new_var</b>), remove it; if
  2098. * <b>free_p</b> is non-zero, call <b>free_old</b>(s). If
  2099. * <b>new_var</b> contains '=', insert it into <b>env_vars</b>. */
  2100. void
  2101. set_environment_variable_in_smartlist(struct smartlist_t *env_vars,
  2102. const char *new_var,
  2103. void (*free_old)(void*),
  2104. int free_p)
  2105. {
  2106. SMARTLIST_FOREACH_BEGIN(env_vars, const char *, s) {
  2107. if (environment_variable_names_equal(s, new_var)) {
  2108. SMARTLIST_DEL_CURRENT(env_vars, s);
  2109. if (free_p) {
  2110. free_old((void *)s);
  2111. }
  2112. }
  2113. } SMARTLIST_FOREACH_END(s);
  2114. if (strchr(new_var, '=') != NULL) {
  2115. smartlist_add(env_vars, (void *)new_var);
  2116. }
  2117. }
  2118. #ifdef _WIN32
  2119. /** Read from a handle <b>h</b> into <b>buf</b>, up to <b>count</b> bytes. If
  2120. * <b>hProcess</b> is NULL, the function will return immediately if there is
  2121. * nothing more to read. Otherwise <b>hProcess</b> should be set to the handle
  2122. * to the process owning the <b>h</b>. In this case, the function will exit
  2123. * only once the process has exited, or <b>count</b> bytes are read. Returns
  2124. * the number of bytes read, or -1 on error. */
  2125. ssize_t
  2126. tor_read_all_handle(HANDLE h, char *buf, size_t count,
  2127. const process_handle_t *process)
  2128. {
  2129. size_t numread = 0;
  2130. BOOL retval;
  2131. DWORD byte_count;
  2132. BOOL process_exited = FALSE;
  2133. if (count > SIZE_T_CEILING || count > SSIZE_MAX)
  2134. return -1;
  2135. while (numread < count) {
  2136. /* Check if there is anything to read */
  2137. retval = PeekNamedPipe(h, NULL, 0, NULL, &byte_count, NULL);
  2138. if (!retval) {
  2139. log_warn(LD_GENERAL,
  2140. "Failed to peek from handle: %s",
  2141. format_win32_error(GetLastError()));
  2142. return -1;
  2143. } else if (0 == byte_count) {
  2144. /* Nothing available: process exited or it is busy */
  2145. /* Exit if we don't know whether the process is running */
  2146. if (NULL == process)
  2147. break;
  2148. /* The process exited and there's nothing left to read from it */
  2149. if (process_exited)
  2150. break;
  2151. /* If process is not running, check for output one more time in case
  2152. it wrote something after the peek was performed. Otherwise keep on
  2153. waiting for output */
  2154. tor_assert(process != NULL);
  2155. byte_count = WaitForSingleObject(process->pid.hProcess, 0);
  2156. if (WAIT_TIMEOUT != byte_count)
  2157. process_exited = TRUE;
  2158. continue;
  2159. }
  2160. /* There is data to read; read it */
  2161. retval = ReadFile(h, buf+numread, count-numread, &byte_count, NULL);
  2162. tor_assert(byte_count + numread <= count);
  2163. if (!retval) {
  2164. log_warn(LD_GENERAL, "Failed to read from handle: %s",
  2165. format_win32_error(GetLastError()));
  2166. return -1;
  2167. } else if (0 == byte_count) {
  2168. /* End of file */
  2169. break;
  2170. }
  2171. numread += byte_count;
  2172. }
  2173. return (ssize_t)numread;
  2174. }
  2175. #else /* !(defined(_WIN32)) */
  2176. /** Read from a handle <b>fd</b> into <b>buf</b>, up to <b>count</b> bytes. If
  2177. * <b>process</b> is NULL, the function will return immediately if there is
  2178. * nothing more to read. Otherwise data will be read until end of file, or
  2179. * <b>count</b> bytes are read. Returns the number of bytes read, or -1 on
  2180. * error. Sets <b>eof</b> to true if <b>eof</b> is not NULL and the end of the
  2181. * file has been reached. */
  2182. ssize_t
  2183. tor_read_all_handle(int fd, char *buf, size_t count,
  2184. const process_handle_t *process,
  2185. int *eof)
  2186. {
  2187. size_t numread = 0;
  2188. ssize_t result;
  2189. if (eof)
  2190. *eof = 0;
  2191. if (count > SIZE_T_CEILING || count > SSIZE_MAX)
  2192. return -1;
  2193. while (numread < count) {
  2194. result = read(fd, buf+numread, count-numread);
  2195. if (result == 0) {
  2196. log_debug(LD_GENERAL, "read() reached end of file");
  2197. if (eof)
  2198. *eof = 1;
  2199. break;
  2200. } else if (result < 0 && errno == EAGAIN) {
  2201. if (process)
  2202. continue;
  2203. else
  2204. break;
  2205. } else if (result < 0) {
  2206. log_warn(LD_GENERAL, "read() failed: %s", strerror(errno));
  2207. return -1;
  2208. }
  2209. numread += result;
  2210. }
  2211. log_debug(LD_GENERAL, "read() read %d bytes from handle", (int)numread);
  2212. return (ssize_t)numread;
  2213. }
  2214. #endif /* defined(_WIN32) */
  2215. /** Read from stdout of a process until the process exits. */
  2216. ssize_t
  2217. tor_read_all_from_process_stdout(const process_handle_t *process_handle,
  2218. char *buf, size_t count)
  2219. {
  2220. #ifdef _WIN32
  2221. return tor_read_all_handle(process_handle->stdout_pipe, buf, count,
  2222. process_handle);
  2223. #else
  2224. return tor_read_all_handle(process_handle->stdout_pipe, buf, count,
  2225. process_handle, NULL);
  2226. #endif /* defined(_WIN32) */
  2227. }
  2228. /** Read from stdout of a process until the process exits. */
  2229. ssize_t
  2230. tor_read_all_from_process_stderr(const process_handle_t *process_handle,
  2231. char *buf, size_t count)
  2232. {
  2233. #ifdef _WIN32
  2234. return tor_read_all_handle(process_handle->stderr_pipe, buf, count,
  2235. process_handle);
  2236. #else
  2237. return tor_read_all_handle(process_handle->stderr_pipe, buf, count,
  2238. process_handle, NULL);
  2239. #endif /* defined(_WIN32) */
  2240. }
  2241. /** Split buf into lines, and add to smartlist. The buffer <b>buf</b> will be
  2242. * modified. The resulting smartlist will consist of pointers to buf, so there
  2243. * is no need to free the contents of sl. <b>buf</b> must be a NUL-terminated
  2244. * string. <b>len</b> should be set to the length of the buffer excluding the
  2245. * NUL. Non-printable characters (including NUL) will be replaced with "." */
  2246. int
  2247. tor_split_lines(smartlist_t *sl, char *buf, int len)
  2248. {
  2249. /* Index in buf of the start of the current line */
  2250. int start = 0;
  2251. /* Index in buf of the current character being processed */
  2252. int cur = 0;
  2253. /* Are we currently in a line */
  2254. char in_line = 0;
  2255. /* Loop over string */
  2256. while (cur < len) {
  2257. /* Loop until end of line or end of string */
  2258. for (; cur < len; cur++) {
  2259. if (in_line) {
  2260. if ('\r' == buf[cur] || '\n' == buf[cur]) {
  2261. /* End of line */
  2262. buf[cur] = '\0';
  2263. /* Point cur to the next line */
  2264. cur++;
  2265. /* Line starts at start and ends with a nul */
  2266. break;
  2267. } else {
  2268. if (!TOR_ISPRINT(buf[cur]))
  2269. buf[cur] = '.';
  2270. }
  2271. } else {
  2272. if ('\r' == buf[cur] || '\n' == buf[cur]) {
  2273. /* Skip leading vertical space */
  2274. ;
  2275. } else {
  2276. in_line = 1;
  2277. start = cur;
  2278. if (!TOR_ISPRINT(buf[cur]))
  2279. buf[cur] = '.';
  2280. }
  2281. }
  2282. }
  2283. /* We are at the end of the line or end of string. If in_line is true there
  2284. * is a line which starts at buf+start and ends at a NUL. cur points to
  2285. * the character after the NUL. */
  2286. if (in_line)
  2287. smartlist_add(sl, (void *)(buf+start));
  2288. in_line = 0;
  2289. }
  2290. return smartlist_len(sl);
  2291. }
  2292. /** Return a string corresponding to <b>stream_status</b>. */
  2293. const char *
  2294. stream_status_to_string(enum stream_status stream_status)
  2295. {
  2296. switch (stream_status) {
  2297. case IO_STREAM_OKAY:
  2298. return "okay";
  2299. case IO_STREAM_EAGAIN:
  2300. return "temporarily unavailable";
  2301. case IO_STREAM_TERM:
  2302. return "terminated";
  2303. case IO_STREAM_CLOSED:
  2304. return "closed";
  2305. default:
  2306. tor_fragile_assert();
  2307. return "unknown";
  2308. }
  2309. }
  2310. #ifdef _WIN32
  2311. /** Return a smartlist containing lines outputted from
  2312. * <b>handle</b>. Return NULL on error, and set
  2313. * <b>stream_status_out</b> appropriately. */
  2314. MOCK_IMPL(smartlist_t *,
  2315. tor_get_lines_from_handle, (HANDLE *handle,
  2316. enum stream_status *stream_status_out))
  2317. {
  2318. int pos;
  2319. char stdout_buf[600] = {0};
  2320. smartlist_t *lines = NULL;
  2321. tor_assert(stream_status_out);
  2322. *stream_status_out = IO_STREAM_TERM;
  2323. pos = tor_read_all_handle(handle, stdout_buf, sizeof(stdout_buf) - 1, NULL);
  2324. if (pos < 0) {
  2325. *stream_status_out = IO_STREAM_TERM;
  2326. return NULL;
  2327. }
  2328. if (pos == 0) {
  2329. *stream_status_out = IO_STREAM_EAGAIN;
  2330. return NULL;
  2331. }
  2332. /* End with a null even if there isn't a \r\n at the end */
  2333. /* TODO: What if this is a partial line? */
  2334. stdout_buf[pos] = '\0';
  2335. /* Split up the buffer */
  2336. lines = smartlist_new();
  2337. tor_split_lines(lines, stdout_buf, pos);
  2338. /* Currently 'lines' is populated with strings residing on the
  2339. stack. Replace them with their exact copies on the heap: */
  2340. SMARTLIST_FOREACH(lines, char *, line,
  2341. SMARTLIST_REPLACE_CURRENT(lines, line, tor_strdup(line)));
  2342. *stream_status_out = IO_STREAM_OKAY;
  2343. return lines;
  2344. }
  2345. #else /* !(defined(_WIN32)) */
  2346. /** Return a smartlist containing lines outputted from
  2347. * <b>fd</b>. Return NULL on error, and set
  2348. * <b>stream_status_out</b> appropriately. */
  2349. MOCK_IMPL(smartlist_t *,
  2350. tor_get_lines_from_handle, (int fd, enum stream_status *stream_status_out))
  2351. {
  2352. enum stream_status stream_status;
  2353. char stdout_buf[400];
  2354. smartlist_t *lines = NULL;
  2355. while (1) {
  2356. memset(stdout_buf, 0, sizeof(stdout_buf));
  2357. stream_status = get_string_from_pipe(fd,
  2358. stdout_buf, sizeof(stdout_buf) - 1);
  2359. if (stream_status != IO_STREAM_OKAY)
  2360. goto done;
  2361. if (!lines) lines = smartlist_new();
  2362. smartlist_split_string(lines, stdout_buf, "\n", 0, 0);
  2363. }
  2364. done:
  2365. *stream_status_out = stream_status;
  2366. return lines;
  2367. }
  2368. #endif /* defined(_WIN32) */
  2369. /** Reads from <b>fd</b> and stores input in <b>buf_out</b> making
  2370. * sure it's below <b>count</b> bytes.
  2371. * If the string has a trailing newline, we strip it off.
  2372. *
  2373. * This function is specifically created to handle input from managed
  2374. * proxies, according to the pluggable transports spec. Make sure it
  2375. * fits your needs before using it.
  2376. *
  2377. * Returns:
  2378. * IO_STREAM_CLOSED: If the stream is closed.
  2379. * IO_STREAM_EAGAIN: If there is nothing to read and we should check back
  2380. * later.
  2381. * IO_STREAM_TERM: If something is wrong with the stream.
  2382. * IO_STREAM_OKAY: If everything went okay and we got a string
  2383. * in <b>buf_out</b>. */
  2384. enum stream_status
  2385. get_string_from_pipe(int fd, char *buf_out, size_t count)
  2386. {
  2387. ssize_t ret;
  2388. tor_assert(count <= INT_MAX);
  2389. ret = read(fd, buf_out, count);
  2390. if (ret == 0)
  2391. return IO_STREAM_CLOSED;
  2392. else if (ret < 0 && errno == EAGAIN)
  2393. return IO_STREAM_EAGAIN;
  2394. else if (ret < 0)
  2395. return IO_STREAM_TERM;
  2396. if (buf_out[ret - 1] == '\n') {
  2397. /* Remove the trailing newline */
  2398. buf_out[ret - 1] = '\0';
  2399. } else
  2400. buf_out[ret] = '\0';
  2401. return IO_STREAM_OKAY;
  2402. }
  2403. /** Initialize the insecure RNG <b>rng</b> from a seed value <b>seed</b>. */
  2404. void
  2405. tor_init_weak_random(tor_weak_rng_t *rng, unsigned seed)
  2406. {
  2407. rng->state = (uint32_t)(seed & 0x7fffffff);
  2408. }
  2409. /** Return a randomly chosen value in the range 0..TOR_WEAK_RANDOM_MAX based
  2410. * on the RNG state of <b>rng</b>. This entropy will not be cryptographically
  2411. * strong; do not rely on it for anything an adversary should not be able to
  2412. * predict. */
  2413. int32_t
  2414. tor_weak_random(tor_weak_rng_t *rng)
  2415. {
  2416. /* Here's a linear congruential generator. OpenBSD and glibc use these
  2417. * parameters; they aren't too bad, and should have maximal period over the
  2418. * range 0..INT32_MAX. We don't want to use the platform rand() or random(),
  2419. * since some platforms have bad weak RNGs that only return values in the
  2420. * range 0..INT16_MAX, which just isn't enough. */
  2421. rng->state = (rng->state * 1103515245 + 12345) & 0x7fffffff;
  2422. return (int32_t) rng->state;
  2423. }
  2424. /** Return a random number in the range [0 , <b>top</b>). {That is, the range
  2425. * of integers i such that 0 <= i < top.} Chooses uniformly. Requires that
  2426. * top is greater than 0. This randomness is not cryptographically strong; do
  2427. * not rely on it for anything an adversary should not be able to predict. */
  2428. int32_t
  2429. tor_weak_random_range(tor_weak_rng_t *rng, int32_t top)
  2430. {
  2431. /* We don't want to just do tor_weak_random() % top, since random() is often
  2432. * implemented with an LCG whose modulus is a power of 2, and those are
  2433. * cyclic in their low-order bits. */
  2434. int divisor, result;
  2435. tor_assert(top > 0);
  2436. divisor = TOR_WEAK_RANDOM_MAX / top;
  2437. do {
  2438. result = (int32_t)(tor_weak_random(rng) / divisor);
  2439. } while (result >= top);
  2440. return result;
  2441. }
  2442. /** Cast a given double value to a int64_t. Return 0 if number is NaN.
  2443. * Returns either INT64_MIN or INT64_MAX if number is outside of the int64_t
  2444. * range. */
  2445. int64_t
  2446. clamp_double_to_int64(double number)
  2447. {
  2448. int exponent;
  2449. #if defined(MINGW_ANY) && GCC_VERSION >= 409
  2450. /*
  2451. Mingw's math.h uses gcc's __builtin_choose_expr() facility to declare
  2452. isnan, isfinite, and signbit. But as implemented in at least some
  2453. versions of gcc, __builtin_choose_expr() can generate type warnings
  2454. even from branches that are not taken. So, suppress those warnings.
  2455. */
  2456. #define PROBLEMATIC_FLOAT_CONVERSION_WARNING
  2457. DISABLE_GCC_WARNING(float-conversion)
  2458. #endif /* defined(MINGW_ANY) && GCC_VERSION >= 409 */
  2459. /*
  2460. With clang 4.0 we apparently run into "double promotion" warnings here,
  2461. since clang thinks we're promoting a double to a long double.
  2462. */
  2463. #if defined(__clang__)
  2464. #if __has_warning("-Wdouble-promotion")
  2465. #define PROBLEMATIC_DOUBLE_PROMOTION_WARNING
  2466. DISABLE_GCC_WARNING(double-promotion)
  2467. #endif
  2468. #endif /* defined(__clang__) */
  2469. /* NaN is a special case that can't be used with the logic below. */
  2470. if (isnan(number)) {
  2471. return 0;
  2472. }
  2473. /* Time to validate if result can overflows a int64_t value. Fun with
  2474. * float! Find that exponent exp such that
  2475. * number == x * 2^exp
  2476. * for some x with abs(x) in [0.5, 1.0). Note that this implies that the
  2477. * magnitude of number is strictly less than 2^exp.
  2478. *
  2479. * If number is infinite, the call to frexp is legal but the contents of
  2480. * are exponent unspecified. */
  2481. frexp(number, &exponent);
  2482. /* If the magnitude of number is strictly less than 2^63, the truncated
  2483. * version of number is guaranteed to be representable. The only
  2484. * representable integer for which this is not the case is INT64_MIN, but
  2485. * it is covered by the logic below. */
  2486. if (isfinite(number) && exponent <= 63) {
  2487. return (int64_t)number;
  2488. }
  2489. /* Handle infinities and finite numbers with magnitude >= 2^63. */
  2490. return signbit(number) ? INT64_MIN : INT64_MAX;
  2491. #ifdef PROBLEMATIC_DOUBLE_PROMOTION_WARNING
  2492. ENABLE_GCC_WARNING(double-promotion)
  2493. #endif
  2494. #ifdef PROBLEMATIC_FLOAT_CONVERSION_WARNING
  2495. ENABLE_GCC_WARNING(float-conversion)
  2496. #endif
  2497. }
  2498. /** Return a uint64_t value from <b>a</b> in network byte order. */
  2499. uint64_t
  2500. tor_htonll(uint64_t a)
  2501. {
  2502. #ifdef WORDS_BIGENDIAN
  2503. /* Big endian. */
  2504. return a;
  2505. #else /* WORDS_BIGENDIAN */
  2506. /* Little endian. The worst... */
  2507. return htonl((uint32_t)(a>>32)) |
  2508. (((uint64_t)htonl((uint32_t)a))<<32);
  2509. #endif /* defined(WORDS_BIGENDIAN) */
  2510. }
  2511. /** Return a uint64_t value from <b>a</b> in host byte order. */
  2512. uint64_t
  2513. tor_ntohll(uint64_t a)
  2514. {
  2515. return tor_htonll(a);
  2516. }