2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006 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 util/os_priority.c
23 * @brief Methods to set process priority
28 #include "gnunet_common.h"
29 #include "gnunet_os_lib.h"
32 * Set process priority
34 * @param proc id of the process
35 * @param prio priority value
36 * @return GNUNET_OK on success, GNUNET_SYSERR on error
39 GNUNET_OS_set_process_priority (pid_t proc,
40 enum GNUNET_SCHEDULER_Priority prio)
44 GNUNET_assert (prio < GNUNET_SCHEDULER_PRIORITY_COUNT);
45 if (prio == GNUNET_SCHEDULER_PRIORITY_KEEP)
47 /* convert to MINGW/Unix values */
50 case GNUNET_SCHEDULER_PRIORITY_DEFAULT:
52 rprio = NORMAL_PRIORITY_CLASS;
57 case GNUNET_SCHEDULER_PRIORITY_HIGH:
59 rprio = ABOVE_NORMAL_PRIORITY_CLASS;
64 case GNUNET_SCHEDULER_PRIORITY_BACKGROUND:
66 rprio = BELOW_NORMAL_PRIORITY_CLASS;
71 case GNUNET_SCHEDULER_PRIORITY_UI:
72 case GNUNET_SCHEDULER_PRIORITY_URGENT:
74 rprio = HIGH_PRIORITY_CLASS;
79 case GNUNET_SCHEDULER_PRIORITY_IDLE:
81 rprio = IDLE_PRIORITY_CLASS;
90 /* Set process priority */
92 SetPriorityClass (GetCurrentProcess (), rprio);
94 if (proc == getpid ())
97 if ((-1 == nice (rprio)) && (errno != 0))
99 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
100 GNUNET_ERROR_TYPE_BULK, "nice");
101 return GNUNET_SYSERR;
106 if (0 != setpriority (PRIO_PROCESS, proc, rprio))
109 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
110 GNUNET_ERROR_TYPE_BULK, "setpriority");
111 return GNUNET_SYSERR;
122 * @param filename name of the binary
123 * @param ... NULL-terminated list of arguments to the process
124 * @return process ID of the new process, -1 on error
127 GNUNET_OS_start_process (const char *filename, ...)
136 #if HAVE_WORKING_VFORK
145 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
149 #if HAVE_WORKING_VFORK
150 /* let's hope vfork actually works; for some extreme cases (including
151 a testcase) we need 'execvp' to have run before we return, since
152 we may send a signal to the process next and we don't want it
153 to be caught by OUR signal handler (but either by the default
154 handler or the actual handler as installed by the process itself). */
156 /* let's give the child process a chance to run execvp, 1s should
157 be plenty in practice */
164 va_start (ap, filename);
165 while (NULL != va_arg (ap, char *))
168 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
170 va_start (ap, filename);
171 while (NULL != (argv[argc] = va_arg (ap, char *)))
174 execvp (filename, argv);
175 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
182 PROCESS_INFORMATION proc;
185 va_start (ap, filename);
186 while (NULL != (arg = va_arg (ap, char *)))
187 cmdlen = cmdlen + strlen (arg) + 3;
190 cmd = idx = GNUNET_malloc (sizeof(char) * cmdlen);
191 va_start (ap, filename);
192 while (NULL != (arg = va_arg (ap, char *)))
193 idx += sprintf (idx, "\"%s\" ", arg);
196 memset (&start, 0, sizeof(start));
197 start.cb = sizeof(start);
199 if (!CreateProcess (filename, cmd, NULL, NULL, FALSE, DETACHED_PROCESS, NULL,
200 NULL, &start, &proc))
202 SetErrnoFromWinError (GetLastError ());
203 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
206 CloseHandle (proc.hProcess);
207 CloseHandle (proc.hThread);
211 return proc.dwProcessId;
221 * @param filename name of the binary
222 * @param argv NULL-terminated list of arguments to the process
223 * @return process ID of the new process, -1 on error
226 GNUNET_OS_start_process_v (const char *filename, char *const argv[])
231 #if HAVE_WORKING_VFORK
240 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
244 #if HAVE_WORKING_VFORK
245 /* let's hope vfork actually works; for some extreme cases (including
246 a testcase) we need 'execvp' to have run before we return, since
247 we may send a signal to the process next and we don't want it
248 to be caught by OUR signal handler (but either by the default
249 handler or the actual handler as installed by the process itself). */
251 /* let's give the child process a chance to run execvp, 1s should
252 be plenty in practice */
258 execvp (filename, argv);
259 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
266 PROCESS_INFORMATION proc;
272 cmdlen = cmdlen + strlen (*arg) + 3;
276 cmd = idx = GNUNET_malloc (sizeof(char) * cmdlen);
280 idx += sprintf (idx, "\"%s\" ", *arg);
284 memset (&start, 0, sizeof(start));
285 start.cb = sizeof(start);
287 if (!CreateProcess (filename, cmd, NULL, NULL, FALSE, DETACHED_PROCESS, NULL,
288 NULL, &start, &proc))
290 SetErrnoFromWinError (GetLastError ());
291 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
294 CloseHandle (proc.hProcess);
295 CloseHandle (proc.hThread);
299 return proc.dwProcessId;
304 * Retrieve the status of a process
305 * @param proc process ID
306 * @param type status type
307 * @param code return code/signal number
308 * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
311 GNUNET_OS_process_status (pid_t proc, enum GNUNET_OS_ProcessStatusType *type,
318 GNUNET_assert (0 != proc);
319 ret = waitpid (proc, &status, WNOHANG);
322 *type = GNUNET_OS_PROCESS_RUNNING;
328 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
329 return GNUNET_SYSERR;
331 if (WIFEXITED (status))
333 *type = GNUNET_OS_PROCESS_EXITED;
334 *code = WEXITSTATUS (status);
336 else if (WIFSIGNALED (status))
338 *type = GNUNET_OS_PROCESS_SIGNALED;
339 *code = WTERMSIG (status);
341 else if (WIFSTOPPED (status))
343 *type = GNUNET_OS_PROCESS_SIGNALED;
344 *code = WSTOPSIG (status);
346 else if (WIFCONTINUED (status))
348 *type = GNUNET_OS_PROCESS_RUNNING;
353 *type = GNUNET_OS_PROCESS_UNKNOWN;
360 h = OpenProcess (PROCESS_QUERY_INFORMATION, FALSE, proc);
361 if (INVALID_HANDLE_VALUE == h)
363 SetErrnoFromWinError (GetLastError ());
364 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "OpenProcess");
365 return GNUNET_SYSERR;
368 c = GetExitCodeProcess (proc, &c);
369 if (STILL_ACTIVE == c)
371 *type = GNUNET_OS_PROCESS_RUNNING;
376 *type = GNUNET_OS_PROCESS_EXITED;
386 * @param proc process ID to wait for
387 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
390 GNUNET_OS_process_wait (pid_t proc)
393 if (proc != waitpid (proc, NULL, 0))
394 return GNUNET_SYSERR;
402 h = OpenProcess (PROCESS_QUERY_INFORMATION, FALSE, proc);
403 if (INVALID_HANDLE_VALUE == h)
405 SetErrnoFromWinError (GetLastError ());
406 return GNUNET_SYSERR;
409 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
411 SetErrnoFromWinError (GetLastError ());
424 /* end of os_priority.c */