icondir
[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 /**
49  * Possible installation paths to request
50  */
51 enum GNUNET_OS_InstallationPathKind
52 {
53   /**
54    * Return the "PREFIX" directory given to configure.
55    */
56   GNUNET_OS_IPK_PREFIX,
57
58   /**
59    * Return the directory where the program binaries are installed. (bin/)
60    */
61   GNUNET_OS_IPK_BINDIR,
62
63   /**
64    * Return the directory where libraries are installed. (lib/)
65    */
66   GNUNET_OS_IPK_LIBDIR,
67
68   /**
69    * Return the directory where data is installed (share/)
70    */
71   GNUNET_OS_IPK_DATADIR,
72
73   /**
74    * Return the directory where translations are installed (share/locale/)
75    */
76   GNUNET_OS_IPK_LOCALEDIR,
77
78   /**
79    * Return the installation directory of this application, not
80    * the one of the overall GNUnet installation (in case they
81    * are different).
82    */
83   GNUNET_OS_IPK_SELF_PREFIX,
84
85   /**
86    * Return the prefix of the path with application icons.
87    */
88   GNUNET_OS_IPK_ICONDIR
89 };
90
91
92 /**
93  * Process status types
94  */
95 enum GNUNET_OS_ProcessStatusType
96 {
97   /**
98    * The process is not known to the OS (or at
99    * least not one of our children).
100    */
101   GNUNET_OS_PROCESS_UNKNOWN,
102
103   /**
104    * The process is still running.
105    */
106   GNUNET_OS_PROCESS_RUNNING,
107
108   /**
109    * The process is paused (but could be resumed).
110    */
111   GNUNET_OS_PROCESS_STOPPED,
112
113   /**
114    * The process exited with a return code.
115    */
116   GNUNET_OS_PROCESS_EXITED,
117
118   /**
119    * The process was killed by a signal.
120    */
121   GNUNET_OS_PROCESS_SIGNALED
122 };
123
124
125 /**
126  * Get the path to a specific GNUnet installation directory or, with
127  * GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation
128  * directory.
129  *
130  * @param dirkind what kind of directory is desired?
131  * @return a pointer to the dir path (to be freed by the caller)
132  */
133 char *GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind
134                                        dirkind);
135
136
137 /**
138  * Callback function invoked for each interface found.
139  *
140  * @param cls closure
141  * @param name name of the interface (can be NULL for unknown)
142  * @param isDefault is this presumably the default interface
143  * @param addr address of this interface (can be NULL for unknown or unassigned)
144  * @param addrlen length of the address
145  * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
146  */
147 typedef int (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls,
148                                                     const char *name,
149                                                     int isDefault,
150                                                     const struct sockaddr *
151                                                     addr, socklen_t addrlen);
152
153
154 /**
155  * @brief Enumerate all network interfaces
156  * @param proc the callback function
157  * @param proc_cls closure for proc
158  */
159 void GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor
160                                         proc, void *proc_cls);
161
162 /**
163  * @brief Get maximum string length returned by gethostname()
164  */
165 #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
166 #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
167 #elif defined(HOST_NAME_MAX)
168 #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
169 #else
170 #define GNUNET_OS_get_hostname_max_length() 255
171 #endif
172
173
174 /**
175  * Get the current CPU load.
176  *
177  * @param cfg to determine acceptable load level (LOAD::MAXCPULOAD)
178  * @return -1 on error, otherwise load value (between 0 and 100,
179  *        (100 is equivalent to full load for one CPU)
180  */
181 int GNUNET_OS_load_cpu_get (const struct GNUNET_CONFIGURATION_Handle *cfg);
182
183
184 /**
185  * Get the current IO load.
186  *
187  * @param cfg to determine acceptable load level (LOAD::MAXIOLOAD)
188  * @return -1 on error, otherwise load value (between 0 and 100,
189  *       100 means that we spend all of our cycles waiting for
190  *       the disk)
191  */
192 int GNUNET_OS_load_disk_get (const struct GNUNET_CONFIGURATION_Handle *cfg);
193
194
195 /**
196  * Set process priority
197  *
198  * @param proc id of the process
199  * @param prio priority value
200  * @return GNUNET_OK on success, GNUNET_SYSERR on error
201  */
202 int GNUNET_OS_set_process_priority (pid_t proc,
203                                     enum GNUNET_SCHEDULER_Priority prio);
204
205
206 /**
207  * Start a process.
208  *
209  * @param pipe_stdin pipe to use to send input to child process (or NULL)
210  * @param pipe_stdout pipe to use to get output from child process (or NULL)
211  * @param filename name of the binary
212  * @param ... NULL-terminated list of arguments to the process
213  * @return process ID of the new process, -1 on error
214  */
215 pid_t
216 GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, 
217                          struct GNUNET_DISK_PipeHandle *pipe_stdout, 
218                          const char *filename, ...);
219
220
221 /**
222  * Start a process.
223  *
224  * @param lsocks array of listen sockets to dup systemd-style (or NULL);
225  *         must be NULL on platforms where dup is not supported
226  * @param filename name of the binary
227  * @param argv NULL-terminated list of arguments to the process,
228  *             including the process name as the first argument
229  * @return process ID of the new process, -1 on error
230  */
231 pid_t GNUNET_OS_start_process_v (const int *lsocks,
232                                  const char *filename, char *const argv[]);
233
234
235 /**
236  * Retrieve the status of a process
237  * @param proc process ID
238  * @param type status type
239  * @param code return code/signal number
240  * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
241  */
242 int GNUNET_OS_process_status (pid_t proc, enum GNUNET_OS_ProcessStatusType *type,
243     unsigned long *code);
244
245
246 /**
247  * Wait for a process
248  * @param proc process ID to wait for
249  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
250  */
251 int GNUNET_OS_process_wait (pid_t proc);
252
253
254 #if 0                           /* keep Emacsens' auto-indent happy */
255 {
256 #endif
257 #ifdef __cplusplus
258 }
259 #endif
260
261
262 /* ifndef GNUNET_OS_LIB_H */
263 #endif
264 /* end of gnunet_os_lib.h */