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