]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e93af6a4 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "process.h" | |
064af421 BP |
19 | #include <errno.h> |
20 | #include <fcntl.h> | |
21 | #include <signal.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
194db781 | 24 | #include <sys/resource.h> |
064af421 BP |
25 | #include <sys/stat.h> |
26 | #include <sys/wait.h> | |
27 | #include <unistd.h> | |
28 | #include "coverage.h" | |
3e8a2ad1 | 29 | #include "openvswitch/dynamic-string.h" |
52dc2ef4 | 30 | #include "fatal-signal.h" |
b19bab5b | 31 | #include "openvswitch/list.h" |
728a8b14 | 32 | #include "ovs-thread.h" |
fd016ae3 | 33 | #include "openvswitch/poll-loop.h" |
b725cf02 | 34 | #include "signals.h" |
064af421 | 35 | #include "socket-util.h" |
ff1d2c16 | 36 | #include "timeval.h" |
064af421 | 37 | #include "util.h" |
e6211adc | 38 | #include "openvswitch/vlog.h" |
064af421 | 39 | |
d98e6007 | 40 | VLOG_DEFINE_THIS_MODULE(process); |
5136ce49 | 41 | |
d76f09ea BP |
42 | COVERAGE_DEFINE(process_start); |
43 | ||
ff1d2c16 BB |
44 | #ifdef __linux__ |
45 | #define LINUX 1 | |
46 | #include <asm/param.h> | |
47 | #else | |
48 | #define LINUX 0 | |
49 | #endif | |
50 | ||
064af421 | 51 | struct process { |
ca6ba700 | 52 | struct ovs_list node; |
064af421 BP |
53 | char *name; |
54 | pid_t pid; | |
55 | ||
57d90319 BP |
56 | /* State. */ |
57 | bool exited; | |
58 | int status; | |
064af421 BP |
59 | }; |
60 | ||
ff1d2c16 BB |
61 | struct raw_process_info { |
62 | unsigned long int vsz; /* Virtual size, in kB. */ | |
63 | unsigned long int rss; /* Resident set size, in kB. */ | |
64 | long long int uptime; /* ms since started. */ | |
65 | long long int cputime; /* ms of CPU used during 'uptime'. */ | |
66 | pid_t ppid; /* Parent. */ | |
0fee5185 BB |
67 | int core_id; /* Core id last executed on. */ |
68 | char name[18]; /* Name. */ | |
ff1d2c16 BB |
69 | }; |
70 | ||
064af421 BP |
71 | /* Pipe used to signal child termination. */ |
72 | static int fds[2]; | |
73 | ||
74 | /* All processes. */ | |
55951e15 | 75 | static struct ovs_list all_processes = OVS_LIST_INITIALIZER(&all_processes); |
064af421 | 76 | |
67a4917b | 77 | static void sigchld_handler(int signr OVS_UNUSED); |
064af421 BP |
78 | |
79 | /* Initializes the process subsystem (if it is not already initialized). Calls | |
80 | * exit() if initialization fails. | |
81 | * | |
ff412c8c BP |
82 | * This function may not be called after creating any additional threads. |
83 | * | |
064af421 BP |
84 | * Calling this function is optional; it will be called automatically by |
85 | * process_start() if necessary. Calling it explicitly allows the client to | |
86 | * prevent the process from exiting at an unexpected time. */ | |
87 | void | |
88 | process_init(void) | |
89 | { | |
41064650 | 90 | #ifndef _WIN32 |
064af421 BP |
91 | static bool inited; |
92 | struct sigaction sa; | |
93 | ||
728a8b14 | 94 | assert_single_threaded(); |
064af421 BP |
95 | if (inited) { |
96 | return; | |
97 | } | |
98 | inited = true; | |
99 | ||
100 | /* Create notification pipe. */ | |
c0d95206 | 101 | xpipe_nonblocking(fds); |
064af421 BP |
102 | |
103 | /* Set up child termination signal handler. */ | |
104 | memset(&sa, 0, sizeof sa); | |
105 | sa.sa_handler = sigchld_handler; | |
106 | sigemptyset(&sa.sa_mask); | |
107 | sa.sa_flags = SA_NOCLDSTOP | SA_RESTART; | |
279c9e03 | 108 | xsigaction(SIGCHLD, &sa, NULL); |
41064650 | 109 | #endif |
064af421 BP |
110 | } |
111 | ||
112 | char * | |
113 | process_escape_args(char **argv) | |
114 | { | |
115 | struct ds ds = DS_EMPTY_INITIALIZER; | |
116 | char **argp; | |
117 | for (argp = argv; *argp; argp++) { | |
118 | const char *arg = *argp; | |
119 | const char *p; | |
120 | if (argp != argv) { | |
121 | ds_put_char(&ds, ' '); | |
122 | } | |
7aee8e5a | 123 | if (arg[strcspn(arg, " \t\r\n\v\\\'\"")]) { |
064af421 BP |
124 | ds_put_char(&ds, '"'); |
125 | for (p = arg; *p; p++) { | |
126 | if (*p == '\\' || *p == '\"') { | |
127 | ds_put_char(&ds, '\\'); | |
128 | } | |
129 | ds_put_char(&ds, *p); | |
130 | } | |
131 | ds_put_char(&ds, '"'); | |
132 | } else { | |
133 | ds_put_cstr(&ds, arg); | |
134 | } | |
135 | } | |
136 | return ds_cstr(&ds); | |
137 | } | |
138 | ||
1fa39e1d BP |
139 | /* Prepare to start a process whose command-line arguments are given by the |
140 | * null-terminated 'argv' array. Returns 0 if successful, otherwise a | |
141 | * positive errno value. */ | |
142 | static int | |
143 | process_prestart(char **argv) | |
144 | { | |
145 | char *binary; | |
146 | ||
147 | process_init(); | |
148 | ||
149 | /* Log the process to be started. */ | |
150 | if (VLOG_IS_DBG_ENABLED()) { | |
151 | char *args = process_escape_args(argv); | |
152 | VLOG_DBG("starting subprocess: %s", args); | |
153 | free(args); | |
154 | } | |
155 | ||
156 | /* execvp() will search PATH too, but the error in that case is more | |
157 | * obscure, since it is only reported post-fork. */ | |
158 | binary = process_search_path(argv[0]); | |
159 | if (!binary) { | |
160 | VLOG_ERR("%s not found in PATH", argv[0]); | |
161 | return ENOENT; | |
162 | } | |
163 | free(binary); | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
168 | /* Creates and returns a new struct process with the specified 'name' and | |
57d90319 | 169 | * 'pid'. */ |
1fa39e1d BP |
170 | static struct process * |
171 | process_register(const char *name, pid_t pid) | |
172 | { | |
173 | struct process *p; | |
174 | const char *slash; | |
175 | ||
ec6fde61 | 176 | p = xzalloc(sizeof *p); |
1fa39e1d BP |
177 | p->pid = pid; |
178 | slash = strrchr(name, '/'); | |
179 | p->name = xstrdup(slash ? slash + 1 : name); | |
180 | p->exited = false; | |
181 | ||
417e7e66 | 182 | ovs_list_push_back(&all_processes, &p->node); |
1fa39e1d BP |
183 | |
184 | return p; | |
185 | } | |
186 | ||
4f57ad10 GS |
187 | #ifndef _WIN32 |
188 | static bool | |
189 | rlim_is_finite(rlim_t limit) | |
190 | { | |
191 | if (limit == RLIM_INFINITY) { | |
192 | return false; | |
193 | } | |
194 | ||
195 | #ifdef RLIM_SAVED_CUR /* FreeBSD 8.0 lacks RLIM_SAVED_CUR. */ | |
196 | if (limit == RLIM_SAVED_CUR) { | |
197 | return false; | |
198 | } | |
199 | #endif | |
200 | ||
201 | #ifdef RLIM_SAVED_MAX /* FreeBSD 8.0 lacks RLIM_SAVED_MAX. */ | |
202 | if (limit == RLIM_SAVED_MAX) { | |
203 | return false; | |
204 | } | |
205 | #endif | |
206 | ||
207 | return true; | |
208 | } | |
209 | ||
210 | /* Returns the maximum valid FD value, plus 1. */ | |
211 | static int | |
212 | get_max_fds(void) | |
213 | { | |
214 | static int max_fds; | |
215 | ||
216 | if (!max_fds) { | |
217 | struct rlimit r; | |
218 | if (!getrlimit(RLIMIT_NOFILE, &r) && rlim_is_finite(r.rlim_cur)) { | |
219 | max_fds = r.rlim_cur; | |
220 | } else { | |
221 | VLOG_WARN("failed to obtain fd limit, defaulting to 1024"); | |
222 | max_fds = 1024; | |
223 | } | |
224 | } | |
225 | ||
226 | return max_fds; | |
227 | } | |
228 | #endif /* _WIN32 */ | |
229 | ||
064af421 BP |
230 | /* Starts a subprocess with the arguments in the null-terminated argv[] array. |
231 | * argv[0] is used as the name of the process. Searches the PATH environment | |
232 | * variable to find the program to execute. | |
233 | * | |
ff412c8c BP |
234 | * This function may not be called after creating any additional threads. |
235 | * | |
064af421 | 236 | * All file descriptors are closed before executing the subprocess, except for |
e1208bc4 | 237 | * fds 0, 1, and 2. |
064af421 BP |
238 | * |
239 | * Returns 0 if successful, otherwise a positive errno value indicating the | |
240 | * error. If successful, '*pp' is assigned a new struct process that may be | |
241 | * used to query the process's status. On failure, '*pp' is set to NULL. */ | |
242 | int | |
e1208bc4 | 243 | process_start(char **argv, struct process **pp) |
064af421 | 244 | { |
41064650 | 245 | #ifndef _WIN32 |
064af421 | 246 | pid_t pid; |
1fa39e1d | 247 | int error; |
1481a755 | 248 | sigset_t prev_mask; |
064af421 | 249 | |
728a8b14 BP |
250 | assert_single_threaded(); |
251 | ||
064af421 | 252 | *pp = NULL; |
064af421 | 253 | COVERAGE_INC(process_start); |
1fa39e1d BP |
254 | error = process_prestart(argv); |
255 | if (error) { | |
256 | return error; | |
064af421 | 257 | } |
064af421 | 258 | |
1481a755 | 259 | fatal_signal_block(&prev_mask); |
064af421 BP |
260 | pid = fork(); |
261 | if (pid < 0) { | |
10a89ef0 | 262 | VLOG_WARN("fork failed: %s", ovs_strerror(errno)); |
1481a755 | 263 | error = errno; |
064af421 BP |
264 | } else if (pid) { |
265 | /* Running in parent process. */ | |
1fa39e1d | 266 | *pp = process_register(argv[0], pid); |
1481a755 | 267 | error = 0; |
064af421 BP |
268 | } else { |
269 | /* Running in child process. */ | |
270 | int fd_max = get_max_fds(); | |
271 | int fd; | |
272 | ||
52dc2ef4 | 273 | fatal_signal_fork(); |
e1208bc4 BP |
274 | for (fd = 3; fd < fd_max; fd++) { |
275 | close(fd); | |
fe81a298 | 276 | } |
1481a755 | 277 | xpthread_sigmask(SIG_SETMASK, &prev_mask, NULL); |
064af421 BP |
278 | execvp(argv[0], argv); |
279 | fprintf(stderr, "execvp(\"%s\") failed: %s\n", | |
10a89ef0 | 280 | argv[0], ovs_strerror(errno)); |
064af421 BP |
281 | _exit(1); |
282 | } | |
1481a755 AA |
283 | xpthread_sigmask(SIG_SETMASK, &prev_mask, NULL); |
284 | return error; | |
41064650 GS |
285 | #else |
286 | *pp = NULL; | |
287 | return ENOSYS; | |
288 | #endif | |
064af421 BP |
289 | } |
290 | ||
291 | /* Destroys process 'p'. */ | |
292 | void | |
293 | process_destroy(struct process *p) | |
294 | { | |
295 | if (p) { | |
417e7e66 | 296 | ovs_list_remove(&p->node); |
064af421 BP |
297 | free(p->name); |
298 | free(p); | |
299 | } | |
300 | } | |
301 | ||
302 | /* Sends signal 'signr' to process 'p'. Returns 0 if successful, otherwise a | |
303 | * positive errno value. */ | |
304 | int | |
305 | process_kill(const struct process *p, int signr) | |
306 | { | |
41064650 | 307 | #ifndef _WIN32 |
064af421 BP |
308 | return (p->exited ? ESRCH |
309 | : !kill(p->pid, signr) ? 0 | |
310 | : errno); | |
41064650 GS |
311 | #else |
312 | return ENOSYS; | |
313 | #endif | |
064af421 BP |
314 | } |
315 | ||
316 | /* Returns the pid of process 'p'. */ | |
317 | pid_t | |
318 | process_pid(const struct process *p) | |
319 | { | |
320 | return p->pid; | |
321 | } | |
322 | ||
323 | /* Returns the name of process 'p' (the name passed to process_start() with any | |
324 | * leading directories stripped). */ | |
325 | const char * | |
326 | process_name(const struct process *p) | |
327 | { | |
328 | return p->name; | |
329 | } | |
330 | ||
331 | /* Returns true if process 'p' has exited, false otherwise. */ | |
332 | bool | |
333 | process_exited(struct process *p) | |
334 | { | |
57d90319 | 335 | return p->exited; |
064af421 BP |
336 | } |
337 | ||
338 | /* Returns process 'p''s exit status, as reported by waitpid(2). | |
339 | * process_status(p) may be called only after process_exited(p) has returned | |
340 | * true. */ | |
341 | int | |
342 | process_status(const struct process *p) | |
343 | { | |
cb22974d | 344 | ovs_assert(p->exited); |
064af421 BP |
345 | return p->status; |
346 | } | |
347 | ||
ff1d2c16 BB |
348 | int |
349 | count_crashes(pid_t pid) | |
350 | { | |
351 | char file_name[128]; | |
352 | const char *paren; | |
353 | char line[128]; | |
354 | int crashes = 0; | |
355 | FILE *stream; | |
356 | ||
357 | ovs_assert(LINUX); | |
358 | ||
359 | sprintf(file_name, "/proc/%lu/cmdline", (unsigned long int) pid); | |
360 | stream = fopen(file_name, "r"); | |
361 | if (!stream) { | |
362 | VLOG_WARN_ONCE("%s: open failed (%s)", file_name, ovs_strerror(errno)); | |
363 | goto exit; | |
364 | } | |
365 | ||
366 | if (!fgets(line, sizeof line, stream)) { | |
367 | VLOG_WARN_ONCE("%s: read failed (%s)", file_name, | |
368 | feof(stream) ? "end of file" : ovs_strerror(errno)); | |
369 | goto exit_close; | |
370 | } | |
371 | ||
372 | paren = strchr(line, '('); | |
373 | if (paren) { | |
374 | int x; | |
375 | if (ovs_scan(paren + 1, "%d", &x)) { | |
376 | crashes = x; | |
377 | } | |
378 | } | |
379 | ||
380 | exit_close: | |
381 | fclose(stream); | |
382 | exit: | |
383 | return crashes; | |
384 | } | |
385 | ||
386 | static unsigned long long int | |
387 | ticks_to_ms(unsigned long long int ticks) | |
388 | { | |
389 | ovs_assert(LINUX); | |
390 | ||
391 | #ifndef USER_HZ | |
392 | #define USER_HZ 100 | |
393 | #endif | |
394 | ||
395 | #if USER_HZ == 100 /* Common case. */ | |
396 | return ticks * (1000 / USER_HZ); | |
397 | #else /* Alpha and some other architectures. */ | |
398 | double factor = 1000.0 / USER_HZ; | |
399 | return ticks * factor + 0.5; | |
400 | #endif | |
401 | } | |
402 | ||
403 | static bool | |
404 | get_raw_process_info(pid_t pid, struct raw_process_info *raw) | |
405 | { | |
406 | unsigned long long int vsize, rss, start_time, utime, stime; | |
407 | long long int start_msec; | |
408 | unsigned long ppid; | |
409 | char file_name[128]; | |
410 | FILE *stream; | |
411 | int n; | |
412 | ||
413 | ovs_assert(LINUX); | |
414 | ||
415 | sprintf(file_name, "/proc/%lu/stat", (unsigned long int) pid); | |
416 | stream = fopen(file_name, "r"); | |
417 | if (!stream) { | |
418 | VLOG_ERR_ONCE("%s: open failed (%s)", | |
419 | file_name, ovs_strerror(errno)); | |
420 | return false; | |
421 | } | |
422 | ||
423 | n = fscanf(stream, | |
424 | "%*d " /* (1. pid) */ | |
0fee5185 | 425 | "(%17[^)]) " /* 2. process name */ |
ff1d2c16 BB |
426 | "%*c " /* (3. state) */ |
427 | "%lu " /* 4. ppid */ | |
428 | "%*d " /* (5. pgid) */ | |
429 | "%*d " /* (6. sid) */ | |
430 | "%*d " /* (7. tty_nr) */ | |
431 | "%*d " /* (8. tty_pgrp) */ | |
432 | "%*u " /* (9. flags) */ | |
433 | "%*u " /* (10. min_flt) */ | |
434 | "%*u " /* (11. cmin_flt) */ | |
435 | "%*u " /* (12. maj_flt) */ | |
436 | "%*u " /* (13. cmaj_flt) */ | |
437 | "%llu " /* 14. utime */ | |
438 | "%llu " /* 15. stime */ | |
439 | "%*d " /* (16. cutime) */ | |
440 | "%*d " /* (17. cstime) */ | |
441 | "%*d " /* (18. priority) */ | |
442 | "%*d " /* (19. nice) */ | |
443 | "%*d " /* (20. num_threads) */ | |
444 | "%*d " /* (21. always 0) */ | |
445 | "%llu " /* 22. start_time */ | |
446 | "%llu " /* 23. vsize */ | |
447 | "%llu " /* 24. rss */ | |
0fee5185 BB |
448 | "%*u " /* (25. rsslim) */ |
449 | "%*u " /* (26. start_code) */ | |
450 | "%*u " /* (27. end_code) */ | |
451 | "%*u " /* (28. start_stack) */ | |
452 | "%*u " /* (29. esp) */ | |
453 | "%*u " /* (30. eip) */ | |
454 | "%*u " /* (31. pending signals) */ | |
455 | "%*u " /* (32. blocked signals) */ | |
456 | "%*u " /* (33. ignored signals) */ | |
457 | "%*u " /* (34. caught signals) */ | |
458 | "%*u " /* (35. whcan) */ | |
459 | "%*u " /* (36. always 0) */ | |
460 | "%*u " /* (37. always 0) */ | |
461 | "%*d " /* (38. exit_signal) */ | |
462 | "%d " /* 39. task_cpu */ | |
ff1d2c16 BB |
463 | #if 0 |
464 | /* These are here for documentation but #if'd out to save | |
465 | * actually parsing them from the stream for no benefit. */ | |
ff1d2c16 BB |
466 | "%*u " /* (40. rt_priority) */ |
467 | "%*u " /* (41. policy) */ | |
468 | "%*llu " /* (42. blkio_ticks) */ | |
469 | "%*lu " /* (43. gtime) */ | |
470 | "%*ld" /* (44. cgtime) */ | |
471 | #endif | |
0fee5185 BB |
472 | , raw->name, &ppid, &utime, &stime, &start_time, |
473 | &vsize, &rss, &raw->core_id); | |
ff1d2c16 | 474 | fclose(stream); |
0fee5185 | 475 | if (n != 8) { |
ff1d2c16 BB |
476 | VLOG_ERR_ONCE("%s: fscanf failed", file_name); |
477 | return false; | |
478 | } | |
479 | ||
480 | start_msec = get_boot_time() + ticks_to_ms(start_time); | |
481 | ||
482 | raw->vsz = vsize / 1024; | |
483 | raw->rss = rss * (get_page_size() / 1024); | |
484 | raw->uptime = time_wall_msec() - start_msec; | |
485 | raw->cputime = ticks_to_ms(utime + stime); | |
486 | raw->ppid = ppid; | |
487 | ||
488 | return true; | |
489 | } | |
490 | ||
491 | bool | |
492 | get_process_info(pid_t pid, struct process_info *pinfo) | |
493 | { | |
494 | struct raw_process_info child; | |
495 | ||
496 | ovs_assert(LINUX); | |
497 | if (!get_raw_process_info(pid, &child)) { | |
498 | return false; | |
499 | } | |
500 | ||
0fee5185 | 501 | ovs_strlcpy(pinfo->name, child.name, sizeof pinfo->name); |
ff1d2c16 BB |
502 | pinfo->vsz = child.vsz; |
503 | pinfo->rss = child.rss; | |
504 | pinfo->booted = child.uptime; | |
505 | pinfo->crashes = 0; | |
506 | pinfo->uptime = child.uptime; | |
507 | pinfo->cputime = child.cputime; | |
0fee5185 | 508 | pinfo->core_id = child.core_id; |
ff1d2c16 BB |
509 | |
510 | if (child.ppid) { | |
511 | struct raw_process_info parent; | |
512 | ||
513 | get_raw_process_info(child.ppid, &parent); | |
514 | if (!strcmp(child.name, parent.name)) { | |
515 | pinfo->booted = parent.uptime; | |
516 | pinfo->crashes = count_crashes(child.ppid); | |
517 | } | |
518 | } | |
519 | ||
520 | return true; | |
521 | } | |
522 | ||
064af421 BP |
523 | /* Given 'status', which is a process status in the form reported by waitpid(2) |
524 | * and returned by process_status(), returns a string describing how the | |
525 | * process terminated. The caller is responsible for freeing the string when | |
526 | * it is no longer needed. */ | |
527 | char * | |
528 | process_status_msg(int status) | |
529 | { | |
530 | struct ds ds = DS_EMPTY_INITIALIZER; | |
41064650 | 531 | #ifndef _WIN32 |
064af421 BP |
532 | if (WIFEXITED(status)) { |
533 | ds_put_format(&ds, "exit status %d", WEXITSTATUS(status)); | |
b725cf02 | 534 | } else if (WIFSIGNALED(status)) { |
eee8089c BP |
535 | char namebuf[SIGNAL_NAME_BUFSIZE]; |
536 | ||
537 | ds_put_format(&ds, "killed (%s)", | |
538 | signal_name(WTERMSIG(status), namebuf, sizeof namebuf)); | |
b725cf02 | 539 | } else if (WIFSTOPPED(status)) { |
eee8089c BP |
540 | char namebuf[SIGNAL_NAME_BUFSIZE]; |
541 | ||
542 | ds_put_format(&ds, "stopped (%s)", | |
543 | signal_name(WSTOPSIG(status), namebuf, sizeof namebuf)); | |
064af421 BP |
544 | } else { |
545 | ds_put_format(&ds, "terminated abnormally (%x)", status); | |
546 | } | |
547 | if (WCOREDUMP(status)) { | |
548 | ds_put_cstr(&ds, ", core dumped"); | |
549 | } | |
41064650 GS |
550 | #else |
551 | ds_put_cstr(&ds, "function not supported."); | |
552 | #endif | |
064af421 BP |
553 | return ds_cstr(&ds); |
554 | } | |
555 | ||
57d90319 BP |
556 | /* Executes periodic maintenance activities required by the process module. */ |
557 | void | |
558 | process_run(void) | |
559 | { | |
41064650 | 560 | #ifndef _WIN32 |
57d90319 BP |
561 | char buf[_POSIX_PIPE_BUF]; |
562 | ||
417e7e66 | 563 | if (!ovs_list_is_empty(&all_processes) && read(fds[0], buf, sizeof buf) > 0) { |
57d90319 BP |
564 | struct process *p; |
565 | ||
566 | LIST_FOR_EACH (p, node, &all_processes) { | |
567 | if (!p->exited) { | |
568 | int retval, status; | |
569 | do { | |
570 | retval = waitpid(p->pid, &status, WNOHANG); | |
571 | } while (retval == -1 && errno == EINTR); | |
572 | if (retval == p->pid) { | |
573 | p->exited = true; | |
574 | p->status = status; | |
575 | } else if (retval < 0) { | |
10a89ef0 | 576 | VLOG_WARN("waitpid: %s", ovs_strerror(errno)); |
57d90319 BP |
577 | p->exited = true; |
578 | p->status = -1; | |
579 | } | |
580 | } | |
581 | } | |
582 | } | |
41064650 | 583 | #endif |
57d90319 BP |
584 | } |
585 | ||
064af421 BP |
586 | /* Causes the next call to poll_block() to wake up when process 'p' has |
587 | * exited. */ | |
588 | void | |
589 | process_wait(struct process *p) | |
590 | { | |
41064650 | 591 | #ifndef _WIN32 |
064af421 BP |
592 | if (p->exited) { |
593 | poll_immediate_wake(); | |
594 | } else { | |
595 | poll_fd_wait(fds[0], POLLIN); | |
596 | } | |
41064650 GS |
597 | #else |
598 | OVS_NOT_REACHED(); | |
599 | #endif | |
064af421 BP |
600 | } |
601 | ||
602 | char * | |
603 | process_search_path(const char *name) | |
604 | { | |
605 | char *save_ptr = NULL; | |
606 | char *path, *dir; | |
607 | struct stat s; | |
608 | ||
609 | if (strchr(name, '/') || !getenv("PATH")) { | |
610 | return stat(name, &s) == 0 ? xstrdup(name) : NULL; | |
611 | } | |
612 | ||
613 | path = xstrdup(getenv("PATH")); | |
614 | for (dir = strtok_r(path, ":", &save_ptr); dir; | |
615 | dir = strtok_r(NULL, ":", &save_ptr)) { | |
616 | char *file = xasprintf("%s/%s", dir, name); | |
617 | if (stat(file, &s) == 0) { | |
618 | free(path); | |
619 | return file; | |
620 | } | |
621 | free(file); | |
622 | } | |
623 | free(path); | |
624 | return NULL; | |
625 | } | |
626 | \f | |
627 | static void | |
67a4917b | 628 | sigchld_handler(int signr OVS_UNUSED) |
064af421 | 629 | { |
18b9283b | 630 | ignore(write(fds[1], "", 1)); |
064af421 | 631 | } |