2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
32 #ifndef GNUNET_OS_LIB_H
33 #define GNUNET_OS_LIB_H
38 #if 0 /* keep Emacsens' auto-indent happy */
43 #include "gnunet_common.h"
44 #include "gnunet_configuration_lib.h"
45 #include "gnunet_scheduler_lib.h"
48 * Process information (OS-dependent)
50 struct GNUNET_OS_Process;
54 * Possible installation paths to request
56 enum GNUNET_OS_InstallationPathKind
59 * Return the "PREFIX" directory given to configure.
64 * Return the directory where the program binaries are installed. (bin/)
69 * Return the directory where libraries are installed. (lib/)
74 * Return the directory where data is installed (share/)
76 GNUNET_OS_IPK_DATADIR,
79 * Return the directory where translations are installed (share/locale/)
81 GNUNET_OS_IPK_LOCALEDIR,
84 * Return the installation directory of this application, not
85 * the one of the overall GNUnet installation (in case they
88 GNUNET_OS_IPK_SELF_PREFIX,
91 * Return the prefix of the path with application icons.
98 * Process status types
100 enum GNUNET_OS_ProcessStatusType
103 * The process is not known to the OS (or at
104 * least not one of our children).
106 GNUNET_OS_PROCESS_UNKNOWN,
109 * The process is still running.
111 GNUNET_OS_PROCESS_RUNNING,
114 * The process is paused (but could be resumed).
116 GNUNET_OS_PROCESS_STOPPED,
119 * The process exited with a return code.
121 GNUNET_OS_PROCESS_EXITED,
124 * The process was killed by a signal.
126 GNUNET_OS_PROCESS_SIGNALED
131 * Get the path to a specific GNUnet installation directory or, with
132 * GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation
135 * @param dirkind what kind of directory is desired?
136 * @return a pointer to the dir path (to be freed by the caller)
139 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind);
143 * Callback function invoked for each interface found.
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
154 typedef int (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls, const char *name,
156 const struct sockaddr *
158 const struct sockaddr *
160 const struct sockaddr *
161 netmask, socklen_t addrlen);
165 * @brief Enumerate all network interfaces
166 * @param proc the callback function
167 * @param proc_cls closure for proc
170 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
174 * @brief Get maximum string length returned by gethostname()
176 #if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
177 #define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
178 #elif defined(HOST_NAME_MAX)
179 #define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
181 #define GNUNET_OS_get_hostname_max_length() 255
186 * Get process structure for current process
188 * The pointer it returns points to static memory location and must not be
191 * @return pointer to the process sturcutre for this process
193 struct GNUNET_OS_Process *
194 GNUNET_OS_process_current (void);
198 * Sends sig to the process
200 * @param proc pointer to process structure
202 * @return 0 on success, -1 on error
205 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig);
209 * Cleans up process structure contents (OS-dependent) and deallocates it
211 * @param proc pointer to process structure
214 GNUNET_OS_process_close (struct GNUNET_OS_Process *proc);
217 * Get the pid of the process in question
219 * @param proc the process to get the pid of
221 * @return the current process id
224 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc);
227 * Set process priority
229 * @param proc pointer to process structure
230 * @param prio priority value
231 * @return GNUNET_OK on success, GNUNET_SYSERR on error
234 GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
235 enum GNUNET_SCHEDULER_Priority prio);
241 * @param pipe_stdin pipe to use to send input to child process (or NULL)
242 * @param pipe_stdout pipe to use to get output from child process (or NULL)
243 * @param filename name of the binary
244 * @param ... NULL-terminated list of arguments to the process
245 * @return pointer to process structure of the new process, NULL on error
247 struct GNUNET_OS_Process *
248 GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
249 struct GNUNET_DISK_PipeHandle *pipe_stdout,
250 const char *filename, ...);
256 * @param pipe_stdin pipe to use to send input to child process (or NULL)
257 * @param pipe_stdout pipe to use to get output from child process (or NULL)
258 * @param filename name of the binary
259 * @param va NULL-terminated list of arguments to the process
260 * @return pointer to process structure of the new process, NULL on error
262 struct GNUNET_OS_Process *
263 GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
264 struct GNUNET_DISK_PipeHandle *pipe_stdout,
265 const char *filename, va_list va);
270 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
271 * must be NULL on platforms where dup is not supported
272 * @param filename name of the binary
273 * @param argv NULL-terminated list of arguments to the process,
274 * including the process name as the first argument
275 * @return pointer to process structure of the new process, NULL on error
277 struct GNUNET_OS_Process *
278 GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
283 * Handle to a command action.
285 struct GNUNET_OS_CommandHandle;
288 * Type of a function to process a line of output.
291 * @param line line of output from a command, NULL for the end
293 typedef void (*GNUNET_OS_LineProcessor) (void *cls, const char *line);
296 * Stop/kill a command.
298 * @param cmd handle to the process
301 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd);
305 * Run the given command line and call the given function
306 * for each line of the output.
308 * @param proc function to call for each line of the output
309 * @param proc_cls closure for proc
310 * @param timeout when to time out
311 * @param binary command to run
312 * @param ... arguments to command
313 * @return NULL on error
315 struct GNUNET_OS_CommandHandle *
316 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
317 struct GNUNET_TIME_Relative timeout, const char *binary,
322 * Retrieve the status of a process. Nonblocking version.
324 * @param proc pointer to process structure
325 * @param type status type
326 * @param code return code/signal number
327 * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
330 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
331 enum GNUNET_OS_ProcessStatusType *type,
332 unsigned long *code);
336 * Wait for a process to terminate. The return code is discarded.
337 * You must not use 'GNUNET_OS_process_status' on the same process
338 * after calling this function! This function is blocking and should
339 * thus only be used if the child process is known to have terminated
340 * or to terminate very soon.
342 * @param proc pointer to process structure of the process to wait for
343 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
346 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc);
350 * Connects this process to its parent via pipe
353 GNUNET_OS_install_parent_control_handler (void *cls,
355 GNUNET_SCHEDULER_TaskContext *tc);
359 * Check whether an executable exists and possibly
360 * if the suid bit is set on the file.
361 * Attempts to find the file using the current
362 * PATH environment variable as a search path.
364 * @param binary the name of the file to check
365 * @return GNUNET_YES if the file is SUID,
366 * GNUNET_NO if not SUID (but binary exists)
367 * GNUNET_SYSERR on error (no such binary or not executable)
370 GNUNET_OS_check_helper_binary (const char *binary);
373 #if 0 /* keep Emacsens' auto-indent happy */
381 /* ifndef GNUNET_OS_LIB_H */
383 /* end of gnunet_os_lib.h */