os_priority.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2002, 2003, 2004, 2005, 2006, 2011 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/os_priority.c
  18. * @brief Methods to set process priority
  19. * @author Nils Durner
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "disk.h"
  24. #include <unistr.h>
  25. #define LOG(kind,...) GNUNET_log_from (kind, "util-os-priority", __VA_ARGS__)
  26. #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-os-priority", syscall)
  27. #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, filename)
  28. #define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE"
  29. struct GNUNET_OS_Process
  30. {
  31. /**
  32. * PID of the process.
  33. */
  34. pid_t pid;
  35. #if WINDOWS
  36. /**
  37. * Process handle.
  38. */
  39. HANDLE handle;
  40. #endif
  41. /**
  42. * Pipe we use to signal the process.
  43. * NULL if unused, or if process was deemed uncontrollable.
  44. */
  45. struct GNUNET_DISK_FileHandle *control_pipe;
  46. };
  47. /**
  48. * Handle for 'this' process.
  49. */
  50. static struct GNUNET_OS_Process current_process;
  51. /**
  52. * Handle for the #parent_control_handler() Task.
  53. */
  54. static struct GNUNET_SCHEDULER_Task *pch;
  55. /**
  56. * Handle for the #shutdown_pch() Task.
  57. */
  58. static struct GNUNET_SCHEDULER_Task *spch;
  59. /**
  60. * This handler is called on shutdown to remove the #pch.
  61. *
  62. * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
  63. */
  64. static void
  65. shutdown_pch (void *cls)
  66. {
  67. struct GNUNET_DISK_FileHandle *control_pipe = cls;
  68. GNUNET_SCHEDULER_cancel (pch);
  69. pch = NULL;
  70. GNUNET_DISK_file_close (control_pipe);
  71. control_pipe = NULL;
  72. }
  73. /**
  74. * This handler is called when there are control data to be read on the pipe
  75. *
  76. * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
  77. */
  78. static void
  79. parent_control_handler (void *cls)
  80. {
  81. struct GNUNET_DISK_FileHandle *control_pipe = cls;
  82. char sig;
  83. char *pipe_fd;
  84. ssize_t ret;
  85. pch = NULL;
  86. ret = GNUNET_DISK_file_read (control_pipe,
  87. &sig,
  88. sizeof (sig));
  89. if (sizeof (sig) != ret)
  90. {
  91. if (-1 == ret)
  92. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
  93. "GNUNET_DISK_file_read");
  94. LOG (GNUNET_ERROR_TYPE_DEBUG,
  95. "Closing control pipe\n");
  96. GNUNET_DISK_file_close (control_pipe);
  97. control_pipe = NULL;
  98. GNUNET_SCHEDULER_cancel (spch);
  99. spch = NULL;
  100. return;
  101. }
  102. pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
  103. GNUNET_assert ( (NULL == pipe_fd) ||
  104. (strlen (pipe_fd) <= 0) );
  105. LOG (GNUNET_ERROR_TYPE_DEBUG,
  106. "Got control code %d from parent via pipe %s\n",
  107. sig,
  108. pipe_fd);
  109. pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
  110. control_pipe,
  111. &parent_control_handler,
  112. control_pipe);
  113. GNUNET_SIGNAL_raise ((int) sig);
  114. }
  115. /**
  116. * Task that connects this process to its parent via pipe;
  117. * essentially, the parent control handler will read signal numbers
  118. * from the #GNUNET_OS_CONTROL_PIPE (as given in an environment
  119. * variable) and raise those signals.
  120. *
  121. * @param cls closure (unused)
  122. */
  123. void
  124. GNUNET_OS_install_parent_control_handler (void *cls)
  125. {
  126. const char *env_buf;
  127. char *env_buf_end;
  128. struct GNUNET_DISK_FileHandle *control_pipe;
  129. uint64_t pipe_fd;
  130. (void) cls;
  131. if (NULL != pch)
  132. {
  133. /* already done, we've been called twice... */
  134. GNUNET_break (0);
  135. return;
  136. }
  137. env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
  138. if ( (NULL == env_buf) || (strlen (env_buf) <= 0) )
  139. {
  140. LOG (GNUNET_ERROR_TYPE_DEBUG,
  141. "Not installing a handler because $%s is empty\n",
  142. GNUNET_OS_CONTROL_PIPE);
  143. putenv (GNUNET_OS_CONTROL_PIPE "=");
  144. return;
  145. }
  146. errno = 0;
  147. pipe_fd = strtoull (env_buf, &env_buf_end, 16);
  148. if ((0 != errno) || (env_buf == env_buf_end))
  149. {
  150. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
  151. "strtoull",
  152. env_buf);
  153. putenv (GNUNET_OS_CONTROL_PIPE "=");
  154. return;
  155. }
  156. #if !defined (WINDOWS)
  157. if (pipe_fd >= FD_SETSIZE)
  158. #else
  159. if ((FILE_TYPE_UNKNOWN == GetFileType ((HANDLE) (uintptr_t) pipe_fd))
  160. && (0 != GetLastError ()))
  161. #endif
  162. {
  163. LOG (GNUNET_ERROR_TYPE_ERROR,
  164. "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n",
  165. env_buf);
  166. putenv (GNUNET_OS_CONTROL_PIPE "=");
  167. return;
  168. }
  169. #if WINDOWS
  170. control_pipe = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) (uintptr_t) pipe_fd);
  171. #else
  172. control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
  173. #endif
  174. if (NULL == control_pipe)
  175. {
  176. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
  177. "open",
  178. env_buf);
  179. putenv (GNUNET_OS_CONTROL_PIPE "=");
  180. return;
  181. }
  182. LOG (GNUNET_ERROR_TYPE_DEBUG,
  183. "Adding parent control handler pipe `%s' to the scheduler\n",
  184. env_buf);
  185. pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
  186. control_pipe,
  187. &parent_control_handler,
  188. control_pipe);
  189. spch = GNUNET_SCHEDULER_add_shutdown (&shutdown_pch,
  190. control_pipe);
  191. putenv (GNUNET_OS_CONTROL_PIPE "=");
  192. }
  193. /**
  194. * Get process structure for current process
  195. *
  196. * The pointer it returns points to static memory location and must
  197. * not be deallocated/closed.
  198. *
  199. * @return pointer to the process sturcutre for this process
  200. */
  201. struct GNUNET_OS_Process *
  202. GNUNET_OS_process_current ()
  203. {
  204. #if WINDOWS
  205. current_process.pid = GetCurrentProcessId ();
  206. current_process.handle = GetCurrentProcess ();
  207. #else
  208. current_process.pid = 0;
  209. #endif
  210. return &current_process;
  211. }
  212. /**
  213. * Sends a signal to the process
  214. *
  215. * @param proc pointer to process structure
  216. * @param sig signal
  217. * @return 0 on success, -1 on error
  218. */
  219. int
  220. GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc,
  221. int sig)
  222. {
  223. int ret;
  224. char csig;
  225. csig = (char) sig;
  226. if (NULL != proc->control_pipe)
  227. {
  228. LOG (GNUNET_ERROR_TYPE_DEBUG,
  229. "Sending signal %d to pid: %u via pipe\n",
  230. sig,
  231. proc->pid);
  232. ret = GNUNET_DISK_file_write (proc->control_pipe,
  233. &csig,
  234. sizeof (csig));
  235. if (sizeof (csig) == ret)
  236. return 0;
  237. }
  238. /* pipe failed or non-existent, try other methods */
  239. switch (sig)
  240. {
  241. #if !defined (WINDOWS)
  242. case SIGHUP:
  243. #endif
  244. case SIGINT:
  245. case SIGKILL:
  246. case SIGTERM:
  247. #if (SIGTERM != GNUNET_TERM_SIG)
  248. case GNUNET_TERM_SIG:
  249. #endif
  250. #if defined(WINDOWS) && !defined(__CYGWIN__)
  251. {
  252. DWORD exitcode;
  253. int must_kill = GNUNET_YES;
  254. if (0 != GetExitCodeProcess (proc->handle, &exitcode))
  255. must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
  256. if (GNUNET_YES == must_kill)
  257. {
  258. if (0 == SafeTerminateProcess (proc->handle, 0, 0))
  259. {
  260. DWORD error_code = GetLastError ();
  261. if ( (error_code != WAIT_TIMEOUT) &&
  262. (error_code != ERROR_PROCESS_ABORTED) )
  263. {
  264. LOG ((error_code == ERROR_ACCESS_DENIED) ?
  265. GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
  266. "SafeTermiateProcess failed with code %lu\n",
  267. error_code);
  268. /* The problem here is that a process that is already dying
  269. * might cause SafeTerminateProcess to fail with
  270. * ERROR_ACCESS_DENIED, but the process WILL die eventually.
  271. * If we really had a permissions problem, hanging up (which
  272. * is what will happen in process_wait() in that case) is
  273. * a valid option.
  274. */
  275. if (ERROR_ACCESS_DENIED == error_code)
  276. {
  277. errno = 0;
  278. }
  279. else
  280. {
  281. SetErrnoFromWinError (error_code);
  282. return -1;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. return 0;
  289. #else
  290. LOG (GNUNET_ERROR_TYPE_DEBUG,
  291. "Sending signal %d to pid: %u via system call\n",
  292. sig,
  293. proc->pid);
  294. return PLIBC_KILL (proc->pid, sig);
  295. #endif
  296. default:
  297. #if defined (WINDOWS)
  298. errno = EINVAL;
  299. return -1;
  300. #else
  301. LOG (GNUNET_ERROR_TYPE_DEBUG,
  302. "Sending signal %d to pid: %u via system call\n",
  303. sig,
  304. proc->pid);
  305. return PLIBC_KILL (proc->pid, sig);
  306. #endif
  307. }
  308. }
  309. /**
  310. * Get the pid of the process in question
  311. *
  312. * @param proc the process to get the pid of
  313. *
  314. * @return the current process id
  315. */
  316. pid_t
  317. GNUNET_OS_process_get_pid (struct GNUNET_OS_Process * proc)
  318. {
  319. return proc->pid;
  320. }
  321. /**
  322. * Cleans up process structure contents (OS-dependent) and deallocates
  323. * it.
  324. *
  325. * @param proc pointer to process structure
  326. */
  327. void
  328. GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
  329. {
  330. if (NULL != proc->control_pipe)
  331. GNUNET_DISK_file_close (proc->control_pipe);
  332. #if defined (WINDOWS)
  333. if (NULL != proc->handle)
  334. CloseHandle (proc->handle);
  335. #endif
  336. GNUNET_free (proc);
  337. }
  338. #if WINDOWS
  339. #include "gnunet_signal_lib.h"
  340. extern GNUNET_SIGNAL_Handler w32_sigchld_handler;
  341. /**
  342. * Make seaspider happy.
  343. */
  344. #define DWORD_WINAPI DWORD WINAPI
  345. /**
  346. * @brief Waits for a process to terminate and invokes the SIGCHLD handler
  347. * @param proc pointer to process structure
  348. */
  349. static DWORD_WINAPI
  350. child_wait_thread (void *arg)
  351. {
  352. struct GNUNET_OS_Process *proc = (struct GNUNET_OS_Process *) arg;
  353. WaitForSingleObject (proc->handle, INFINITE);
  354. if (w32_sigchld_handler)
  355. w32_sigchld_handler ();
  356. return 0;
  357. }
  358. #endif
  359. #if MINGW
  360. static char *
  361. CreateCustomEnvTable (char **vars)
  362. {
  363. char *win32_env_table;
  364. char *ptr;
  365. char **var_ptr;
  366. char *result;
  367. char *result_ptr;
  368. size_t tablesize = 0;
  369. size_t items_count = 0;
  370. size_t n_found = 0;
  371. size_t n_var;
  372. char *index = NULL;
  373. size_t c;
  374. size_t var_len;
  375. char *var;
  376. char *val;
  377. win32_env_table = GetEnvironmentStringsA ();
  378. if (NULL == win32_env_table)
  379. return NULL;
  380. for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ;
  381. n_var = c;
  382. index = GNUNET_malloc (sizeof (char *) * n_var);
  383. for (c = 0; c < n_var; c++)
  384. index[c] = 0;
  385. for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
  386. {
  387. size_t len = strlen (ptr);
  388. int found = 0;
  389. for (var_ptr = vars; *var_ptr; var_ptr++)
  390. {
  391. var = *var_ptr++;
  392. val = *var_ptr;
  393. var_len = strlen (var);
  394. if (strncmp (var, ptr, var_len) == 0)
  395. {
  396. found = 1;
  397. index[c] = 1;
  398. tablesize += var_len + strlen (val) + 1;
  399. break;
  400. }
  401. }
  402. if (!found)
  403. tablesize += len + 1;
  404. ptr += len + 1;
  405. }
  406. for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
  407. {
  408. var = *var_ptr++;
  409. val = *var_ptr;
  410. if (index[c] != 1)
  411. n_found += strlen (var) + strlen (val) + 1;
  412. }
  413. result = GNUNET_malloc (tablesize + n_found + 1);
  414. for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
  415. {
  416. size_t len = strlen (ptr);
  417. int found = 0;
  418. for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
  419. {
  420. var = *var_ptr++;
  421. val = *var_ptr;
  422. var_len = strlen (var);
  423. if (strncmp (var, ptr, var_len) == 0)
  424. {
  425. found = 1;
  426. break;
  427. }
  428. }
  429. if (!found)
  430. {
  431. strcpy (result_ptr, ptr);
  432. result_ptr += len + 1;
  433. }
  434. else
  435. {
  436. strcpy (result_ptr, var);
  437. result_ptr += var_len;
  438. strcpy (result_ptr, val);
  439. result_ptr += strlen (val) + 1;
  440. }
  441. ptr += len + 1;
  442. }
  443. for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
  444. {
  445. var = *var_ptr++;
  446. val = *var_ptr;
  447. var_len = strlen (var);
  448. if (index[c] != 1)
  449. {
  450. strcpy (result_ptr, var);
  451. result_ptr += var_len;
  452. strcpy (result_ptr, val);
  453. result_ptr += strlen (val) + 1;
  454. }
  455. }
  456. FreeEnvironmentStrings (win32_env_table);
  457. GNUNET_free (index);
  458. *result_ptr = 0;
  459. return result;
  460. }
  461. #else
  462. /**
  463. * Open '/dev/null' and make the result the given
  464. * file descriptor.
  465. *
  466. * @param target_fd desired FD to point to /dev/null
  467. * @param flags open flags (O_RDONLY, O_WRONLY)
  468. */
  469. static void
  470. open_dev_null (int target_fd,
  471. int flags)
  472. {
  473. int fd;
  474. fd = open ("/dev/null", flags);
  475. if (-1 == fd)
  476. {
  477. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
  478. "open",
  479. "/dev/null");
  480. return;
  481. }
  482. if (fd == target_fd)
  483. return;
  484. if (-1 == dup2 (fd, target_fd))
  485. {
  486. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
  487. (void) close (fd);
  488. return;
  489. }
  490. GNUNET_break (0 == close (fd));
  491. }
  492. #endif
  493. /**
  494. * Start a process.
  495. *
  496. * @param pipe_control should a pipe be used to send signals to the child?
  497. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
  498. * std handles of the parent are inherited by the child.
  499. * pipe_stdin and pipe_stdout take priority over std_inheritance
  500. * (when they are non-NULL).
  501. * @param pipe_stdin pipe to use to send input to child process (or NULL)
  502. * @param pipe_stdout pipe to use to get output from child process (or NULL)
  503. * @param pipe_stderr pipe to use for stderr for child process (or NULL)
  504. * @param lsocks array of listen sockets to dup systemd-style (or NULL);
  505. * must be NULL on platforms where dup is not supported
  506. * @param filename name of the binary
  507. * @param argv NULL-terminated list of arguments to the process
  508. * @return process ID of the new process, -1 on error
  509. */
  510. static struct GNUNET_OS_Process *
  511. start_process (int pipe_control,
  512. enum GNUNET_OS_InheritStdioFlags std_inheritance,
  513. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  514. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  515. struct GNUNET_DISK_PipeHandle *pipe_stderr,
  516. const SOCKTYPE *lsocks,
  517. const char *filename,
  518. char *const argv[])
  519. {
  520. #ifndef MINGW
  521. pid_t ret;
  522. char fds[16];
  523. struct GNUNET_OS_Process *gnunet_proc;
  524. struct GNUNET_DISK_FileHandle *childpipe_read;
  525. struct GNUNET_DISK_FileHandle *childpipe_write;
  526. int childpipe_read_fd;
  527. int i;
  528. int j;
  529. int k;
  530. int tgt;
  531. int flags;
  532. int *lscp;
  533. unsigned int ls;
  534. int fd_stdout_write;
  535. int fd_stdout_read;
  536. int fd_stderr_write;
  537. int fd_stderr_read;
  538. int fd_stdin_read;
  539. int fd_stdin_write;
  540. if (GNUNET_SYSERR ==
  541. GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
  542. return NULL; /* not executable */
  543. if (GNUNET_YES == pipe_control)
  544. {
  545. struct GNUNET_DISK_PipeHandle *childpipe;
  546. int dup_childpipe_read_fd = -1;
  547. childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
  548. GNUNET_YES, GNUNET_NO);
  549. if (NULL == childpipe)
  550. return NULL;
  551. childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe,
  552. GNUNET_DISK_PIPE_END_READ);
  553. childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe,
  554. GNUNET_DISK_PIPE_END_WRITE);
  555. GNUNET_DISK_pipe_close (childpipe);
  556. if ( (NULL == childpipe_read) ||
  557. (NULL == childpipe_write) ||
  558. (GNUNET_OK !=
  559. GNUNET_DISK_internal_file_handle_ (childpipe_read,
  560. &childpipe_read_fd,
  561. sizeof (int))) ||
  562. (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
  563. {
  564. if (NULL != childpipe_read)
  565. GNUNET_DISK_file_close (childpipe_read);
  566. if (NULL != childpipe_write)
  567. GNUNET_DISK_file_close (childpipe_write);
  568. if (0 <= dup_childpipe_read_fd)
  569. close (dup_childpipe_read_fd);
  570. return NULL;
  571. }
  572. childpipe_read_fd = dup_childpipe_read_fd;
  573. GNUNET_DISK_file_close (childpipe_read);
  574. }
  575. else
  576. {
  577. childpipe_write = NULL;
  578. childpipe_read_fd = -1;
  579. }
  580. if (NULL != pipe_stdin)
  581. {
  582. GNUNET_assert (GNUNET_OK ==
  583. GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
  584. (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
  585. &fd_stdin_read, sizeof (int)));
  586. GNUNET_assert (GNUNET_OK ==
  587. GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
  588. (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
  589. &fd_stdin_write, sizeof (int)));
  590. }
  591. if (NULL != pipe_stdout)
  592. {
  593. GNUNET_assert (GNUNET_OK ==
  594. GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
  595. (pipe_stdout,
  596. GNUNET_DISK_PIPE_END_WRITE),
  597. &fd_stdout_write, sizeof (int)));
  598. GNUNET_assert (GNUNET_OK ==
  599. GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
  600. (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
  601. &fd_stdout_read, sizeof (int)));
  602. }
  603. if (NULL != pipe_stderr)
  604. {
  605. GNUNET_assert (GNUNET_OK ==
  606. GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
  607. (pipe_stderr,
  608. GNUNET_DISK_PIPE_END_READ),
  609. &fd_stderr_read, sizeof (int)));
  610. GNUNET_assert (GNUNET_OK ==
  611. GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
  612. (pipe_stderr,
  613. GNUNET_DISK_PIPE_END_WRITE),
  614. &fd_stderr_write, sizeof (int)));
  615. }
  616. lscp = NULL;
  617. ls = 0;
  618. if (NULL != lsocks)
  619. {
  620. i = 0;
  621. while (-1 != (k = lsocks[i++]))
  622. GNUNET_array_append (lscp, ls, k);
  623. GNUNET_array_append (lscp, ls, -1);
  624. }
  625. #if DARWIN
  626. /* see https://gnunet.org/vfork */
  627. ret = vfork ();
  628. #else
  629. ret = fork ();
  630. #endif
  631. if (-1 == ret)
  632. {
  633. int eno = errno;
  634. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
  635. GNUNET_array_grow (lscp, ls, 0);
  636. if (NULL != childpipe_write)
  637. GNUNET_DISK_file_close (childpipe_write);
  638. if (0 <= childpipe_read_fd)
  639. close (childpipe_read_fd);
  640. errno = eno;
  641. return NULL;
  642. }
  643. if (0 != ret)
  644. {
  645. unsetenv (GNUNET_OS_CONTROL_PIPE);
  646. gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
  647. gnunet_proc->pid = ret;
  648. gnunet_proc->control_pipe = childpipe_write;
  649. if (GNUNET_YES == pipe_control)
  650. {
  651. close (childpipe_read_fd);
  652. }
  653. GNUNET_array_grow (lscp, ls, 0);
  654. return gnunet_proc;
  655. }
  656. if (0 <= childpipe_read_fd)
  657. {
  658. char fdbuf[100];
  659. #ifndef DARWIN
  660. /* due to vfork, we must NOT free memory on DARWIN! */
  661. GNUNET_DISK_file_close (childpipe_write);
  662. #endif
  663. snprintf (fdbuf, 100, "%x", childpipe_read_fd);
  664. setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
  665. }
  666. else
  667. unsetenv (GNUNET_OS_CONTROL_PIPE);
  668. if (NULL != pipe_stdin)
  669. {
  670. GNUNET_break (0 == close (fd_stdin_write));
  671. if (-1 == dup2 (fd_stdin_read, 0))
  672. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
  673. GNUNET_break (0 == close (fd_stdin_read));
  674. }
  675. else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
  676. {
  677. GNUNET_break (0 == close (0));
  678. open_dev_null (0, O_RDONLY);
  679. }
  680. if (NULL != pipe_stdout)
  681. {
  682. GNUNET_break (0 == close (fd_stdout_read));
  683. if (-1 == dup2 (fd_stdout_write, 1))
  684. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
  685. GNUNET_break (0 == close (fd_stdout_write));
  686. }
  687. else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
  688. {
  689. GNUNET_break (0 == close (1));
  690. open_dev_null (1, O_WRONLY);
  691. }
  692. if (NULL != pipe_stderr)
  693. {
  694. GNUNET_break (0 == close (fd_stderr_read));
  695. if (-1 == dup2 (fd_stderr_write, 2))
  696. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
  697. GNUNET_break (0 == close (fd_stderr_write));
  698. }
  699. else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
  700. {
  701. GNUNET_break (0 == close (2));
  702. open_dev_null (2, O_WRONLY);
  703. }
  704. if (NULL != lscp)
  705. {
  706. /* read systemd documentation... */
  707. i = 0;
  708. tgt = 3;
  709. while (-1 != lscp[i])
  710. {
  711. j = i + 1;
  712. while (-1 != lscp[j])
  713. {
  714. if (lscp[j] == tgt)
  715. {
  716. /* dup away */
  717. k = dup (lscp[j]);
  718. GNUNET_assert (-1 != k);
  719. GNUNET_assert (0 == close (lscp[j]));
  720. lscp[j] = k;
  721. break;
  722. }
  723. j++;
  724. }
  725. if (lscp[i] != tgt)
  726. {
  727. /* Bury any existing FD, no matter what; they should all be closed
  728. * on exec anyway and the important onces have been dup'ed away */
  729. (void) close (tgt);
  730. GNUNET_assert (-1 != dup2 (lscp[i], tgt));
  731. }
  732. /* unset close-on-exec flag */
  733. flags = fcntl (tgt, F_GETFD);
  734. GNUNET_assert (flags >= 0);
  735. flags &= ~FD_CLOEXEC;
  736. fflush (stderr);
  737. (void) fcntl (tgt, F_SETFD, flags);
  738. tgt++;
  739. i++;
  740. }
  741. GNUNET_snprintf (fds, sizeof (fds), "%u", i);
  742. setenv ("LISTEN_FDS", fds, 1);
  743. }
  744. #ifndef DARWIN
  745. /* due to vfork, we must NOT free memory on DARWIN! */
  746. GNUNET_array_grow (lscp, ls, 0);
  747. #endif
  748. execvp (filename, argv);
  749. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
  750. _exit (1);
  751. #else
  752. struct GNUNET_DISK_FileHandle *childpipe_read;
  753. struct GNUNET_DISK_FileHandle *childpipe_write;
  754. HANDLE childpipe_read_handle;
  755. char **arg;
  756. char **non_const_argv;
  757. unsigned int cmdlen;
  758. char *cmd;
  759. char *idx;
  760. STARTUPINFOW start;
  761. PROCESS_INFORMATION proc;
  762. int argcount = 0;
  763. struct GNUNET_OS_Process *gnunet_proc;
  764. char path[MAX_PATH + 1];
  765. char *our_env[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  766. char *env_block = NULL;
  767. char *pathbuf;
  768. DWORD pathbuf_len;
  769. DWORD alloc_len;
  770. char *self_prefix;
  771. char *bindir;
  772. char *libdir;
  773. char *ptr;
  774. char *non_const_filename;
  775. char win_path[MAX_PATH + 1];
  776. struct GNUNET_DISK_PipeHandle *lsocks_pipe;
  777. const struct GNUNET_DISK_FileHandle *lsocks_write_fd;
  778. HANDLE lsocks_read;
  779. HANDLE lsocks_write;
  780. wchar_t *wpath;
  781. wchar_t *wcmd;
  782. size_t wpath_len;
  783. size_t wcmd_len;
  784. int env_off;
  785. int fail;
  786. long lRet;
  787. HANDLE stdin_handle;
  788. HANDLE stdout_handle;
  789. HANDLE stdih, stdoh, stdeh;
  790. DWORD stdif, stdof, stdef;
  791. BOOL bresult;
  792. DWORD error_code;
  793. DWORD create_no_window;
  794. if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
  795. return NULL; /* not executable */
  796. /* Search in prefix dir (hopefully - the directory from which
  797. * the current module was loaded), bindir and libdir, then in PATH
  798. */
  799. self_prefix = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_SELF_PREFIX);
  800. bindir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
  801. libdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
  802. pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
  803. alloc_len =
  804. pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
  805. strlen (libdir);
  806. pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
  807. ptr = pathbuf;
  808. ptr += sprintf (pathbuf, "%s;%s;%s;", self_prefix, bindir, libdir);
  809. GNUNET_free (self_prefix);
  810. GNUNET_free (bindir);
  811. GNUNET_free (libdir);
  812. alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len);
  813. if (alloc_len != pathbuf_len - 1)
  814. {
  815. GNUNET_free (pathbuf);
  816. errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */
  817. return NULL;
  818. }
  819. cmdlen = strlen (filename);
  820. if ( (cmdlen < 5) || (0 != strcmp (&filename[cmdlen - 4], ".exe")) )
  821. GNUNET_asprintf (&non_const_filename, "%s.exe", filename);
  822. else
  823. GNUNET_asprintf (&non_const_filename, "%s", filename);
  824. /* It could be in POSIX form, convert it to a DOS path early on */
  825. if (ERROR_SUCCESS != (lRet = plibc_conv_to_win_path (non_const_filename, win_path)))
  826. {
  827. SetErrnoFromWinError (lRet);
  828. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "plibc_conv_to_win_path",
  829. non_const_filename);
  830. GNUNET_free (non_const_filename);
  831. GNUNET_free (pathbuf);
  832. return NULL;
  833. }
  834. GNUNET_free (non_const_filename);
  835. non_const_filename = GNUNET_strdup (win_path);
  836. /* Check that this is the full path. If it isn't, search. */
  837. /* FIXME: convert it to wchar_t and use SearchPathW?
  838. * Remember: arguments to _start_process() are technically in UTF-8...
  839. */
  840. if (non_const_filename[1] == ':')
  841. {
  842. snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
  843. LOG (GNUNET_ERROR_TYPE_DEBUG,
  844. "Using path `%s' as-is. PATH is %s\n", path, ptr);
  845. }
  846. else if (!SearchPathA
  847. (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
  848. path, NULL))
  849. {
  850. SetErrnoFromWinError (GetLastError ());
  851. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
  852. non_const_filename);
  853. GNUNET_free (non_const_filename);
  854. GNUNET_free (pathbuf);
  855. return NULL;
  856. }
  857. else
  858. LOG (GNUNET_ERROR_TYPE_DEBUG,
  859. "Found `%s' in PATH `%s'\n", path, pathbuf);
  860. GNUNET_free (pathbuf);
  861. GNUNET_free (non_const_filename);
  862. /* Count the number of arguments */
  863. arg = (char **) argv;
  864. while (*arg)
  865. {
  866. arg++;
  867. argcount++;
  868. }
  869. /* Allocate a copy argv */
  870. non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1));
  871. /* Copy all argv strings */
  872. argcount = 0;
  873. arg = (char **) argv;
  874. while (*arg)
  875. {
  876. if (arg == argv)
  877. non_const_argv[argcount] = GNUNET_strdup (path);
  878. else
  879. non_const_argv[argcount] = GNUNET_strdup (*arg);
  880. arg++;
  881. argcount++;
  882. }
  883. non_const_argv[argcount] = NULL;
  884. /* Count cmd len */
  885. cmdlen = 1;
  886. arg = non_const_argv;
  887. while (*arg)
  888. {
  889. cmdlen = cmdlen + strlen (*arg) + 4;
  890. arg++;
  891. }
  892. /* Allocate and create cmd */
  893. cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen);
  894. arg = non_const_argv;
  895. while (*arg)
  896. {
  897. char arg_last_char = (*arg)[strlen (*arg) - 1];
  898. idx += sprintf (idx, "\"%s%s\"%s", *arg,
  899. arg_last_char == '\\' ? "\\" : "", *(arg + 1) ? " " : "");
  900. arg++;
  901. }
  902. while (argcount > 0)
  903. GNUNET_free (non_const_argv[--argcount]);
  904. GNUNET_free (non_const_argv);
  905. memset (&start, 0, sizeof (start));
  906. start.cb = sizeof (start);
  907. if ((pipe_stdin != NULL) || (pipe_stdout != NULL) || (std_inheritance != 0))
  908. start.dwFlags |= STARTF_USESTDHANDLES;
  909. stdih = GetStdHandle (STD_INPUT_HANDLE);
  910. GetHandleInformation (stdih, &stdif);
  911. if (pipe_stdin != NULL)
  912. {
  913. GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
  914. (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
  915. &stdin_handle, sizeof (HANDLE));
  916. start.hStdInput = stdin_handle;
  917. }
  918. else if (stdih)
  919. {
  920. if (std_inheritance & GNUNET_OS_INHERIT_STD_IN)
  921. {
  922. SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 1);
  923. if (pipe_stdin == NULL)
  924. start.hStdInput = stdih;
  925. }
  926. else
  927. SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 0);
  928. }
  929. stdoh = GetStdHandle (STD_OUTPUT_HANDLE);
  930. GetHandleInformation (stdoh, &stdof);
  931. if (NULL != pipe_stdout)
  932. {
  933. GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
  934. (pipe_stdout,
  935. GNUNET_DISK_PIPE_END_WRITE),
  936. &stdout_handle, sizeof (HANDLE));
  937. start.hStdOutput = stdout_handle;
  938. }
  939. else if (stdoh)
  940. {
  941. if (std_inheritance & GNUNET_OS_INHERIT_STD_OUT)
  942. {
  943. SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 1);
  944. if (pipe_stdout == NULL)
  945. start.hStdOutput = stdoh;
  946. }
  947. else
  948. SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 0);
  949. }
  950. stdeh = GetStdHandle (STD_ERROR_HANDLE);
  951. GetHandleInformation (stdeh, &stdef);
  952. if (stdeh)
  953. {
  954. if (std_inheritance & GNUNET_OS_INHERIT_STD_ERR)
  955. {
  956. SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 1);
  957. start.hStdError = stdeh;
  958. }
  959. else
  960. SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 0);
  961. }
  962. if (GNUNET_YES == pipe_control)
  963. {
  964. struct GNUNET_DISK_PipeHandle *childpipe;
  965. childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
  966. if (NULL == childpipe)
  967. return NULL;
  968. childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
  969. childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
  970. GNUNET_DISK_pipe_close (childpipe);
  971. if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
  972. (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
  973. &childpipe_read_handle, sizeof (HANDLE))))
  974. {
  975. if (childpipe_read)
  976. GNUNET_DISK_file_close (childpipe_read);
  977. if (childpipe_write)
  978. GNUNET_DISK_file_close (childpipe_write);
  979. GNUNET_free (cmd);
  980. return NULL;
  981. }
  982. /* Unlike *nix variant, we don't dup the handle, so can't close
  983. * filehandle right now.
  984. */
  985. SetHandleInformation (childpipe_read_handle, HANDLE_FLAG_INHERIT, 1);
  986. }
  987. else
  988. {
  989. childpipe_read = NULL;
  990. childpipe_write = NULL;
  991. }
  992. if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
  993. {
  994. lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
  995. if (lsocks_pipe == NULL)
  996. {
  997. GNUNET_free (cmd);
  998. GNUNET_DISK_pipe_close (lsocks_pipe);
  999. if (GNUNET_YES == pipe_control)
  1000. {
  1001. GNUNET_DISK_file_close (childpipe_write);
  1002. GNUNET_DISK_file_close (childpipe_read);
  1003. }
  1004. return NULL;
  1005. }
  1006. lsocks_write_fd = GNUNET_DISK_pipe_handle (lsocks_pipe,
  1007. GNUNET_DISK_PIPE_END_WRITE);
  1008. GNUNET_DISK_internal_file_handle_ (lsocks_write_fd,
  1009. &lsocks_write, sizeof (HANDLE));
  1010. GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
  1011. (lsocks_pipe, GNUNET_DISK_PIPE_END_READ),
  1012. &lsocks_read, sizeof (HANDLE));
  1013. }
  1014. else
  1015. {
  1016. lsocks_pipe = NULL;
  1017. lsocks_write_fd = NULL;
  1018. }
  1019. env_off = 0;
  1020. if (GNUNET_YES == pipe_control)
  1021. {
  1022. GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE);
  1023. GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle);
  1024. }
  1025. if ( (lsocks != NULL) && (lsocks[0] != INVALID_SOCKET))
  1026. {
  1027. /*This will tell the child that we're going to send lsocks over the pipe*/
  1028. GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS");
  1029. GNUNET_asprintf (&our_env[env_off++], "%lu", lsocks_read);
  1030. }
  1031. our_env[env_off++] = NULL;
  1032. env_block = CreateCustomEnvTable (our_env);
  1033. while (0 > env_off)
  1034. GNUNET_free_non_null (our_env[--env_off]);
  1035. wpath_len = 0;
  1036. if (NULL == (wpath = u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len)))
  1037. {
  1038. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1039. "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", path, errno);
  1040. GNUNET_free (env_block);
  1041. GNUNET_free (cmd);
  1042. if (lsocks_pipe)
  1043. GNUNET_DISK_pipe_close (lsocks_pipe);
  1044. if (GNUNET_YES == pipe_control)
  1045. {
  1046. GNUNET_DISK_file_close (childpipe_write);
  1047. GNUNET_DISK_file_close (childpipe_read);
  1048. }
  1049. return NULL;
  1050. }
  1051. wcmd_len = 0;
  1052. if (NULL == (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len)))
  1053. {
  1054. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1055. "Failed to convert `%s' from UTF-8 to UTF-16: %d\n",
  1056. cmd,
  1057. errno);
  1058. GNUNET_free (env_block);
  1059. GNUNET_free (cmd);
  1060. free (wpath);
  1061. if (lsocks_pipe)
  1062. GNUNET_DISK_pipe_close (lsocks_pipe);
  1063. if (GNUNET_YES == pipe_control)
  1064. {
  1065. GNUNET_DISK_file_close (childpipe_write);
  1066. GNUNET_DISK_file_close (childpipe_read);
  1067. }
  1068. return NULL;
  1069. }
  1070. create_no_window = 0;
  1071. {
  1072. HANDLE console_input = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  1073. if (INVALID_HANDLE_VALUE == console_input)
  1074. create_no_window = CREATE_NO_WINDOW;
  1075. else
  1076. CloseHandle (console_input);
  1077. }
  1078. bresult = CreateProcessW (wpath, wcmd, NULL, NULL, GNUNET_YES,
  1079. create_no_window | CREATE_SUSPENDED, env_block, NULL, &start, &proc);
  1080. error_code = GetLastError ();
  1081. if ((NULL == pipe_stdin) && (stdih))
  1082. SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, stdif);
  1083. if ((NULL == pipe_stdout) && (stdoh))
  1084. SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, stdof);
  1085. if (stdeh)
  1086. SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef);
  1087. if (!bresult)
  1088. LOG (GNUNET_ERROR_TYPE_ERROR,
  1089. "CreateProcess(%s, %s) failed: %lu\n",
  1090. path,
  1091. cmd,
  1092. error_code);
  1093. GNUNET_free (env_block);
  1094. GNUNET_free (cmd);
  1095. free (wpath);
  1096. free (wcmd);
  1097. if (GNUNET_YES == pipe_control)
  1098. {
  1099. GNUNET_DISK_file_close (childpipe_read);
  1100. }
  1101. if (!bresult)
  1102. {
  1103. if (GNUNET_YES == pipe_control)
  1104. {
  1105. GNUNET_DISK_file_close (childpipe_write);
  1106. }
  1107. if (NULL != lsocks)
  1108. GNUNET_DISK_pipe_close (lsocks_pipe);
  1109. SetErrnoFromWinError (error_code);
  1110. return NULL;
  1111. }
  1112. gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
  1113. gnunet_proc->pid = proc.dwProcessId;
  1114. gnunet_proc->handle = proc.hProcess;
  1115. gnunet_proc->control_pipe = childpipe_write;
  1116. CreateThread (NULL, 64000, &child_wait_thread, (void *) gnunet_proc, 0, NULL);
  1117. ResumeThread (proc.hThread);
  1118. CloseHandle (proc.hThread);
  1119. if ( (NULL == lsocks) || (INVALID_SOCKET == lsocks[0]) )
  1120. return gnunet_proc;
  1121. GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ);
  1122. /* This is a replacement for "goto error" that doesn't use goto */
  1123. fail = 1;
  1124. do
  1125. {
  1126. ssize_t wrote;
  1127. uint64_t size;
  1128. uint64_t count;
  1129. unsigned int i;
  1130. /* Tell the number of sockets */
  1131. for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++);
  1132. wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
  1133. if (sizeof (count) != wrote)
  1134. {
  1135. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1136. "Failed to write %u count bytes to the child: %lu\n",
  1137. sizeof (count), GetLastError ());
  1138. break;
  1139. }
  1140. for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++)
  1141. {
  1142. WSAPROTOCOL_INFOA pi;
  1143. /* Get a socket duplication info */
  1144. if (SOCKET_ERROR == WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi))
  1145. {
  1146. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1147. "Failed to duplicate an socket[%u]: %lu\n", i,
  1148. GetLastError ());
  1149. break;
  1150. }
  1151. /* Synchronous I/O is not nice, but we can't schedule this:
  1152. * lsocks will be closed/freed by the caller soon, and until
  1153. * the child creates a duplicate, closing a socket here will
  1154. * close it for good.
  1155. */
  1156. /* Send the size of the structure
  1157. * (the child might be built with different headers...)
  1158. */
  1159. size = sizeof (pi);
  1160. wrote = GNUNET_DISK_file_write (lsocks_write_fd, &size, sizeof (size));
  1161. if (sizeof (size) != wrote)
  1162. {
  1163. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1164. "Failed to write %u size[%u] bytes to the child: %lu\n",
  1165. sizeof (size), i, GetLastError ());
  1166. break;
  1167. }
  1168. /* Finally! Send the data */
  1169. wrote = GNUNET_DISK_file_write (lsocks_write_fd, &pi, sizeof (pi));
  1170. if (sizeof (pi) != wrote)
  1171. {
  1172. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1173. "Failed to write %u socket[%u] bytes to the child: %lu\n",
  1174. sizeof (pi), i, GetLastError ());
  1175. break;
  1176. }
  1177. }
  1178. /* This will block us until the child makes a final read or closes
  1179. * the pipe (hence no 'wrote' check), since we have to wait for it
  1180. * to duplicate the last socket, before we return and start closing
  1181. * our own copies)
  1182. */
  1183. wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
  1184. fail = 0;
  1185. }
  1186. while (fail);
  1187. GNUNET_DISK_file_sync (lsocks_write_fd);
  1188. GNUNET_DISK_pipe_close (lsocks_pipe);
  1189. if (fail)
  1190. {
  1191. /* If we can't pass on the socket(s), the child will block forever,
  1192. * better put it out of its misery.
  1193. */
  1194. SafeTerminateProcess (gnunet_proc->handle, 0, 0);
  1195. CloseHandle (gnunet_proc->handle);
  1196. if (NULL != gnunet_proc->control_pipe)
  1197. GNUNET_DISK_file_close (gnunet_proc->control_pipe);
  1198. GNUNET_free (gnunet_proc);
  1199. return NULL;
  1200. }
  1201. return gnunet_proc;
  1202. #endif
  1203. }
  1204. /**
  1205. * Start a process.
  1206. *
  1207. * @param pipe_control should a pipe be used to send signals to the child?
  1208. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  1209. * @param pipe_stdin pipe to use to send input to child process (or NULL)
  1210. * @param pipe_stdout pipe to use to get output from child process (or NULL)
  1211. * @param pipe_stderr pipe to use to get output from child process (or NULL)
  1212. * @param filename name of the binary
  1213. * @param argv NULL-terminated array of arguments to the process
  1214. * @return pointer to process structure of the new process, NULL on error
  1215. */
  1216. struct GNUNET_OS_Process *
  1217. GNUNET_OS_start_process_vap (int pipe_control,
  1218. enum GNUNET_OS_InheritStdioFlags std_inheritance,
  1219. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  1220. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  1221. struct GNUNET_DISK_PipeHandle *pipe_stderr,
  1222. const char *filename,
  1223. char *const argv[])
  1224. {
  1225. return start_process (pipe_control,
  1226. std_inheritance,
  1227. pipe_stdin,
  1228. pipe_stdout,
  1229. pipe_stderr,
  1230. NULL,
  1231. filename,
  1232. argv);
  1233. }
  1234. /**
  1235. * Start a process.
  1236. *
  1237. * @param pipe_control should a pipe be used to send signals to the child?
  1238. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  1239. * @param pipe_stdin pipe to use to send input to child process (or NULL)
  1240. * @param pipe_stdout pipe to use to get output from child process (or NULL)
  1241. * @param pipe_stderr pipe to use to get output from child process (or NULL)
  1242. * @param filename name of the binary
  1243. * @param va NULL-terminated list of arguments to the process
  1244. * @return pointer to process structure of the new process, NULL on error
  1245. */
  1246. struct GNUNET_OS_Process *
  1247. GNUNET_OS_start_process_va (int pipe_control,
  1248. enum GNUNET_OS_InheritStdioFlags std_inheritance,
  1249. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  1250. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  1251. struct GNUNET_DISK_PipeHandle *pipe_stderr,
  1252. const char *filename, va_list va)
  1253. {
  1254. struct GNUNET_OS_Process *ret;
  1255. va_list ap;
  1256. char **argv;
  1257. int argc;
  1258. argc = 0;
  1259. va_copy (ap, va);
  1260. while (NULL != va_arg (ap, char *))
  1261. argc++;
  1262. va_end (ap);
  1263. argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
  1264. argc = 0;
  1265. va_copy (ap, va);
  1266. while (NULL != (argv[argc] = va_arg (ap, char *)))
  1267. argc++;
  1268. va_end (ap);
  1269. ret = GNUNET_OS_start_process_vap (pipe_control,
  1270. std_inheritance,
  1271. pipe_stdin,
  1272. pipe_stdout,
  1273. pipe_stderr,
  1274. filename,
  1275. argv);
  1276. GNUNET_free (argv);
  1277. return ret;
  1278. }
  1279. /**
  1280. * Start a process.
  1281. *
  1282. * @param pipe_control should a pipe be used to send signals to the child?
  1283. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  1284. * @param pipe_stdin pipe to use to send input to child process (or NULL)
  1285. * @param pipe_stdout pipe to use to get output from child process (or NULL)
  1286. * @param filename name of the binary
  1287. * @param ... NULL-terminated list of arguments to the process
  1288. * @return pointer to process structure of the new process, NULL on error
  1289. */
  1290. struct GNUNET_OS_Process *
  1291. GNUNET_OS_start_process (int pipe_control,
  1292. enum GNUNET_OS_InheritStdioFlags std_inheritance,
  1293. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  1294. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  1295. struct GNUNET_DISK_PipeHandle *pipe_stderr,
  1296. const char *filename, ...)
  1297. {
  1298. struct GNUNET_OS_Process *ret;
  1299. va_list ap;
  1300. va_start (ap, filename);
  1301. ret = GNUNET_OS_start_process_va (pipe_control,
  1302. std_inheritance,
  1303. pipe_stdin,
  1304. pipe_stdout,
  1305. pipe_stderr,
  1306. filename,
  1307. ap);
  1308. va_end (ap);
  1309. return ret;
  1310. }
  1311. /**
  1312. * Start a process.
  1313. *
  1314. * @param pipe_control should a pipe be used to send signals to the child?
  1315. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
  1316. * std handles of the parent are inherited by the child.
  1317. * pipe_stdin and pipe_stdout take priority over std_inheritance
  1318. * (when they are non-NULL).
  1319. * @param lsocks array of listen sockets to dup systemd-style (or NULL);
  1320. * must be NULL on platforms where dup is not supported
  1321. * @param filename name of the binary
  1322. * @param argv NULL-terminated list of arguments to the process
  1323. * @return process ID of the new process, -1 on error
  1324. */
  1325. struct GNUNET_OS_Process *
  1326. GNUNET_OS_start_process_v (int pipe_control,
  1327. enum GNUNET_OS_InheritStdioFlags std_inheritance,
  1328. const SOCKTYPE *lsocks,
  1329. const char *filename,
  1330. char *const argv[])
  1331. {
  1332. return start_process (pipe_control,
  1333. std_inheritance,
  1334. NULL,
  1335. NULL,
  1336. NULL,
  1337. lsocks,
  1338. filename,
  1339. argv);
  1340. }
  1341. /**
  1342. * Start a process. This function is similar to the GNUNET_OS_start_process_*
  1343. * except that the filename and arguments can have whole strings which contain
  1344. * the arguments. These arguments are to be separated by spaces and are parsed
  1345. * in the order they appear. Arguments containing spaces can be used by
  1346. * quoting them with @em ".
  1347. *
  1348. * @param pipe_control should a pipe be used to send signals to the child?
  1349. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  1350. * @param lsocks array of listen sockets to dup systemd-style (or NULL);
  1351. * must be NULL on platforms where dup is not supported
  1352. * @param filename name of the binary. It is valid to have the arguments
  1353. * in this string when they are separated by spaces.
  1354. * @param ... more arguments. Should be of type `char *`. It is valid
  1355. * to have the arguments in these strings when they are separated by
  1356. * spaces. The last argument MUST be NULL.
  1357. * @return pointer to process structure of the new process, NULL on error
  1358. */
  1359. struct GNUNET_OS_Process *
  1360. GNUNET_OS_start_process_s (int pipe_control,
  1361. unsigned int std_inheritance,
  1362. const SOCKTYPE * lsocks,
  1363. const char *filename, ...)
  1364. {
  1365. va_list ap;
  1366. char **argv;
  1367. unsigned int argv_size;
  1368. const char *arg;
  1369. const char *rpos;
  1370. char *pos;
  1371. char *cp;
  1372. const char *last;
  1373. struct GNUNET_OS_Process *proc;
  1374. char *binary_path;
  1375. int quote_on;
  1376. unsigned int i;
  1377. size_t len;
  1378. argv_size = 1;
  1379. va_start (ap, filename);
  1380. arg = filename;
  1381. last = NULL;
  1382. do
  1383. {
  1384. rpos = arg;
  1385. quote_on = 0;
  1386. while ('\0' != *rpos)
  1387. {
  1388. if ('"' == *rpos)
  1389. {
  1390. if (1 == quote_on)
  1391. quote_on = 0;
  1392. else
  1393. quote_on = 1;
  1394. }
  1395. if ( (' ' == *rpos) && (0 == quote_on) )
  1396. {
  1397. if (NULL != last)
  1398. argv_size++;
  1399. last = NULL;
  1400. rpos++;
  1401. while (' ' == *rpos)
  1402. rpos++;
  1403. }
  1404. if ( (NULL == last) && ('\0' != *rpos) ) // FIXME: == or !=?
  1405. last = rpos;
  1406. if ('\0' != *rpos)
  1407. rpos++;
  1408. }
  1409. if (NULL != last)
  1410. argv_size++;
  1411. }
  1412. while (NULL != (arg = (va_arg (ap, const char*))));
  1413. va_end (ap);
  1414. argv = GNUNET_malloc (argv_size * sizeof (char *));
  1415. argv_size = 0;
  1416. va_start (ap, filename);
  1417. arg = filename;
  1418. last = NULL;
  1419. do
  1420. {
  1421. cp = GNUNET_strdup (arg);
  1422. quote_on = 0;
  1423. pos = cp;
  1424. while ('\0' != *pos)
  1425. {
  1426. if ('"' == *pos)
  1427. {
  1428. if (1 == quote_on)
  1429. quote_on = 0;
  1430. else
  1431. quote_on = 1;
  1432. }
  1433. if ( (' ' == *pos) && (0 == quote_on) )
  1434. {
  1435. *pos = '\0';
  1436. if (NULL != last)
  1437. argv[argv_size++] = GNUNET_strdup (last);
  1438. last = NULL;
  1439. pos++;
  1440. while (' ' == *pos)
  1441. pos++;
  1442. }
  1443. if ( (NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
  1444. last = pos;
  1445. if ('\0' != *pos)
  1446. pos++;
  1447. }
  1448. if (NULL != last)
  1449. argv[argv_size++] = GNUNET_strdup (last);
  1450. last = NULL;
  1451. GNUNET_free (cp);
  1452. }
  1453. while (NULL != (arg = (va_arg (ap, const char*))));
  1454. va_end (ap);
  1455. argv[argv_size] = NULL;
  1456. for(i = 0; i < argv_size; i++)
  1457. {
  1458. len = strlen (argv[i]);
  1459. if ( (argv[i][0] == '"') && (argv[i][len-1] == '"'))
  1460. {
  1461. memmove (&argv[i][0], &argv[i][1], len - 2);
  1462. argv[i][len-2] = '\0';
  1463. }
  1464. }
  1465. binary_path = argv[0];
  1466. proc = GNUNET_OS_start_process_v (pipe_control, std_inheritance, lsocks,
  1467. binary_path, argv);
  1468. while (argv_size > 0)
  1469. GNUNET_free (argv[--argv_size]);
  1470. GNUNET_free (argv);
  1471. return proc;
  1472. }
  1473. /**
  1474. * Retrieve the status of a process, waiting on it if dead.
  1475. * Nonblocking version.
  1476. *
  1477. * @param proc process ID
  1478. * @param type status type
  1479. * @param code return code/signal number
  1480. * @param options WNOHANG if non-blocking is desired
  1481. * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
  1482. */
  1483. static int
  1484. process_status (struct GNUNET_OS_Process *proc,
  1485. enum GNUNET_OS_ProcessStatusType *type,
  1486. unsigned long *code,
  1487. int options)
  1488. {
  1489. #ifndef MINGW
  1490. int status;
  1491. int ret;
  1492. GNUNET_assert (0 != proc);
  1493. ret = waitpid (proc->pid, &status, options);
  1494. if (ret < 0)
  1495. {
  1496. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
  1497. "waitpid");
  1498. return GNUNET_SYSERR;
  1499. }
  1500. if (0 == ret)
  1501. {
  1502. *type = GNUNET_OS_PROCESS_RUNNING;
  1503. *code = 0;
  1504. return GNUNET_NO;
  1505. }
  1506. if (proc->pid != ret)
  1507. {
  1508. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
  1509. return GNUNET_SYSERR;
  1510. }
  1511. if (WIFEXITED (status))
  1512. {
  1513. *type = GNUNET_OS_PROCESS_EXITED;
  1514. *code = WEXITSTATUS (status);
  1515. }
  1516. else if (WIFSIGNALED (status))
  1517. {
  1518. *type = GNUNET_OS_PROCESS_SIGNALED;
  1519. *code = WTERMSIG (status);
  1520. }
  1521. else if (WIFSTOPPED (status))
  1522. {
  1523. *type = GNUNET_OS_PROCESS_SIGNALED;
  1524. *code = WSTOPSIG (status);
  1525. }
  1526. #ifdef WIFCONTINUED
  1527. else if (WIFCONTINUED (status))
  1528. {
  1529. *type = GNUNET_OS_PROCESS_RUNNING;
  1530. *code = 0;
  1531. }
  1532. #endif
  1533. else
  1534. {
  1535. *type = GNUNET_OS_PROCESS_UNKNOWN;
  1536. *code = 0;
  1537. }
  1538. #else
  1539. #ifndef WNOHANG
  1540. #define WNOHANG 42 /* just a flag for W32, purely internal at this point */
  1541. #endif
  1542. HANDLE h;
  1543. DWORD c, error_code, ret;
  1544. h = proc->handle;
  1545. ret = proc->pid;
  1546. if (h == NULL || ret == 0)
  1547. {
  1548. LOG (GNUNET_ERROR_TYPE_WARNING,
  1549. "Invalid process information {%d, %08X}\n",
  1550. ret, h);
  1551. return GNUNET_SYSERR;
  1552. }
  1553. if (h == NULL)
  1554. h = GetCurrentProcess ();
  1555. if (WNOHANG != options)
  1556. {
  1557. if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
  1558. {
  1559. SetErrnoFromWinError (GetLastError ());
  1560. return GNUNET_SYSERR;
  1561. }
  1562. }
  1563. SetLastError (0);
  1564. ret = GetExitCodeProcess (h, &c);
  1565. error_code = GetLastError ();
  1566. if (ret == 0 || error_code != NO_ERROR)
  1567. {
  1568. SetErrnoFromWinError (error_code);
  1569. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
  1570. return GNUNET_SYSERR;
  1571. }
  1572. if (STILL_ACTIVE == c)
  1573. {
  1574. *type = GNUNET_OS_PROCESS_RUNNING;
  1575. *code = 0;
  1576. return GNUNET_NO;
  1577. }
  1578. *type = GNUNET_OS_PROCESS_EXITED;
  1579. *code = c;
  1580. #endif
  1581. return GNUNET_OK;
  1582. }
  1583. /**
  1584. * Retrieve the status of a process, waiting on it if dead.
  1585. * Nonblocking version.
  1586. *
  1587. * @param proc process ID
  1588. * @param type status type
  1589. * @param code return code/signal number
  1590. * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
  1591. */
  1592. int
  1593. GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
  1594. enum GNUNET_OS_ProcessStatusType *type,
  1595. unsigned long *code)
  1596. {
  1597. return process_status (proc,
  1598. type,
  1599. code,
  1600. WNOHANG);
  1601. }
  1602. /**
  1603. * Retrieve the status of a process, waiting on it if dead.
  1604. * Blocking version.
  1605. *
  1606. * @param proc pointer to process structure
  1607. * @param type status type
  1608. * @param code return code/signal number
  1609. * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
  1610. */
  1611. int
  1612. GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc,
  1613. enum GNUNET_OS_ProcessStatusType *type,
  1614. unsigned long *code)
  1615. {
  1616. return process_status (proc,
  1617. type,
  1618. code,
  1619. 0);
  1620. }
  1621. /**
  1622. * Wait for a process to terminate. The return code is discarded.
  1623. * You must not use #GNUNET_OS_process_status() on the same process
  1624. * after calling this function! This function is blocking and should
  1625. * thus only be used if the child process is known to have terminated
  1626. * or to terminate very soon.
  1627. *
  1628. * @param proc pointer to process structure
  1629. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  1630. */
  1631. int
  1632. GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
  1633. {
  1634. #ifndef MINGW
  1635. pid_t pid = proc->pid;
  1636. pid_t ret;
  1637. while ( (pid != (ret = waitpid (pid, NULL, 0))) &&
  1638. (EINTR == errno) ) ;
  1639. if (pid != ret)
  1640. {
  1641. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
  1642. "waitpid");
  1643. return GNUNET_SYSERR;
  1644. }
  1645. return GNUNET_OK;
  1646. #else
  1647. HANDLE h;
  1648. h = proc->handle;
  1649. if (NULL == h)
  1650. {
  1651. LOG (GNUNET_ERROR_TYPE_WARNING,
  1652. "Invalid process information {%d, %08X}\n",
  1653. proc->pid, h);
  1654. return GNUNET_SYSERR;
  1655. }
  1656. if (NULL == h)
  1657. h = GetCurrentProcess ();
  1658. if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
  1659. {
  1660. SetErrnoFromWinError (GetLastError ());
  1661. return GNUNET_SYSERR;
  1662. }
  1663. return GNUNET_OK;
  1664. #endif
  1665. }
  1666. /**
  1667. * Handle to a command.
  1668. */
  1669. struct GNUNET_OS_CommandHandle
  1670. {
  1671. /**
  1672. * Process handle.
  1673. */
  1674. struct GNUNET_OS_Process *eip;
  1675. /**
  1676. * Handle to the output pipe.
  1677. */
  1678. struct GNUNET_DISK_PipeHandle *opipe;
  1679. /**
  1680. * Read-end of output pipe.
  1681. */
  1682. const struct GNUNET_DISK_FileHandle *r;
  1683. /**
  1684. * Function to call on each line of output.
  1685. */
  1686. GNUNET_OS_LineProcessor proc;
  1687. /**
  1688. * Closure for @e proc.
  1689. */
  1690. void *proc_cls;
  1691. /**
  1692. * Buffer for the output.
  1693. */
  1694. char buf[1024];
  1695. /**
  1696. * Task reading from pipe.
  1697. */
  1698. struct GNUNET_SCHEDULER_Task *rtask;
  1699. /**
  1700. * When to time out.
  1701. */
  1702. struct GNUNET_TIME_Absolute timeout;
  1703. /**
  1704. * Current read offset in buf.
  1705. */
  1706. size_t off;
  1707. };
  1708. /**
  1709. * Stop/kill a command. Must ONLY be called either from
  1710. * the callback after 'NULL' was passed for 'line' *OR*
  1711. * from an independent task (not within the line processor).
  1712. *
  1713. * @param cmd handle to the process
  1714. */
  1715. void
  1716. GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
  1717. {
  1718. if (NULL != cmd->proc)
  1719. {
  1720. GNUNET_assert (NULL != cmd->rtask);
  1721. GNUNET_SCHEDULER_cancel (cmd->rtask);
  1722. }
  1723. (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
  1724. GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
  1725. GNUNET_OS_process_destroy (cmd->eip);
  1726. GNUNET_DISK_pipe_close (cmd->opipe);
  1727. GNUNET_free (cmd);
  1728. }
  1729. /**
  1730. * Read from the process and call the line processor.
  1731. *
  1732. * @param cls the `struct GNUNET_OS_CommandHandle *`
  1733. */
  1734. static void
  1735. cmd_read (void *cls)
  1736. {
  1737. struct GNUNET_OS_CommandHandle *cmd = cls;
  1738. const struct GNUNET_SCHEDULER_TaskContext *tc;
  1739. GNUNET_OS_LineProcessor proc;
  1740. char *end;
  1741. ssize_t ret;
  1742. cmd->rtask = NULL;
  1743. tc = GNUNET_SCHEDULER_get_task_context ();
  1744. if (GNUNET_YES !=
  1745. GNUNET_NETWORK_fdset_handle_isset (tc->read_ready,
  1746. cmd->r))
  1747. {
  1748. /* timeout */
  1749. proc = cmd->proc;
  1750. cmd->proc = NULL;
  1751. proc (cmd->proc_cls, NULL);
  1752. return;
  1753. }
  1754. ret = GNUNET_DISK_file_read (cmd->r,
  1755. &cmd->buf[cmd->off],
  1756. sizeof (cmd->buf) - cmd->off);
  1757. if (ret <= 0)
  1758. {
  1759. if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
  1760. {
  1761. cmd->buf[cmd->off] = '\0';
  1762. cmd->proc (cmd->proc_cls, cmd->buf);
  1763. }
  1764. proc = cmd->proc;
  1765. cmd->proc = NULL;
  1766. proc (cmd->proc_cls, NULL);
  1767. return;
  1768. }
  1769. end = memchr (&cmd->buf[cmd->off], '\n', ret);
  1770. cmd->off += ret;
  1771. while (NULL != end)
  1772. {
  1773. *end = '\0';
  1774. cmd->proc (cmd->proc_cls, cmd->buf);
  1775. memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
  1776. cmd->off -= (end + 1 - cmd->buf);
  1777. end = memchr (cmd->buf, '\n', cmd->off);
  1778. }
  1779. cmd->rtask
  1780. = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
  1781. (cmd->timeout),
  1782. cmd->r,
  1783. &cmd_read, cmd);
  1784. }
  1785. /**
  1786. * Run the given command line and call the given function
  1787. * for each line of the output.
  1788. *
  1789. * @param proc function to call for each line of the output
  1790. * @param proc_cls closure for @a proc
  1791. * @param timeout when to time out
  1792. * @param binary command to run
  1793. * @param ... arguments to command
  1794. * @return NULL on error
  1795. */
  1796. struct GNUNET_OS_CommandHandle *
  1797. GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
  1798. void *proc_cls,
  1799. struct GNUNET_TIME_Relative timeout,
  1800. const char *binary,
  1801. ...)
  1802. {
  1803. struct GNUNET_OS_CommandHandle *cmd;
  1804. struct GNUNET_OS_Process *eip;
  1805. struct GNUNET_DISK_PipeHandle *opipe;
  1806. va_list ap;
  1807. opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
  1808. GNUNET_NO, GNUNET_YES);
  1809. if (NULL == opipe)
  1810. return NULL;
  1811. va_start (ap, binary);
  1812. /* redirect stdout, don't inherit stderr/stdin */
  1813. eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL,
  1814. opipe, NULL, binary,
  1815. ap);
  1816. va_end (ap);
  1817. if (NULL == eip)
  1818. {
  1819. GNUNET_DISK_pipe_close (opipe);
  1820. return NULL;
  1821. }
  1822. GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
  1823. cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
  1824. cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  1825. cmd->eip = eip;
  1826. cmd->opipe = opipe;
  1827. cmd->proc = proc;
  1828. cmd->proc_cls = proc_cls;
  1829. cmd->r = GNUNET_DISK_pipe_handle (opipe,
  1830. GNUNET_DISK_PIPE_END_READ);
  1831. cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout,
  1832. cmd->r,
  1833. &cmd_read,
  1834. cmd);
  1835. return cmd;
  1836. }
  1837. /* end of os_priority.c */