2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_os_lib.h
23 * @brief low level process routines
24 * @author Christian Grothoff
25 * @author Krista Bennett
26 * @author Gerd Knorr <kraxel@bytesex.org>
27 * @author Ioana Patrascu
28 * @author Tzvetan Horozov
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.
42 #ifndef GNUNET_OS_LIB_H
43 #define GNUNET_OS_LIB_H
48 #if 0 /* keep Emacsens' auto-indent happy */
53 #include "gnunet_common.h"
54 #include "gnunet_configuration_lib.h"
55 #include "gnunet_scheduler_lib.h"
59 * Flags that determine which of the standard streams
60 * should be inherited by the child process.
62 enum GNUNET_OS_InheritStdioFlags
66 * No standard streams should be inherited.
68 GNUNET_OS_INHERIT_STD_NONE = 0,
71 * When this flag is set, the child process will
72 * inherit stdin of the parent.
74 GNUNET_OS_INHERIT_STD_IN = 1,
77 * When this flag is set, the child process will
78 * inherit stdout of the parent.
80 GNUNET_OS_INHERIT_STD_OUT = 2,
83 * When this flag is set, the child process will
84 * inherit stderr of the parent.
86 GNUNET_OS_INHERIT_STD_ERR = 4,
89 * When these flags are set, the child process will
90 * inherit stdout and stderr of the parent.
92 GNUNET_OS_INHERIT_STD_OUT_AND_ERR = 6,
95 * Use this option to have all of the standard streams
96 * (stdin, stdout and stderror) be inherited.
98 GNUNET_OS_INHERIT_STD_ALL = 7
103 * Process information (OS-dependent)
105 struct GNUNET_OS_Process;
109 * Possible installation paths to request
111 enum GNUNET_OS_InstallationPathKind
114 * Return the "PREFIX" directory given to configure.
116 GNUNET_OS_IPK_PREFIX,
119 * Return the directory where the program binaries are installed. (bin/)
121 GNUNET_OS_IPK_BINDIR,
124 * Return the directory where libraries are installed. (lib/gnunet/)
126 GNUNET_OS_IPK_LIBDIR,
129 * Return the directory where data is installed (share/gnunet/)
131 GNUNET_OS_IPK_DATADIR,
134 * Return the directory where translations are installed (share/locale/)
136 GNUNET_OS_IPK_LOCALEDIR,
139 * Return the installation directory of this application, not
140 * the one of the overall GNUnet installation (in case they
143 GNUNET_OS_IPK_SELF_PREFIX,
146 * Return the prefix of the path with application icons (share/icons/).
148 GNUNET_OS_IPK_ICONDIR,
151 * Return the prefix of the path with documentation files, including the
152 * license (share/doc/gnunet/).
154 GNUNET_OS_IPK_DOCDIR,
157 * Return the directory where helper binaries are installed (lib/gnunet/libexec/)
159 GNUNET_OS_IPK_LIBEXECDIR
164 * Process status types
166 enum GNUNET_OS_ProcessStatusType
169 * The process is not known to the OS (or at
170 * least not one of our children).
172 GNUNET_OS_PROCESS_UNKNOWN,
175 * The process is still running.
177 GNUNET_OS_PROCESS_RUNNING,
180 * The process is paused (but could be resumed).
182 GNUNET_OS_PROCESS_STOPPED,
185 * The process exited with a return code.
187 GNUNET_OS_PROCESS_EXITED,
190 * The process was killed by a signal.
192 GNUNET_OS_PROCESS_SIGNALED
197 * Get the path to a specific GNUnet installation directory or, with
198 * #GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation
201 * @param dirkind what kind of directory is desired?
202 * @return a pointer to the dir path (to be freed by the caller)
205 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind);
209 * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon
210 * binary, try to prefix it with the libexec/-directory to get the
213 * @param progname name of the binary
214 * @return full path to the binary, if possible, otherwise copy of 'progname'
217 GNUNET_OS_get_libexec_binary_path (const char *progname);
221 * Callback function invoked for each interface found.
224 * @param name name of the interface (can be NULL for unknown)
225 * @param isDefault is this presumably the default interface
226 * @param addr address of this interface (can be NULL for unknown or unassigned)
227 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
228 * @param netmask the network mask (can be NULL for unknown or unassigned)
229 * @param addrlen length of the address
230 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
233 (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls,
236 const struct sockaddr *addr,
237 const struct sockaddr *broadcast_addr,
238 const struct sockaddr *netmask,
243 * @brief Enumerate all network interfaces
245 * @param proc the callback function
246 * @param proc_cls closure for @a proc
249 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
253 * @brief Get maximum string length returned by gethostname()
255 #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
256 #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
257 #elif defined(HOST_NAME_MAX)
258 #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
260 #define GNUNET_OS_get_hostname_max_length() 255
265 * Get process structure for current process
267 * The pointer it returns points to static memory location and must not be
270 * @return pointer to the process sturcutre for this process
272 struct GNUNET_OS_Process *
273 GNUNET_OS_process_current (void);
277 * Sends a signal to the process
279 * @param proc pointer to process structure
281 * @return 0 on success, -1 on error
284 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig);
288 * Cleans up process structure contents (OS-dependent) and deallocates it
290 * @param proc pointer to process structure
293 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc);
297 * Get the pid of the process in question
299 * @param proc the process to get the pid of
301 * @return the current process id
304 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc);
310 * @param pipe_control should a pipe be used to send signals to the child?
311 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
312 * @param pipe_stdin pipe to use to send input to child process (or NULL)
313 * @param pipe_stdout pipe to use to get output from child process (or NULL)
314 * @param pipe_stderr pipe to use to get error output from child process (or NULL)
315 * @param filename name of the binary
316 * @param argv NULL-terminated array of arguments to the process
317 * @return pointer to process structure of the new process, NULL on error
319 struct GNUNET_OS_Process *
320 GNUNET_OS_start_process_vap (int pipe_control,
321 enum GNUNET_OS_InheritStdioFlags std_inheritance,
322 struct GNUNET_DISK_PipeHandle *pipe_stdin,
323 struct GNUNET_DISK_PipeHandle *pipe_stdout,
324 struct GNUNET_DISK_PipeHandle *pipe_stderr,
325 const char *filename,
332 * @param pipe_control should a pipe be used to send signals to the child?
333 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
334 * @param pipe_stdin pipe to use to send input to child process (or NULL)
335 * @param pipe_stdout pipe to use to get output from child process (or NULL)
336 * @param pipe_stderr pipe to use to get error output from child process (or NULL)
337 * @param filename name of the binary
338 * @param ... NULL-terminated list of arguments to the process
339 * @return pointer to process structure of the new process, NULL on error
341 struct GNUNET_OS_Process *
342 GNUNET_OS_start_process (int pipe_control,
343 enum GNUNET_OS_InheritStdioFlags std_inheritance,
344 struct GNUNET_DISK_PipeHandle *pipe_stdin,
345 struct GNUNET_DISK_PipeHandle *pipe_stdout,
346 struct GNUNET_DISK_PipeHandle *pipe_stderr,
347 const char *filename, ...);
353 * @param pipe_control should a pipe be used to send signals to the child?
354 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
355 * @param pipe_stdin pipe to use to send input to child process (or NULL)
356 * @param pipe_stdout pipe to use to get output from child process (or NULL)
357 * @param pipe_stderr pipe to use to get error output from child process (or NULL)
358 * @param filename name of the binary
359 * @param va NULL-terminated list of arguments to the process
360 * @return pointer to process structure of the new process, NULL on error
362 struct GNUNET_OS_Process *
363 GNUNET_OS_start_process_va (int pipe_control,
364 enum GNUNET_OS_InheritStdioFlags std_inheritance,
365 struct GNUNET_DISK_PipeHandle *pipe_stdin,
366 struct GNUNET_DISK_PipeHandle *pipe_stdout,
367 struct GNUNET_DISK_PipeHandle *pipe_stderr,
368 const char *filename, va_list va);
373 * @param pipe_control should a pipe be used to send signals to the child?
374 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
375 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
376 * must be NULL on platforms where dup is not supported
377 * @param filename name of the binary
378 * @param argv NULL-terminated list of arguments to the process,
379 * including the process name as the first argument
380 * @return pointer to process structure of the new process, NULL on error
382 struct GNUNET_OS_Process *
383 GNUNET_OS_start_process_v (int pipe_control,
384 enum GNUNET_OS_InheritStdioFlags std_inheritance,
385 const SOCKTYPE *lsocks,
386 const char *filename,
391 * Start a process. This function is similar to the GNUNET_OS_start_process_*
392 * except that the filename and arguments can have whole strings which contain
393 * the arguments. These arguments are to be separated by spaces and are parsed
394 * in the order they appear. Arguments containing spaces can be used by
395 * quoting them with @em ".
397 * @param pipe_control should a pipe be used to send signals to the child?
398 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
399 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
400 * must be NULL on platforms where dup is not supported
401 * @param filename name of the binary. It is valid to have the arguments
402 * in this string when they are separated by spaces.
403 * @param ... more arguments. Should be of type `char *`. It is valid
404 * to have the arguments in these strings when they are separated by
405 * spaces. The last argument MUST be NULL.
406 * @return pointer to process structure of the new process, NULL on error
408 struct GNUNET_OS_Process *
409 GNUNET_OS_start_process_s (int pipe_control,
410 unsigned int std_inheritance,
411 const SOCKTYPE * lsocks,
412 const char *filename, ...);
416 * Handle to a command action.
418 struct GNUNET_OS_CommandHandle;
422 * Type of a function to process a line of output.
425 * @param line line of output from a command, NULL for the end
427 typedef void (*GNUNET_OS_LineProcessor) (void *cls, const char *line);
431 * Stop/kill a command.
433 * @param cmd handle to the process
436 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd);
440 * Run the given command line and call the given function
441 * for each line of the output.
443 * @param proc function to call for each line of the output
444 * @param proc_cls closure for proc
445 * @param timeout when to time out
446 * @param binary command to run
447 * @param ... arguments to command
448 * @return NULL on error
450 struct GNUNET_OS_CommandHandle *
451 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
452 struct GNUNET_TIME_Relative timeout, const char *binary,
457 * Retrieve the status of a process, waiting on him if dead.
458 * Nonblocking version.
460 * @param proc pointer to process structure
461 * @param type status type
462 * @param code return code/signal number
463 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
466 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
467 enum GNUNET_OS_ProcessStatusType *type,
468 unsigned long *code);
472 * Wait for a process to terminate. The return code is discarded.
473 * You must not use #GNUNET_OS_process_status() on the same process
474 * after calling this function! This function is blocking and should
475 * thus only be used if the child process is known to have terminated
476 * or to terminate very soon.
478 * @param proc pointer to process structure of the process to wait for
479 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
482 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc);
486 * Connects this process to its parent via pipe;
487 * essentially, the parent control handler will read signal numbers
488 * from the #GNUNET_OS_CONTROL_PIPE (as given in an environment
489 * variable) and raise those signals.
491 * @param cls closure (unused)
492 * @param tc scheduler context (unused)
495 GNUNET_OS_install_parent_control_handler (void *cls,
497 GNUNET_SCHEDULER_TaskContext *tc);
501 * Check whether an executable exists and possibly
502 * if the suid bit is set on the file.
503 * Attempts to find the file using the current
504 * PATH environment variable as a search path.
506 * @param binary the name of the file to check.
507 * W32: must not have an .exe suffix.
508 * @param check_suid input true if the binary should be checked for SUID (*nix)
509 * W32: checks if the program has sufficient privileges by executing this
510 * binary with the -d flag. -d omits a programs main loop and only
511 * executes all privileged operations in an binary.
512 * @param params parameters used for w32 privilege checking (can be NULL for != w32, or when not checking for suid/permissions )
513 * @return #GNUNET_YES if the file is SUID (*nix) or can be executed with current privileges (W32),
514 * #GNUNET_NO if not SUID (but binary exists),
515 * #GNUNET_SYSERR on error (no such binary or not executable)
518 GNUNET_OS_check_helper_binary (const char *binary,
523 #if 0 /* keep Emacsens' auto-indent happy */
531 /* ifndef GNUNET_OS_LIB_H */
533 /* end of gnunet_os_lib.h */