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