5785bd67fc3defbe1a467bed1b50f41e3966e5ca
[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  * Project-specific data used to help the OS subsystem
204  * find installation paths.
205  */
206 struct GNUNET_OS_ProjectData
207 {
208   /**
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.
212    */
213   const char *libname;
214
215   /**
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/".
220    */
221   const char *project_dirname;
222
223   /**
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.
227    */
228   const char *binary_name;
229
230   /**
231    * Name of an environment variable that can be used to override
232    * installation path detection, for example "GNUNET_PREFIX".
233    */
234   const char *env_varname;
235
236   /**
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".
240    */
241   const char *env_varname_alt;
242
243   /**
244    * E-mail address for reporting bugs.
245    */
246   const char *bug_email;
247
248   /**
249    * Project homepage.
250    */
251   const char *homepage;
252
253   /**
254    * Configuration file name (in $XDG_CONFIG_HOME) to use.
255    */
256   const char *config_file;
257
258 };
259
260
261 /**
262  * Return default project data used by 'libgnunetutil' for GNUnet.
263  */
264 const struct GNUNET_OS_ProjectData *
265 GNUNET_OS_project_data_default (void);
266
267
268 /**
269  * @return current (actual) project data.
270  */
271 const struct GNUNET_OS_ProjectData *
272 GNUNET_OS_project_data_get (void);
273
274
275 /**
276  * Setup OS subsystem with project data.
277  *
278  * @param pd project data used to determine paths.
279  */
280 void
281 GNUNET_OS_init (const struct GNUNET_OS_ProjectData *pd);
282
283
284 /**
285  * Get the path to a specific GNUnet installation directory or, with
286  * #GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation
287  * directory.
288  *
289  * @param dirkind what kind of directory is desired?
290  * @return a pointer to the dir path (to be freed by the caller)
291  */
292 char *
293 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind);
294
295
296 /**
297  * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon
298  * binary, try to prefix it with the libexec/-directory to get the
299  * full path.
300  *
301  * @param progname name of the binary
302  * @return full path to the binary, if possible, otherwise copy of 'progname'
303  */
304 char *
305 GNUNET_OS_get_libexec_binary_path (const char *progname);
306
307
308 /**
309  * Callback function invoked for each interface found.
310  *
311  * @param cls closure
312  * @param name name of the interface (can be NULL for unknown)
313  * @param isDefault is this presumably the default interface
314  * @param addr address of this interface (can be NULL for unknown or unassigned)
315  * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
316  * @param netmask the network mask (can be NULL for unknown or unassigned)
317  * @param addrlen length of the address
318  * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
319  */
320 typedef int
321 (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls,
322                                         const char *name,
323                                         int isDefault,
324                                         const struct sockaddr *addr,
325                                         const struct sockaddr *broadcast_addr,
326                                         const struct sockaddr *netmask,
327                                         socklen_t addrlen);
328
329
330 /**
331  * @brief Enumerate all network interfaces
332  *
333  * @param proc the callback function
334  * @param proc_cls closure for @a proc
335  */
336 void
337 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
338                                    void *proc_cls);
339
340 /**
341  * @brief Get maximum string length returned by gethostname()
342  */
343 #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
344 #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
345 #elif defined(HOST_NAME_MAX)
346 #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
347 #else
348 #define GNUNET_OS_get_hostname_max_length() 255
349 #endif
350
351
352 /**
353  * Get process structure for current process
354  *
355  * The pointer it returns points to static memory location and must not be
356  * deallocated/closed
357  *
358  * @return pointer to the process sturcutre for this process
359  */
360 struct GNUNET_OS_Process *
361 GNUNET_OS_process_current (void);
362
363
364 /**
365  * Sends a signal to the process
366  *
367  * @param proc pointer to process structure
368  * @param sig signal
369  * @return 0 on success, -1 on error
370  */
371 int
372 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig);
373
374
375 /**
376  * Cleans up process structure contents (OS-dependent) and deallocates it
377  *
378  * @param proc pointer to process structure
379  */
380 void
381 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc);
382
383
384 /**
385  * Get the pid of the process in question
386  *
387  * @param proc the process to get the pid of
388  *
389  * @return the current process id
390  */
391 pid_t
392 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc);
393
394
395 /**
396  * Start a process.
397  *
398  * @param pipe_control should a pipe be used to send signals to the child?
399  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
400  * @param pipe_stdin pipe to use to send input to child process (or NULL)
401  * @param pipe_stdout pipe to use to get output from child process (or NULL)
402  * @param pipe_stderr pipe to use to get error output from child process (or NULL)
403  * @param filename name of the binary
404  * @param argv NULL-terminated array of arguments to the process
405  * @return pointer to process structure of the new process, NULL on error
406  */
407 struct GNUNET_OS_Process *
408 GNUNET_OS_start_process_vap (int pipe_control,
409                              enum GNUNET_OS_InheritStdioFlags std_inheritance,
410                              struct GNUNET_DISK_PipeHandle *pipe_stdin,
411                              struct GNUNET_DISK_PipeHandle *pipe_stdout,
412                              struct GNUNET_DISK_PipeHandle *pipe_stderr,
413                              const char *filename,
414                              char *const argv[]);
415
416
417 /**
418  * Start a process.
419  *
420  * @param pipe_control should a pipe be used to send signals to the child?
421  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
422  * @param pipe_stdin pipe to use to send input to child process (or NULL)
423  * @param pipe_stdout pipe to use to get output from child process (or NULL)
424  * @param pipe_stderr pipe to use to get error output from child process (or NULL)
425  * @param filename name of the binary
426  * @param ... NULL-terminated list of arguments to the process
427  * @return pointer to process structure of the new process, NULL on error
428  */
429 struct GNUNET_OS_Process *
430 GNUNET_OS_start_process (int pipe_control,
431                          enum GNUNET_OS_InheritStdioFlags std_inheritance,
432                          struct GNUNET_DISK_PipeHandle *pipe_stdin,
433                          struct GNUNET_DISK_PipeHandle *pipe_stdout,
434                          struct GNUNET_DISK_PipeHandle *pipe_stderr,
435                          const char *filename, ...);
436
437
438 /**
439  * Start a process.
440  *
441  * @param pipe_control should a pipe be used to send signals to the child?
442  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
443  * @param pipe_stdin pipe to use to send input to child process (or NULL)
444  * @param pipe_stdout pipe to use to get output from child process (or NULL)
445  * @param pipe_stderr pipe to use to get error output from child process (or NULL)
446  * @param filename name of the binary
447  * @param va NULL-terminated list of arguments to the process
448  * @return pointer to process structure of the new process, NULL on error
449  */
450 struct GNUNET_OS_Process *
451 GNUNET_OS_start_process_va (int pipe_control,
452                             enum GNUNET_OS_InheritStdioFlags std_inheritance,
453                             struct GNUNET_DISK_PipeHandle *pipe_stdin,
454                             struct GNUNET_DISK_PipeHandle *pipe_stdout,
455                             struct GNUNET_DISK_PipeHandle *pipe_stderr,
456                             const char *filename, va_list va);
457
458 /**
459  * Start a process.
460  *
461  * @param pipe_control should a pipe be used to send signals to the child?
462  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
463  * @param lsocks array of listen sockets to dup systemd-style (or NULL);
464  *         must be NULL on platforms where dup is not supported
465  * @param filename name of the binary
466  * @param argv NULL-terminated list of arguments to the process,
467  *             including the process name as the first argument
468  * @return pointer to process structure of the new process, NULL on error
469  */
470 struct GNUNET_OS_Process *
471 GNUNET_OS_start_process_v (int pipe_control,
472                            enum GNUNET_OS_InheritStdioFlags std_inheritance,
473                            const SOCKTYPE *lsocks,
474                            const char *filename,
475                            char *const argv[]);
476
477
478 /**
479  * Start a process.  This function is similar to the GNUNET_OS_start_process_*
480  * except that the filename and arguments can have whole strings which contain
481  * the arguments.  These arguments are to be separated by spaces and are parsed
482  * in the order they appear.  Arguments containing spaces can be used by
483  * quoting them with @em ".
484  *
485  * @param pipe_control should a pipe be used to send signals to the child?
486  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
487  * @param lsocks array of listen sockets to dup systemd-style (or NULL);
488  *         must be NULL on platforms where dup is not supported
489  * @param filename name of the binary.  It is valid to have the arguments
490  *         in this string when they are separated by spaces.
491  * @param ... more arguments.  Should be of type `char *`.  It is valid
492  *         to have the arguments in these strings when they are separated by
493  *         spaces.  The last argument MUST be NULL.
494  * @return pointer to process structure of the new process, NULL on error
495  */
496 struct GNUNET_OS_Process *
497 GNUNET_OS_start_process_s (int pipe_control,
498                            unsigned int std_inheritance,
499                            const SOCKTYPE * lsocks,
500                            const char *filename, ...);
501
502
503 /**
504  * Handle to a command action.
505  */
506 struct GNUNET_OS_CommandHandle;
507
508
509 /**
510  * Type of a function to process a line of output.
511  *
512  * @param cls closure
513  * @param line line of output from a command, NULL for the end
514  */
515 typedef void (*GNUNET_OS_LineProcessor) (void *cls, const char *line);
516
517
518 /**
519  * Stop/kill a command.
520  *
521  * @param cmd handle to the process
522  */
523 void
524 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd);
525
526
527 /**
528  * Run the given command line and call the given function
529  * for each line of the output.
530  *
531  * @param proc function to call for each line of the output
532  * @param proc_cls closure for proc
533  * @param timeout when to time out
534  * @param binary command to run
535  * @param ... arguments to command
536  * @return NULL on error
537  */
538 struct GNUNET_OS_CommandHandle *
539 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
540                        struct GNUNET_TIME_Relative timeout, const char *binary,
541                        ...);
542
543
544 /**
545  * Retrieve the status of a process, waiting on him if dead.
546  * Nonblocking version.
547  *
548  * @param proc pointer to process structure
549  * @param type status type
550  * @param code return code/signal number
551  * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
552  */
553 int
554 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
555                           enum GNUNET_OS_ProcessStatusType *type,
556                           unsigned long *code);
557
558
559 /**
560  * Wait for a process to terminate.  The return code is discarded.
561  * You must not use #GNUNET_OS_process_status() on the same process
562  * after calling this function!  This function is blocking and should
563  * thus only be used if the child process is known to have terminated
564  * or to terminate very soon.
565  *
566  * @param proc pointer to process structure of the process to wait for
567  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
568  */
569 int
570 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc);
571
572
573 /**
574  * Connects this process to its parent via pipe;
575  * essentially, the parent control handler will read signal numbers
576  * from the #GNUNET_OS_CONTROL_PIPE (as given in an environment
577  * variable) and raise those signals.
578  *
579  * @param cls closure (unused)
580  */
581 void
582 GNUNET_OS_install_parent_control_handler (void *cls);
583
584
585 /**
586  * Check whether an executable exists and possibly
587  * if the suid bit is set on the file.
588  * Attempts to find the file using the current
589  * PATH environment variable as a search path.
590  *
591  * @param binary the name of the file to check.
592  *        W32: must not have an .exe suffix.
593  * @param check_suid input true if the binary should be checked for SUID (*nix)
594  *        W32: checks if the program has sufficient privileges by executing this
595  *             binary with the -d flag. -d omits a programs main loop and only
596  *             executes all privileged operations in an binary.
597  * @param params parameters used for w32 privilege checking (can be NULL for != w32, or when not checking for suid/permissions )
598  * @return #GNUNET_YES if the file is SUID (*nix) or can be executed with current privileges (W32),
599  *         #GNUNET_NO if not SUID (but binary exists),
600  *         #GNUNET_SYSERR on error (no such binary or not executable)
601  */
602 int
603 GNUNET_OS_check_helper_binary (const char *binary,
604                                int check_suid,
605                                const char *params);
606
607
608 #if 0                           /* keep Emacsens' auto-indent happy */
609 {
610 #endif
611 #ifdef __cplusplus
612 }
613 #endif
614
615 /* ifndef GNUNET_OS_LIB_H */
616 #endif
617
618 /** @} */  /* end of group */
619
620 /* end of gnunet_os_lib.h */