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