fix
[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 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 *GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind
139                                        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 addrlen length of the address
150  * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
151  */
152 typedef int (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls,
153                                                     const char *name,
154                                                     int isDefault,
155                                                     const struct sockaddr *
156                                                     addr, socklen_t addrlen);
157
158
159 /**
160  * @brief Enumerate all network interfaces
161  * @param proc the callback function
162  * @param proc_cls closure for proc
163  */
164 void GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor
165                                         proc, void *proc_cls);
166
167 /**
168  * @brief Get maximum string length returned by gethostname()
169  */
170 #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
171 #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
172 #elif defined(HOST_NAME_MAX)
173 #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
174 #else
175 #define GNUNET_OS_get_hostname_max_length() 255
176 #endif
177
178
179 /**
180  * Get process structure for current process
181  *
182  * The pointer it returns points to static memory location and must not be
183  * deallocated/closed
184  *
185  * @return pointer to the process sturcutre for this process
186  */
187 struct GNUNET_OS_Process *GNUNET_OS_process_current (void);
188
189
190 /**
191  * Sends sig to the process
192  *
193  * @param proc pointer to process structure
194  * @param sig signal
195  * @return 0 on success, -1 on error
196  */
197 int GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig);
198
199
200 /**
201  * Cleans up process structure contents (OS-dependent) and deallocates it
202  *
203  * @param proc pointer to process structure
204  */
205 void GNUNET_OS_process_close (struct GNUNET_OS_Process *proc);
206
207 /**
208  * Get the pid of the process in question
209  *
210  * @param proc the process to get the pid of
211  *
212  * @return the current process id
213  */
214 pid_t
215 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc);
216
217 /**
218  * Set process priority
219  *
220  * @param proc pointer to process structure
221  * @param prio priority value
222  * @return GNUNET_OK on success, GNUNET_SYSERR on error
223  */
224 int GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
225                                     enum GNUNET_SCHEDULER_Priority prio);
226
227
228 /**
229  * Start a process.
230  *
231  * @param pipe_stdin pipe to use to send input to child process (or NULL)
232  * @param pipe_stdout pipe to use to get output from child process (or NULL)
233  * @param filename name of the binary
234  * @param ... NULL-terminated list of arguments to the process
235  * @return pointer to process structure of the new process, NULL on error
236  */
237 struct GNUNET_OS_Process *
238 GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, 
239                          struct GNUNET_DISK_PipeHandle *pipe_stdout, 
240                          const char *filename, ...);
241
242
243 /**
244  * Start a process.
245  *
246  * @param lsocks array of listen sockets to dup systemd-style (or NULL);
247  *         must be NULL on platforms where dup is not supported
248  * @param filename name of the binary
249  * @param argv NULL-terminated list of arguments to the process,
250  *             including the process name as the first argument
251  * @return pointer to process structure of the new process, NULL on error
252  */
253 struct GNUNET_OS_Process *
254 GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
255                            char *const argv[]);
256
257
258 /**
259  * Retrieve the status of a process
260  * @param proc pointer to process structure
261  * @param type status type
262  * @param code return code/signal number
263  * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
264  */
265 int GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
266                               enum GNUNET_OS_ProcessStatusType *type, unsigned long *code);
267
268
269 /**
270  * Wait for a process
271  * @param proc pointer to process structure of the process to wait for
272  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
273  */
274 int GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc);
275
276
277 /**
278  * Connects this process to its parent via pipe
279  */
280 void
281 GNUNET_OS_install_parent_control_handler (void *cls,
282                                           const struct
283                                           GNUNET_SCHEDULER_TaskContext * tc);
284
285 #if 0                           /* keep Emacsens' auto-indent happy */
286 {
287 #endif
288 #ifdef __cplusplus
289 }
290 #endif
291
292
293 /* ifndef GNUNET_OS_LIB_H */
294 #endif
295 /* end of gnunet_os_lib.h */