1 /* Copyright (c) 2008, 2009 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
45 #include "dynamic-string.h"
47 #include "poll-loop.h"
48 #include "socket-util.h"
51 #define THIS_MODULE VLM_process
59 /* Modified by signal handler. */
64 /* Pipe used to signal child termination. */
68 static struct list all_processes = LIST_INITIALIZER(&all_processes);
70 static void block_sigchld(sigset_t *);
71 static void unblock_sigchld(const sigset_t *);
72 static void sigchld_handler(int signr UNUSED);
73 static bool is_member(int x, const int *array, size_t);
74 static bool find_in_path(const char *name);
76 /* Initializes the process subsystem (if it is not already initialized). Calls
77 * exit() if initialization fails.
79 * Calling this function is optional; it will be called automatically by
80 * process_start() if necessary. Calling it explicitly allows the client to
81 * prevent the process from exiting at an unexpected time. */
93 /* Create notification pipe. */
95 ofp_fatal(errno, "could not create pipe");
97 set_nonblocking(fds[0]);
98 set_nonblocking(fds[1]);
100 /* Set up child termination signal handler. */
101 memset(&sa, 0, sizeof sa);
102 sa.sa_handler = sigchld_handler;
103 sigemptyset(&sa.sa_mask);
104 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
105 if (sigaction(SIGCHLD, &sa, NULL)) {
106 ofp_fatal(errno, "sigaction(SIGCHLD) failed");
110 /* Starts a subprocess with the arguments in the null-terminated argv[] array.
111 * argv[0] is used as the name of the process. Searches the PATH environment
112 * variable to find the program to execute.
114 * All file descriptors are closed before executing the subprocess, except for
115 * fds 0, 1, and 2 and the 'n_keep_fds' fds listed in 'keep_fds'. Also, any of
116 * the 'n_null_fds' fds listed in 'null_fds' are replaced by /dev/null.
118 * Returns 0 if successful, otherwise a positive errno value indicating the
119 * error. If successful, '*pp' is assigned a new struct process that may be
120 * used to query the process's status. On failure, '*pp' is set to NULL. */
122 process_start(char **argv,
123 const int keep_fds[], size_t n_keep_fds,
124 const int null_fds[], size_t n_null_fds,
133 if (VLOG_IS_DBG_ENABLED()) {
134 struct ds ds = DS_EMPTY_INITIALIZER;
136 for (argp = argv; *argp; argp++) {
137 const char *arg = *argp;
140 ds_put_char(&ds, ' ');
142 if (arg[strcspn(arg, " \t\r\n\v\\")]) {
143 ds_put_char(&ds, '"');
144 for (p = arg; *p; p++) {
145 if (*p == '\\' || *p == '\"') {
146 ds_put_char(&ds, '\\');
148 ds_put_char(&ds, *p);
150 ds_put_char(&ds, '"');
152 ds_put_cstr(&ds, arg);
155 VLOG_DBG("starting subprocess: %s", ds_cstr(&ds));
159 /* execvp() will search PATH too, but the error in that case is more
160 * obscure, since it is only reported post-fork. */
161 if (!find_in_path(argv[0])) {
162 VLOG_ERR("%s not found in PATH", argv[0]);
166 block_sigchld(&oldsigs);
169 unblock_sigchld(&oldsigs);
170 VLOG_WARN("fork failed: %s", strerror(errno));
173 /* Running in parent process. */
177 p = xcalloc(1, sizeof *p);
179 slash = strrchr(argv[0], '/');
180 p->name = xstrdup(slash ? slash + 1 : argv[0]);
183 list_push_back(&all_processes, &p->node);
184 unblock_sigchld(&oldsigs);
189 /* Running in child process. */
190 int fd_max = get_max_fds();
193 unblock_sigchld(&oldsigs);
194 for (fd = 0; fd < fd_max; fd++) {
195 if (is_member(fd, null_fds, n_null_fds)) {
196 int nullfd = open("/dev/null", O_RDWR);
199 } else if (fd >= 3 && !is_member(fd, keep_fds, n_keep_fds)) {
203 execvp(argv[0], argv);
204 fprintf(stderr, "execvp(\"%s\") failed: %s\n",
205 argv[0], strerror(errno));
210 /* Destroys process 'p'. */
212 process_destroy(struct process *p)
217 block_sigchld(&oldsigs);
218 list_remove(&p->node);
219 unblock_sigchld(&oldsigs);
226 /* Sends signal 'signr' to process 'p'. Returns 0 if successful, otherwise a
227 * positive errno value. */
229 process_kill(const struct process *p, int signr)
231 return (p->exited ? ESRCH
232 : !kill(p->pid, signr) ? 0
236 /* Returns the pid of process 'p'. */
238 process_pid(const struct process *p)
243 /* Returns the name of process 'p' (the name passed to process_start() with any
244 * leading directories stripped). */
246 process_name(const struct process *p)
251 /* Returns true if process 'p' has exited, false otherwise. */
253 process_exited(struct process *p)
258 char buf[_POSIX_PIPE_BUF];
259 read(fds[0], buf, sizeof buf);
264 /* Returns process 'p''s exit status, as reported by waitpid(2).
265 * process_status(p) may be called only after process_exited(p) has returned
268 process_status(const struct process *p)
275 process_run(char **argv,
276 const int keep_fds[], size_t n_keep_fds,
277 const int null_fds[], size_t n_null_fds,
283 retval = process_start(argv, keep_fds, n_keep_fds, null_fds, n_null_fds,
290 while (!process_exited(p)) {
294 *status = process_status(p);
299 /* Given 'status', which is a process status in the form reported by waitpid(2)
300 * and returned by process_status(), returns a string describing how the
301 * process terminated. The caller is responsible for freeing the string when
302 * it is no longer needed. */
304 process_status_msg(int status)
306 struct ds ds = DS_EMPTY_INITIALIZER;
307 if (WIFEXITED(status)) {
308 ds_put_format(&ds, "exit status %d", WEXITSTATUS(status));
309 } else if (WIFSIGNALED(status)) {
310 const char *name = NULL;
311 #ifdef HAVE_STRSIGNAL
312 name = strsignal(WTERMSIG(status));
314 ds_put_format(&ds, "killed by signal %d", WTERMSIG(status));
316 ds_put_format(&ds, " (%s)", name);
319 if (WCOREDUMP(status)) {
320 ds_put_cstr(&ds, ", core dumped");
325 /* Causes the next call to poll_block() to wake up when process 'p' has
328 process_wait(struct process *p)
331 poll_immediate_wake();
333 poll_fd_wait(fds[0], POLLIN);
338 sigchld_handler(int signr UNUSED)
345 pid = waitpid(-1, &status, WNOHANG);
350 LIST_FOR_EACH (p, struct process, node, &all_processes) {
358 write(fds[1], "", 1);
362 is_member(int x, const int *array, size_t n)
366 for (i = 0; i < n; i++) {
375 block_sigchld(sigset_t *oldsigs)
378 sigemptyset(&sigchld);
379 sigaddset(&sigchld, SIGCHLD);
380 if (sigprocmask(SIG_BLOCK, &sigchld, oldsigs)) {
381 ofp_fatal(errno, "sigprocmask");
386 unblock_sigchld(const sigset_t *oldsigs)
388 if (sigprocmask(SIG_SETMASK, oldsigs, NULL)) {
389 ofp_fatal(errno, "sigprocmask");
394 find_in_path(const char *name)
396 char *save_ptr = NULL;
400 if (strchr(name, '/') || !getenv("PATH")) {
401 return stat(name, &s) == 0;
404 path = xstrdup(getenv("PATH"));
405 for (dir = strtok_r(path, ":", &save_ptr); dir;
406 dir = strtok_r(NULL, ":", &save_ptr)) {
407 char *file = xasprintf("%s/%s", dir, name);
408 if (stat(file, &s) == 0) {