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