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/>.
20 * @author Christian Grothoff
21 * @author Krista Bennett
22 * @author Gerd Knorr <kraxel@bytesex.org>
23 * @author Ioana Patrascu
24 * @author Tzvetan Horozov
28 * Low level process routines
30 * @defgroup os OS library
31 * Low level process routines.
33 * This code manages child processes. We can communicate with child
34 * processes using signals. Because signals are not supported on W32
35 * and Java (at least not nicely), we can alternatively use a pipe
36 * to send signals to the child processes (if the child process is
37 * a full-blown GNUnet process that supports reading signals from
38 * a pipe, of course). Naturally, this also only works for 'normal'
39 * termination via signals, and not as a replacement for SIGKILL.
40 * Thus using pipes to communicate signals should only be enabled if
41 * the child is a Java process OR if we are on Windoze.
46 #ifndef GNUNET_OS_LIB_H
47 #define GNUNET_OS_LIB_H
52 #if 0 /* keep Emacsens' auto-indent happy */
57 #include "gnunet_common.h"
58 #include "gnunet_configuration_lib.h"
59 #include "gnunet_scheduler_lib.h"
63 * Flags that determine which of the standard streams
64 * should be inherited by the child process.
66 enum GNUNET_OS_InheritStdioFlags
70 * No standard streams should be inherited.
72 GNUNET_OS_INHERIT_STD_NONE = 0,
75 * When this flag is set, the child process will
76 * inherit stdin of the parent.
78 GNUNET_OS_INHERIT_STD_IN = 1,
81 * When this flag is set, the child process will
82 * inherit stdout of the parent.
84 GNUNET_OS_INHERIT_STD_OUT = 2,
87 * When this flag is set, the child process will
88 * inherit stderr of the parent.
90 GNUNET_OS_INHERIT_STD_ERR = 4,
93 * When these flags are set, the child process will
94 * inherit stdout and stderr of the parent.
96 GNUNET_OS_INHERIT_STD_OUT_AND_ERR = 6,
99 * Use this option to have all of the standard streams
100 * (stdin, stdout and stderror) be inherited.
102 GNUNET_OS_INHERIT_STD_ALL = 7
107 * Process information (OS-dependent)
109 struct GNUNET_OS_Process;
113 * Possible installation paths to request
115 enum GNUNET_OS_InstallationPathKind
118 * Return the "PREFIX" directory given to configure.
120 GNUNET_OS_IPK_PREFIX,
123 * Return the directory where the program binaries are installed. (bin/)
125 GNUNET_OS_IPK_BINDIR,
128 * Return the directory where libraries are installed. (lib/gnunet/)
130 GNUNET_OS_IPK_LIBDIR,
133 * Return the directory where data is installed (share/gnunet/)
135 GNUNET_OS_IPK_DATADIR,
138 * Return the directory where translations are installed (share/locale/)
140 GNUNET_OS_IPK_LOCALEDIR,
143 * Return the installation directory of this application, not
144 * the one of the overall GNUnet installation (in case they
147 GNUNET_OS_IPK_SELF_PREFIX,
150 * Return the prefix of the path with application icons (share/icons/).
152 GNUNET_OS_IPK_ICONDIR,
155 * Return the prefix of the path with documentation files, including the
156 * license (share/doc/gnunet/).
158 GNUNET_OS_IPK_DOCDIR,
161 * Return the directory where helper binaries are installed (lib/gnunet/libexec/)
163 GNUNET_OS_IPK_LIBEXECDIR
168 * Process status types
170 enum GNUNET_OS_ProcessStatusType
173 * The process is not known to the OS (or at
174 * least not one of our children).
176 GNUNET_OS_PROCESS_UNKNOWN,
179 * The process is still running.
181 GNUNET_OS_PROCESS_RUNNING,
184 * The process is paused (but could be resumed).
186 GNUNET_OS_PROCESS_STOPPED,
189 * The process exited with a return code.
191 GNUNET_OS_PROCESS_EXITED,
194 * The process was killed by a signal.
196 GNUNET_OS_PROCESS_SIGNALED
201 * Project-specific data used to help the OS subsystem
202 * find installation paths.
204 struct GNUNET_OS_ProjectData
207 * Name of a library that is installed in the "lib/" directory of
208 * the project, such as "libgnunetutil". Used to locate the
209 * installation by scanning dependencies of the current process.
214 * Name of the project that is used in the "libexec" prefix, For
215 * example, "gnunet". Certain helper binaries are then expected to
216 * be installed in "$PREFIX/libexec/gnunet/" and resources in
217 * "$PREFIX/share/gnunet/".
219 const char *project_dirname;
222 * Name of a project-specific binary that should be in "$PREFIX/bin/".
223 * Used to determine installation path from $PATH variable.
224 * For example "gnunet-arm". On W32, ".exe" should be omitted.
226 const char *binary_name;
229 * Name of an environment variable that can be used to override
230 * installation path detection, for example "GNUNET_PREFIX".
232 const char *env_varname;
235 * Alternative name of an environment variable that can be used to
236 * override installation path detection, if "env_varname" is not
237 * set. Again, for example, "GNUNET_PREFIX".
239 const char *env_varname_alt;
242 * Name of an environment variable that can be used to override
243 * the location from which default configuration files are loaded
244 * from, for example "GNUNET_BASE_CONFIG".
246 const char *base_config_varname;
249 * E-mail address for reporting bugs.
251 const char *bug_email;
256 const char *homepage;
259 * Configuration file name (in $XDG_CONFIG_HOME) to use.
261 const char *config_file;
264 * Configuration file name to use (if $XDG_CONFIG_HOME is not set).
266 const char *user_config_file;
272 * Return default project data used by 'libgnunetutil' for GNUnet.
274 const struct GNUNET_OS_ProjectData *
275 GNUNET_OS_project_data_default (void);
279 * @return current (actual) project data.
281 const struct GNUNET_OS_ProjectData *
282 GNUNET_OS_project_data_get (void);
286 * Setup OS subsystem with project data.
288 * @param pd project data used to determine paths.
291 GNUNET_OS_init (const struct GNUNET_OS_ProjectData *pd);
295 * Get the path to a specific GNUnet installation directory or, with
296 * #GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation
299 * @param dirkind what kind of directory is desired?
300 * @return a pointer to the dir path (to be freed by the caller)
303 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind);
307 * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon
308 * binary, try to prefix it with the libexec/-directory to get the
311 * @param progname name of the binary
312 * @return full path to the binary, if possible, otherwise copy of 'progname'
315 GNUNET_OS_get_libexec_binary_path (const char *progname);
319 * Callback function invoked for each interface found.
322 * @param name name of the interface (can be NULL for unknown)
323 * @param isDefault is this presumably the default interface
324 * @param addr address of this interface (can be NULL for unknown or unassigned)
325 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
326 * @param netmask the network mask (can be NULL for unknown or unassigned)
327 * @param addrlen length of the address
328 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
331 (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls,
334 const struct sockaddr *addr,
335 const struct sockaddr *broadcast_addr,
336 const struct sockaddr *netmask,
341 * @brief Enumerate all network interfaces
343 * @param proc the callback function
344 * @param proc_cls closure for @a proc
347 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
351 * @brief Get maximum string length returned by gethostname()
353 #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
354 #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
355 #elif defined(HOST_NAME_MAX)
356 #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
358 #define GNUNET_OS_get_hostname_max_length() 255
363 * Get process structure for current process
365 * The pointer it returns points to static memory location and must not be
368 * @return pointer to the process sturcutre for this process
370 struct GNUNET_OS_Process *
371 GNUNET_OS_process_current (void);
375 * Sends a signal to the process
377 * @param proc pointer to process structure
379 * @return 0 on success, -1 on error
382 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc,
387 * Cleans up process structure contents (OS-dependent) and deallocates it
389 * @param proc pointer to process structure
392 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc);
396 * Get the pid of the process in question
398 * @param proc the process to get the pid of
400 * @return the current process id
403 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc);
409 * @param pipe_control should a pipe be used to send signals to the child?
410 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
411 * @param pipe_stdin pipe to use to send input to child process (or NULL)
412 * @param pipe_stdout pipe to use to get output from child process (or NULL)
413 * @param pipe_stderr pipe to use to get error output from child process (or NULL)
414 * @param filename name of the binary
415 * @param argv NULL-terminated array of arguments to the process
416 * @return pointer to process structure of the new process, NULL on error
418 struct GNUNET_OS_Process *
419 GNUNET_OS_start_process_vap (int pipe_control,
420 enum GNUNET_OS_InheritStdioFlags std_inheritance,
421 struct GNUNET_DISK_PipeHandle *pipe_stdin,
422 struct GNUNET_DISK_PipeHandle *pipe_stdout,
423 struct GNUNET_DISK_PipeHandle *pipe_stderr,
424 const char *filename,
431 * @param pipe_control should a pipe be used to send signals to the child?
432 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
433 * @param pipe_stdin pipe to use to send input to child process (or NULL)
434 * @param pipe_stdout pipe to use to get output from child process (or NULL)
435 * @param pipe_stderr pipe to use to get error output from child process (or NULL)
436 * @param filename name of the binary
437 * @param ... NULL-terminated list of arguments to the process
438 * @return pointer to process structure of the new process, NULL on error
440 struct GNUNET_OS_Process *
441 GNUNET_OS_start_process (int pipe_control,
442 enum GNUNET_OS_InheritStdioFlags std_inheritance,
443 struct GNUNET_DISK_PipeHandle *pipe_stdin,
444 struct GNUNET_DISK_PipeHandle *pipe_stdout,
445 struct GNUNET_DISK_PipeHandle *pipe_stderr,
446 const char *filename, ...);
452 * @param pipe_control should a pipe be used to send signals to the child?
453 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
454 * @param pipe_stdin pipe to use to send input to child process (or NULL)
455 * @param pipe_stdout pipe to use to get output from child process (or NULL)
456 * @param pipe_stderr pipe to use to get error output from child process (or NULL)
457 * @param filename name of the binary
458 * @param va NULL-terminated list of arguments to the process
459 * @return pointer to process structure of the new process, NULL on error
461 struct GNUNET_OS_Process *
462 GNUNET_OS_start_process_va (int pipe_control,
463 enum GNUNET_OS_InheritStdioFlags std_inheritance,
464 struct GNUNET_DISK_PipeHandle *pipe_stdin,
465 struct GNUNET_DISK_PipeHandle *pipe_stdout,
466 struct GNUNET_DISK_PipeHandle *pipe_stderr,
467 const char *filename, va_list va);
472 * @param pipe_control should a pipe be used to send signals to the child?
473 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
474 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
475 * must be NULL on platforms where dup is not supported
476 * @param filename name of the binary
477 * @param argv NULL-terminated list of arguments to the process,
478 * including the process name as the first argument
479 * @return pointer to process structure of the new process, NULL on error
481 struct GNUNET_OS_Process *
482 GNUNET_OS_start_process_v (int pipe_control,
483 enum GNUNET_OS_InheritStdioFlags std_inheritance,
484 const SOCKTYPE *lsocks,
485 const char *filename,
490 * Start a process. This function is similar to the GNUNET_OS_start_process_*
491 * except that the filename and arguments can have whole strings which contain
492 * the arguments. These arguments are to be separated by spaces and are parsed
493 * in the order they appear. Arguments containing spaces can be used by
494 * quoting them with @em ".
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
498 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
499 * must be NULL on platforms where dup is not supported
500 * @param filename name of the binary. It is valid to have the arguments
501 * in this string when they are separated by spaces.
502 * @param ... more arguments. Should be of type `char *`. It is valid
503 * to have the arguments in these strings when they are separated by
504 * spaces. The last argument MUST be NULL.
505 * @return pointer to process structure of the new process, NULL on error
507 struct GNUNET_OS_Process *
508 GNUNET_OS_start_process_s (int pipe_control,
509 unsigned int std_inheritance,
510 const SOCKTYPE * lsocks,
511 const char *filename, ...);
515 * Handle to a command action.
517 struct GNUNET_OS_CommandHandle;
521 * Type of a function to process a line of output.
524 * @param line line of output from a command, NULL for the end
527 (*GNUNET_OS_LineProcessor) (void *cls, const char *line);
531 * Stop/kill a command.
533 * @param cmd handle to the process
536 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd);
540 * Run the given command line and call the given function
541 * for each line of the output.
543 * @param proc function to call for each line of the output
544 * @param proc_cls closure for proc
545 * @param timeout when to time out
546 * @param binary command to run
547 * @param ... arguments to command
548 * @return NULL on error
550 struct GNUNET_OS_CommandHandle *
551 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
553 struct GNUNET_TIME_Relative timeout,
559 * Retrieve the status of a process, waiting on it if dead.
560 * Nonblocking version.
562 * @param proc pointer to process structure
563 * @param type status type
564 * @param code return code/signal number
565 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
568 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
569 enum GNUNET_OS_ProcessStatusType *type,
570 unsigned long *code);
574 * Wait for a process to terminate. The return code is discarded.
575 * You must not use #GNUNET_OS_process_status() on the same process
576 * after calling this function! This function is blocking and should
577 * thus only be used if the child process is known to have terminated
578 * or to terminate very soon.
580 * @param proc pointer to process structure of the process to wait for
581 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
584 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc);
589 * Retrieve the status of a process, waiting on it if dead.
592 * @param proc pointer to process structure
593 * @param type status type
594 * @param code return code/signal number
595 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
598 GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc,
599 enum GNUNET_OS_ProcessStatusType *type,
600 unsigned long *code);
604 * Connects this process to its parent via pipe;
605 * essentially, the parent control handler will read signal numbers
606 * from the #GNUNET_OS_CONTROL_PIPE (as given in an environment
607 * variable) and raise those signals.
609 * @param cls closure (unused)
612 GNUNET_OS_install_parent_control_handler (void *cls);
616 * Check whether an executable exists and possibly
617 * if the suid bit is set on the file.
618 * Attempts to find the file using the current
619 * PATH environment variable as a search path.
621 * @param binary the name of the file to check.
622 * W32: must not have an .exe suffix.
623 * @param check_suid input true if the binary should be checked for SUID (*nix)
624 * W32: checks if the program has sufficient privileges by executing this
625 * binary with the -d flag. -d omits a programs main loop and only
626 * executes all privileged operations in an binary.
627 * @param params parameters used for w32 privilege checking (can be NULL for != w32, or when not checking for suid/permissions )
628 * @return #GNUNET_YES if the file is SUID (*nix) or can be executed with current privileges (W32),
629 * #GNUNET_NO if not SUID (but binary exists),
630 * #GNUNET_SYSERR on error (no such binary or not executable)
633 GNUNET_OS_check_helper_binary (const char *binary,
638 #if 0 /* keep Emacsens' auto-indent happy */
645 /* ifndef GNUNET_OS_LIB_H */
648 /** @} */ /* end of group */
650 /* end of gnunet_os_lib.h */