aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-11-03 21:38:45 +0000
committerChristian Grothoff <christian@grothoff.org>2010-11-03 21:38:45 +0000
commitd8abe51562c11473ebcb823ad67c529be2c9dc92 (patch)
tree95f6f53b8060d72c7c0b206c728ab03d73590292 /src/util
parent721e49caeea6ba5073f8bc5c6c08359295c02bb5 (diff)
downloadgnunet-d8abe51562c11473ebcb823ad67c529be2c9dc92.tar.gz
gnunet-d8abe51562c11473ebcb823ad67c529be2c9dc92.zip
style improvments wrt Mantis 1614 patch
Diffstat (limited to 'src/util')
-rw-r--r--src/util/crypto_random.c2
-rw-r--r--src/util/os_priority.c128
-rw-r--r--src/util/test_os_start_process.c2
-rw-r--r--src/util/test_resolver_api.c2
4 files changed, 57 insertions, 77 deletions
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c
index 75435a5e6..622817492 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 */
191static GNUNET_OS_Process *genproc; 191static struct GNUNET_OS_Process *genproc;
192 192
193/** 193/**
194 * Function called by libgcrypt whenever we are 194 * Function called by libgcrypt whenever we are
diff --git a/src/util/os_priority.c b/src/util/os_priority.c
index b9e5f010b..5f78e2179 100644
--- a/src/util/os_priority.c
+++ b/src/util/os_priority.c
@@ -29,7 +29,7 @@
29#include "gnunet_os_lib.h" 29#include "gnunet_os_lib.h"
30#include "disk.h" 30#include "disk.h"
31 31
32struct _GNUNET_OS_Process 32struct GNUNET_OS_Process
33{ 33{
34 pid_t pid; 34 pid_t pid;
35#if WINDOWS 35#if WINDOWS
@@ -37,18 +37,19 @@ struct _GNUNET_OS_Process
37#endif 37#endif
38}; 38};
39 39
40static GNUNET_OS_Process current_process; 40static struct GNUNET_OS_Process current_process;
41
41 42
42GNUNET_OS_Process *
43GNUNET_OS_process_alloc ()
44{
45 GNUNET_OS_Process *ret = GNUNET_malloc (sizeof (GNUNET_OS_Process));
46 ret->pid = 0;
47#if WINDOWS 43#if WINDOWS
48 ret->handle = NULL; 44void
49#endif 45GNUNET_OS_process_set_handle(struct GNUNET_OS_Process *proc, HANDLE handle)
50 return ret; 46{
47 if (proc->handle != NULL)
48 CloseHandle (proc->handle);
49 proc->handle = handle;
51} 50}
51#endif
52
52 53
53/** 54/**
54 * Get process structure for current process 55 * Get process structure for current process
@@ -58,7 +59,7 @@ GNUNET_OS_process_alloc ()
58 * 59 *
59 * @return pointer to the process sturcutre for this process 60 * @return pointer to the process sturcutre for this process
60 */ 61 */
61GNUNET_OS_Process * 62struct GNUNET_OS_Process *
62GNUNET_OS_process_current () 63GNUNET_OS_process_current ()
63{ 64{
64#if WINDOWS 65#if WINDOWS
@@ -71,7 +72,7 @@ GNUNET_OS_process_current ()
71} 72}
72 73
73int 74int
74GNUNET_OS_process_kill (GNUNET_OS_Process *proc, int sig) 75GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
75{ 76{
76#if WINDOWS 77#if WINDOWS
77 if (sig == SIGKILL || sig == SIGTERM) 78 if (sig == SIGKILL || sig == SIGTERM)
@@ -79,7 +80,10 @@ GNUNET_OS_process_kill (GNUNET_OS_Process *proc, int sig)
79 HANDLE h = GNUNET_OS_process_get_handle (proc); 80 HANDLE h = GNUNET_OS_process_get_handle (proc);
80 if (NULL == h) 81 if (NULL == h)
81 { 82 {
82 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n", GNUNET_OS_process_get_pid (proc), h); 83 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
84 _("Invalid process information {%d, %08X}\n"),
85 proc->pid,
86 h);
83 return -1; 87 return -1;
84 } 88 }
85 if (!TerminateProcess (h, 0)) 89 if (!TerminateProcess (h, 0))
@@ -93,40 +97,13 @@ GNUNET_OS_process_kill (GNUNET_OS_Process *proc, int sig)
93 errno = EINVAL; 97 errno = EINVAL;
94 return -1; 98 return -1;
95#else 99#else
96 return kill (GNUNET_OS_process_get_pid (proc), sig); 100 return kill (proc->pid, sig);
97#endif 101#endif
98} 102}
99 103
100pid_t
101GNUNET_OS_process_get_pid (GNUNET_OS_Process *proc)
102{
103 return proc->pid;
104}
105
106void
107GNUNET_OS_process_set_pid (GNUNET_OS_Process *proc, pid_t pid)
108{
109 proc->pid = pid;
110}
111
112#if WINDOWS
113HANDLE
114GNUNET_OS_process_get_handle (GNUNET_OS_Process *proc)
115{
116 return proc->handle;
117}
118
119void
120GNUNET_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 104
128void 105void
129GNUNET_OS_process_close (GNUNET_OS_Process *proc) 106GNUNET_OS_process_close (struct GNUNET_OS_Process *proc)
130{ 107{
131#if WINDOWS 108#if WINDOWS
132 if (proc->handle != NULL) 109 if (proc->handle != NULL)
@@ -147,7 +124,7 @@ extern GNUNET_SIGNAL_Handler w32_sigchld_handler;
147static DWORD WINAPI 124static DWORD WINAPI
148ChildWaitThread (void *arg) 125ChildWaitThread (void *arg)
149{ 126{
150 GNUNET_OS_Process *proc = (GNUNET_OS_Process *) arg; 127 struct GNUNET_OS_Process *proc = (struct GNUNET_OS_Process *) arg;
151 WaitForSingleObject (proc->handle, INFINITE); 128 WaitForSingleObject (proc->handle, INFINITE);
152 129
153 if (w32_sigchld_handler) 130 if (w32_sigchld_handler)
@@ -165,11 +142,10 @@ ChildWaitThread (void *arg)
165 * @return GNUNET_OK on success, GNUNET_SYSERR on error 142 * @return GNUNET_OK on success, GNUNET_SYSERR on error
166 */ 143 */
167int 144int
168GNUNET_OS_set_process_priority (GNUNET_OS_Process *proc, 145GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
169 enum GNUNET_SCHEDULER_Priority prio) 146 enum GNUNET_SCHEDULER_Priority prio)
170{ 147{
171 int rprio; 148 int rprio;
172 pid_t pid;
173 149
174 GNUNET_assert (prio < GNUNET_SCHEDULER_PRIORITY_COUNT); 150 GNUNET_assert (prio < GNUNET_SCHEDULER_PRIORITY_COUNT);
175 if (prio == GNUNET_SCHEDULER_PRIORITY_KEEP) 151 if (prio == GNUNET_SCHEDULER_PRIORITY_KEEP)
@@ -223,16 +199,17 @@ GNUNET_OS_set_process_priority (GNUNET_OS_Process *proc,
223 return GNUNET_SYSERR; 199 return GNUNET_SYSERR;
224 } 200 }
225 201
226 pid = GNUNET_OS_process_get_pid (proc);
227
228 /* Set process priority */ 202 /* Set process priority */
229#ifdef MINGW 203#ifdef MINGW
230 { 204 {
231 HANDLE h = GNUNET_OS_process_get_handle (proc); 205 HANDLE h = proc->handle;
232 GNUNET_assert (h != NULL); 206 GNUNET_assert (h != NULL);
233 SetPriorityClass (h, rprio); 207 SetPriorityClass (h, rprio);
234 } 208 }
235#elif LINUX 209#elif LINUX
210 pid_t pid;
211
212 pid = proc->pid;
236 if ( (0 == pid) || 213 if ( (0 == pid) ||
237 (pid == getpid () ) ) 214 (pid == getpid () ) )
238 { 215 {
@@ -251,7 +228,6 @@ GNUNET_OS_set_process_priority (GNUNET_OS_Process *proc,
251 else 228 else
252 { 229 {
253 if (0 != setpriority (PRIO_PROCESS, pid, rprio)) 230 if (0 != setpriority (PRIO_PROCESS, pid, rprio))
254
255 { 231 {
256 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | 232 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
257 GNUNET_ERROR_TYPE_BULK, "setpriority"); 233 GNUNET_ERROR_TYPE_BULK, "setpriority");
@@ -274,7 +250,7 @@ GNUNET_OS_set_process_priority (GNUNET_OS_Process *proc,
274 * @param ... NULL-terminated list of arguments to the process 250 * @param ... NULL-terminated list of arguments to the process
275 * @return pointer to process structure of the new process, NULL on error 251 * @return pointer to process structure of the new process, NULL on error
276 */ 252 */
277GNUNET_OS_Process * 253struct GNUNET_OS_Process *
278GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin, 254GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
279 struct GNUNET_DISK_PipeHandle *pipe_stdout, 255 struct GNUNET_DISK_PipeHandle *pipe_stdout,
280 const char *filename, ...) 256 const char *filename, ...)
@@ -283,7 +259,7 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
283 259
284#ifndef MINGW 260#ifndef MINGW
285 pid_t ret; 261 pid_t ret;
286 GNUNET_OS_Process *gnunet_proc = NULL; 262 struct GNUNET_OS_Process *gnunet_proc = NULL;
287 char **argv; 263 char **argv;
288 int argc; 264 int argc;
289 int fd_stdout_write; 265 int fd_stdout_write;
@@ -342,8 +318,8 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
342 GNUNET_DISK_pipe_close_end(pipe_stdin, GNUNET_DISK_PIPE_END_READ); 318 GNUNET_DISK_pipe_close_end(pipe_stdin, GNUNET_DISK_PIPE_END_READ);
343 sleep (1); 319 sleep (1);
344#endif 320#endif
345 gnunet_proc = GNUNET_OS_process_alloc (); 321 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
346 GNUNET_OS_process_set_pid (gnunet_proc, ret); 322 gnunet_proc->pid = ret;
347 } 323 }
348 GNUNET_free (argv); 324 GNUNET_free (argv);
349 return gnunet_proc; 325 return gnunet_proc;
@@ -375,7 +351,7 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
375 int findresult; 351 int findresult;
376 STARTUPINFO start; 352 STARTUPINFO start;
377 PROCESS_INFORMATION proc; 353 PROCESS_INFORMATION proc;
378 GNUNET_OS_Process *gnunet_proc = NULL; 354 struct GNUNET_OS_Process *gnunet_proc = NULL;
379 355
380 HANDLE stdin_handle; 356 HANDLE stdin_handle;
381 HANDLE stdout_handle; 357 HANDLE stdout_handle;
@@ -429,9 +405,9 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
429 return NULL; 405 return NULL;
430 } 406 }
431 407
432 gnunet_proc = GNUNET_OS_process_alloc (); 408 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
433 GNUNET_OS_process_set_pid (gnunet_proc, proc.dwProcessId); 409 gnunet_proc->pid = proc.dwProcessId;
434 GNUNET_OS_process_set_handle (gnunet_proc, proc.hProcess); 410 gnunet_proc->handle = proc.hProcess;
435 411
436 CreateThread (NULL, 64000, ChildWaitThread, (void *) gnunet_proc, 0, NULL); 412 CreateThread (NULL, 64000, ChildWaitThread, (void *) gnunet_proc, 0, NULL);
437 413
@@ -455,7 +431,7 @@ GNUNET_OS_start_process (struct GNUNET_DISK_PipeHandle *pipe_stdin,
455 * @param argv NULL-terminated list of arguments to the process 431 * @param argv NULL-terminated list of arguments to the process
456 * @return process ID of the new process, -1 on error 432 * @return process ID of the new process, -1 on error
457 */ 433 */
458GNUNET_OS_Process * 434struct GNUNET_OS_Process *
459GNUNET_OS_start_process_v (const int *lsocks, 435GNUNET_OS_start_process_v (const int *lsocks,
460 const char *filename, char *const argv[]) 436 const char *filename, char *const argv[])
461{ 437{
@@ -463,7 +439,7 @@ GNUNET_OS_start_process_v (const int *lsocks,
463 pid_t ret; 439 pid_t ret;
464 char lpid[16]; 440 char lpid[16];
465 char fds[16]; 441 char fds[16];
466 GNUNET_OS_Process *gnunet_proc = NULL; 442 struct GNUNET_OS_Process *gnunet_proc = NULL;
467 int i; 443 int i;
468 int j; 444 int j;
469 int k; 445 int k;
@@ -505,8 +481,8 @@ GNUNET_OS_start_process_v (const int *lsocks,
505 be plenty in practice */ 481 be plenty in practice */
506 sleep (1); 482 sleep (1);
507#endif 483#endif
508 gnunet_proc = GNUNET_OS_process_alloc (); 484 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
509 GNUNET_OS_process_set_pid (gnunet_proc, ret); 485 gnunet_proc->pid = ret;
510 } 486 }
511 GNUNET_array_grow (lscp, ls, 0); 487 GNUNET_array_grow (lscp, ls, 0);
512 return gnunet_proc; 488 return gnunet_proc;
@@ -566,7 +542,7 @@ GNUNET_OS_start_process_v (const int *lsocks,
566 int argcount = 0; 542 int argcount = 0;
567 char *non_const_filename = NULL; 543 char *non_const_filename = NULL;
568 int filenamelen = 0; 544 int filenamelen = 0;
569 GNUNET_OS_Process *gnunet_proc = NULL; 545 struct GNUNET_OS_Process *gnunet_proc = NULL;
570 546
571 GNUNET_assert (lsocks == NULL); 547 GNUNET_assert (lsocks == NULL);
572 /* Count the number of arguments */ 548 /* Count the number of arguments */
@@ -634,9 +610,9 @@ GNUNET_OS_start_process_v (const int *lsocks,
634 return NULL; 610 return NULL;
635 } 611 }
636 612
637 gnunet_proc = GNUNET_OS_process_alloc (); 613 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
638 GNUNET_OS_process_set_pid (gnunet_proc, proc.dwProcessId); 614 gnunet_proc->pid = proc.dwProcessId;
639 GNUNET_OS_process_set_handle (gnunet_proc, proc.hProcess); 615 gnunet_proc->handle = proc.hProcess;
640 616
641 CreateThread (NULL, 64000, ChildWaitThread, (void *) gnunet_proc, 0, NULL); 617 CreateThread (NULL, 64000, ChildWaitThread, (void *) gnunet_proc, 0, NULL);
642 618
@@ -659,7 +635,8 @@ GNUNET_OS_start_process_v (const int *lsocks,
659 * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise 635 * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
660 */ 636 */
661int 637int
662GNUNET_OS_process_status (GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, 638GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
639 enum GNUNET_OS_ProcessStatusType *type,
663 unsigned long *code) 640 unsigned long *code)
664{ 641{
665#ifndef MINGW 642#ifndef MINGW
@@ -667,7 +644,7 @@ GNUNET_OS_process_status (GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusT
667 int ret; 644 int ret;
668 645
669 GNUNET_assert (0 != proc); 646 GNUNET_assert (0 != proc);
670 ret = waitpid (GNUNET_OS_process_get_pid (proc), &status, WNOHANG); 647 ret = waitpid (proc->pid, &status, WNOHANG);
671 if (ret < 0) 648 if (ret < 0)
672 { 649 {
673 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); 650 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
@@ -679,7 +656,7 @@ GNUNET_OS_process_status (GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusT
679 *code = 0; 656 *code = 0;
680 return GNUNET_NO; 657 return GNUNET_NO;
681 } 658 }
682 if (GNUNET_OS_process_get_pid (proc) != ret) 659 if (proc->pid != ret)
683 { 660 {
684 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); 661 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
685 return GNUNET_SYSERR; 662 return GNUNET_SYSERR;
@@ -716,7 +693,7 @@ GNUNET_OS_process_status (GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusT
716 DWORD c, error_code, ret; 693 DWORD c, error_code, ret;
717 694
718 h = GNUNET_OS_process_get_handle (proc); 695 h = GNUNET_OS_process_get_handle (proc);
719 ret = GNUNET_OS_process_get_pid (proc); 696 ret = proc->pid;
720 if (h == NULL || ret == 0) 697 if (h == NULL || ret == 0)
721 { 698 {
722 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n", ret, h); 699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n", ret, h);
@@ -753,22 +730,25 @@ GNUNET_OS_process_status (GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusT
753 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 730 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
754 */ 731 */
755int 732int
756GNUNET_OS_process_wait (GNUNET_OS_Process *proc) 733GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
757{ 734{
758 pid_t pid = GNUNET_OS_process_get_pid (proc); 735
759#ifndef MINGW 736#ifndef MINGW
737 pid_t pid = proc->pid;
760 if (pid != waitpid (pid, NULL, 0)) 738 if (pid != waitpid (pid, NULL, 0))
761 return GNUNET_SYSERR; 739 return GNUNET_SYSERR;
762
763 return GNUNET_OK; 740 return GNUNET_OK;
764#else 741#else
765 HANDLE h; 742 HANDLE h;
766 int ret; 743 int ret;
767 744
768 h = GNUNET_OS_process_get_handle (proc); 745 h = proc->handle;
769 if (NULL == h) 746 if (NULL == h)
770 { 747 {
771 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n", pid, h); 748 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
749 "Invalid process information {%d, %08X}\n",
750 proc->pid,
751 h);
772 return GNUNET_SYSERR; 752 return GNUNET_SYSERR;
773 } 753 }
774 if (h == NULL) 754 if (h == NULL)
diff --git a/src/util/test_os_start_process.c b/src/util/test_os_start_process.c
index bd2eb2b09..bce1c7151 100644
--- a/src/util/test_os_start_process.c
+++ b/src/util/test_os_start_process.c
@@ -38,7 +38,7 @@
38static char *test_phrase = "HELLO WORLD"; 38static char *test_phrase = "HELLO WORLD";
39static int ok; 39static int ok;
40 40
41static GNUNET_OS_Process *proc; 41static struct 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) */
43static struct GNUNET_DISK_PipeHandle *hello_pipe_stdin; 43static 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) */
diff --git a/src/util/test_resolver_api.c b/src/util/test_resolver_api.c
index 7321ed587..734420e84 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 GNUNET_OS_Process *proc; 363 struct 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