diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-11-03 21:26:40 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-11-03 21:26:40 +0000 |
commit | 721e49caeea6ba5073f8bc5c6c08359295c02bb5 (patch) | |
tree | e06e80ba90af91e9452a48a7a5782913199b4877 /src/util | |
parent | 37ac1b7c9e9e05f93d4100cfb53450ec2d370989 (diff) | |
download | gnunet-721e49caeea6ba5073f8bc5c6c08359295c02bb5.tar.gz gnunet-721e49caeea6ba5073f8bc5c6c08359295c02bb5.zip |
original patch from Mantis 1614
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/crypto_random.c | 24 | ||||
-rw-r--r-- | src/util/os_priority.c | 225 | ||||
-rw-r--r-- | src/util/scheduler.c | 2 | ||||
-rw-r--r-- | src/util/test_os_priority.c | 12 | ||||
-rw-r--r-- | src/util/test_os_start_process.c | 10 | ||||
-rw-r--r-- | src/util/test_resolver_api.c | 10 |
6 files changed, 215 insertions, 68 deletions
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c index 4fcb157d9..75435a5e6 100644 --- a/src/util/crypto_random.c +++ b/src/util/crypto_random.c | |||
@@ -188,7 +188,7 @@ GNUNET_CRYPTO_random_disable_entropy_gathering () | |||
188 | * Process ID of the "find" process that we use for | 188 | * Process ID of the "find" process that we use for |
189 | * entropy gathering. | 189 | * entropy gathering. |
190 | */ | 190 | */ |
191 | static pid_t genproc; | 191 | static GNUNET_OS_Process *genproc; |
192 | 192 | ||
193 | /** | 193 | /** |
194 | * Function called by libgcrypt whenever we are | 194 | * Function called by libgcrypt whenever we are |
@@ -206,16 +206,17 @@ entropy_generator (void *cls, | |||
206 | return; | 206 | return; |
207 | if (current == total) | 207 | if (current == total) |
208 | { | 208 | { |
209 | if (genproc != 0) | 209 | if (genproc != NULL) |
210 | { | 210 | { |
211 | if (0 != PLIBC_KILL (genproc, SIGTERM)) | 211 | if (0 != GNUNET_OS_process_kill (genproc, SIGTERM)) |
212 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill"); | 212 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill"); |
213 | GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc)); | 213 | GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc)); |
214 | genproc = 0; | 214 | GNUNET_OS_process_close (genproc); |
215 | genproc = NULL; | ||
215 | } | 216 | } |
216 | return; | 217 | return; |
217 | } | 218 | } |
218 | if (genproc != 0) | 219 | if (genproc != NULL) |
219 | { | 220 | { |
220 | ret = GNUNET_OS_process_status (genproc, &type, &code); | 221 | ret = GNUNET_OS_process_status (genproc, &type, &code); |
221 | if (ret == GNUNET_NO) | 222 | if (ret == GNUNET_NO) |
@@ -225,10 +226,11 @@ entropy_generator (void *cls, | |||
225 | GNUNET_break (0); | 226 | GNUNET_break (0); |
226 | return; | 227 | return; |
227 | } | 228 | } |
228 | if (0 != PLIBC_KILL (genproc, SIGTERM)) | 229 | if (0 != GNUNET_OS_process_kill (genproc, SIGTERM)) |
229 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill"); | 230 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill"); |
230 | GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc)); | 231 | GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc)); |
231 | genproc = 0; | 232 | GNUNET_OS_process_close (genproc); |
233 | genproc = NULL; | ||
232 | } | 234 | } |
233 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 235 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
234 | _("Starting `%s' process to generate entropy\n"), "find"); | 236 | _("Starting `%s' process to generate entropy\n"), "find"); |
@@ -243,10 +245,11 @@ entropy_generator (void *cls, | |||
243 | static void | 245 | static void |
244 | killfind () | 246 | killfind () |
245 | { | 247 | { |
246 | if (genproc != 0) | 248 | if (genproc != NULL) |
247 | { | 249 | { |
248 | PLIBC_KILL (genproc, SIGKILL); | 250 | GNUNET_OS_process_kill (genproc, SIGKILL); |
249 | genproc = 0; | 251 | GNUNET_OS_process_close (genproc); |
252 | genproc = NULL; | ||
250 | } | 253 | } |
251 | } | 254 | } |
252 | 255 | ||
@@ -279,3 +282,4 @@ void __attribute__ ((destructor)) GNUNET_CRYPTO_random_fini () | |||
279 | 282 | ||
280 | 283 | ||
281 | /* end of crypto_random.c */ | 284 | /* end of crypto_random.c */ |
285 | |||
diff --git a/src/util/os_priority.c b/src/util/os_priority.c index e2e170cb9..b9e5f010b 100644 --- a/src/util/os_priority.c +++ b/src/util/os_priority.c | |||
@@ -29,6 +29,112 @@ | |||
29 | #include "gnunet_os_lib.h" | 29 | #include "gnunet_os_lib.h" |
30 | #include "disk.h" | 30 | #include "disk.h" |
31 | 31 | ||
32 | struct _GNUNET_OS_Process | ||
33 | { | ||
34 | pid_t pid; | ||
35 | #if WINDOWS | ||
36 | HANDLE handle; | ||
37 | #endif | ||
38 | }; | ||
39 | |||
40 | static GNUNET_OS_Process current_process; | ||
41 | |||
42 | GNUNET_OS_Process * | ||
43 | GNUNET_OS_process_alloc () | ||
44 | { | ||
45 | GNUNET_OS_Process *ret = GNUNET_malloc (sizeof (GNUNET_OS_Process)); | ||
46 | ret->pid = 0; | ||
47 | #if WINDOWS | ||
48 | ret->handle = NULL; | ||
49 | #endif | ||
50 | return ret; | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Get process structure for current process | ||
55 | * | ||
56 | * The pointer it returns points to static memory location and must not be | ||
57 | * deallocated/closed | ||
58 | * | ||
59 | * @return pointer to the process sturcutre for this process | ||
60 | */ | ||
61 | GNUNET_OS_Process * | ||
62 | GNUNET_OS_process_current () | ||
63 | { | ||
64 | #if WINDOWS | ||
65 | current_process.pid = GetCurrentProcessId (); | ||
66 | current_process.handle = GetCurrentProcess (); | ||
67 | #else | ||
68 | current_process.pid = 0; | ||
69 | #endif | ||
70 | return ¤t_process; | ||
71 | } | ||
72 | |||
73 | int | ||
74 | GNUNET_OS_process_kill (GNUNET_OS_Process *proc, int sig) | ||
75 | { | ||
76 | #if WINDOWS | ||
77 | if (sig == SIGKILL || sig == SIGTERM) | ||
78 | { | ||
79 | HANDLE h = GNUNET_OS_process_get_handle (proc); | ||
80 | if (NULL == h) | ||
81 | { | ||
82 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n", GNUNET_OS_process_get_pid (proc), h); | ||
83 | return -1; | ||
84 | } | ||
85 | if (!TerminateProcess (h, 0)) | ||
86 | { | ||
87 | SetErrnoFromWinError (GetLastError ()); | ||
88 | return -1; | ||
89 | } | ||
90 | else | ||
91 | return 0; | ||
92 | } | ||
93 | errno = EINVAL; | ||
94 | return -1; | ||
95 | #else | ||
96 | return kill (GNUNET_OS_process_get_pid (proc), sig); | ||
97 | #endif | ||
98 | } | ||
99 | |||
100 | pid_t | ||
101 | GNUNET_OS_process_get_pid (GNUNET_OS_Process *proc) | ||
102 | { | ||
103 | return proc->pid; | ||
104 | } | ||
105 | |||
106 | void | ||
107 | GNUNET_OS_process_set_pid (GNUNET_OS_Process *proc, pid_t pid) | ||
108 | { | ||
109 | proc->pid = pid; | ||
110 | } | ||
111 | |||
112 | #if WINDOWS | ||
113 | HANDLE | ||
114 | GNUNET_OS_process_get_handle (GNUNET_OS_Process *proc) | ||
115 | { | ||
116 | return proc->handle; | ||
117 | } | ||
118 | |||
119 | void | ||
120 | GNUNET_OS_process_set_handle(GNUNET_OS_Process *proc, HANDLE handle) | ||
121 | { | ||
122 | if (proc->handle != NULL) | ||
123 | CloseHandle (proc->handle); | ||
124 | proc->handle = handle; | ||
125 | } | ||
126 | #endif | ||
127 | |||
128 | void | ||
129 | GNUNET_OS_process_close (GNUNET_OS_Process *proc) | ||
130 | { | ||
131 | #if WINDOWS | ||
132 | if (proc->handle != NULL) | ||
133 | CloseHandle (proc->handle); | ||
134 | #endif | ||
135 | GNUNET_free (proc); | ||
136 | } | ||
137 | |||
32 | #if WINDOWS | 138 | #if WINDOWS |
33 | #include "gnunet_signal_lib.h" | 139 | #include "gnunet_signal_lib.h" |
34 | 140 | ||
@@ -36,17 +142,17 @@ extern GNUNET_SIGNAL_Handler w32_sigchld_handler; | |||
36 | 142 | ||
37 | /** | 143 | /** |
38 | * @brief Waits for a process to terminate and invokes the SIGCHLD handler | 144 | * @brief Waits for a process to terminate and invokes the SIGCHLD handler |
39 | * @param h handle to the process | 145 | * @param proc pointer to process structure |
40 | */ | 146 | */ |
41 | static DWORD WINAPI | 147 | static DWORD WINAPI |
42 | ChildWaitThread (HANDLE h) | 148 | ChildWaitThread (void *arg) |
43 | { | 149 | { |
44 | WaitForSingleObject (h, INFINITE); | 150 | GNUNET_OS_Process *proc = (GNUNET_OS_Process *) arg; |
151 | WaitForSingleObject (proc->handle, INFINITE); | ||
45 | 152 | ||
46 | if (w32_sigchld_handler) | 153 | if (w32_sigchld_handler) |
47 | w32_sigchld_handler (); | 154 | w32_sigchld_handler (); |
48 | 155 | ||
49 | CloseHandle (h); | ||
50 | return 0; | 156 | return 0; |
51 | } | 157 | } |
52 | #endif | 158 | #endif |
@@ -54,19 +160,21 @@ ChildWaitThread (HANDLE h) | |||
54 | /** | 160 | /** |
55 | * Set process priority | 161 | * Set process priority |
56 | * | 162 | * |
57 | * @param proc id of the process | 163 | * @param proc pointer to process structure |
58 | * @param prio priority value | 164 | * @param prio priority value |
59 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | 165 | * @return GNUNET_OK on success, GNUNET_SYSERR on error |
60 | */ | 166 | */ |
61 | int | 167 | int |
62 | GNUNET_OS_set_process_priority (pid_t proc, | 168 | GNUNET_OS_set_process_priority (GNUNET_OS_Process *proc, |
63 | enum GNUNET_SCHEDULER_Priority prio) | 169 | enum GNUNET_SCHEDULER_Priority prio) |
64 | { | 170 | { |
65 | int rprio; | 171 | int rprio; |
172 | pid_t pid; | ||
66 | 173 | ||
67 | GNUNET_assert (prio < GNUNET_SCHEDULER_PRIORITY_COUNT); | 174 | GNUNET_assert (prio < GNUNET_SCHEDULER_PRIORITY_COUNT); |
68 | if (prio == GNUNET_SCHEDULER_PRIORITY_KEEP) | 175 | if (prio == GNUNET_SCHEDULER_PRIORITY_KEEP) |
69 | return GNUNET_OK; | 176 | return GNUNET_OK; |
177 | |||
70 | /* convert to MINGW/Unix values */ | 178 | /* convert to MINGW/Unix values */ |
71 | switch (prio) | 179 | switch (prio) |
72 | { | 180 | { |
@@ -114,12 +222,19 @@ GNUNET_OS_set_process_priority (pid_t proc, | |||
114 | GNUNET_assert (0); | 222 | GNUNET_assert (0); |
115 | return GNUNET_SYSERR; | 223 | return GNUNET_SYSERR; |
116 | } | 224 | } |
225 | |||
226 | pid = GNUNET_OS_process_get_pid (proc); | ||
227 | |||
117 | /* Set process priority */ | 228 | /* Set process priority */ |
118 | #ifdef MINGW | 229 | #ifdef MINGW |
119 | SetPriorityClass (GetCurrentProcess (), rprio); | 230 | { |
231 | HANDLE h = GNUNET_OS_process_get_handle (proc); | ||
232 | GNUNET_assert (h != NULL); | ||
233 | SetPriorityClass (h, rprio); | ||
234 | } | ||
120 | #elif LINUX | 235 | #elif LINUX |
121 | if ( (0 == proc) || | 236 | if ( (0 == pid) || |
122 | (proc == getpid () ) ) | 237 | (pid == getpid () ) ) |
123 | { | 238 | { |
124 | int have = nice (0); | 239 | int have = nice (0); |
125 | int delta = rprio - have; | 240 | int delta = rprio - have; |
@@ -135,7 +250,7 @@ GNUNET_OS_set_process_priority (pid_t proc, | |||
135 | } | 250 | } |
136 | else | 251 | else |
137 | { | 252 | { |
138 | if (0 != setpriority (PRIO_PROCESS, proc, rprio)) | 253 | if (0 != setpriority (PRIO_PROCESS, pid, rprio)) |
139 | 254 | ||
140 | { | 255 | { |
141 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | | 256 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | |
@@ -157,18 +272,18 @@ GNUNET_OS_set_process_priority (pid_t proc, | |||
157 | * @param pipe_stdout pipe to use to get output from child process (or NULL) | 272 | * @param pipe_stdout pipe to use to get output from child process (or NULL) |
158 | * @param filename name of the binary | 273 | * @param filename name of the binary |
159 | * @param ... NULL-terminated list of arguments to the process | 274 | * @param ... NULL-terminated list of arguments to the process |
160 | * @return process ID of the new process, -1 on error | 275 | * @return pointer to process structure of the new process, NULL on error |
161 | */ | 276 | */ |
162 | pid_t | 277 | GNUNET_OS_Process * |
163 | GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, | 278 | GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, |
164 | struct GNUNET_DISK_PipeHandle *pipe_stdout, | 279 | struct GNUNET_DISK_PipeHandle *pipe_stdout, |
165 | const char *filename, ...) | 280 | const char *filename, ...) |
166 | { | 281 | { |
167 | /* FIXME: Make this work on windows!!! */ | ||
168 | va_list ap; | 282 | va_list ap; |
169 | 283 | ||
170 | #ifndef MINGW | 284 | #ifndef MINGW |
171 | pid_t ret; | 285 | pid_t ret; |
286 | GNUNET_OS_Process *gnunet_proc = NULL; | ||
172 | char **argv; | 287 | char **argv; |
173 | int argc; | 288 | int argc; |
174 | int fd_stdout_write; | 289 | int fd_stdout_write; |
@@ -227,9 +342,11 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, | |||
227 | GNUNET_DISK_pipe_close_end(pipe_stdin, GNUNET_DISK_PIPE_END_READ); | 342 | GNUNET_DISK_pipe_close_end(pipe_stdin, GNUNET_DISK_PIPE_END_READ); |
228 | sleep (1); | 343 | sleep (1); |
229 | #endif | 344 | #endif |
345 | gnunet_proc = GNUNET_OS_process_alloc (); | ||
346 | GNUNET_OS_process_set_pid (gnunet_proc, ret); | ||
230 | } | 347 | } |
231 | GNUNET_free (argv); | 348 | GNUNET_free (argv); |
232 | return ret; | 349 | return gnunet_proc; |
233 | } | 350 | } |
234 | 351 | ||
235 | if (pipe_stdout != NULL) | 352 | if (pipe_stdout != NULL) |
@@ -258,6 +375,7 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, | |||
258 | int findresult; | 375 | int findresult; |
259 | STARTUPINFO start; | 376 | STARTUPINFO start; |
260 | PROCESS_INFORMATION proc; | 377 | PROCESS_INFORMATION proc; |
378 | GNUNET_OS_Process *gnunet_proc = NULL; | ||
261 | 379 | ||
262 | HANDLE stdin_handle; | 380 | HANDLE stdin_handle; |
263 | HANDLE stdout_handle; | 381 | HANDLE stdout_handle; |
@@ -299,7 +417,7 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, | |||
299 | { | 417 | { |
300 | SetErrnoFromWinError (GetLastError ()); | 418 | SetErrnoFromWinError (GetLastError ()); |
301 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "FindExecutable", filename); | 419 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "FindExecutable", filename); |
302 | return (pid_t) -1; | 420 | return NULL; |
303 | } | 421 | } |
304 | 422 | ||
305 | if (!CreateProcessA | 423 | if (!CreateProcessA |
@@ -308,16 +426,20 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, | |||
308 | { | 426 | { |
309 | SetErrnoFromWinError (GetLastError ()); | 427 | SetErrnoFromWinError (GetLastError ()); |
310 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "CreateProcess", path); | 428 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "CreateProcess", path); |
311 | return (pid_t) -1; | 429 | return NULL; |
312 | } | 430 | } |
313 | 431 | ||
314 | CreateThread (NULL, 64000, ChildWaitThread, proc.hProcess, 0, NULL); | 432 | gnunet_proc = GNUNET_OS_process_alloc (); |
433 | GNUNET_OS_process_set_pid (gnunet_proc, proc.dwProcessId); | ||
434 | GNUNET_OS_process_set_handle (gnunet_proc, proc.hProcess); | ||
435 | |||
436 | CreateThread (NULL, 64000, ChildWaitThread, (void *) gnunet_proc, 0, NULL); | ||
315 | 437 | ||
316 | CloseHandle (proc.hThread); | 438 | CloseHandle (proc.hThread); |
317 | 439 | ||
318 | GNUNET_free (cmd); | 440 | GNUNET_free (cmd); |
319 | 441 | ||
320 | return proc.dwProcessId; | 442 | return gnunet_proc; |
321 | #endif | 443 | #endif |
322 | 444 | ||
323 | } | 445 | } |
@@ -333,7 +455,7 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, | |||
333 | * @param argv NULL-terminated list of arguments to the process | 455 | * @param argv NULL-terminated list of arguments to the process |
334 | * @return process ID of the new process, -1 on error | 456 | * @return process ID of the new process, -1 on error |
335 | */ | 457 | */ |
336 | pid_t | 458 | GNUNET_OS_Process * |
337 | GNUNET_OS_start_process_v (const int *lsocks, | 459 | GNUNET_OS_start_process_v (const int *lsocks, |
338 | const char *filename, char *const argv[]) | 460 | const char *filename, char *const argv[]) |
339 | { | 461 | { |
@@ -341,6 +463,7 @@ GNUNET_OS_start_process_v (const int *lsocks, | |||
341 | pid_t ret; | 463 | pid_t ret; |
342 | char lpid[16]; | 464 | char lpid[16]; |
343 | char fds[16]; | 465 | char fds[16]; |
466 | GNUNET_OS_Process *gnunet_proc = NULL; | ||
344 | int i; | 467 | int i; |
345 | int j; | 468 | int j; |
346 | int k; | 469 | int k; |
@@ -382,9 +505,11 @@ GNUNET_OS_start_process_v (const int *lsocks, | |||
382 | be plenty in practice */ | 505 | be plenty in practice */ |
383 | sleep (1); | 506 | sleep (1); |
384 | #endif | 507 | #endif |
508 | gnunet_proc = GNUNET_OS_process_alloc (); | ||
509 | GNUNET_OS_process_set_pid (gnunet_proc, ret); | ||
385 | } | 510 | } |
386 | GNUNET_array_grow (lscp, ls, 0); | 511 | GNUNET_array_grow (lscp, ls, 0); |
387 | return ret; | 512 | return gnunet_proc; |
388 | } | 513 | } |
389 | if (lscp != NULL) | 514 | if (lscp != NULL) |
390 | { | 515 | { |
@@ -441,6 +566,7 @@ GNUNET_OS_start_process_v (const int *lsocks, | |||
441 | int argcount = 0; | 566 | int argcount = 0; |
442 | char *non_const_filename = NULL; | 567 | char *non_const_filename = NULL; |
443 | int filenamelen = 0; | 568 | int filenamelen = 0; |
569 | GNUNET_OS_Process *gnunet_proc = NULL; | ||
444 | 570 | ||
445 | GNUNET_assert (lsocks == NULL); | 571 | GNUNET_assert (lsocks == NULL); |
446 | /* Count the number of arguments */ | 572 | /* Count the number of arguments */ |
@@ -504,11 +630,15 @@ GNUNET_OS_start_process_v (const int *lsocks, | |||
504 | &proc)) | 630 | &proc)) |
505 | { | 631 | { |
506 | SetErrnoFromWinError (GetLastError ()); | 632 | SetErrnoFromWinError (GetLastError ()); |
507 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork"); | 633 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "CreateProcess"); |
508 | return -1; | 634 | return NULL; |
509 | } | 635 | } |
510 | 636 | ||
511 | CreateThread (NULL, 64000, ChildWaitThread, proc.hProcess, 0, NULL); | 637 | gnunet_proc = GNUNET_OS_process_alloc (); |
638 | GNUNET_OS_process_set_pid (gnunet_proc, proc.dwProcessId); | ||
639 | GNUNET_OS_process_set_handle (gnunet_proc, proc.hProcess); | ||
640 | |||
641 | CreateThread (NULL, 64000, ChildWaitThread, (void *) gnunet_proc, 0, NULL); | ||
512 | 642 | ||
513 | CloseHandle (proc.hThread); | 643 | CloseHandle (proc.hThread); |
514 | GNUNET_free (cmd); | 644 | GNUNET_free (cmd); |
@@ -517,7 +647,7 @@ GNUNET_OS_start_process_v (const int *lsocks, | |||
517 | GNUNET_free (non_const_argv[--argcount]); | 647 | GNUNET_free (non_const_argv[--argcount]); |
518 | GNUNET_free (non_const_argv); | 648 | GNUNET_free (non_const_argv); |
519 | 649 | ||
520 | return proc.dwProcessId; | 650 | return gnunet_proc; |
521 | #endif | 651 | #endif |
522 | } | 652 | } |
523 | 653 | ||
@@ -529,7 +659,7 @@ GNUNET_OS_start_process_v (const int *lsocks, | |||
529 | * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise | 659 | * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise |
530 | */ | 660 | */ |
531 | int | 661 | int |
532 | GNUNET_OS_process_status (pid_t proc, enum GNUNET_OS_ProcessStatusType *type, | 662 | GNUNET_OS_process_status (GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, |
533 | unsigned long *code) | 663 | unsigned long *code) |
534 | { | 664 | { |
535 | #ifndef MINGW | 665 | #ifndef MINGW |
@@ -537,7 +667,7 @@ GNUNET_OS_process_status (pid_t proc, enum GNUNET_OS_ProcessStatusType *type, | |||
537 | int ret; | 667 | int ret; |
538 | 668 | ||
539 | GNUNET_assert (0 != proc); | 669 | GNUNET_assert (0 != proc); |
540 | ret = waitpid (proc, &status, WNOHANG); | 670 | ret = waitpid (GNUNET_OS_process_get_pid (proc), &status, WNOHANG); |
541 | if (ret < 0) | 671 | if (ret < 0) |
542 | { | 672 | { |
543 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); | 673 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); |
@@ -549,7 +679,7 @@ GNUNET_OS_process_status (pid_t proc, enum GNUNET_OS_ProcessStatusType *type, | |||
549 | *code = 0; | 679 | *code = 0; |
550 | return GNUNET_NO; | 680 | return GNUNET_NO; |
551 | } | 681 | } |
552 | if (proc != ret) | 682 | if (GNUNET_OS_process_get_pid (proc) != ret) |
553 | { | 683 | { |
554 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); | 684 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); |
555 | return GNUNET_SYSERR; | 685 | return GNUNET_SYSERR; |
@@ -583,27 +713,35 @@ GNUNET_OS_process_status (pid_t proc, enum GNUNET_OS_ProcessStatusType *type, | |||
583 | } | 713 | } |
584 | #else | 714 | #else |
585 | HANDLE h; | 715 | HANDLE h; |
586 | DWORD c; | 716 | DWORD c, error_code, ret; |
587 | 717 | ||
588 | h = OpenProcess (PROCESS_QUERY_INFORMATION, FALSE, proc); | 718 | h = GNUNET_OS_process_get_handle (proc); |
589 | if (INVALID_HANDLE_VALUE == h) | 719 | ret = GNUNET_OS_process_get_pid (proc); |
720 | if (h == NULL || ret == 0) | ||
590 | { | 721 | { |
591 | SetErrnoFromWinError (GetLastError ()); | 722 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n", ret, h); |
592 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "OpenProcess"); | ||
593 | return GNUNET_SYSERR; | 723 | return GNUNET_SYSERR; |
594 | } | 724 | } |
725 | if (h == NULL) | ||
726 | h = GetCurrentProcess (); | ||
595 | 727 | ||
596 | c = GetExitCodeProcess (h, &c); | 728 | SetLastError (0); |
729 | ret = GetExitCodeProcess (h, &c); | ||
730 | error_code = GetLastError (); | ||
731 | if (ret == 0 || error_code != NO_ERROR) | ||
732 | { | ||
733 | SetErrnoFromWinError (error_code); | ||
734 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess"); | ||
735 | return GNUNET_SYSERR; | ||
736 | } | ||
597 | if (STILL_ACTIVE == c) | 737 | if (STILL_ACTIVE == c) |
598 | { | 738 | { |
599 | *type = GNUNET_OS_PROCESS_RUNNING; | 739 | *type = GNUNET_OS_PROCESS_RUNNING; |
600 | *code = 0; | 740 | *code = 0; |
601 | CloseHandle (h); | ||
602 | return GNUNET_NO; | 741 | return GNUNET_NO; |
603 | } | 742 | } |
604 | *type = GNUNET_OS_PROCESS_EXITED; | 743 | *type = GNUNET_OS_PROCESS_EXITED; |
605 | *code = c; | 744 | *code = c; |
606 | CloseHandle (h); | ||
607 | #endif | 745 | #endif |
608 | 746 | ||
609 | return GNUNET_OK; | 747 | return GNUNET_OK; |
@@ -611,14 +749,15 @@ GNUNET_OS_process_status (pid_t proc, enum GNUNET_OS_ProcessStatusType *type, | |||
611 | 749 | ||
612 | /** | 750 | /** |
613 | * Wait for a process | 751 | * Wait for a process |
614 | * @param proc process ID to wait for | 752 | * @param proc pointer to process structure |
615 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | 753 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise |
616 | */ | 754 | */ |
617 | int | 755 | int |
618 | GNUNET_OS_process_wait (pid_t proc) | 756 | GNUNET_OS_process_wait (GNUNET_OS_Process *proc) |
619 | { | 757 | { |
758 | pid_t pid = GNUNET_OS_process_get_pid (proc); | ||
620 | #ifndef MINGW | 759 | #ifndef MINGW |
621 | if (proc != waitpid (proc, NULL, 0)) | 760 | if (pid != waitpid (pid, NULL, 0)) |
622 | return GNUNET_SYSERR; | 761 | return GNUNET_SYSERR; |
623 | 762 | ||
624 | return GNUNET_OK; | 763 | return GNUNET_OK; |
@@ -626,12 +765,14 @@ GNUNET_OS_process_wait (pid_t proc) | |||
626 | HANDLE h; | 765 | HANDLE h; |
627 | int ret; | 766 | int ret; |
628 | 767 | ||
629 | h = OpenProcess (PROCESS_QUERY_INFORMATION, FALSE, proc); | 768 | h = GNUNET_OS_process_get_handle (proc); |
630 | if (INVALID_HANDLE_VALUE == h) | 769 | if (NULL == h) |
631 | { | 770 | { |
632 | SetErrnoFromWinError (GetLastError ()); | 771 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n", pid, h); |
633 | return GNUNET_SYSERR; | 772 | return GNUNET_SYSERR; |
634 | } | 773 | } |
774 | if (h == NULL) | ||
775 | h = GetCurrentProcess (); | ||
635 | 776 | ||
636 | if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE)) | 777 | if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE)) |
637 | { | 778 | { |
@@ -641,8 +782,6 @@ GNUNET_OS_process_wait (pid_t proc) | |||
641 | else | 782 | else |
642 | ret = GNUNET_OK; | 783 | ret = GNUNET_OK; |
643 | 784 | ||
644 | CloseHandle (h); | ||
645 | |||
646 | return ret; | 785 | return ret; |
647 | #endif | 786 | #endif |
648 | } | 787 | } |
diff --git a/src/util/scheduler.c b/src/util/scheduler.c index 67406e21c..35e0a70c9 100644 --- a/src/util/scheduler.c +++ b/src/util/scheduler.c | |||
@@ -633,7 +633,7 @@ run_ready (struct GNUNET_SCHEDULER_Handle *sched, | |||
633 | if (sched->current_priority != pos->priority) | 633 | if (sched->current_priority != pos->priority) |
634 | { | 634 | { |
635 | sched->current_priority = pos->priority; | 635 | sched->current_priority = pos->priority; |
636 | (void) GNUNET_OS_set_process_priority (0, pos->priority); | 636 | (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), pos->priority); |
637 | } | 637 | } |
638 | sched->active_task = pos; | 638 | sched->active_task = pos; |
639 | #if PROFILE_DELAYS | 639 | #if PROFILE_DELAYS |
diff --git a/src/util/test_os_priority.c b/src/util/test_os_priority.c index c2fdcc6f5..17874a6ee 100644 --- a/src/util/test_os_priority.c +++ b/src/util/test_os_priority.c | |||
@@ -32,27 +32,27 @@ testprio () | |||
32 | { | 32 | { |
33 | pid_t child; | 33 | pid_t child; |
34 | if (GNUNET_OK != | 34 | if (GNUNET_OK != |
35 | GNUNET_OS_set_process_priority (getpid (), | 35 | GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), |
36 | GNUNET_SCHEDULER_PRIORITY_DEFAULT)) | 36 | GNUNET_SCHEDULER_PRIORITY_DEFAULT)) |
37 | return 1; | 37 | return 1; |
38 | if (GNUNET_OK != | 38 | if (GNUNET_OK != |
39 | GNUNET_OS_set_process_priority (getpid (), | 39 | GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), |
40 | GNUNET_SCHEDULER_PRIORITY_UI)) | 40 | GNUNET_SCHEDULER_PRIORITY_UI)) |
41 | return 1; | 41 | return 1; |
42 | if (GNUNET_OK != | 42 | if (GNUNET_OK != |
43 | GNUNET_OS_set_process_priority (getpid (), | 43 | GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), |
44 | GNUNET_SCHEDULER_PRIORITY_IDLE)) | 44 | GNUNET_SCHEDULER_PRIORITY_IDLE)) |
45 | return 1; | 45 | return 1; |
46 | if (GNUNET_OK != | 46 | if (GNUNET_OK != |
47 | GNUNET_OS_set_process_priority (getpid (), | 47 | GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), |
48 | GNUNET_SCHEDULER_PRIORITY_BACKGROUND)) | 48 | GNUNET_SCHEDULER_PRIORITY_BACKGROUND)) |
49 | return 1; | 49 | return 1; |
50 | if (GNUNET_OK != | 50 | if (GNUNET_OK != |
51 | GNUNET_OS_set_process_priority (getpid (), | 51 | GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), |
52 | GNUNET_SCHEDULER_PRIORITY_HIGH)) | 52 | GNUNET_SCHEDULER_PRIORITY_HIGH)) |
53 | return 1; | 53 | return 1; |
54 | if (GNUNET_OK != | 54 | if (GNUNET_OK != |
55 | GNUNET_OS_set_process_priority (getpid (), | 55 | GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), |
56 | GNUNET_SCHEDULER_PRIORITY_HIGH)) | 56 | GNUNET_SCHEDULER_PRIORITY_HIGH)) |
57 | return 1; | 57 | return 1; |
58 | #ifndef MINGW | 58 | #ifndef MINGW |
diff --git a/src/util/test_os_start_process.c b/src/util/test_os_start_process.c index 0b8860c7d..bd2eb2b09 100644 --- a/src/util/test_os_start_process.c +++ b/src/util/test_os_start_process.c | |||
@@ -38,7 +38,7 @@ | |||
38 | static char *test_phrase = "HELLO WORLD"; | 38 | static char *test_phrase = "HELLO WORLD"; |
39 | static int ok; | 39 | static int ok; |
40 | 40 | ||
41 | static pid_t pid; | 41 | static GNUNET_OS_Process *proc; |
42 | /* Pipe to write to started processes stdin (on write end) */ | 42 | /* Pipe to write to started processes stdin (on write end) */ |
43 | static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin; | 43 | static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin; |
44 | /* Pipe to read from started processes stdout (on read end) */ | 44 | /* Pipe to read from started processes stdout (on read end) */ |
@@ -50,11 +50,13 @@ static void | |||
50 | end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 50 | end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
51 | { | 51 | { |
52 | 52 | ||
53 | if (0 != PLIBC_KILL (pid, SIGTERM)) | 53 | if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) |
54 | { | 54 | { |
55 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | 55 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); |
56 | } | 56 | } |
57 | GNUNET_OS_process_wait (pid); | 57 | GNUNET_OS_process_wait (proc); |
58 | GNUNET_OS_process_close (proc); | ||
59 | proc = NULL; | ||
58 | GNUNET_DISK_pipe_close(hello_pipe_stdout); | 60 | GNUNET_DISK_pipe_close(hello_pipe_stdout); |
59 | GNUNET_DISK_pipe_close(hello_pipe_stdin); | 61 | GNUNET_DISK_pipe_close(hello_pipe_stdin); |
60 | } | 62 | } |
@@ -119,7 +121,7 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
119 | return; | 121 | return; |
120 | } | 122 | } |
121 | 123 | ||
122 | pid = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn, | 124 | proc = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn, |
123 | "test_gnunet_echo_hello", "-", NULL); | 125 | "test_gnunet_echo_hello", "-", NULL); |
124 | GNUNET_free (fn); | 126 | GNUNET_free (fn); |
125 | 127 | ||
diff --git a/src/util/test_resolver_api.c b/src/util/test_resolver_api.c index 2c3f95367..7321ed587 100644 --- a/src/util/test_resolver_api.c +++ b/src/util/test_resolver_api.c | |||
@@ -360,7 +360,7 @@ check() | |||
360 | int ok = 1 + 2 + 4 + 8; | 360 | int ok = 1 + 2 + 4 + 8; |
361 | char *fn; | 361 | char *fn; |
362 | char *pfx; | 362 | char *pfx; |
363 | pid_t pid; | 363 | GNUNET_OS_Process *proc; |
364 | char * const argv[] = | 364 | char * const argv[] = |
365 | { "test-resolver-api", "-c", "test_resolver_api_data.conf", | 365 | { "test-resolver-api", "-c", "test_resolver_api_data.conf", |
366 | #if VERBOSE | 366 | #if VERBOSE |
@@ -372,7 +372,7 @@ check() | |||
372 | pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR); | 372 | pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR); |
373 | GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR); | 373 | GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR); |
374 | GNUNET_free(pfx); | 374 | GNUNET_free(pfx); |
375 | pid = GNUNET_OS_start_process(NULL, NULL, fn, "gnunet-service-resolver", | 375 | proc = GNUNET_OS_start_process(NULL, NULL, fn, "gnunet-service-resolver", |
376 | #if VERBOSE | 376 | #if VERBOSE |
377 | "-L", "DEBUG", | 377 | "-L", "DEBUG", |
378 | #endif | 378 | #endif |
@@ -380,12 +380,14 @@ check() | |||
380 | GNUNET_free(fn); | 380 | GNUNET_free(fn); |
381 | GNUNET_assert(GNUNET_OK == GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *)) | 381 | GNUNET_assert(GNUNET_OK == GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *)) |
382 | - 1, argv, "test-resolver-api", "nohelp", options, &run, &ok)); | 382 | - 1, argv, "test-resolver-api", "nohelp", options, &run, &ok)); |
383 | if (0 != PLIBC_KILL(pid, SIGTERM)) | 383 | if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) |
384 | { | 384 | { |
385 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); | 385 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); |
386 | ok = 1; | 386 | ok = 1; |
387 | } | 387 | } |
388 | GNUNET_OS_process_wait(pid); | 388 | GNUNET_OS_process_wait (proc); |
389 | GNUNET_OS_process_close (proc); | ||
390 | proc = NULL; | ||
389 | if (ok != 0) | 391 | if (ok != 0) |
390 | fprintf(stderr, "Missed some resolutions: %u\n", ok); | 392 | fprintf(stderr, "Missed some resolutions: %u\n", ok); |
391 | return ok; | 393 | return ok; |