diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-05-29 00:46:26 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-05-29 00:46:26 +0000 |
commit | 0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9 (patch) | |
tree | 6b552f40eb089db96409a312a98d9b12bd669102 /src/util/os_priority.c | |
download | gnunet-0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9.tar.gz gnunet-0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9.zip |
ng
Diffstat (limited to 'src/util/os_priority.c')
-rw-r--r-- | src/util/os_priority.c | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/src/util/os_priority.c b/src/util/os_priority.c new file mode 100644 index 000000000..7862d68e9 --- /dev/null +++ b/src/util/os_priority.c | |||
@@ -0,0 +1,186 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 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 util/os/priority.c | ||
23 | * @brief Methods to set process priority | ||
24 | * @author Nils Durner | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_common.h" | ||
29 | #include "gnunet_os_lib.h" | ||
30 | |||
31 | /** | ||
32 | * Set our process priority | ||
33 | */ | ||
34 | int | ||
35 | GNUNET_OS_set_process_priority (pid_t proc, | ||
36 | enum GNUNET_SCHEDULER_Priority eprio) | ||
37 | { | ||
38 | int prio = 0; | ||
39 | |||
40 | GNUNET_assert (eprio < GNUNET_SCHEDULER_PRIORITY_COUNT); | ||
41 | if (eprio == GNUNET_SCHEDULER_PRIORITY_KEEP) | ||
42 | return GNUNET_OK; | ||
43 | /* convert to MINGW/Unix values */ | ||
44 | switch (eprio) | ||
45 | { | ||
46 | case GNUNET_SCHEDULER_PRIORITY_DEFAULT: | ||
47 | #ifdef MINGW | ||
48 | prio = NORMAL_PRIORITY_CLASS; | ||
49 | #else | ||
50 | prio = 0; | ||
51 | #endif | ||
52 | break; | ||
53 | case GNUNET_SCHEDULER_PRIORITY_HIGH: | ||
54 | #ifdef MINGW | ||
55 | prio = ABOVE_NORMAL_PRIORITY_CLASS; | ||
56 | #else | ||
57 | prio = -5; | ||
58 | #endif | ||
59 | break; | ||
60 | case GNUNET_SCHEDULER_PRIORITY_BACKGROUND: | ||
61 | #ifdef MINGW | ||
62 | prio = BELOW_NORMAL_PRIORITY_CLASS; | ||
63 | #else | ||
64 | prio = 10; | ||
65 | #endif | ||
66 | break; | ||
67 | case GNUNET_SCHEDULER_PRIORITY_UI: | ||
68 | case GNUNET_SCHEDULER_PRIORITY_URGENT: | ||
69 | #ifdef MINGW | ||
70 | prio = HIGH_PRIORITY_CLASS; | ||
71 | #else | ||
72 | prio = -10; | ||
73 | #endif | ||
74 | break; | ||
75 | case GNUNET_SCHEDULER_PRIORITY_IDLE: | ||
76 | #ifdef MINGW | ||
77 | prio = IDLE_PRIORITY_CLASS; | ||
78 | #else | ||
79 | prio = 19; | ||
80 | #endif | ||
81 | break; | ||
82 | default: | ||
83 | GNUNET_assert (0); | ||
84 | return GNUNET_SYSERR; | ||
85 | } | ||
86 | /* Set process priority */ | ||
87 | #ifdef MINGW | ||
88 | SetPriorityClass (GetCurrentProcess (), prio); | ||
89 | #else | ||
90 | if (proc == getpid ()) | ||
91 | { | ||
92 | errno = 0; | ||
93 | if ((-1 == nice (prio)) && (errno != 0)) | ||
94 | { | ||
95 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | | ||
96 | GNUNET_ERROR_TYPE_BULK, "nice"); | ||
97 | return GNUNET_SYSERR; | ||
98 | } | ||
99 | } | ||
100 | else | ||
101 | { | ||
102 | if (0 != setpriority (PRIO_PROCESS, proc, prio)) | ||
103 | |||
104 | { | ||
105 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | | ||
106 | GNUNET_ERROR_TYPE_BULK, "setpriority"); | ||
107 | return GNUNET_SYSERR; | ||
108 | } | ||
109 | } | ||
110 | #endif | ||
111 | return GNUNET_OK; | ||
112 | } | ||
113 | |||
114 | |||
115 | |||
116 | /** | ||
117 | * Start a process. | ||
118 | * | ||
119 | * @param filename name of the binary | ||
120 | * @param ... NULL-terminated list of arguments to the process | ||
121 | * @return process ID of the new process, -1 on error | ||
122 | */ | ||
123 | pid_t | ||
124 | GNUNET_OS_start_process (const char *filename, ...) | ||
125 | { | ||
126 | pid_t ret; | ||
127 | char **argv; | ||
128 | va_list ap; | ||
129 | int argc; | ||
130 | |||
131 | ret = fork (); | ||
132 | if (ret != 0) | ||
133 | { | ||
134 | if (ret == -1) | ||
135 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork"); | ||
136 | return ret; | ||
137 | } | ||
138 | argc = 0; | ||
139 | va_start (ap, filename); | ||
140 | while (NULL != va_arg (ap, char *)) | ||
141 | argc++; | ||
142 | va_end (ap); | ||
143 | argv = GNUNET_malloc (sizeof (char *) * (argc + 1)); | ||
144 | argc = 0; | ||
145 | va_start (ap, filename); | ||
146 | while (NULL != (argv[argc] = va_arg (ap, char *))) | ||
147 | argc++; | ||
148 | va_end (ap); | ||
149 | execvp (filename, argv); | ||
150 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); | ||
151 | exit (1); | ||
152 | } | ||
153 | |||
154 | |||
155 | |||
156 | |||
157 | /** | ||
158 | * Start a process. | ||
159 | * | ||
160 | * @param filename name of the binary | ||
161 | * @param argv NULL-terminated list of arguments to the process | ||
162 | * @return process ID of the new process, -1 on error | ||
163 | */ | ||
164 | pid_t | ||
165 | GNUNET_OS_start_process_v (const char *filename, char *const argv[]) | ||
166 | { | ||
167 | pid_t ret; | ||
168 | |||
169 | ret = fork (); | ||
170 | if (ret != 0) | ||
171 | { | ||
172 | if (ret == -1) | ||
173 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork"); | ||
174 | return ret; | ||
175 | } | ||
176 | execvp (filename, argv); | ||
177 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "execvp", filename); | ||
178 | exit (1); | ||
179 | } | ||
180 | |||
181 | |||
182 | |||
183 | |||
184 | |||
185 | |||
186 | /* end of os_priority.c */ | ||