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