os_priority.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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) \
  27. GNUNET_log_from_strerror (kind, "util-os-priority", syscall)
  28. #define LOG_STRERROR_FILE(kind, syscall, filename) \
  29. GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, filename)
  30. #define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE"
  31. struct GNUNET_OS_Process
  32. {
  33. /**
  34. * PID of the process.
  35. */
  36. pid_t pid;
  37. /**
  38. * Pipe we use to signal the process.
  39. * NULL if unused, or if process was deemed uncontrollable.
  40. */
  41. struct GNUNET_DISK_FileHandle *control_pipe;
  42. };
  43. /**
  44. * Handle for 'this' process.
  45. */
  46. static struct GNUNET_OS_Process current_process;
  47. /**
  48. * Handle for the #parent_control_handler() Task.
  49. */
  50. static struct GNUNET_SCHEDULER_Task *pch;
  51. /**
  52. * Handle for the #shutdown_pch() Task.
  53. */
  54. static struct GNUNET_SCHEDULER_Task *spch;
  55. /**
  56. * This handler is called on shutdown to remove the #pch.
  57. *
  58. * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
  59. */
  60. static void
  61. shutdown_pch (void *cls)
  62. {
  63. struct GNUNET_DISK_FileHandle *control_pipe = cls;
  64. GNUNET_SCHEDULER_cancel (pch);
  65. pch = NULL;
  66. GNUNET_DISK_file_close (control_pipe);
  67. control_pipe = NULL;
  68. }
  69. /**
  70. * This handler is called when there are control data to be read on the pipe
  71. *
  72. * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
  73. */
  74. static void
  75. parent_control_handler (void *cls)
  76. {
  77. struct GNUNET_DISK_FileHandle *control_pipe = cls;
  78. char sig;
  79. char *pipe_fd;
  80. ssize_t ret;
  81. pch = NULL;
  82. ret = GNUNET_DISK_file_read (control_pipe, &sig, sizeof(sig));
  83. if (sizeof(sig) != ret)
  84. {
  85. if (-1 == ret)
  86. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
  87. LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing control pipe\n");
  88. GNUNET_DISK_file_close (control_pipe);
  89. control_pipe = NULL;
  90. GNUNET_SCHEDULER_cancel (spch);
  91. spch = NULL;
  92. return;
  93. }
  94. pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
  95. GNUNET_assert ((NULL == pipe_fd) || (strlen (pipe_fd) <= 0));
  96. LOG (GNUNET_ERROR_TYPE_DEBUG,
  97. "Got control code %d from parent via pipe %s\n",
  98. sig,
  99. pipe_fd);
  100. pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
  101. control_pipe,
  102. &parent_control_handler,
  103. control_pipe);
  104. GNUNET_SIGNAL_raise ((int) sig);
  105. }
  106. /**
  107. * Task that connects this process to its parent via pipe;
  108. * essentially, the parent control handler will read signal numbers
  109. * from the #GNUNET_OS_CONTROL_PIPE (as given in an environment
  110. * variable) and raise those signals.
  111. *
  112. * @param cls closure (unused)
  113. */
  114. void
  115. GNUNET_OS_install_parent_control_handler (void *cls)
  116. {
  117. const char *env_buf;
  118. char *env_buf_end;
  119. struct GNUNET_DISK_FileHandle *control_pipe;
  120. uint64_t pipe_fd;
  121. (void) cls;
  122. if (NULL != pch)
  123. {
  124. /* already done, we've been called twice... */
  125. GNUNET_break (0);
  126. return;
  127. }
  128. env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
  129. if ((NULL == env_buf) || (strlen (env_buf) <= 0))
  130. {
  131. LOG (GNUNET_ERROR_TYPE_DEBUG,
  132. "Not installing a handler because $%s is empty\n",
  133. GNUNET_OS_CONTROL_PIPE);
  134. putenv (GNUNET_OS_CONTROL_PIPE "=");
  135. return;
  136. }
  137. errno = 0;
  138. pipe_fd = strtoull (env_buf, &env_buf_end, 16);
  139. if ((0 != errno) || (env_buf == env_buf_end))
  140. {
  141. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "strtoull", env_buf);
  142. putenv (GNUNET_OS_CONTROL_PIPE "=");
  143. return;
  144. }
  145. if (pipe_fd >= FD_SETSIZE)
  146. {
  147. LOG (GNUNET_ERROR_TYPE_ERROR,
  148. "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n",
  149. env_buf);
  150. putenv (GNUNET_OS_CONTROL_PIPE "=");
  151. return;
  152. }
  153. control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
  154. if (NULL == control_pipe)
  155. {
  156. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", env_buf);
  157. putenv (GNUNET_OS_CONTROL_PIPE "=");
  158. return;
  159. }
  160. LOG (GNUNET_ERROR_TYPE_DEBUG,
  161. "Adding parent control handler pipe `%s' to the scheduler\n",
  162. env_buf);
  163. pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
  164. control_pipe,
  165. &parent_control_handler,
  166. control_pipe);
  167. spch = GNUNET_SCHEDULER_add_shutdown (&shutdown_pch, control_pipe);
  168. putenv (GNUNET_OS_CONTROL_PIPE "=");
  169. }
  170. /**
  171. * Get process structure for current process
  172. *
  173. * The pointer it returns points to static memory location and must
  174. * not be deallocated/closed.
  175. *
  176. * @return pointer to the process sturcutre for this process
  177. */
  178. struct GNUNET_OS_Process *
  179. GNUNET_OS_process_current ()
  180. {
  181. current_process.pid = 0;
  182. return &current_process;
  183. }
  184. /**
  185. * Sends a signal to the process
  186. *
  187. * @param proc pointer to process structure
  188. * @param sig signal
  189. * @return 0 on success, -1 on error
  190. */
  191. int
  192. GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
  193. {
  194. int ret;
  195. char csig;
  196. csig = (char) sig;
  197. if (NULL != proc->control_pipe)
  198. {
  199. LOG (GNUNET_ERROR_TYPE_DEBUG,
  200. "Sending signal %d to pid: %u via pipe\n",
  201. sig,
  202. proc->pid);
  203. ret = GNUNET_DISK_file_write (proc->control_pipe, &csig, sizeof(csig));
  204. if (sizeof(csig) == ret)
  205. return 0;
  206. }
  207. /* pipe failed or non-existent, try other methods */
  208. switch (sig)
  209. {
  210. case SIGHUP:
  211. case SIGINT:
  212. case SIGKILL:
  213. case SIGTERM:
  214. #if (SIGTERM != GNUNET_TERM_SIG)
  215. case GNUNET_TERM_SIG:
  216. #endif
  217. LOG (GNUNET_ERROR_TYPE_DEBUG,
  218. "Sending signal %d to pid: %u via system call\n",
  219. sig,
  220. proc->pid);
  221. return kill (proc->pid, sig);
  222. default:
  223. LOG (GNUNET_ERROR_TYPE_DEBUG,
  224. "Sending signal %d to pid: %u via system call\n",
  225. sig,
  226. proc->pid);
  227. return kill (proc->pid, sig);
  228. }
  229. }
  230. /**
  231. * Get the pid of the process in question
  232. *
  233. * @param proc the process to get the pid of
  234. *
  235. * @return the current process id
  236. */
  237. pid_t
  238. GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc)
  239. {
  240. return proc->pid;
  241. }
  242. /**
  243. * Cleans up process structure contents (OS-dependent) and deallocates
  244. * it.
  245. *
  246. * @param proc pointer to process structure
  247. */
  248. void
  249. GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
  250. {
  251. if (NULL != proc->control_pipe)
  252. GNUNET_DISK_file_close (proc->control_pipe);
  253. GNUNET_free (proc);
  254. }
  255. /**
  256. * Open '/dev/null' and make the result the given
  257. * file descriptor.
  258. *
  259. * @param target_fd desired FD to point to /dev/null
  260. * @param flags open flags (O_RDONLY, O_WRONLY)
  261. */
  262. static void
  263. open_dev_null (int target_fd, int flags)
  264. {
  265. int fd;
  266. fd = open ("/dev/null", flags);
  267. if (-1 == fd)
  268. {
  269. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", "/dev/null");
  270. return;
  271. }
  272. if (fd == target_fd)
  273. return;
  274. if (-1 == dup2 (fd, target_fd))
  275. {
  276. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
  277. (void) close (fd);
  278. return;
  279. }
  280. GNUNET_break (0 == close (fd));
  281. }
  282. /**
  283. * Start a process.
  284. *
  285. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
  286. * std handles of the parent are inherited by the child.
  287. * pipe_stdin and pipe_stdout take priority over std_inheritance
  288. * (when they are non-NULL).
  289. * @param pipe_stdin pipe to use to send input to child process (or NULL)
  290. * @param pipe_stdout pipe to use to get output from child process (or NULL)
  291. * @param pipe_stderr pipe to use for stderr for child process (or NULL)
  292. * @param lsocks array of listen sockets to dup systemd-style (or NULL);
  293. * must be NULL on platforms where dup is not supported
  294. * @param filename name of the binary
  295. * @param argv NULL-terminated list of arguments to the process
  296. * @return process ID of the new process, -1 on error
  297. */
  298. static struct GNUNET_OS_Process *
  299. start_process (enum GNUNET_OS_InheritStdioFlags std_inheritance,
  300. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  301. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  302. struct GNUNET_DISK_PipeHandle *pipe_stderr,
  303. const int *lsocks,
  304. const char *filename,
  305. char *const argv[])
  306. {
  307. pid_t ret;
  308. char fds[16];
  309. struct GNUNET_OS_Process *gnunet_proc;
  310. struct GNUNET_DISK_FileHandle *childpipe_read;
  311. struct GNUNET_DISK_FileHandle *childpipe_write;
  312. int childpipe_read_fd;
  313. int i;
  314. int j;
  315. int k;
  316. int tgt;
  317. int flags;
  318. int *lscp;
  319. unsigned int ls;
  320. int fd_stdout_write;
  321. int fd_stdout_read;
  322. int fd_stderr_write;
  323. int fd_stderr_read;
  324. int fd_stdin_read;
  325. int fd_stdin_write;
  326. if (GNUNET_SYSERR ==
  327. GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
  328. return NULL; /* not executable */
  329. if (0 != (std_inheritance & GNUNET_OS_USE_PIPE_CONTROL))
  330. {
  331. struct GNUNET_DISK_PipeHandle *childpipe;
  332. int dup_childpipe_read_fd = -1;
  333. childpipe = GNUNET_DISK_pipe (GNUNET_DISK_PF_NONE);
  334. if (NULL == childpipe)
  335. return NULL;
  336. childpipe_read =
  337. GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
  338. childpipe_write =
  339. GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
  340. GNUNET_DISK_pipe_close (childpipe);
  341. if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
  342. (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
  343. &childpipe_read_fd,
  344. sizeof(int))) ||
  345. (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
  346. {
  347. if (NULL != childpipe_read)
  348. GNUNET_DISK_file_close (childpipe_read);
  349. if (NULL != childpipe_write)
  350. GNUNET_DISK_file_close (childpipe_write);
  351. if (0 <= dup_childpipe_read_fd)
  352. close (dup_childpipe_read_fd);
  353. return NULL;
  354. }
  355. childpipe_read_fd = dup_childpipe_read_fd;
  356. GNUNET_DISK_file_close (childpipe_read);
  357. }
  358. else
  359. {
  360. childpipe_write = NULL;
  361. childpipe_read_fd = -1;
  362. }
  363. if (NULL != pipe_stdin)
  364. {
  365. GNUNET_assert (
  366. GNUNET_OK ==
  367. GNUNET_DISK_internal_file_handle_ (
  368. GNUNET_DISK_pipe_handle (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
  369. &fd_stdin_read,
  370. sizeof(int)));
  371. GNUNET_assert (
  372. GNUNET_OK ==
  373. GNUNET_DISK_internal_file_handle_ (
  374. GNUNET_DISK_pipe_handle (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
  375. &fd_stdin_write,
  376. sizeof(int)));
  377. }
  378. if (NULL != pipe_stdout)
  379. {
  380. GNUNET_assert (
  381. GNUNET_OK ==
  382. GNUNET_DISK_internal_file_handle_ (
  383. GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE),
  384. &fd_stdout_write,
  385. sizeof(int)));
  386. GNUNET_assert (
  387. GNUNET_OK ==
  388. GNUNET_DISK_internal_file_handle_ (
  389. GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
  390. &fd_stdout_read,
  391. sizeof(int)));
  392. }
  393. if (NULL != pipe_stderr)
  394. {
  395. GNUNET_assert (
  396. GNUNET_OK ==
  397. GNUNET_DISK_internal_file_handle_ (
  398. GNUNET_DISK_pipe_handle (pipe_stderr, GNUNET_DISK_PIPE_END_READ),
  399. &fd_stderr_read,
  400. sizeof(int)));
  401. GNUNET_assert (
  402. GNUNET_OK ==
  403. GNUNET_DISK_internal_file_handle_ (
  404. GNUNET_DISK_pipe_handle (pipe_stderr, GNUNET_DISK_PIPE_END_WRITE),
  405. &fd_stderr_write,
  406. sizeof(int)));
  407. }
  408. lscp = NULL;
  409. ls = 0;
  410. if (NULL != lsocks)
  411. {
  412. i = 0;
  413. while (-1 != (k = lsocks[i++]))
  414. GNUNET_array_append (lscp, ls, k);
  415. GNUNET_array_append (lscp, ls, -1);
  416. }
  417. #if DARWIN
  418. /* see https://web.archive.org/web/20150924082249/gnunet.org/vfork */
  419. ret = vfork ();
  420. #else
  421. ret = fork ();
  422. #endif
  423. if (-1 == ret)
  424. {
  425. int eno = errno;
  426. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
  427. GNUNET_array_grow (lscp, ls, 0);
  428. if (NULL != childpipe_write)
  429. GNUNET_DISK_file_close (childpipe_write);
  430. if (0 <= childpipe_read_fd)
  431. close (childpipe_read_fd);
  432. errno = eno;
  433. return NULL;
  434. }
  435. if (0 != ret)
  436. {
  437. unsetenv (GNUNET_OS_CONTROL_PIPE);
  438. gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
  439. gnunet_proc->pid = ret;
  440. gnunet_proc->control_pipe = childpipe_write;
  441. if (0 != (std_inheritance & GNUNET_OS_USE_PIPE_CONTROL))
  442. {
  443. close (childpipe_read_fd);
  444. }
  445. GNUNET_array_grow (lscp, ls, 0);
  446. return gnunet_proc;
  447. }
  448. if (0 <= childpipe_read_fd)
  449. {
  450. char fdbuf[100];
  451. #ifndef DARWIN
  452. /* due to vfork, we must NOT free memory on DARWIN! */
  453. GNUNET_DISK_file_close (childpipe_write);
  454. #endif
  455. snprintf (fdbuf, 100, "%x", childpipe_read_fd);
  456. setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
  457. }
  458. else
  459. unsetenv (GNUNET_OS_CONTROL_PIPE);
  460. if (NULL != pipe_stdin)
  461. {
  462. GNUNET_break (0 == close (fd_stdin_write));
  463. if (-1 == dup2 (fd_stdin_read, 0))
  464. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
  465. GNUNET_break (0 == close (fd_stdin_read));
  466. }
  467. else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
  468. {
  469. GNUNET_break (0 == close (0));
  470. open_dev_null (0, O_RDONLY);
  471. }
  472. if (NULL != pipe_stdout)
  473. {
  474. GNUNET_break (0 == close (fd_stdout_read));
  475. if (-1 == dup2 (fd_stdout_write, 1))
  476. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
  477. GNUNET_break (0 == close (fd_stdout_write));
  478. }
  479. else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
  480. {
  481. GNUNET_break (0 == close (1));
  482. open_dev_null (1, O_WRONLY);
  483. }
  484. if (NULL != pipe_stderr)
  485. {
  486. GNUNET_break (0 == close (fd_stderr_read));
  487. if (-1 == dup2 (fd_stderr_write, 2))
  488. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
  489. GNUNET_break (0 == close (fd_stderr_write));
  490. }
  491. else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
  492. {
  493. GNUNET_break (0 == close (2));
  494. open_dev_null (2, O_WRONLY);
  495. }
  496. if (NULL != lscp)
  497. {
  498. /* read systemd documentation... */
  499. i = 0;
  500. tgt = 3;
  501. while (-1 != lscp[i])
  502. {
  503. j = i + 1;
  504. while (-1 != lscp[j])
  505. {
  506. if (lscp[j] == tgt)
  507. {
  508. /* dup away */
  509. k = dup (lscp[j]);
  510. GNUNET_assert (-1 != k);
  511. GNUNET_assert (0 == close (lscp[j]));
  512. lscp[j] = k;
  513. break;
  514. }
  515. j++;
  516. }
  517. if (lscp[i] != tgt)
  518. {
  519. /* Bury any existing FD, no matter what; they should all be closed
  520. * on exec anyway and the important ones have been dup'ed away */
  521. (void) close (tgt);
  522. GNUNET_assert (-1 != dup2 (lscp[i], tgt));
  523. }
  524. /* unset close-on-exec flag */
  525. flags = fcntl (tgt, F_GETFD);
  526. GNUNET_assert (flags >= 0);
  527. flags &= ~FD_CLOEXEC;
  528. fflush (stderr);
  529. (void) fcntl (tgt, F_SETFD, flags);
  530. tgt++;
  531. i++;
  532. }
  533. GNUNET_snprintf (fds, sizeof(fds), "%u", i);
  534. setenv ("LISTEN_FDS", fds, 1);
  535. }
  536. #ifndef DARWIN
  537. /* due to vfork, we must NOT free memory on DARWIN! */
  538. GNUNET_array_grow (lscp, ls, 0);
  539. #endif
  540. execvp (filename, argv);
  541. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
  542. _exit (1);
  543. }
  544. /**
  545. * Start a process.
  546. *
  547. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  548. * @param pipe_stdin pipe to use to send input to child process (or NULL)
  549. * @param pipe_stdout pipe to use to get output from child process (or NULL)
  550. * @param pipe_stderr pipe to use to get output from child process (or NULL)
  551. * @param filename name of the binary
  552. * @param argv NULL-terminated array of arguments to the process
  553. * @return pointer to process structure of the new process, NULL on error
  554. */
  555. struct GNUNET_OS_Process *
  556. GNUNET_OS_start_process_vap (enum GNUNET_OS_InheritStdioFlags std_inheritance,
  557. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  558. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  559. struct GNUNET_DISK_PipeHandle *pipe_stderr,
  560. const char *filename,
  561. char *const argv[])
  562. {
  563. return start_process (std_inheritance,
  564. pipe_stdin,
  565. pipe_stdout,
  566. pipe_stderr,
  567. NULL,
  568. filename,
  569. argv);
  570. }
  571. /**
  572. * Start a process.
  573. *
  574. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  575. * @param pipe_stdin pipe to use to send input to child process (or NULL)
  576. * @param pipe_stdout pipe to use to get output from child process (or NULL)
  577. * @param pipe_stderr pipe to use to get output from child process (or NULL)
  578. * @param filename name of the binary
  579. * @param va NULL-terminated list of arguments to the process
  580. * @return pointer to process structure of the new process, NULL on error
  581. */
  582. struct GNUNET_OS_Process *
  583. GNUNET_OS_start_process_va (enum GNUNET_OS_InheritStdioFlags std_inheritance,
  584. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  585. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  586. struct GNUNET_DISK_PipeHandle *pipe_stderr,
  587. const char *filename,
  588. va_list va)
  589. {
  590. struct GNUNET_OS_Process *ret;
  591. va_list ap;
  592. char **argv;
  593. int argc;
  594. argc = 0;
  595. va_copy (ap, va);
  596. while (NULL != va_arg (ap, char *))
  597. argc++;
  598. va_end (ap);
  599. argv = GNUNET_malloc (sizeof(char *) * (argc + 1));
  600. argc = 0;
  601. va_copy (ap, va);
  602. while (NULL != (argv[argc] = va_arg (ap, char *)))
  603. argc++;
  604. va_end (ap);
  605. ret = GNUNET_OS_start_process_vap (std_inheritance,
  606. pipe_stdin,
  607. pipe_stdout,
  608. pipe_stderr,
  609. filename,
  610. argv);
  611. GNUNET_free (argv);
  612. return ret;
  613. }
  614. /**
  615. * Start a process.
  616. *
  617. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  618. * @param pipe_stdin pipe to use to send input to child process (or NULL)
  619. * @param pipe_stdout pipe to use to get output from child process (or NULL)
  620. * @param filename name of the binary
  621. * @param ... NULL-terminated list of arguments to the process
  622. * @return pointer to process structure of the new process, NULL on error
  623. */
  624. struct GNUNET_OS_Process *
  625. GNUNET_OS_start_process (enum GNUNET_OS_InheritStdioFlags std_inheritance,
  626. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  627. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  628. struct GNUNET_DISK_PipeHandle *pipe_stderr,
  629. const char *filename,
  630. ...)
  631. {
  632. struct GNUNET_OS_Process *ret;
  633. va_list ap;
  634. va_start (ap, filename);
  635. ret = GNUNET_OS_start_process_va (std_inheritance,
  636. pipe_stdin,
  637. pipe_stdout,
  638. pipe_stderr,
  639. filename,
  640. ap);
  641. va_end (ap);
  642. return ret;
  643. }
  644. /**
  645. * Start a process.
  646. *
  647. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
  648. * std handles of the parent are inherited by the child.
  649. * pipe_stdin and pipe_stdout take priority over std_inheritance
  650. * (when they are non-NULL).
  651. * @param lsocks array of listen sockets to dup systemd-style (or NULL);
  652. * must be NULL on platforms where dup is not supported
  653. * @param filename name of the binary
  654. * @param argv NULL-terminated list of arguments to the process
  655. * @return process ID of the new process, -1 on error
  656. */
  657. struct GNUNET_OS_Process *
  658. GNUNET_OS_start_process_v (enum GNUNET_OS_InheritStdioFlags std_inheritance,
  659. const int *lsocks,
  660. const char *filename,
  661. char *const argv[])
  662. {
  663. return start_process (std_inheritance,
  664. NULL,
  665. NULL,
  666. NULL,
  667. lsocks,
  668. filename,
  669. argv);
  670. }
  671. /**
  672. * Start a process. This function is similar to the GNUNET_OS_start_process_*
  673. * except that the filename and arguments can have whole strings which contain
  674. * the arguments. These arguments are to be separated by spaces and are parsed
  675. * in the order they appear. Arguments containing spaces can be used by
  676. * quoting them with @em ".
  677. *
  678. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  679. * @param lsocks array of listen sockets to dup systemd-style (or NULL);
  680. * must be NULL on platforms where dup is not supported
  681. * @param filename name of the binary. It is valid to have the arguments
  682. * in this string when they are separated by spaces.
  683. * @param ... more arguments. Should be of type `char *`. It is valid
  684. * to have the arguments in these strings when they are separated by
  685. * spaces. The last argument MUST be NULL.
  686. * @return pointer to process structure of the new process, NULL on error
  687. */
  688. struct GNUNET_OS_Process *
  689. GNUNET_OS_start_process_s (enum GNUNET_OS_InheritStdioFlags std_inheritance,
  690. const int *lsocks,
  691. const char *filename,
  692. ...)
  693. {
  694. va_list ap;
  695. char **argv;
  696. unsigned int argv_size;
  697. const char *arg;
  698. const char *rpos;
  699. char *pos;
  700. char *cp;
  701. const char *last;
  702. struct GNUNET_OS_Process *proc;
  703. char *binary_path;
  704. int quote_on;
  705. unsigned int i;
  706. size_t len;
  707. argv_size = 1;
  708. va_start (ap, filename);
  709. arg = filename;
  710. last = NULL;
  711. do
  712. {
  713. rpos = arg;
  714. quote_on = 0;
  715. while ('\0' != *rpos)
  716. {
  717. if ('"' == *rpos)
  718. {
  719. if (1 == quote_on)
  720. quote_on = 0;
  721. else
  722. quote_on = 1;
  723. }
  724. if ((' ' == *rpos) && (0 == quote_on))
  725. {
  726. if (NULL != last)
  727. argv_size++;
  728. last = NULL;
  729. rpos++;
  730. while (' ' == *rpos)
  731. rpos++;
  732. }
  733. if ((NULL == last) && ('\0' != *rpos)) // FIXME: == or !=?
  734. last = rpos;
  735. if ('\0' != *rpos)
  736. rpos++;
  737. }
  738. if (NULL != last)
  739. argv_size++;
  740. }
  741. while (NULL != (arg = (va_arg (ap, const char *))));
  742. va_end (ap);
  743. argv = GNUNET_malloc (argv_size * sizeof(char *));
  744. argv_size = 0;
  745. va_start (ap, filename);
  746. arg = filename;
  747. last = NULL;
  748. do
  749. {
  750. cp = GNUNET_strdup (arg);
  751. quote_on = 0;
  752. pos = cp;
  753. while ('\0' != *pos)
  754. {
  755. if ('"' == *pos)
  756. {
  757. if (1 == quote_on)
  758. quote_on = 0;
  759. else
  760. quote_on = 1;
  761. }
  762. if ((' ' == *pos) && (0 == quote_on))
  763. {
  764. *pos = '\0';
  765. if (NULL != last)
  766. argv[argv_size++] = GNUNET_strdup (last);
  767. last = NULL;
  768. pos++;
  769. while (' ' == *pos)
  770. pos++;
  771. }
  772. if ((NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
  773. last = pos;
  774. if ('\0' != *pos)
  775. pos++;
  776. }
  777. if (NULL != last)
  778. argv[argv_size++] = GNUNET_strdup (last);
  779. last = NULL;
  780. GNUNET_free (cp);
  781. }
  782. while (NULL != (arg = (va_arg (ap, const char *))));
  783. va_end (ap);
  784. argv[argv_size] = NULL;
  785. for (i = 0; i < argv_size; i++)
  786. {
  787. len = strlen (argv[i]);
  788. if ((argv[i][0] == '"') && (argv[i][len - 1] == '"'))
  789. {
  790. memmove (&argv[i][0], &argv[i][1], len - 2);
  791. argv[i][len - 2] = '\0';
  792. }
  793. }
  794. binary_path = argv[0];
  795. proc = GNUNET_OS_start_process_v (std_inheritance,
  796. lsocks,
  797. binary_path,
  798. argv);
  799. while (argv_size > 0)
  800. GNUNET_free_nz (argv[--argv_size]);
  801. GNUNET_free (argv);
  802. return proc;
  803. }
  804. /**
  805. * Retrieve the status of a process, waiting on it if dead.
  806. * Nonblocking version.
  807. *
  808. * @param proc process ID
  809. * @param type status type
  810. * @param code return code/signal number
  811. * @param options WNOHANG if non-blocking is desired
  812. * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
  813. */
  814. static int
  815. process_status (struct GNUNET_OS_Process *proc,
  816. enum GNUNET_OS_ProcessStatusType *type,
  817. unsigned long *code,
  818. int options)
  819. {
  820. int status;
  821. int ret;
  822. GNUNET_assert (0 != proc);
  823. ret = waitpid (proc->pid, &status, options);
  824. if (ret < 0)
  825. {
  826. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
  827. return GNUNET_SYSERR;
  828. }
  829. if (0 == ret)
  830. {
  831. *type = GNUNET_OS_PROCESS_RUNNING;
  832. *code = 0;
  833. return GNUNET_NO;
  834. }
  835. if (proc->pid != ret)
  836. {
  837. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
  838. return GNUNET_SYSERR;
  839. }
  840. if (WIFEXITED (status))
  841. {
  842. *type = GNUNET_OS_PROCESS_EXITED;
  843. *code = WEXITSTATUS (status);
  844. }
  845. else if (WIFSIGNALED (status))
  846. {
  847. *type = GNUNET_OS_PROCESS_SIGNALED;
  848. *code = WTERMSIG (status);
  849. }
  850. else if (WIFSTOPPED (status))
  851. {
  852. *type = GNUNET_OS_PROCESS_SIGNALED;
  853. *code = WSTOPSIG (status);
  854. }
  855. #ifdef WIFCONTINUED
  856. else if (WIFCONTINUED (status))
  857. {
  858. *type = GNUNET_OS_PROCESS_RUNNING;
  859. *code = 0;
  860. }
  861. #endif
  862. else
  863. {
  864. *type = GNUNET_OS_PROCESS_UNKNOWN;
  865. *code = 0;
  866. }
  867. return GNUNET_OK;
  868. }
  869. /**
  870. * Retrieve the status of a process.
  871. * Nonblocking version.
  872. *
  873. * @param proc process ID
  874. * @param type status type
  875. * @param code return code/signal number
  876. * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
  877. */
  878. int
  879. GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
  880. enum GNUNET_OS_ProcessStatusType *type,
  881. unsigned long *code)
  882. {
  883. return process_status (proc, type, code, WNOHANG);
  884. }
  885. /**
  886. * Retrieve the status of a process, waiting on it if dead.
  887. * Blocking version.
  888. *
  889. * @param proc pointer to process structure
  890. * @param type status type
  891. * @param code return code/signal number
  892. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  893. */
  894. int
  895. GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc,
  896. enum GNUNET_OS_ProcessStatusType *type,
  897. unsigned long *code)
  898. {
  899. return process_status (proc, type, code, 0);
  900. }
  901. /**
  902. * Wait for a process to terminate. The return code is discarded.
  903. * You must not use #GNUNET_OS_process_status() on the same process
  904. * after calling this function! This function is blocking and should
  905. * thus only be used if the child process is known to have terminated
  906. * or to terminate very soon.
  907. *
  908. * @param proc pointer to process structure
  909. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  910. */
  911. int
  912. GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
  913. {
  914. pid_t pid = proc->pid;
  915. pid_t ret;
  916. while ((pid != (ret = waitpid (pid, NULL, 0))) && (EINTR == errno))
  917. ;
  918. if (pid != ret)
  919. {
  920. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
  921. return GNUNET_SYSERR;
  922. }
  923. return GNUNET_OK;
  924. }
  925. /**
  926. * Handle to a command.
  927. */
  928. struct GNUNET_OS_CommandHandle
  929. {
  930. /**
  931. * Process handle.
  932. */
  933. struct GNUNET_OS_Process *eip;
  934. /**
  935. * Handle to the output pipe.
  936. */
  937. struct GNUNET_DISK_PipeHandle *opipe;
  938. /**
  939. * Read-end of output pipe.
  940. */
  941. const struct GNUNET_DISK_FileHandle *r;
  942. /**
  943. * Function to call on each line of output.
  944. */
  945. GNUNET_OS_LineProcessor proc;
  946. /**
  947. * Closure for @e proc.
  948. */
  949. void *proc_cls;
  950. /**
  951. * Buffer for the output.
  952. */
  953. char buf[1024];
  954. /**
  955. * Task reading from pipe.
  956. */
  957. struct GNUNET_SCHEDULER_Task *rtask;
  958. /**
  959. * When to time out.
  960. */
  961. struct GNUNET_TIME_Absolute timeout;
  962. /**
  963. * Current read offset in buf.
  964. */
  965. size_t off;
  966. };
  967. /**
  968. * Stop/kill a command. Must ONLY be called either from
  969. * the callback after 'NULL' was passed for 'line' *OR*
  970. * from an independent task (not within the line processor).
  971. *
  972. * @param cmd handle to the process
  973. */
  974. void
  975. GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
  976. {
  977. if (NULL != cmd->proc)
  978. {
  979. GNUNET_assert (NULL != cmd->rtask);
  980. GNUNET_SCHEDULER_cancel (cmd->rtask);
  981. }
  982. (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
  983. GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
  984. GNUNET_OS_process_destroy (cmd->eip);
  985. GNUNET_DISK_pipe_close (cmd->opipe);
  986. GNUNET_free (cmd);
  987. }
  988. /**
  989. * Read from the process and call the line processor.
  990. *
  991. * @param cls the `struct GNUNET_OS_CommandHandle *`
  992. */
  993. static void
  994. cmd_read (void *cls)
  995. {
  996. struct GNUNET_OS_CommandHandle *cmd = cls;
  997. const struct GNUNET_SCHEDULER_TaskContext *tc;
  998. GNUNET_OS_LineProcessor proc;
  999. char *end;
  1000. ssize_t ret;
  1001. cmd->rtask = NULL;
  1002. tc = GNUNET_SCHEDULER_get_task_context ();
  1003. if (GNUNET_YES != GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r))
  1004. {
  1005. /* timeout */
  1006. proc = cmd->proc;
  1007. cmd->proc = NULL;
  1008. proc (cmd->proc_cls, NULL);
  1009. return;
  1010. }
  1011. ret = GNUNET_DISK_file_read (cmd->r,
  1012. &cmd->buf[cmd->off],
  1013. sizeof(cmd->buf) - cmd->off);
  1014. if (ret <= 0)
  1015. {
  1016. if ((cmd->off > 0) && (cmd->off < sizeof(cmd->buf)))
  1017. {
  1018. cmd->buf[cmd->off] = '\0';
  1019. cmd->proc (cmd->proc_cls, cmd->buf);
  1020. }
  1021. proc = cmd->proc;
  1022. cmd->proc = NULL;
  1023. proc (cmd->proc_cls, NULL);
  1024. return;
  1025. }
  1026. end = memchr (&cmd->buf[cmd->off], '\n', ret);
  1027. cmd->off += ret;
  1028. while (NULL != end)
  1029. {
  1030. *end = '\0';
  1031. cmd->proc (cmd->proc_cls, cmd->buf);
  1032. memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
  1033. cmd->off -= (end + 1 - cmd->buf);
  1034. end = memchr (cmd->buf, '\n', cmd->off);
  1035. }
  1036. cmd->rtask =
  1037. GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining (
  1038. cmd->timeout),
  1039. cmd->r,
  1040. &cmd_read,
  1041. cmd);
  1042. }
  1043. /**
  1044. * Run the given command line and call the given function
  1045. * for each line of the output.
  1046. *
  1047. * @param proc function to call for each line of the output
  1048. * @param proc_cls closure for @a proc
  1049. * @param timeout when to time out
  1050. * @param binary command to run
  1051. * @param ... arguments to command
  1052. * @return NULL on error
  1053. */
  1054. struct GNUNET_OS_CommandHandle *
  1055. GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
  1056. void *proc_cls,
  1057. struct GNUNET_TIME_Relative timeout,
  1058. const char *binary,
  1059. ...)
  1060. {
  1061. struct GNUNET_OS_CommandHandle *cmd;
  1062. struct GNUNET_OS_Process *eip;
  1063. struct GNUNET_DISK_PipeHandle *opipe;
  1064. va_list ap;
  1065. opipe = GNUNET_DISK_pipe (GNUNET_DISK_PF_BLOCKING_RW);
  1066. if (NULL == opipe)
  1067. return NULL;
  1068. va_start (ap, binary);
  1069. /* redirect stdout, don't inherit stderr/stdin */
  1070. eip =
  1071. GNUNET_OS_start_process_va (GNUNET_OS_INHERIT_STD_NONE,
  1072. NULL,
  1073. opipe,
  1074. NULL,
  1075. binary,
  1076. ap);
  1077. va_end (ap);
  1078. if (NULL == eip)
  1079. {
  1080. GNUNET_DISK_pipe_close (opipe);
  1081. return NULL;
  1082. }
  1083. GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
  1084. cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
  1085. cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  1086. cmd->eip = eip;
  1087. cmd->opipe = opipe;
  1088. cmd->proc = proc;
  1089. cmd->proc_cls = proc_cls;
  1090. cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ);
  1091. cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd);
  1092. return cmd;
  1093. }
  1094. /* end of os_priority.c */