12ec3a54e4e8906282e3be495ac3894a95c5584d
[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
32 #ifndef GNUNET_OS_LIB_H
33 #define GNUNET_OS_LIB_H
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #if 0                           /* keep Emacsens' auto-indent happy */
39 }
40 #endif
41 #endif
42
43 #include "gnunet_common.h"
44 #include "gnunet_configuration_lib.h"
45 #include "gnunet_scheduler_lib.h"
46
47 /**
48  * Process information (OS-dependent)
49  */
50 struct GNUNET_OS_Process;
51
52
53 /**
54  * Possible installation paths to request
55  */
56 enum GNUNET_OS_InstallationPathKind
57 {
58   /**
59    * Return the "PREFIX" directory given to configure.
60    */
61   GNUNET_OS_IPK_PREFIX,
62
63   /**
64    * Return the directory where the program binaries are installed. (bin/)
65    */
66   GNUNET_OS_IPK_BINDIR,
67
68   /**
69    * Return the directory where libraries are installed. (lib/)
70    */
71   GNUNET_OS_IPK_LIBDIR,
72
73   /**
74    * Return the directory where data is installed (share/)
75    */
76   GNUNET_OS_IPK_DATADIR,
77
78   /**
79    * Return the directory where translations are installed (share/locale/)
80    */
81   GNUNET_OS_IPK_LOCALEDIR,
82
83   /**
84    * Return the installation directory of this application, not
85    * the one of the overall GNUnet installation (in case they
86    * are different).
87    */
88   GNUNET_OS_IPK_SELF_PREFIX,
89
90   /**
91    * Return the prefix of the path with application icons.
92    */
93   GNUNET_OS_IPK_ICONDIR
94 };
95
96
97 /**
98  * Process status types
99  */
100 enum GNUNET_OS_ProcessStatusType
101 {
102   /**
103    * The process is not known to the OS (or at
104    * least not one of our children).
105    */
106   GNUNET_OS_PROCESS_UNKNOWN,
107
108   /**
109    * The process is still running.
110    */
111   GNUNET_OS_PROCESS_RUNNING,
112
113   /**
114    * The process is paused (but could be resumed).
115    */
116   GNUNET_OS_PROCESS_STOPPED,
117
118   /**
119    * The process exited with a return code.
120    */
121   GNUNET_OS_PROCESS_EXITED,
122
123   /**
124    * The process was killed by a signal.
125    */
126   GNUNET_OS_PROCESS_SIGNALED
127 };
128
129
130 /**
131  * Get the path to a specific GNUnet installation directory or, with
132  * GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation
133  * directory.
134  *
135  * @param dirkind what kind of directory is desired?
136  * @return a pointer to the dir path (to be freed by the caller)
137  */
138 char *
139 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind);
140
141
142 /**
143  * Callback function invoked for each interface found.
144  *
145  * @param cls closure
146  * @param name name of the interface (can be NULL for unknown)
147  * @param isDefault is this presumably the default interface
148  * @param addr address of this interface (can be NULL for unknown or unassigned)
149  * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
150  * @param netmask the network mask (can be NULL for unknown or unassigned))
151  * @param addrlen length of the address
152  * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
153  */
154 typedef int (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls, const char *name,
155                                                     int isDefault,
156                                                     const struct sockaddr * addr,
157                                                     const struct sockaddr * broadcast_addr,
158                                                     const struct sockaddr * netmask,
159                                                     socklen_t addrlen);
160
161
162 /**
163  * @brief Enumerate all network interfaces
164  * @param proc the callback function
165  * @param proc_cls closure for proc
166  */
167 void
168 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
169                                    void *proc_cls);
170
171 /**
172  * @brief Get maximum string length returned by gethostname()
173  */
174 #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
175 #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
176 #elif defined(HOST_NAME_MAX)
177 #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
178 #else
179 #define GNUNET_OS_get_hostname_max_length() 255
180 #endif
181
182
183 /**
184  * Get process structure for current process
185  *
186  * The pointer it returns points to static memory location and must not be
187  * deallocated/closed
188  *
189  * @return pointer to the process sturcutre for this process
190  */
191 struct GNUNET_OS_Process *
192 GNUNET_OS_process_current (void);
193
194
195 /**
196  * Sends sig to the process
197  *
198  * @param proc pointer to process structure
199  * @param sig signal
200  * @return 0 on success, -1 on error
201  */
202 int
203 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig);
204
205
206 /**
207  * Cleans up process structure contents (OS-dependent) and deallocates it
208  *
209  * @param proc pointer to process structure
210  */
211 void
212 GNUNET_OS_process_close (struct GNUNET_OS_Process *proc);
213
214 /**
215  * Get the pid of the process in question
216  *
217  * @param proc the process to get the pid of
218  *
219  * @return the current process id
220  */
221 pid_t
222 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc);
223
224 /**
225  * Set process priority
226  *
227  * @param proc pointer to process structure
228  * @param prio priority value
229  * @return GNUNET_OK on success, GNUNET_SYSERR on error
230  */
231 int
232 GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
233                                 enum GNUNET_SCHEDULER_Priority prio);
234
235
236 /**
237  * Start a process.
238  *
239  * @param pipe_stdin pipe to use to send input to child process (or NULL)
240  * @param pipe_stdout pipe to use to get output from child process (or NULL)
241  * @param filename name of the binary
242  * @param ... NULL-terminated list of arguments to the process
243  * @return pointer to process structure of the new process, NULL on error
244  */
245 struct GNUNET_OS_Process *
246 GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
247                          struct GNUNET_DISK_PipeHandle *pipe_stdout,
248                          const char *filename, ...);
249
250
251 /**
252  * Start a process.
253  *
254  * @param pipe_stdin pipe to use to send input to child process (or NULL)
255  * @param pipe_stdout pipe to use to get output from child process (or NULL)
256  * @param filename name of the binary
257  * @param va NULL-terminated list of arguments to the process
258  * @return pointer to process structure of the new process, NULL on error
259  */
260 struct GNUNET_OS_Process *
261 GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
262                             struct GNUNET_DISK_PipeHandle *pipe_stdout,
263                             const char *filename, va_list va);
264
265 /**
266  * Start a process.
267  *
268  * @param lsocks array of listen sockets to dup systemd-style (or NULL);
269  *         must be NULL on platforms where dup is not supported
270  * @param filename name of the binary
271  * @param argv NULL-terminated list of arguments to the process,
272  *             including the process name as the first argument
273  * @return pointer to process structure of the new process, NULL on error
274  */
275 struct GNUNET_OS_Process *
276 GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
277                            char *const argv[]);
278
279
280 /**
281  * Handle to a command action.
282  */
283 struct GNUNET_OS_CommandHandle;
284
285 /**
286  * Type of a function to process a line of output.
287  *
288  * @param cls closure
289  * @param line line of output from a command, NULL for the end
290  */
291 typedef void (*GNUNET_OS_LineProcessor) (void *cls, const char *line);
292
293 /**
294  * Stop/kill a command.
295  *
296  * @param cmd handle to the process
297  */
298 void
299 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd);
300
301
302 /**
303  * Run the given command line and call the given function
304  * for each line of the output.
305  *
306  * @param proc function to call for each line of the output
307  * @param proc_cls closure for proc
308  * @param timeout when to time out
309  * @param binary command to run
310  * @param ... arguments to command
311  * @return NULL on error
312  */
313 struct GNUNET_OS_CommandHandle *
314 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
315                        struct GNUNET_TIME_Relative timeout, const char *binary,
316                        ...);
317
318
319 /**
320  * Retrieve the status of a process.  Nonblocking version.
321  *
322  * @param proc pointer to process structure
323  * @param type status type
324  * @param code return code/signal number
325  * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
326  */
327 int
328 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
329                           enum GNUNET_OS_ProcessStatusType *type,
330                           unsigned long *code);
331
332
333 /**
334  * Wait for a process to terminate.  The return code is discarded.
335  * You must not use 'GNUNET_OS_process_status' on the same process
336  * after calling this function!  This function is blocking and should
337  * thus only be used if the child process is known to have terminated
338  * or to terminate very soon.
339  *
340  * @param proc pointer to process structure of the process to wait for
341  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
342  */
343 int
344 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc);
345
346
347 /**
348  * Connects this process to its parent via pipe
349  */
350 void
351 GNUNET_OS_install_parent_control_handler (void *cls,
352                                           const struct
353                                           GNUNET_SCHEDULER_TaskContext *tc);
354
355
356 /**
357  * Check whether an executable exists and possibly
358  * if the suid bit is set on the file.
359  * Attempts to find the file using the current
360  * PATH environment variable as a search path.
361  *
362  * @param binary the name of the file to check
363  * @return GNUNET_YES if the file is SUID,
364  *         GNUNET_NO if not SUID (but binary exists)
365  *         GNUNET_SYSERR on error (no such binary or not executable)
366  */
367 int
368 GNUNET_OS_check_helper_binary (const char *binary);
369
370
371 #if 0                           /* keep Emacsens' auto-indent happy */
372 {
373 #endif
374 #ifdef __cplusplus
375 }
376 #endif
377
378
379 /* ifndef GNUNET_OS_LIB_H */
380 #endif
381 /* end of gnunet_os_lib.h */