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