gnunet_os_lib.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2011 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @author Christian Grothoff
  19. * @author Krista Bennett
  20. * @author Gerd Knorr <kraxel@bytesex.org>
  21. * @author Ioana Patrascu
  22. * @author Tzvetan Horozov
  23. * @author Milan
  24. *
  25. * @file
  26. * Low level process routines
  27. *
  28. * @defgroup os OS library
  29. * Low level process routines.
  30. *
  31. * This code manages child processes. We can communicate with child
  32. * processes using signals. Because signals are not supported on W32
  33. * and Java (at least not nicely), we can alternatively use a pipe
  34. * to send signals to the child processes (if the child process is
  35. * a full-blown GNUnet process that supports reading signals from
  36. * a pipe, of course). Naturally, this also only works for 'normal'
  37. * termination via signals, and not as a replacement for SIGKILL.
  38. * Thus using pipes to communicate signals should only be enabled if
  39. * the child is a Java process OR if we are on Windoze.
  40. *
  41. * @{
  42. */
  43. #ifndef GNUNET_OS_LIB_H
  44. #define GNUNET_OS_LIB_H
  45. #ifdef __cplusplus
  46. extern "C"
  47. {
  48. #if 0 /* keep Emacsens' auto-indent happy */
  49. }
  50. #endif
  51. #endif
  52. #include "gnunet_common.h"
  53. #include "gnunet_configuration_lib.h"
  54. #include "gnunet_scheduler_lib.h"
  55. /**
  56. * Flags that determine which of the standard streams
  57. * should be inherited by the child process.
  58. */
  59. enum GNUNET_OS_InheritStdioFlags
  60. {
  61. /**
  62. * No standard streams should be inherited.
  63. */
  64. GNUNET_OS_INHERIT_STD_NONE = 0,
  65. /**
  66. * When this flag is set, the child process will
  67. * inherit stdin of the parent.
  68. */
  69. GNUNET_OS_INHERIT_STD_IN = 1,
  70. /**
  71. * When this flag is set, the child process will
  72. * inherit stdout of the parent.
  73. */
  74. GNUNET_OS_INHERIT_STD_OUT = 2,
  75. /**
  76. * When this flag is set, the child process will
  77. * inherit stderr of the parent.
  78. */
  79. GNUNET_OS_INHERIT_STD_ERR = 4,
  80. /**
  81. * When these flags are set, the child process will
  82. * inherit stdout and stderr of the parent.
  83. */
  84. GNUNET_OS_INHERIT_STD_OUT_AND_ERR = 6,
  85. /**
  86. * Use this option to have all of the standard streams
  87. * (stdin, stdout and stderror) be inherited.
  88. */
  89. GNUNET_OS_INHERIT_STD_ALL = 7
  90. };
  91. /**
  92. * Process information (OS-dependent)
  93. */
  94. struct GNUNET_OS_Process;
  95. /**
  96. * Possible installation paths to request
  97. */
  98. enum GNUNET_OS_InstallationPathKind
  99. {
  100. /**
  101. * Return the "PREFIX" directory given to configure.
  102. */
  103. GNUNET_OS_IPK_PREFIX,
  104. /**
  105. * Return the directory where the program binaries are installed. (bin/)
  106. */
  107. GNUNET_OS_IPK_BINDIR,
  108. /**
  109. * Return the directory where libraries are installed. (lib/gnunet/)
  110. */
  111. GNUNET_OS_IPK_LIBDIR,
  112. /**
  113. * Return the directory where data is installed (share/gnunet/)
  114. */
  115. GNUNET_OS_IPK_DATADIR,
  116. /**
  117. * Return the directory where translations are installed (share/locale/)
  118. */
  119. GNUNET_OS_IPK_LOCALEDIR,
  120. /**
  121. * Return the installation directory of this application, not
  122. * the one of the overall GNUnet installation (in case they
  123. * are different).
  124. */
  125. GNUNET_OS_IPK_SELF_PREFIX,
  126. /**
  127. * Return the prefix of the path with application icons (share/icons/).
  128. */
  129. GNUNET_OS_IPK_ICONDIR,
  130. /**
  131. * Return the prefix of the path with documentation files, including the
  132. * license (share/doc/gnunet/).
  133. */
  134. GNUNET_OS_IPK_DOCDIR,
  135. /**
  136. * Return the directory where helper binaries are installed (lib/gnunet/libexec/)
  137. */
  138. GNUNET_OS_IPK_LIBEXECDIR
  139. };
  140. /**
  141. * Process status types
  142. */
  143. enum GNUNET_OS_ProcessStatusType
  144. {
  145. /**
  146. * The process is not known to the OS (or at
  147. * least not one of our children).
  148. */
  149. GNUNET_OS_PROCESS_UNKNOWN,
  150. /**
  151. * The process is still running.
  152. */
  153. GNUNET_OS_PROCESS_RUNNING,
  154. /**
  155. * The process is paused (but could be resumed).
  156. */
  157. GNUNET_OS_PROCESS_STOPPED,
  158. /**
  159. * The process exited with a return code.
  160. */
  161. GNUNET_OS_PROCESS_EXITED,
  162. /**
  163. * The process was killed by a signal.
  164. */
  165. GNUNET_OS_PROCESS_SIGNALED
  166. };
  167. /**
  168. * Get the path to a specific GNUnet installation directory or, with
  169. * #GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation
  170. * directory.
  171. *
  172. * @param dirkind what kind of directory is desired?
  173. * @return a pointer to the dir path (to be freed by the caller)
  174. */
  175. char *
  176. GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind);
  177. /**
  178. * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon
  179. * binary, try to prefix it with the libexec/-directory to get the
  180. * full path.
  181. *
  182. * @param progname name of the binary
  183. * @return full path to the binary, if possible, otherwise copy of 'progname'
  184. */
  185. char *
  186. GNUNET_OS_get_libexec_binary_path (const char *progname);
  187. /**
  188. * Callback function invoked for each interface found.
  189. *
  190. * @param cls closure
  191. * @param name name of the interface (can be NULL for unknown)
  192. * @param isDefault is this presumably the default interface
  193. * @param addr address of this interface (can be NULL for unknown or unassigned)
  194. * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
  195. * @param netmask the network mask (can be NULL for unknown or unassigned)
  196. * @param addrlen length of the address
  197. * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
  198. */
  199. typedef int
  200. (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls,
  201. const char *name,
  202. int isDefault,
  203. const struct sockaddr *addr,
  204. const struct sockaddr *broadcast_addr,
  205. const struct sockaddr *netmask,
  206. socklen_t addrlen);
  207. /**
  208. * @brief Enumerate all network interfaces
  209. *
  210. * @param proc the callback function
  211. * @param proc_cls closure for @a proc
  212. */
  213. void
  214. GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
  215. void *proc_cls);
  216. /**
  217. * @brief Get maximum string length returned by gethostname()
  218. */
  219. #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
  220. #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
  221. #elif defined(HOST_NAME_MAX)
  222. #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
  223. #else
  224. #define GNUNET_OS_get_hostname_max_length() 255
  225. #endif
  226. /**
  227. * Get process structure for current process
  228. *
  229. * The pointer it returns points to static memory location and must not be
  230. * deallocated/closed
  231. *
  232. * @return pointer to the process sturcutre for this process
  233. */
  234. struct GNUNET_OS_Process *
  235. GNUNET_OS_process_current (void);
  236. /**
  237. * Sends a signal to the process
  238. *
  239. * @param proc pointer to process structure
  240. * @param sig signal
  241. * @return 0 on success, -1 on error
  242. */
  243. int
  244. GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig);
  245. /**
  246. * Cleans up process structure contents (OS-dependent) and deallocates it
  247. *
  248. * @param proc pointer to process structure
  249. */
  250. void
  251. GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc);
  252. /**
  253. * Get the pid of the process in question
  254. *
  255. * @param proc the process to get the pid of
  256. *
  257. * @return the current process id
  258. */
  259. pid_t
  260. GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc);
  261. /**
  262. * Start a process.
  263. *
  264. * @param pipe_control should a pipe be used to send signals to the child?
  265. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  266. * @param pipe_stdin pipe to use to send input to child process (or NULL)
  267. * @param pipe_stdout pipe to use to get output from child process (or NULL)
  268. * @param pipe_stderr pipe to use to get error output from child process (or NULL)
  269. * @param filename name of the binary
  270. * @param argv NULL-terminated array of arguments to the process
  271. * @return pointer to process structure of the new process, NULL on error
  272. */
  273. struct GNUNET_OS_Process *
  274. GNUNET_OS_start_process_vap (int pipe_control,
  275. enum GNUNET_OS_InheritStdioFlags std_inheritance,
  276. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  277. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  278. struct GNUNET_DISK_PipeHandle *pipe_stderr,
  279. const char *filename,
  280. char *const argv[]);
  281. /**
  282. * Start a process.
  283. *
  284. * @param pipe_control should a pipe be used to send signals to the child?
  285. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  286. * @param pipe_stdin pipe to use to send input to child process (or NULL)
  287. * @param pipe_stdout pipe to use to get output from child process (or NULL)
  288. * @param pipe_stderr pipe to use to get error output from child process (or NULL)
  289. * @param filename name of the binary
  290. * @param ... NULL-terminated list of arguments to the process
  291. * @return pointer to process structure of the new process, NULL on error
  292. */
  293. struct GNUNET_OS_Process *
  294. GNUNET_OS_start_process (int pipe_control,
  295. enum GNUNET_OS_InheritStdioFlags std_inheritance,
  296. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  297. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  298. struct GNUNET_DISK_PipeHandle *pipe_stderr,
  299. const char *filename, ...);
  300. /**
  301. * Start a process.
  302. *
  303. * @param pipe_control should a pipe be used to send signals to the child?
  304. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  305. * @param pipe_stdin pipe to use to send input to child process (or NULL)
  306. * @param pipe_stdout pipe to use to get output from child process (or NULL)
  307. * @param pipe_stderr pipe to use to get error output from child process (or NULL)
  308. * @param filename name of the binary
  309. * @param va NULL-terminated list of arguments to the process
  310. * @return pointer to process structure of the new process, NULL on error
  311. */
  312. struct GNUNET_OS_Process *
  313. GNUNET_OS_start_process_va (int pipe_control,
  314. enum GNUNET_OS_InheritStdioFlags std_inheritance,
  315. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  316. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  317. struct GNUNET_DISK_PipeHandle *pipe_stderr,
  318. const char *filename, va_list va);
  319. /**
  320. * Start a process.
  321. *
  322. * @param pipe_control should a pipe be used to send signals to the child?
  323. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  324. * @param lsocks array of listen sockets to dup systemd-style (or NULL);
  325. * must be NULL on platforms where dup is not supported
  326. * @param filename name of the binary
  327. * @param argv NULL-terminated list of arguments to the process,
  328. * including the process name as the first argument
  329. * @return pointer to process structure of the new process, NULL on error
  330. */
  331. struct GNUNET_OS_Process *
  332. GNUNET_OS_start_process_v (int pipe_control,
  333. enum GNUNET_OS_InheritStdioFlags std_inheritance,
  334. const SOCKTYPE *lsocks,
  335. const char *filename,
  336. char *const argv[]);
  337. /**
  338. * Start a process. This function is similar to the GNUNET_OS_start_process_*
  339. * except that the filename and arguments can have whole strings which contain
  340. * the arguments. These arguments are to be separated by spaces and are parsed
  341. * in the order they appear. Arguments containing spaces can be used by
  342. * quoting them with @em ".
  343. *
  344. * @param pipe_control should a pipe be used to send signals to the child?
  345. * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
  346. * @param lsocks array of listen sockets to dup systemd-style (or NULL);
  347. * must be NULL on platforms where dup is not supported
  348. * @param filename name of the binary. It is valid to have the arguments
  349. * in this string when they are separated by spaces.
  350. * @param ... more arguments. Should be of type `char *`. It is valid
  351. * to have the arguments in these strings when they are separated by
  352. * spaces. The last argument MUST be NULL.
  353. * @return pointer to process structure of the new process, NULL on error
  354. */
  355. struct GNUNET_OS_Process *
  356. GNUNET_OS_start_process_s (int pipe_control,
  357. unsigned int std_inheritance,
  358. const SOCKTYPE * lsocks,
  359. const char *filename, ...);
  360. /**
  361. * Handle to a command action.
  362. */
  363. struct GNUNET_OS_CommandHandle;
  364. /**
  365. * Type of a function to process a line of output.
  366. *
  367. * @param cls closure
  368. * @param line line of output from a command, NULL for the end
  369. */
  370. typedef void (*GNUNET_OS_LineProcessor) (void *cls, const char *line);
  371. /**
  372. * Stop/kill a command.
  373. *
  374. * @param cmd handle to the process
  375. */
  376. void
  377. GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd);
  378. /**
  379. * Run the given command line and call the given function
  380. * for each line of the output.
  381. *
  382. * @param proc function to call for each line of the output
  383. * @param proc_cls closure for proc
  384. * @param timeout when to time out
  385. * @param binary command to run
  386. * @param ... arguments to command
  387. * @return NULL on error
  388. */
  389. struct GNUNET_OS_CommandHandle *
  390. GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
  391. struct GNUNET_TIME_Relative timeout, const char *binary,
  392. ...);
  393. /**
  394. * Retrieve the status of a process, waiting on him if dead.
  395. * Nonblocking version.
  396. *
  397. * @param proc pointer to process structure
  398. * @param type status type
  399. * @param code return code/signal number
  400. * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
  401. */
  402. int
  403. GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
  404. enum GNUNET_OS_ProcessStatusType *type,
  405. unsigned long *code);
  406. /**
  407. * Wait for a process to terminate. The return code is discarded.
  408. * You must not use #GNUNET_OS_process_status() on the same process
  409. * after calling this function! This function is blocking and should
  410. * thus only be used if the child process is known to have terminated
  411. * or to terminate very soon.
  412. *
  413. * @param proc pointer to process structure of the process to wait for
  414. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  415. */
  416. int
  417. GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc);
  418. /**
  419. * Connects this process to its parent via pipe;
  420. * essentially, the parent control handler will read signal numbers
  421. * from the #GNUNET_OS_CONTROL_PIPE (as given in an environment
  422. * variable) and raise those signals.
  423. *
  424. * @param cls closure (unused)
  425. * @param tc scheduler context (unused)
  426. */
  427. void
  428. GNUNET_OS_install_parent_control_handler (void *cls,
  429. const struct
  430. GNUNET_SCHEDULER_TaskContext *tc);
  431. /**
  432. * Check whether an executable exists and possibly
  433. * if the suid bit is set on the file.
  434. * Attempts to find the file using the current
  435. * PATH environment variable as a search path.
  436. *
  437. * @param binary the name of the file to check.
  438. * W32: must not have an .exe suffix.
  439. * @param check_suid input true if the binary should be checked for SUID (*nix)
  440. * W32: checks if the program has sufficient privileges by executing this
  441. * binary with the -d flag. -d omits a programs main loop and only
  442. * executes all privileged operations in an binary.
  443. * @param params parameters used for w32 privilege checking (can be NULL for != w32, or when not checking for suid/permissions )
  444. * @return #GNUNET_YES if the file is SUID (*nix) or can be executed with current privileges (W32),
  445. * #GNUNET_NO if not SUID (but binary exists),
  446. * #GNUNET_SYSERR on error (no such binary or not executable)
  447. */
  448. int
  449. GNUNET_OS_check_helper_binary (const char *binary,
  450. int check_suid,
  451. const char *params);
  452. #if 0 /* keep Emacsens' auto-indent happy */
  453. {
  454. #endif
  455. #ifdef __cplusplus
  456. }
  457. #endif
  458. /* ifndef GNUNET_OS_LIB_H */
  459. #endif
  460. /** @} */ /* end of group */
  461. /* end of gnunet_os_lib.h */