-fixes
[oweals/gnunet.git] / src / include / gnunet_os_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2011 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_os_lib.h
23  * @brief low level process routines
24  * @author Christian Grothoff
25  * @author Krista Bennett
26  * @author Gerd Knorr <kraxel@bytesex.org>
27  * @author Ioana Patrascu
28  * @author Tzvetan Horozov
29  * @author Milan
30  *
31  * This code manages child processes.  We can communicate with child
32  * processes using signals.  Because signals are not supported on W32
33  * and Java (at least not nicely), we can alternatively use a pipe
34  * to send signals to the child processes (if the child process is
35  * a full-blown GNUnet process that supports reading signals from 
36  * a pipe, of course).  Naturally, this also only works for 'normal'
37  * termination via signals, and not as a replacement for SIGKILL.
38  * Thus using pipes to communicate signals should only be enabled if
39  * the child is a Java process OR if we are on Windoze.
40  */
41
42 #ifndef GNUNET_OS_LIB_H
43 #define GNUNET_OS_LIB_H
44
45 #ifdef __cplusplus
46 extern "C"
47 {
48 #if 0                           /* keep Emacsens' auto-indent happy */
49 }
50 #endif
51 #endif
52
53 #include "gnunet_common.h"
54 #include "gnunet_configuration_lib.h"
55 #include "gnunet_scheduler_lib.h"
56
57
58 /**
59  * Flags that determine which of the standard streams
60  * should be inherited by the child process.
61  */
62 enum GNUNET_OS_InheritStdioFlags
63 {
64
65   /**
66    * No standard streams should be inherited.
67    */
68   GNUNET_OS_INHERIT_STD_NONE = 0,
69
70   /**
71    * When this flag is set, the child process will
72    * inherit stdin of the parent.
73    */
74   GNUNET_OS_INHERIT_STD_IN = 1,
75   
76   /**
77    * When this flag is set, the child process will
78    * inherit stdout of the parent.
79    */
80   GNUNET_OS_INHERIT_STD_OUT = 2,
81
82   /**
83    * When this flag is set, the child process will
84    * inherit stderr of the parent.
85    */
86   GNUNET_OS_INHERIT_STD_ERR = 4,
87
88   /**
89    * When these flags are set, the child process will
90    * inherit stdout and stderr of the parent.
91    */
92   GNUNET_OS_INHERIT_STD_OUT_AND_ERR = 6,
93
94   /**
95    * Use this option to have all of the standard streams
96    * (stdin, stdout and stderror) be inherited.
97    */
98   GNUNET_OS_INHERIT_STD_ALL = 7
99 };
100
101
102 /**
103  * Process information (OS-dependent)
104  */
105 struct GNUNET_OS_Process;
106
107
108 /**
109  * Possible installation paths to request
110  */
111 enum GNUNET_OS_InstallationPathKind
112 {
113   /**
114    * Return the "PREFIX" directory given to configure.
115    */
116   GNUNET_OS_IPK_PREFIX,
117
118   /**
119    * Return the directory where the program binaries are installed. (bin/)
120    */
121   GNUNET_OS_IPK_BINDIR,
122
123   /**
124    * Return the directory where libraries are installed. (lib/gnunet/)
125    */
126   GNUNET_OS_IPK_LIBDIR,
127
128   /**
129    * Return the directory where data is installed (share/gnunet/)
130    */
131   GNUNET_OS_IPK_DATADIR,
132
133   /**
134    * Return the directory where translations are installed (share/locale/)
135    */
136   GNUNET_OS_IPK_LOCALEDIR,
137
138   /**
139    * Return the installation directory of this application, not
140    * the one of the overall GNUnet installation (in case they
141    * are different).
142    */
143   GNUNET_OS_IPK_SELF_PREFIX,
144
145   /**
146    * Return the prefix of the path with application icons (share/icons/).
147    */
148   GNUNET_OS_IPK_ICONDIR,
149
150   /**
151    * Return the prefix of the path with documentation files, including the
152    * license (share/doc/gnunet/).
153    */
154   GNUNET_OS_IPK_DOCDIR,
155
156   /**
157    * Return the directory where helper binaries are installed (lib/gnunet/libexec/)
158    */
159   GNUNET_OS_IPK_LIBEXECDIR
160 };
161
162
163 /**
164  * Process status types
165  */
166 enum GNUNET_OS_ProcessStatusType
167 {
168   /**
169    * The process is not known to the OS (or at
170    * least not one of our children).
171    */
172   GNUNET_OS_PROCESS_UNKNOWN,
173
174   /**
175    * The process is still running.
176    */
177   GNUNET_OS_PROCESS_RUNNING,
178
179   /**
180    * The process is paused (but could be resumed).
181    */
182   GNUNET_OS_PROCESS_STOPPED,
183
184   /**
185    * The process exited with a return code.
186    */
187   GNUNET_OS_PROCESS_EXITED,
188
189   /**
190    * The process was killed by a signal.
191    */
192   GNUNET_OS_PROCESS_SIGNALED
193 };
194
195
196 /**
197  * Get the path to a specific GNUnet installation directory or, with
198  * GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation
199  * directory.
200  *
201  * @param dirkind what kind of directory is desired?
202  * @return a pointer to the dir path (to be freed by the caller)
203  */
204 char *
205 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind);
206
207
208 /**
209  * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon
210  * binary, try to prefix it with the libexec/-directory to get the
211  * full path.
212  *
213  * @param progname name of the binary
214  * @return full path to the binary, if possible, otherwise copy of 'progname'
215  */
216 char *
217 GNUNET_OS_get_libexec_binary_path (const char *progname);
218
219
220 /**
221  * Callback function invoked for each interface found.
222  *
223  * @param cls closure
224  * @param name name of the interface (can be NULL for unknown)
225  * @param isDefault is this presumably the default interface
226  * @param addr address of this interface (can be NULL for unknown or unassigned)
227  * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
228  * @param netmask the network mask (can be NULL for unknown or unassigned))
229  * @param addrlen length of the address
230  * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
231  */
232 typedef int (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls, const char *name,
233                                                     int isDefault,
234                                                     const struct sockaddr *
235                                                     addr,
236                                                     const struct sockaddr *
237                                                     broadcast_addr,
238                                                     const struct sockaddr *
239                                                     netmask, socklen_t addrlen);
240
241
242 /**
243  * @brief Enumerate all network interfaces
244  * @param proc the callback function
245  * @param proc_cls closure for proc
246  */
247 void
248 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
249                                    void *proc_cls);
250
251 /**
252  * @brief Get maximum string length returned by gethostname()
253  */
254 #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
255 #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
256 #elif defined(HOST_NAME_MAX)
257 #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
258 #else
259 #define GNUNET_OS_get_hostname_max_length() 255
260 #endif
261
262
263 /**
264  * Get process structure for current process
265  *
266  * The pointer it returns points to static memory location and must not be
267  * deallocated/closed
268  *
269  * @return pointer to the process sturcutre for this process
270  */
271 struct GNUNET_OS_Process *
272 GNUNET_OS_process_current (void);
273
274
275 /**
276  * Sends a signal to the process
277  *
278  * @param proc pointer to process structure
279  * @param sig signal
280  * @return 0 on success, -1 on error
281  */
282 int
283 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig);
284
285
286 /**
287  * Cleans up process structure contents (OS-dependent) and deallocates it
288  *
289  * @param proc pointer to process structure
290  */
291 void
292 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc);
293
294
295 /**
296  * Get the pid of the process in question
297  *
298  * @param proc the process to get the pid of
299  *
300  * @return the current process id
301  */
302 pid_t
303 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc);
304
305
306 /**
307  * Set process priority
308  *
309  * @param proc pointer to process structure
310  * @param prio priority value
311  * @return GNUNET_OK on success, GNUNET_SYSERR on error
312  */
313 int
314 GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
315                                 enum GNUNET_SCHEDULER_Priority prio);
316
317
318 /**
319  * Start a process.
320  *
321  * @param pipe_control should a pipe be used to send signals to the child?
322  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
323  * @param pipe_stdin pipe to use to send input to child process (or NULL)
324  * @param pipe_stdout pipe to use to get output from child process (or NULL)
325  * @param filename name of the binary
326  * @param argv NULL-terminated array of arguments to the process
327  * @return pointer to process structure of the new process, NULL on error
328  */
329 struct GNUNET_OS_Process *
330 GNUNET_OS_start_process_vap (int pipe_control,
331                              enum GNUNET_OS_InheritStdioFlags std_inheritance,
332                              struct GNUNET_DISK_PipeHandle *pipe_stdin,
333                              struct GNUNET_DISK_PipeHandle *pipe_stdout,
334                              const char *filename, 
335                              char *const argv[]);
336
337
338 /**
339  * Start a process.
340  *
341  * @param pipe_control should a pipe be used to send signals to the child?
342  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
343  * @param pipe_stdin pipe to use to send input to child process (or NULL)
344  * @param pipe_stdout pipe to use to get output from child process (or NULL)
345  * @param filename name of the binary
346  * @param ... NULL-terminated list of arguments to the process
347  * @return pointer to process structure of the new process, NULL on error
348  */
349 struct GNUNET_OS_Process *
350 GNUNET_OS_start_process (int pipe_control,
351                          enum GNUNET_OS_InheritStdioFlags std_inheritance,
352                          struct GNUNET_DISK_PipeHandle *pipe_stdin,
353                          struct GNUNET_DISK_PipeHandle *pipe_stdout,
354                          const char *filename, ...);
355
356
357 /**
358  * Start a process.
359  *
360  * @param pipe_control should a pipe be used to send signals to the child?
361  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
362  * @param pipe_stdin pipe to use to send input to child process (or NULL)
363  * @param pipe_stdout pipe to use to get output from child process (or NULL)
364  * @param filename name of the binary
365  * @param va NULL-terminated list of arguments to the process
366  * @return pointer to process structure of the new process, NULL on error
367  */
368 struct GNUNET_OS_Process *
369 GNUNET_OS_start_process_va (int pipe_control,
370                             enum GNUNET_OS_InheritStdioFlags std_inheritance,
371                             struct GNUNET_DISK_PipeHandle *pipe_stdin,
372                             struct GNUNET_DISK_PipeHandle *pipe_stdout,
373                             const char *filename, va_list va);
374
375 /**
376  * Start a process.
377  *
378  * @param pipe_control should a pipe be used to send signals to the child?
379  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
380  * @param lsocks array of listen sockets to dup systemd-style (or NULL);
381  *         must be NULL on platforms where dup is not supported
382  * @param filename name of the binary
383  * @param argv NULL-terminated list of arguments to the process,
384  *             including the process name as the first argument
385  * @return pointer to process structure of the new process, NULL on error
386  */
387 struct GNUNET_OS_Process *
388 GNUNET_OS_start_process_v (int pipe_control,
389                            enum GNUNET_OS_InheritStdioFlags std_inheritance,
390                            const SOCKTYPE *lsocks, 
391                            const char *filename,
392                            char *const argv[]);
393
394
395 /**
396  * Handle to a command action.
397  */
398 struct GNUNET_OS_CommandHandle;
399
400
401 /**
402  * Type of a function to process a line of output.
403  *
404  * @param cls closure
405  * @param line line of output from a command, NULL for the end
406  */
407 typedef void (*GNUNET_OS_LineProcessor) (void *cls, const char *line);
408
409
410 /**
411  * Stop/kill a command.
412  *
413  * @param cmd handle to the process
414  */
415 void
416 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd);
417
418
419 /**
420  * Run the given command line and call the given function
421  * for each line of the output.
422  *
423  * @param proc function to call for each line of the output
424  * @param proc_cls closure for proc
425  * @param timeout when to time out
426  * @param binary command to run
427  * @param ... arguments to command
428  * @return NULL on error
429  */
430 struct GNUNET_OS_CommandHandle *
431 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
432                        struct GNUNET_TIME_Relative timeout, const char *binary,
433                        ...);
434
435
436 /**
437  * Retrieve the status of a process, waiting on him if dead.
438  * Nonblocking version.
439  *
440  * @param proc pointer to process structure
441  * @param type status type
442  * @param code return code/signal number
443  * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
444  */
445 int
446 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
447                           enum GNUNET_OS_ProcessStatusType *type,
448                           unsigned long *code);
449
450
451 /**
452  * Wait for a process to terminate.  The return code is discarded.
453  * You must not use 'GNUNET_OS_process_status' on the same process
454  * after calling this function!  This function is blocking and should
455  * thus only be used if the child process is known to have terminated
456  * or to terminate very soon.
457  *
458  * @param proc pointer to process structure of the process to wait for
459  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
460  */
461 int
462 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc);
463
464
465 /**
466  * Connects this process to its parent via pipe;
467  * essentially, the parent control handler will read signal numbers
468  * from the 'GNUNET_OS_CONTROL_PIPE' (as given in an environment
469  * variable) and raise those signals.
470  *
471  * @param cls closure (unused)
472  * @param tc scheduler context (unused)
473  */
474 void
475 GNUNET_OS_install_parent_control_handler (void *cls,
476                                           const struct
477                                           GNUNET_SCHEDULER_TaskContext *tc);
478
479
480 /**
481  * Check whether an executable exists and possibly
482  * if the suid bit is set on the file.
483  * Attempts to find the file using the current
484  * PATH environment variable as a search path.
485  *
486  * @param binary the name of the file to check
487  * @return GNUNET_YES if the file is SUID,
488  *         GNUNET_NO if not SUID (but binary exists)
489  *         GNUNET_SYSERR on error (no such binary or not executable)
490  */
491 int
492 GNUNET_OS_check_helper_binary (const char *binary);
493
494
495 #if 0                           /* keep Emacsens' auto-indent happy */
496 {
497 #endif
498 #ifdef __cplusplus
499 }
500 #endif
501
502
503 /* ifndef GNUNET_OS_LIB_H */
504 #endif
505 /* end of gnunet_os_lib.h */