2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2011 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @author Christian Grothoff
23 * @author Krista Bennett
24 * @author Gerd Knorr <kraxel@bytesex.org>
25 * @author Ioana Patrascu
26 * @author Tzvetan Horozov
30 * Low level process routines
32 * @defgroup os OS library
33 * Low level process routines.
35 * This code manages child processes. We can communicate with child
36 * processes using signals. Because signals are not supported on W32
37 * and Java (at least not nicely), we can alternatively use a pipe
38 * to send signals to the child processes (if the child process is
39 * a full-blown GNUnet process that supports reading signals from
40 * a pipe, of course). Naturally, this also only works for 'normal'
41 * termination via signals, and not as a replacement for SIGKILL.
42 * Thus using pipes to communicate signals should only be enabled if
43 * the child is a Java process OR if we are on Windoze.
48 #ifndef GNUNET_OS_LIB_H
49 #define GNUNET_OS_LIB_H
54 #if 0 /* keep Emacsens' auto-indent happy */
59 #include "gnunet_common.h"
60 #include "gnunet_configuration_lib.h"
61 #include "gnunet_scheduler_lib.h"
65 * Flags that determine which of the standard streams
66 * should be inherited by the child process.
68 enum GNUNET_OS_InheritStdioFlags
72 * No standard streams should be inherited.
74 GNUNET_OS_INHERIT_STD_NONE = 0,
77 * When this flag is set, the child process will
78 * inherit stdin of the parent.
80 GNUNET_OS_INHERIT_STD_IN = 1,
83 * When this flag is set, the child process will
84 * inherit stdout of the parent.
86 GNUNET_OS_INHERIT_STD_OUT = 2,
89 * When this flag is set, the child process will
90 * inherit stderr of the parent.
92 GNUNET_OS_INHERIT_STD_ERR = 4,
95 * When these flags are set, the child process will
96 * inherit stdout and stderr of the parent.
98 GNUNET_OS_INHERIT_STD_OUT_AND_ERR = 6,
101 * Use this option to have all of the standard streams
102 * (stdin, stdout and stderror) be inherited.
104 GNUNET_OS_INHERIT_STD_ALL = 7
109 * Process information (OS-dependent)
111 struct GNUNET_OS_Process;
115 * Possible installation paths to request
117 enum GNUNET_OS_InstallationPathKind
120 * Return the "PREFIX" directory given to configure.
122 GNUNET_OS_IPK_PREFIX,
125 * Return the directory where the program binaries are installed. (bin/)
127 GNUNET_OS_IPK_BINDIR,
130 * Return the directory where libraries are installed. (lib/gnunet/)
132 GNUNET_OS_IPK_LIBDIR,
135 * Return the directory where data is installed (share/gnunet/)
137 GNUNET_OS_IPK_DATADIR,
140 * Return the directory where translations are installed (share/locale/)
142 GNUNET_OS_IPK_LOCALEDIR,
145 * Return the installation directory of this application, not
146 * the one of the overall GNUnet installation (in case they
149 GNUNET_OS_IPK_SELF_PREFIX,
152 * Return the prefix of the path with application icons (share/icons/).
154 GNUNET_OS_IPK_ICONDIR,
157 * Return the prefix of the path with documentation files, including the
158 * license (share/doc/gnunet/).
160 GNUNET_OS_IPK_DOCDIR,
163 * Return the directory where helper binaries are installed (lib/gnunet/libexec/)
165 GNUNET_OS_IPK_LIBEXECDIR
170 * Process status types
172 enum GNUNET_OS_ProcessStatusType
175 * The process is not known to the OS (or at
176 * least not one of our children).
178 GNUNET_OS_PROCESS_UNKNOWN,
181 * The process is still running.
183 GNUNET_OS_PROCESS_RUNNING,
186 * The process is paused (but could be resumed).
188 GNUNET_OS_PROCESS_STOPPED,
191 * The process exited with a return code.
193 GNUNET_OS_PROCESS_EXITED,
196 * The process was killed by a signal.
198 GNUNET_OS_PROCESS_SIGNALED
203 * Project-specific data used to help the OS subsystem
204 * find installation paths.
206 struct GNUNET_OS_ProjectData
209 * Name of a library that is installed in the "lib/" directory of
210 * the project, such as "libgnunetutil". Used to locate the
211 * installation by scanning dependencies of the current process.
216 * Name of the project that is used in the "libexec" prefix, For
217 * example, "gnunet". Certain helper binaries are then expected to
218 * be installed in "$PREFIX/libexec/gnunet/" and resources in
219 * "$PREFIX/share/gnunet/".
221 const char *project_dirname;
224 * Name of a project-specific binary that should be in "$PREFIX/bin/".
225 * Used to determine installation path from $PATH variable.
226 * For example "gnunet-arm". On W32, ".exe" should be omitted.
228 const char *binary_name;
231 * Name of an environment variable that can be used to override
232 * installation path detection, for example "GNUNET_PREFIX".
234 const char *env_varname;
237 * Alternative name of an environment variable that can be used to
238 * override installation path detection, if "env_varname" is not
239 * set. Again, for example, "GNUNET_PREFIX".
241 const char *env_varname_alt;
244 * Name of an environment variable that can be used to override
245 * the location from which default configuration files are loaded
246 * from, for example "GNUNET_BASE_CONFIG".
248 const char *base_config_varname;
251 * E-mail address for reporting bugs.
253 const char *bug_email;
258 const char *homepage;
261 * Configuration file name (in $XDG_CONFIG_HOME) to use.
263 const char *config_file;
266 * Configuration file name to use (if $XDG_CONFIG_HOME is not set).
268 const char *user_config_file;
274 * Return default project data used by 'libgnunetutil' for GNUnet.
276 const struct GNUNET_OS_ProjectData *
277 GNUNET_OS_project_data_default (void);
281 * @return current (actual) project data.
283 const struct GNUNET_OS_ProjectData *
284 GNUNET_OS_project_data_get (void);
288 * Setup OS subsystem with project data.
290 * @param pd project data used to determine paths.
293 GNUNET_OS_init (const struct GNUNET_OS_ProjectData *pd);
297 * Get the path to a specific GNUnet installation directory or, with
298 * #GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation
301 * @param dirkind what kind of directory is desired?
302 * @return a pointer to the dir path (to be freed by the caller)
305 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind);
309 * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon
310 * binary, try to prefix it with the libexec/-directory to get the
313 * @param progname name of the binary
314 * @return full path to the binary, if possible, otherwise copy of 'progname'
317 GNUNET_OS_get_libexec_binary_path (const char *progname);
321 * Callback function invoked for each interface found.
324 * @param name name of the interface (can be NULL for unknown)
325 * @param isDefault is this presumably the default interface
326 * @param addr address of this interface (can be NULL for unknown or unassigned)
327 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
328 * @param netmask the network mask (can be NULL for unknown or unassigned)
329 * @param addrlen length of the address
330 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
333 (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls,
336 const struct sockaddr *addr,
337 const struct sockaddr *broadcast_addr,
338 const struct sockaddr *netmask,
343 * @brief Enumerate all network interfaces
345 * @param proc the callback function
346 * @param proc_cls closure for @a proc
349 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
353 * @brief Get maximum string length returned by gethostname()
355 #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
356 #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
357 #elif defined(HOST_NAME_MAX)
358 #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
360 #define GNUNET_OS_get_hostname_max_length() 255
365 * Get process structure for current process
367 * The pointer it returns points to static memory location and must not be
370 * @return pointer to the process sturcutre for this process
372 struct GNUNET_OS_Process *
373 GNUNET_OS_process_current (void);
377 * Sends a signal to the process
379 * @param proc pointer to process structure
381 * @return 0 on success, -1 on error
384 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc,
389 * Cleans up process structure contents (OS-dependent) and deallocates it
391 * @param proc pointer to process structure
394 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc);
398 * Get the pid of the process in question
400 * @param proc the process to get the pid of
402 * @return the current process id
405 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc);
411 * @param pipe_control should a pipe be used to send signals to the child?
412 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
413 * @param pipe_stdin pipe to use to send input to child process (or NULL)
414 * @param pipe_stdout pipe to use to get output from child process (or NULL)
415 * @param pipe_stderr pipe to use to get error output from child process (or NULL)
416 * @param filename name of the binary
417 * @param argv NULL-terminated array of arguments to the process
418 * @return pointer to process structure of the new process, NULL on error
420 struct GNUNET_OS_Process *
421 GNUNET_OS_start_process_vap (int pipe_control,
422 enum GNUNET_OS_InheritStdioFlags std_inheritance,
423 struct GNUNET_DISK_PipeHandle *pipe_stdin,
424 struct GNUNET_DISK_PipeHandle *pipe_stdout,
425 struct GNUNET_DISK_PipeHandle *pipe_stderr,
426 const char *filename,
433 * @param pipe_control should a pipe be used to send signals to the child?
434 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
435 * @param pipe_stdin pipe to use to send input to child process (or NULL)
436 * @param pipe_stdout pipe to use to get output from child process (or NULL)
437 * @param pipe_stderr pipe to use to get error output from child process (or NULL)
438 * @param filename name of the binary
439 * @param ... NULL-terminated list of arguments to the process
440 * @return pointer to process structure of the new process, NULL on error
442 struct GNUNET_OS_Process *
443 GNUNET_OS_start_process (int pipe_control,
444 enum GNUNET_OS_InheritStdioFlags std_inheritance,
445 struct GNUNET_DISK_PipeHandle *pipe_stdin,
446 struct GNUNET_DISK_PipeHandle *pipe_stdout,
447 struct GNUNET_DISK_PipeHandle *pipe_stderr,
448 const char *filename, ...);
454 * @param pipe_control should a pipe be used to send signals to the child?
455 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
456 * @param pipe_stdin pipe to use to send input to child process (or NULL)
457 * @param pipe_stdout pipe to use to get output from child process (or NULL)
458 * @param pipe_stderr pipe to use to get error output from child process (or NULL)
459 * @param filename name of the binary
460 * @param va NULL-terminated list of arguments to the process
461 * @return pointer to process structure of the new process, NULL on error
463 struct GNUNET_OS_Process *
464 GNUNET_OS_start_process_va (int pipe_control,
465 enum GNUNET_OS_InheritStdioFlags std_inheritance,
466 struct GNUNET_DISK_PipeHandle *pipe_stdin,
467 struct GNUNET_DISK_PipeHandle *pipe_stdout,
468 struct GNUNET_DISK_PipeHandle *pipe_stderr,
469 const char *filename, va_list va);
474 * @param pipe_control should a pipe be used to send signals to the child?
475 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
476 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
477 * must be NULL on platforms where dup is not supported
478 * @param filename name of the binary
479 * @param argv NULL-terminated list of arguments to the process,
480 * including the process name as the first argument
481 * @return pointer to process structure of the new process, NULL on error
483 struct GNUNET_OS_Process *
484 GNUNET_OS_start_process_v (int pipe_control,
485 enum GNUNET_OS_InheritStdioFlags std_inheritance,
486 const SOCKTYPE *lsocks,
487 const char *filename,
492 * Start a process. This function is similar to the GNUNET_OS_start_process_*
493 * except that the filename and arguments can have whole strings which contain
494 * the arguments. These arguments are to be separated by spaces and are parsed
495 * in the order they appear. Arguments containing spaces can be used by
496 * quoting them with @em ".
498 * @param pipe_control should a pipe be used to send signals to the child?
499 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
500 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
501 * must be NULL on platforms where dup is not supported
502 * @param filename name of the binary. It is valid to have the arguments
503 * in this string when they are separated by spaces.
504 * @param ... more arguments. Should be of type `char *`. It is valid
505 * to have the arguments in these strings when they are separated by
506 * spaces. The last argument MUST be NULL.
507 * @return pointer to process structure of the new process, NULL on error
509 struct GNUNET_OS_Process *
510 GNUNET_OS_start_process_s (int pipe_control,
511 unsigned int std_inheritance,
512 const SOCKTYPE * lsocks,
513 const char *filename, ...);
517 * Handle to a command action.
519 struct GNUNET_OS_CommandHandle;
523 * Type of a function to process a line of output.
526 * @param line line of output from a command, NULL for the end
529 (*GNUNET_OS_LineProcessor) (void *cls, const char *line);
533 * Stop/kill a command.
535 * @param cmd handle to the process
538 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd);
542 * Run the given command line and call the given function
543 * for each line of the output.
545 * @param proc function to call for each line of the output
546 * @param proc_cls closure for proc
547 * @param timeout when to time out
548 * @param binary command to run
549 * @param ... arguments to command
550 * @return NULL on error
552 struct GNUNET_OS_CommandHandle *
553 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
555 struct GNUNET_TIME_Relative timeout,
561 * Retrieve the status of a process, waiting on it if dead.
562 * Nonblocking version.
564 * @param proc pointer to process structure
565 * @param type status type
566 * @param code return code/signal number
567 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
570 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
571 enum GNUNET_OS_ProcessStatusType *type,
572 unsigned long *code);
576 * Wait for a process to terminate. The return code is discarded.
577 * You must not use #GNUNET_OS_process_status() on the same process
578 * after calling this function! This function is blocking and should
579 * thus only be used if the child process is known to have terminated
580 * or to terminate very soon.
582 * @param proc pointer to process structure of the process to wait for
583 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
586 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc);
591 * Retrieve the status of a process, waiting on it if dead.
594 * @param proc pointer to process structure
595 * @param type status type
596 * @param code return code/signal number
597 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
600 GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc,
601 enum GNUNET_OS_ProcessStatusType *type,
602 unsigned long *code);
606 * Connects this process to its parent via pipe;
607 * essentially, the parent control handler will read signal numbers
608 * from the #GNUNET_OS_CONTROL_PIPE (as given in an environment
609 * variable) and raise those signals.
611 * @param cls closure (unused)
614 GNUNET_OS_install_parent_control_handler (void *cls);
618 * Check whether an executable exists and possibly
619 * if the suid bit is set on the file.
620 * Attempts to find the file using the current
621 * PATH environment variable as a search path.
623 * @param binary the name of the file to check.
624 * W32: must not have an .exe suffix.
625 * @param check_suid input true if the binary should be checked for SUID (*nix)
626 * W32: checks if the program has sufficient privileges by executing this
627 * binary with the -d flag. -d omits a programs main loop and only
628 * executes all privileged operations in an binary.
629 * @param params parameters used for w32 privilege checking (can be NULL for != w32, or when not checking for suid/permissions )
630 * @return #GNUNET_YES if the file is SUID (*nix) or can be executed with current privileges (W32),
631 * #GNUNET_NO if not SUID (but binary exists),
632 * #GNUNET_SYSERR on error (no such binary or not executable)
635 GNUNET_OS_check_helper_binary (const char *binary,
640 #if 0 /* keep Emacsens' auto-indent happy */
647 /* ifndef GNUNET_OS_LIB_H */
650 /** @} */ /* end of group */
652 /* end of gnunet_os_lib.h */