util.c 82 KB

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