fixes for invalid reads
[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
158 /**
159  * Process status types
160  */
161 enum GNUNET_OS_ProcessStatusType
162 {
163   /**
164    * The process is not known to the OS (or at
165    * least not one of our children).
166    */
167   GNUNET_OS_PROCESS_UNKNOWN,
168
169   /**
170    * The process is still running.
171    */
172   GNUNET_OS_PROCESS_RUNNING,
173
174   /**
175    * The process is paused (but could be resumed).
176    */
177   GNUNET_OS_PROCESS_STOPPED,
178
179   /**
180    * The process exited with a return code.
181    */
182   GNUNET_OS_PROCESS_EXITED,
183
184   /**
185    * The process was killed by a signal.
186    */
187   GNUNET_OS_PROCESS_SIGNALED
188 };
189
190
191 /**
192  * Get the path to a specific GNUnet installation directory or, with
193  * GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation
194  * directory.
195  *
196  * @param dirkind what kind of directory is desired?
197  * @return a pointer to the dir path (to be freed by the caller)
198  */
199 char *
200 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind);
201
202
203 /**
204  * Callback function invoked for each interface found.
205  *
206  * @param cls closure
207  * @param name name of the interface (can be NULL for unknown)
208  * @param isDefault is this presumably the default interface
209  * @param addr address of this interface (can be NULL for unknown or unassigned)
210  * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
211  * @param netmask the network mask (can be NULL for unknown or unassigned))
212  * @param addrlen length of the address
213  * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
214  */
215 typedef int (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls, const char *name,
216                                                     int isDefault,
217                                                     const struct sockaddr *
218                                                     addr,
219                                                     const struct sockaddr *
220                                                     broadcast_addr,
221                                                     const struct sockaddr *
222                                                     netmask, socklen_t addrlen);
223
224
225 /**
226  * @brief Enumerate all network interfaces
227  * @param proc the callback function
228  * @param proc_cls closure for proc
229  */
230 void
231 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
232                                    void *proc_cls);
233
234 /**
235  * @brief Get maximum string length returned by gethostname()
236  */
237 #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
238 #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
239 #elif defined(HOST_NAME_MAX)
240 #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
241 #else
242 #define GNUNET_OS_get_hostname_max_length() 255
243 #endif
244
245
246 /**
247  * Get process structure for current process
248  *
249  * The pointer it returns points to static memory location and must not be
250  * deallocated/closed
251  *
252  * @return pointer to the process sturcutre for this process
253  */
254 struct GNUNET_OS_Process *
255 GNUNET_OS_process_current (void);
256
257
258 /**
259  * Sends a signal to the process
260  *
261  * @param proc pointer to process structure
262  * @param sig signal
263  * @return 0 on success, -1 on error
264  */
265 int
266 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig);
267
268
269 /**
270  * Cleans up process structure contents (OS-dependent) and deallocates it
271  *
272  * @param proc pointer to process structure
273  */
274 void
275 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc);
276
277
278 /**
279  * Get the pid of the process in question
280  *
281  * @param proc the process to get the pid of
282  *
283  * @return the current process id
284  */
285 pid_t
286 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc);
287
288
289 /**
290  * Set process priority
291  *
292  * @param proc pointer to process structure
293  * @param prio priority value
294  * @return GNUNET_OK on success, GNUNET_SYSERR on error
295  */
296 int
297 GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
298                                 enum GNUNET_SCHEDULER_Priority prio);
299
300
301 /**
302  * Start a process.
303  *
304  * @param pipe_control should a pipe be used to send signals to the child?
305  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
306  * @param pipe_stdin pipe to use to send input to child process (or NULL)
307  * @param pipe_stdout pipe to use to get output from child process (or NULL)
308  * @param filename name of the binary
309  * @param argv NULL-terminated array of arguments to the process
310  * @return pointer to process structure of the new process, NULL on error
311  */
312 struct GNUNET_OS_Process *
313 GNUNET_OS_start_process_vap (int pipe_control,
314                              enum GNUNET_OS_InheritStdioFlags std_inheritance,
315                              struct GNUNET_DISK_PipeHandle *pipe_stdin,
316                              struct GNUNET_DISK_PipeHandle *pipe_stdout,
317                              const char *filename, 
318                              char *const argv[]);
319
320
321 /**
322  * Start a process.
323  *
324  * @param pipe_control should a pipe be used to send signals to the child?
325  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
326  * @param pipe_stdin pipe to use to send input to child process (or NULL)
327  * @param pipe_stdout pipe to use to get output from child process (or NULL)
328  * @param filename name of the binary
329  * @param ... NULL-terminated list of arguments to the process
330  * @return pointer to process structure of the new process, NULL on error
331  */
332 struct GNUNET_OS_Process *
333 GNUNET_OS_start_process (int pipe_control,
334                          enum GNUNET_OS_InheritStdioFlags std_inheritance,
335                          struct GNUNET_DISK_PipeHandle *pipe_stdin,
336                          struct GNUNET_DISK_PipeHandle *pipe_stdout,
337                          const char *filename, ...);
338
339
340 /**
341  * Start a process.
342  *
343  * @param pipe_control should a pipe be used to send signals to the child?
344  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
345  * @param pipe_stdin pipe to use to send input to child process (or NULL)
346  * @param pipe_stdout pipe to use to get output from child process (or NULL)
347  * @param filename name of the binary
348  * @param va NULL-terminated list of arguments to the process
349  * @return pointer to process structure of the new process, NULL on error
350  */
351 struct GNUNET_OS_Process *
352 GNUNET_OS_start_process_va (int pipe_control,
353                             enum GNUNET_OS_InheritStdioFlags std_inheritance,
354                             struct GNUNET_DISK_PipeHandle *pipe_stdin,
355                             struct GNUNET_DISK_PipeHandle *pipe_stdout,
356                             const char *filename, va_list va);
357
358 /**
359  * Start a process.
360  *
361  * @param pipe_control should a pipe be used to send signals to the child?
362  * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
363  * @param lsocks array of listen sockets to dup systemd-style (or NULL);
364  *         must be NULL on platforms where dup is not supported
365  * @param filename name of the binary
366  * @param argv NULL-terminated list of arguments to the process,
367  *             including the process name as the first argument
368  * @return pointer to process structure of the new process, NULL on error
369  */
370 struct GNUNET_OS_Process *
371 GNUNET_OS_start_process_v (int pipe_control,
372                            enum GNUNET_OS_InheritStdioFlags std_inheritance,
373                            const SOCKTYPE *lsocks, 
374                            const char *filename,
375                            char *const argv[]);
376
377
378 /**
379  * Handle to a command action.
380  */
381 struct GNUNET_OS_CommandHandle;
382
383
384 /**
385  * Type of a function to process a line of output.
386  *
387  * @param cls closure
388  * @param line line of output from a command, NULL for the end
389  */
390 typedef void (*GNUNET_OS_LineProcessor) (void *cls, const char *line);
391
392
393 /**
394  * Stop/kill a command.
395  *
396  * @param cmd handle to the process
397  */
398 void
399 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd);
400
401
402 /**
403  * Run the given command line and call the given function
404  * for each line of the output.
405  *
406  * @param proc function to call for each line of the output
407  * @param proc_cls closure for proc
408  * @param timeout when to time out
409  * @param binary command to run
410  * @param ... arguments to command
411  * @return NULL on error
412  */
413 struct GNUNET_OS_CommandHandle *
414 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
415                        struct GNUNET_TIME_Relative timeout, const char *binary,
416                        ...);
417
418
419 /**
420  * Retrieve the status of a process, waiting on him if dead.
421  * Nonblocking version.
422  *
423  * @param proc pointer to process structure
424  * @param type status type
425  * @param code return code/signal number
426  * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
427  */
428 int
429 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
430                           enum GNUNET_OS_ProcessStatusType *type,
431                           unsigned long *code);
432
433
434 /**
435  * Wait for a process to terminate.  The return code is discarded.
436  * You must not use 'GNUNET_OS_process_status' on the same process
437  * after calling this function!  This function is blocking and should
438  * thus only be used if the child process is known to have terminated
439  * or to terminate very soon.
440  *
441  * @param proc pointer to process structure of the process to wait for
442  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
443  */
444 int
445 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc);
446
447
448 /**
449  * Connects this process to its parent via pipe;
450  * essentially, the parent control handler will read signal numbers
451  * from the 'GNUNET_OS_CONTROL_PIPE' (as given in an environment
452  * variable) and raise those signals.
453  *
454  * @param cls closure (unused)
455  * @param tc scheduler context (unused)
456  */
457 void
458 GNUNET_OS_install_parent_control_handler (void *cls,
459                                           const struct
460                                           GNUNET_SCHEDULER_TaskContext *tc);
461
462
463 /**
464  * Check whether an executable exists and possibly
465  * if the suid bit is set on the file.
466  * Attempts to find the file using the current
467  * PATH environment variable as a search path.
468  *
469  * @param binary the name of the file to check
470  * @return GNUNET_YES if the file is SUID,
471  *         GNUNET_NO if not SUID (but binary exists)
472  *         GNUNET_SYSERR on error (no such binary or not executable)
473  */
474 int
475 GNUNET_OS_check_helper_binary (const char *binary);
476
477
478 #if 0                           /* keep Emacsens' auto-indent happy */
479 {
480 #endif
481 #ifdef __cplusplus
482 }
483 #endif
484
485
486 /* ifndef GNUNET_OS_LIB_H */
487 #endif
488 /* end of gnunet_os_lib.h */