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