2 This file is part of GNUnet.
3 (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 2, 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/).
159 * Process status types
161 enum GNUNET_OS_ProcessStatusType
164 * The process is not known to the OS (or at
165 * least not one of our children).
167 GNUNET_OS_PROCESS_UNKNOWN,
170 * The process is still running.
172 GNUNET_OS_PROCESS_RUNNING,
175 * The process is paused (but could be resumed).
177 GNUNET_OS_PROCESS_STOPPED,
180 * The process exited with a return code.
182 GNUNET_OS_PROCESS_EXITED,
185 * The process was killed by a signal.
187 GNUNET_OS_PROCESS_SIGNALED
192 * Get the path to a specific GNUnet installation directory or, with
193 * GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation
196 * @param dirkind what kind of directory is desired?
197 * @return a pointer to the dir path (to be freed by the caller)
200 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind);
204 * Callback function invoked for each interface found.
207 * @param name name of the interface (can be NULL for unknown)
208 * @param isDefault is this presumably the default interface
209 * @param addr address of this interface (can be NULL for unknown or unassigned)
210 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
211 * @param netmask the network mask (can be NULL for unknown or unassigned))
212 * @param addrlen length of the address
213 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
215 typedef int (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls, const char *name,
217 const struct sockaddr *
219 const struct sockaddr *
221 const struct sockaddr *
222 netmask, socklen_t addrlen);
226 * @brief Enumerate all network interfaces
227 * @param proc the callback function
228 * @param proc_cls closure for proc
231 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
235 * @brief Get maximum string length returned by gethostname()
237 #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
238 #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
239 #elif defined(HOST_NAME_MAX)
240 #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
242 #define GNUNET_OS_get_hostname_max_length() 255
247 * Get process structure for current process
249 * The pointer it returns points to static memory location and must not be
252 * @return pointer to the process sturcutre for this process
254 struct GNUNET_OS_Process *
255 GNUNET_OS_process_current (void);
259 * Sends a signal to the process
261 * @param proc pointer to process structure
263 * @return 0 on success, -1 on error
266 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig);
270 * Cleans up process structure contents (OS-dependent) and deallocates it
272 * @param proc pointer to process structure
275 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc);
279 * Get the pid of the process in question
281 * @param proc the process to get the pid of
283 * @return the current process id
286 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc);
290 * Set process priority
292 * @param proc pointer to process structure
293 * @param prio priority value
294 * @return GNUNET_OK on success, GNUNET_SYSERR on error
297 GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
298 enum GNUNET_SCHEDULER_Priority prio);
304 * @param pipe_control should a pipe be used to send signals to the child?
305 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
306 * @param pipe_stdin pipe to use to send input to child process (or NULL)
307 * @param pipe_stdout pipe to use to get output from child process (or NULL)
308 * @param filename name of the binary
309 * @param argv NULL-terminated array of arguments to the process
310 * @return pointer to process structure of the new process, NULL on error
312 struct GNUNET_OS_Process *
313 GNUNET_OS_start_process_vap (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 const char *filename,
324 * @param pipe_control should a pipe be used to send signals to the child?
325 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
326 * @param pipe_stdin pipe to use to send input to child process (or NULL)
327 * @param pipe_stdout pipe to use to get output from child process (or NULL)
328 * @param filename name of the binary
329 * @param ... NULL-terminated list of arguments to the process
330 * @return pointer to process structure of the new process, NULL on error
332 struct GNUNET_OS_Process *
333 GNUNET_OS_start_process (int pipe_control,
334 enum GNUNET_OS_InheritStdioFlags std_inheritance,
335 struct GNUNET_DISK_PipeHandle *pipe_stdin,
336 struct GNUNET_DISK_PipeHandle *pipe_stdout,
337 const char *filename, ...);
343 * @param pipe_control should a pipe be used to send signals to the child?
344 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
345 * @param pipe_stdin pipe to use to send input to child process (or NULL)
346 * @param pipe_stdout pipe to use to get output from child process (or NULL)
347 * @param filename name of the binary
348 * @param va NULL-terminated list of arguments to the process
349 * @return pointer to process structure of the new process, NULL on error
351 struct GNUNET_OS_Process *
352 GNUNET_OS_start_process_va (int pipe_control,
353 enum GNUNET_OS_InheritStdioFlags std_inheritance,
354 struct GNUNET_DISK_PipeHandle *pipe_stdin,
355 struct GNUNET_DISK_PipeHandle *pipe_stdout,
356 const char *filename, va_list va);
361 * @param pipe_control should a pipe be used to send signals to the child?
362 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
363 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
364 * must be NULL on platforms where dup is not supported
365 * @param filename name of the binary
366 * @param argv NULL-terminated list of arguments to the process,
367 * including the process name as the first argument
368 * @return pointer to process structure of the new process, NULL on error
370 struct GNUNET_OS_Process *
371 GNUNET_OS_start_process_v (int pipe_control,
372 enum GNUNET_OS_InheritStdioFlags std_inheritance,
373 const SOCKTYPE *lsocks,
374 const char *filename,
379 * Handle to a command action.
381 struct GNUNET_OS_CommandHandle;
385 * Type of a function to process a line of output.
388 * @param line line of output from a command, NULL for the end
390 typedef void (*GNUNET_OS_LineProcessor) (void *cls, const char *line);
394 * Stop/kill a command.
396 * @param cmd handle to the process
399 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd);
403 * Run the given command line and call the given function
404 * for each line of the output.
406 * @param proc function to call for each line of the output
407 * @param proc_cls closure for proc
408 * @param timeout when to time out
409 * @param binary command to run
410 * @param ... arguments to command
411 * @return NULL on error
413 struct GNUNET_OS_CommandHandle *
414 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
415 struct GNUNET_TIME_Relative timeout, const char *binary,
420 * Retrieve the status of a process, waiting on him if dead.
421 * Nonblocking version.
423 * @param proc pointer to process structure
424 * @param type status type
425 * @param code return code/signal number
426 * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
429 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
430 enum GNUNET_OS_ProcessStatusType *type,
431 unsigned long *code);
435 * Wait for a process to terminate. The return code is discarded.
436 * You must not use 'GNUNET_OS_process_status' on the same process
437 * after calling this function! This function is blocking and should
438 * thus only be used if the child process is known to have terminated
439 * or to terminate very soon.
441 * @param proc pointer to process structure of the process to wait for
442 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
445 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc);
449 * Connects this process to its parent via pipe;
450 * essentially, the parent control handler will read signal numbers
451 * from the 'GNUNET_OS_CONTROL_PIPE' (as given in an environment
452 * variable) and raise those signals.
454 * @param cls closure (unused)
455 * @param tc scheduler context (unused)
458 GNUNET_OS_install_parent_control_handler (void *cls,
460 GNUNET_SCHEDULER_TaskContext *tc);
464 * Check whether an executable exists and possibly
465 * if the suid bit is set on the file.
466 * Attempts to find the file using the current
467 * PATH environment variable as a search path.
469 * @param binary the name of the file to check
470 * @return GNUNET_YES if the file is SUID,
471 * GNUNET_NO if not SUID (but binary exists)
472 * GNUNET_SYSERR on error (no such binary or not executable)
475 GNUNET_OS_check_helper_binary (const char *binary);
478 #if 0 /* keep Emacsens' auto-indent happy */
486 /* ifndef GNUNET_OS_LIB_H */
488 /* end of gnunet_os_lib.h */