]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/process.c
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <sys/resource.h>
29 #include "openvswitch/dynamic-string.h"
30 #include "fatal-signal.h"
31 #include "openvswitch/list.h"
32 #include "ovs-thread.h"
33 #include "poll-loop.h"
35 #include "socket-util.h"
37 #include "openvswitch/vlog.h"
39 VLOG_DEFINE_THIS_MODULE(process
);
41 COVERAGE_DEFINE(process_start
);
53 /* Pipe used to signal child termination. */
57 static struct ovs_list all_processes
= OVS_LIST_INITIALIZER(&all_processes
);
59 static void sigchld_handler(int signr OVS_UNUSED
);
61 /* Initializes the process subsystem (if it is not already initialized). Calls
62 * exit() if initialization fails.
64 * This function may not be called after creating any additional threads.
66 * Calling this function is optional; it will be called automatically by
67 * process_start() if necessary. Calling it explicitly allows the client to
68 * prevent the process from exiting at an unexpected time. */
76 assert_single_threaded();
82 /* Create notification pipe. */
83 xpipe_nonblocking(fds
);
85 /* Set up child termination signal handler. */
86 memset(&sa
, 0, sizeof sa
);
87 sa
.sa_handler
= sigchld_handler
;
88 sigemptyset(&sa
.sa_mask
);
89 sa
.sa_flags
= SA_NOCLDSTOP
| SA_RESTART
;
90 xsigaction(SIGCHLD
, &sa
, NULL
);
95 process_escape_args(char **argv
)
97 struct ds ds
= DS_EMPTY_INITIALIZER
;
99 for (argp
= argv
; *argp
; argp
++) {
100 const char *arg
= *argp
;
103 ds_put_char(&ds
, ' ');
105 if (arg
[strcspn(arg
, " \t\r\n\v\\\'\"")]) {
106 ds_put_char(&ds
, '"');
107 for (p
= arg
; *p
; p
++) {
108 if (*p
== '\\' || *p
== '\"') {
109 ds_put_char(&ds
, '\\');
111 ds_put_char(&ds
, *p
);
113 ds_put_char(&ds
, '"');
115 ds_put_cstr(&ds
, arg
);
121 /* Prepare to start a process whose command-line arguments are given by the
122 * null-terminated 'argv' array. Returns 0 if successful, otherwise a
123 * positive errno value. */
125 process_prestart(char **argv
)
131 /* Log the process to be started. */
132 if (VLOG_IS_DBG_ENABLED()) {
133 char *args
= process_escape_args(argv
);
134 VLOG_DBG("starting subprocess: %s", args
);
138 /* execvp() will search PATH too, but the error in that case is more
139 * obscure, since it is only reported post-fork. */
140 binary
= process_search_path(argv
[0]);
142 VLOG_ERR("%s not found in PATH", argv
[0]);
150 /* Creates and returns a new struct process with the specified 'name' and
152 static struct process
*
153 process_register(const char *name
, pid_t pid
)
158 p
= xzalloc(sizeof *p
);
160 slash
= strrchr(name
, '/');
161 p
->name
= xstrdup(slash
? slash
+ 1 : name
);
164 ovs_list_push_back(&all_processes
, &p
->node
);
171 rlim_is_finite(rlim_t limit
)
173 if (limit
== RLIM_INFINITY
) {
177 #ifdef RLIM_SAVED_CUR /* FreeBSD 8.0 lacks RLIM_SAVED_CUR. */
178 if (limit
== RLIM_SAVED_CUR
) {
183 #ifdef RLIM_SAVED_MAX /* FreeBSD 8.0 lacks RLIM_SAVED_MAX. */
184 if (limit
== RLIM_SAVED_MAX
) {
192 /* Returns the maximum valid FD value, plus 1. */
200 if (!getrlimit(RLIMIT_NOFILE
, &r
) && rlim_is_finite(r
.rlim_cur
)) {
201 max_fds
= r
.rlim_cur
;
203 VLOG_WARN("failed to obtain fd limit, defaulting to 1024");
212 /* Starts a subprocess with the arguments in the null-terminated argv[] array.
213 * argv[0] is used as the name of the process. Searches the PATH environment
214 * variable to find the program to execute.
216 * This function may not be called after creating any additional threads.
218 * All file descriptors are closed before executing the subprocess, except for
221 * Returns 0 if successful, otherwise a positive errno value indicating the
222 * error. If successful, '*pp' is assigned a new struct process that may be
223 * used to query the process's status. On failure, '*pp' is set to NULL. */
225 process_start(char **argv
, struct process
**pp
)
232 assert_single_threaded();
235 COVERAGE_INC(process_start
);
236 error
= process_prestart(argv
);
241 fatal_signal_block(&prev_mask
);
244 VLOG_WARN("fork failed: %s", ovs_strerror(errno
));
247 /* Running in parent process. */
248 *pp
= process_register(argv
[0], pid
);
251 /* Running in child process. */
252 int fd_max
= get_max_fds();
256 for (fd
= 3; fd
< fd_max
; fd
++) {
259 xpthread_sigmask(SIG_SETMASK
, &prev_mask
, NULL
);
260 execvp(argv
[0], argv
);
261 fprintf(stderr
, "execvp(\"%s\") failed: %s\n",
262 argv
[0], ovs_strerror(errno
));
265 xpthread_sigmask(SIG_SETMASK
, &prev_mask
, NULL
);
273 /* Destroys process 'p'. */
275 process_destroy(struct process
*p
)
278 ovs_list_remove(&p
->node
);
284 /* Sends signal 'signr' to process 'p'. Returns 0 if successful, otherwise a
285 * positive errno value. */
287 process_kill(const struct process
*p
, int signr
)
290 return (p
->exited
? ESRCH
291 : !kill(p
->pid
, signr
) ? 0
298 /* Returns the pid of process 'p'. */
300 process_pid(const struct process
*p
)
305 /* Returns the name of process 'p' (the name passed to process_start() with any
306 * leading directories stripped). */
308 process_name(const struct process
*p
)
313 /* Returns true if process 'p' has exited, false otherwise. */
315 process_exited(struct process
*p
)
320 /* Returns process 'p''s exit status, as reported by waitpid(2).
321 * process_status(p) may be called only after process_exited(p) has returned
324 process_status(const struct process
*p
)
326 ovs_assert(p
->exited
);
330 /* Given 'status', which is a process status in the form reported by waitpid(2)
331 * and returned by process_status(), returns a string describing how the
332 * process terminated. The caller is responsible for freeing the string when
333 * it is no longer needed. */
335 process_status_msg(int status
)
337 struct ds ds
= DS_EMPTY_INITIALIZER
;
339 if (WIFEXITED(status
)) {
340 ds_put_format(&ds
, "exit status %d", WEXITSTATUS(status
));
341 } else if (WIFSIGNALED(status
)) {
342 char namebuf
[SIGNAL_NAME_BUFSIZE
];
344 ds_put_format(&ds
, "killed (%s)",
345 signal_name(WTERMSIG(status
), namebuf
, sizeof namebuf
));
346 } else if (WIFSTOPPED(status
)) {
347 char namebuf
[SIGNAL_NAME_BUFSIZE
];
349 ds_put_format(&ds
, "stopped (%s)",
350 signal_name(WSTOPSIG(status
), namebuf
, sizeof namebuf
));
352 ds_put_format(&ds
, "terminated abnormally (%x)", status
);
354 if (WCOREDUMP(status
)) {
355 ds_put_cstr(&ds
, ", core dumped");
358 ds_put_cstr(&ds
, "function not supported.");
363 /* Executes periodic maintenance activities required by the process module. */
368 char buf
[_POSIX_PIPE_BUF
];
370 if (!ovs_list_is_empty(&all_processes
) && read(fds
[0], buf
, sizeof buf
) > 0) {
373 LIST_FOR_EACH (p
, node
, &all_processes
) {
377 retval
= waitpid(p
->pid
, &status
, WNOHANG
);
378 } while (retval
== -1 && errno
== EINTR
);
379 if (retval
== p
->pid
) {
382 } else if (retval
< 0) {
383 VLOG_WARN("waitpid: %s", ovs_strerror(errno
));
394 /* Causes the next call to poll_block() to wake up when process 'p' has
397 process_wait(struct process
*p
)
401 poll_immediate_wake();
403 poll_fd_wait(fds
[0], POLLIN
);
411 process_search_path(const char *name
)
413 char *save_ptr
= NULL
;
417 if (strchr(name
, '/') || !getenv("PATH")) {
418 return stat(name
, &s
) == 0 ? xstrdup(name
) : NULL
;
421 path
= xstrdup(getenv("PATH"));
422 for (dir
= strtok_r(path
, ":", &save_ptr
); dir
;
423 dir
= strtok_r(NULL
, ":", &save_ptr
)) {
424 char *file
= xasprintf("%s/%s", dir
, name
);
425 if (stat(file
, &s
) == 0) {
436 sigchld_handler(int signr OVS_UNUSED
)
438 ignore(write(fds
[1], "", 1));