diff options
| author | Ben Pfaff <blp@nicira.com> | 2011-03-31 16:23:50 -0700 |
|---|---|---|
| committer | Ben Pfaff <blp@nicira.com> | 2011-04-04 10:58:55 -0700 |
| commit | 279c9e030818e039bb1c26be76e2cc1e8f6b13a6 (patch) | |
| tree | d0b34b231861a6611ebe227326365c778aa3eac8 /lib | |
| parent | fcaddd4dd1e8feefc09ea2664ad51fb669d136f8 (diff) | |
| download | openvswitch-279c9e030818e039bb1c26be76e2cc1e8f6b13a6.tar.gz | |
Log anything that could prevent a daemon from starting.
If a daemon doesn't start, we need to know why. Being able to
consistently consult the log to find out is helpful.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/command-line.c | 19 | ||||
| -rw-r--r-- | lib/daemon.c | 13 | ||||
| -rw-r--r-- | lib/entropy.c | 5 | ||||
| -rw-r--r-- | lib/fatal-signal.c | 11 | ||||
| -rw-r--r-- | lib/process.c | 22 | ||||
| -rw-r--r-- | lib/random.c | 7 | ||||
| -rw-r--r-- | lib/signals.c | 28 | ||||
| -rw-r--r-- | lib/signals.h | 6 | ||||
| -rw-r--r-- | lib/socket-util.c | 7 | ||||
| -rw-r--r-- | lib/socket-util.h | 4 | ||||
| -rw-r--r-- | lib/timeval.c | 25 | ||||
| -rw-r--r-- | lib/timeval.h | 4 | ||||
| -rw-r--r-- | lib/uuid.c | 7 | ||||
| -rw-r--r-- | lib/vlog.c | 27 | ||||
| -rw-r--r-- | lib/vlog.h | 13 |
15 files changed, 129 insertions, 69 deletions
diff --git a/lib/command-line.c b/lib/command-line.c index 23ed53fb4..9adf47f4d 100644 --- a/lib/command-line.c +++ b/lib/command-line.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010 Nicira Networks. + * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,9 @@ #include <limits.h> #include <stdlib.h> #include "util.h" +#include "vlog.h" + +VLOG_DEFINE_THIS_MODULE(command_line); /* Given the GNU-style long options in 'options', returns a string that may be * passed to getopt() with the corresponding short options. The caller is @@ -66,25 +69,25 @@ run_command(int argc, char *argv[], const struct command commands[]) if (!strcmp(p->name, argv[0])) { int n_arg = argc - 1; if (n_arg < p->min_args) { - ovs_fatal(0, "'%s' command requires at least %d arguments", - p->name, p->min_args); + VLOG_FATAL( "'%s' command requires at least %d arguments", + p->name, p->min_args); } else if (n_arg > p->max_args) { - ovs_fatal(0, "'%s' command takes at most %d arguments", - p->name, p->max_args); + VLOG_FATAL("'%s' command takes at most %d arguments", + p->name, p->max_args); } else { p->handler(argc, argv); if (ferror(stdout)) { - ovs_fatal(0, "write to stdout failed"); + VLOG_FATAL("write to stdout failed"); } if (ferror(stderr)) { - ovs_fatal(0, "write to stderr failed"); + VLOG_FATAL("write to stderr failed"); } return; } } } - ovs_fatal(0, "unknown command '%s'; use --help for help", argv[0]); + VLOG_FATAL("unknown command '%s'; use --help for help", argv[0]); } /* Process title. */ diff --git a/lib/daemon.c b/lib/daemon.c index cb440d168..64e2f9e9a 100644 --- a/lib/daemon.c +++ b/lib/daemon.c @@ -239,9 +239,7 @@ fork_and_wait_for_startup(int *fdp) int fds[2]; pid_t pid; - if (pipe(fds) < 0) { - ovs_fatal(errno, "pipe failed"); - } + xpipe(fds); pid = fork(); if (pid > 0) { @@ -266,7 +264,8 @@ fork_and_wait_for_startup(int *fdp) exit(WEXITSTATUS(status)); } - ovs_fatal(errno, "fork child failed to signal startup"); + VLOG_FATAL("fork child failed to signal startup (%s)", + strerror(errno)); } close(fds[0]); *fdp = -1; @@ -277,7 +276,7 @@ fork_and_wait_for_startup(int *fdp) lockfile_postfork(); *fdp = fds[1]; } else { - ovs_fatal(errno, "could not fork"); + VLOG_FATAL("fork failed (%s)", strerror(errno)); } return pid; @@ -292,7 +291,7 @@ fork_notify_startup(int fd) error = write_fully(fd, "", 1, &bytes_written); if (error) { - ovs_fatal(error, "could not write to pipe"); + VLOG_FATAL("pipe write failed (%s)", strerror(error)); } close(fd); @@ -346,7 +345,7 @@ monitor_daemon(pid_t daemon_pid) } while (retval == -1 && errno == EINTR); if (retval == -1) { - ovs_fatal(errno, "waitpid failed"); + VLOG_FATAL("waitpid failed (%s)", strerror(errno)); } else if (retval == daemon_pid) { char *s = process_status_msg(status); if (should_restart(status)) { diff --git a/lib/entropy.c b/lib/entropy.c index 1f1af50a5..f38655cd2 100644 --- a/lib/entropy.c +++ b/lib/entropy.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2008, 2009, 2010 Nicira Networks +/* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,6 +59,7 @@ get_entropy_or_die(void *buffer, size_t n) { int error = get_entropy(buffer, n); if (error) { - ovs_fatal(error, "%s: read error", urandom); + VLOG_FATAL("%s: read error (%s)", + urandom, ovs_retval_to_string(error)); } } diff --git a/lib/fatal-signal.c b/lib/fatal-signal.c index ed82173a4..c7e87dbe9 100644 --- a/lib/fatal-signal.c +++ b/lib/fatal-signal.c @@ -27,6 +27,7 @@ #include "poll-loop.h" #include "shash.h" #include "sset.h" +#include "signals.h" #include "socket-util.h" #include "util.h" #include "vlog.h" @@ -67,9 +68,7 @@ fatal_signal_init(void) inited = true; - if (pipe(signal_fds)) { - ovs_fatal(errno, "could not create pipe"); - } + xpipe(signal_fds); set_nonblocking(signal_fds[0]); set_nonblocking(signal_fds[1]); @@ -79,12 +78,10 @@ fatal_signal_init(void) struct sigaction old_sa; sigaddset(&fatal_signal_set, sig_nr); - if (sigaction(sig_nr, NULL, &old_sa)) { - ovs_fatal(errno, "sigaction"); - } + xsigaction(sig_nr, NULL, &old_sa); if (old_sa.sa_handler == SIG_DFL && signal(sig_nr, fatal_signal_handler) == SIG_ERR) { - ovs_fatal(errno, "signal"); + VLOG_FATAL("signal failed (%s)", strerror(errno)); } } atexit(atexit_handler); diff --git a/lib/process.c b/lib/process.c index f772833d8..55092f503 100644 --- a/lib/process.c +++ b/lib/process.c @@ -82,9 +82,7 @@ process_init(void) inited = true; /* Create notification pipe. */ - if (pipe(fds)) { - ovs_fatal(errno, "could not create pipe"); - } + xpipe(fds); set_nonblocking(fds[0]); set_nonblocking(fds[1]); @@ -93,9 +91,7 @@ process_init(void) sa.sa_handler = sigchld_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_NOCLDSTOP | SA_RESTART; - if (sigaction(SIGCHLD, &sa, NULL)) { - ovs_fatal(errno, "sigaction(SIGCHLD) failed"); - } + xsigaction(SIGCHLD, &sa, NULL); } char * @@ -638,9 +634,8 @@ static bool sigchld_is_blocked(void) { sigset_t sigs; - if (sigprocmask(SIG_SETMASK, NULL, &sigs)) { - ovs_fatal(errno, "sigprocmask"); - } + + xsigprocmask(SIG_SETMASK, NULL, &sigs); return sigismember(&sigs, SIGCHLD); } @@ -648,17 +643,14 @@ static void block_sigchld(sigset_t *oldsigs) { sigset_t sigchld; + sigemptyset(&sigchld); sigaddset(&sigchld, SIGCHLD); - if (sigprocmask(SIG_BLOCK, &sigchld, oldsigs)) { - ovs_fatal(errno, "sigprocmask"); - } + xsigprocmask(SIG_BLOCK, &sigchld, oldsigs); } static void unblock_sigchld(const sigset_t *oldsigs) { - if (sigprocmask(SIG_SETMASK, oldsigs, NULL)) { - ovs_fatal(errno, "sigprocmask"); - } + xsigprocmask(SIG_SETMASK, oldsigs, NULL); } diff --git a/lib/random.c b/lib/random.c index 6b0244601..a802bc743 100644 --- a/lib/random.c +++ b/lib/random.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010 Nicira Networks. + * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ #include <sys/time.h> #include "entropy.h" +#include "timeval.h" #include "util.h" /* This is the 32-bit PRNG recommended in G. Marsaglia, "Xorshift RNGs", @@ -48,9 +49,7 @@ random_init(void) struct timeval tv; uint32_t entropy; - if (gettimeofday(&tv, NULL) < 0) { - ovs_fatal(errno, "gettimeofday"); - } + xgettimeofday(&tv); get_entropy_or_die(&entropy, 4); seed = tv.tv_sec ^ tv.tv_usec ^ entropy; diff --git a/lib/signals.c b/lib/signals.c index 707bf8368..37f063732 100644 --- a/lib/signals.c +++ b/lib/signals.c @@ -25,6 +25,9 @@ #include "socket-util.h" #include "type-props.h" #include "util.h" +#include "vlog.h" + +VLOG_DEFINE_THIS_MODULE(signals); #if defined(_NSIG) #define N_SIGNALS _NSIG @@ -58,9 +61,7 @@ signal_init(void) static bool inited; if (!inited) { inited = true; - if (pipe(fds)) { - ovs_fatal(errno, "could not create pipe"); - } + xpipe(fds); set_nonblocking(fds[0]); set_nonblocking(fds[1]); } @@ -83,9 +84,7 @@ signal_register(int signr) sa.sa_handler = signal_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; - if (sigaction(signr, &sa, NULL)) { - ovs_fatal(errno, "sigaction(%d) failed", signr); - } + xsigaction(signr, &sa, NULL); /* Return structure. */ s = xmalloc(sizeof *s); @@ -148,3 +147,20 @@ signal_name(int signum) } return name; } + +void +xsigaction(int signum, const struct sigaction *new, struct sigaction *old) +{ + if (sigaction(signum, new, old)) { + VLOG_FATAL("sigaction(%s) failed (%s)", + signal_name(signum), strerror(errno)); + } +} + +void +xsigprocmask(int how, const sigset_t *new, sigset_t *old) +{ + if (sigprocmask(how, new, old)) { + VLOG_FATAL("sigprocmask failed (%s)", strerror(errno)); + } +} diff --git a/lib/signals.h b/lib/signals.h index 41066c40c..12fb31139 100644 --- a/lib/signals.h +++ b/lib/signals.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008 Nicira Networks. + * Copyright (c) 2008, 2011 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ #ifndef SIGNALS_H #define SIGNALS_H 1 +#include <signal.h> #include <stdbool.h> void signal_init(void); @@ -26,4 +27,7 @@ void signal_wait(struct signal *); const char *signal_name(int signum); +void xsigaction(int signum, const struct sigaction *, struct sigaction *old); +void xsigprocmask(int how, const sigset_t *, sigset_t *old); + #endif /* signals.h */ diff --git a/lib/socket-util.c b/lib/socket-util.c index e0f34e714..12bbc716d 100644 --- a/lib/socket-util.c +++ b/lib/socket-util.c @@ -767,3 +767,10 @@ get_mtime(const char *file_name, struct timespec *mtime) } } +void +xpipe(int fds[2]) +{ + if (pipe(fds)) { + VLOG_FATAL("failed to create pipe (%s)", strerror(errno)); + } +} diff --git a/lib/socket-util.h b/lib/socket-util.h index f4e617a65..8c5af3966 100644 --- a/lib/socket-util.h +++ b/lib/socket-util.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010 Nicira Networks. + * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,4 +48,6 @@ int write_fully(int fd, const void *, size_t, size_t *bytes_written); int fsync_parent_dir(const char *file_name); int get_mtime(const char *file_name, struct timespec *mtime); +void xpipe(int fds[2]); + #endif /* socket-util.h */ diff --git a/lib/timeval.c b/lib/timeval.c index 84c90f36c..099730e8f 100644 --- a/lib/timeval.c +++ b/lib/timeval.c @@ -26,6 +26,7 @@ #include <unistd.h> #include "coverage.h" #include "fatal-signal.h" +#include "signals.h" #include "util.h" #include "vlog.h" @@ -96,9 +97,7 @@ set_up_signal(int flags) sa.sa_handler = sigalrm_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = flags; - if (sigaction(SIGALRM, &sa, NULL)) { - ovs_fatal(errno, "sigaction(SIGALRM) failed"); - } + xsigaction(SIGALRM, &sa, NULL); } /* Remove SA_RESTART from the flags for SIGALRM, so that any system call that @@ -137,7 +136,7 @@ set_up_timer(void) struct itimerspec itimer; if (timer_create(monotonic_clock, NULL, &timer_id)) { - ovs_fatal(errno, "timer_create failed"); + VLOG_FATAL("timer_create failed (%s)", strerror(errno)); } itimer.it_interval.tv_sec = 0; @@ -145,7 +144,7 @@ set_up_timer(void) itimer.it_value = itimer.it_interval; if (timer_settime(timer_id, 0, &itimer, NULL)) { - ovs_fatal(errno, "timer_settime failed"); + VLOG_FATAL("timer_settime failed (%s)", strerror(errno)); } } @@ -364,17 +363,13 @@ block_sigalrm(sigset_t *oldsigs) sigset_t sigalrm; sigemptyset(&sigalrm); sigaddset(&sigalrm, SIGALRM); - if (sigprocmask(SIG_BLOCK, &sigalrm, oldsigs)) { - ovs_fatal(errno, "sigprocmask"); - } + xsigprocmask(SIG_BLOCK, &sigalrm, oldsigs); } static void unblock_sigalrm(const sigset_t *oldsigs) { - if (sigprocmask(SIG_SETMASK, oldsigs, NULL)) { - ovs_fatal(errno, "sigprocmask"); - } + xsigprocmask(SIG_SETMASK, oldsigs, NULL); } long long int @@ -389,6 +384,14 @@ timeval_to_msec(const struct timeval *tv) return (long long int) tv->tv_sec * 1000 + tv->tv_usec / 1000; } +void +xgettimeofday(struct timeval *tv) +{ + if (gettimeofday(tv, NULL) == -1) { + VLOG_FATAL("gettimeofday failed (%s)", strerror(errno)); + } +} + static long long int timeval_diff_msec(const struct timeval *a, const struct timeval *b) { diff --git a/lib/timeval.h b/lib/timeval.h index 904023302..303461adf 100644 --- a/lib/timeval.h +++ b/lib/timeval.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010 Nicira Networks. + * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,6 +60,8 @@ int time_poll(struct pollfd *, int n_pollfds, int timeout); long long int timespec_to_msec(const struct timespec *); long long int timeval_to_msec(const struct timeval *); +void xgettimeofday(struct timeval *); + #ifdef __cplusplus } #endif diff --git a/lib/uuid.c b/lib/uuid.c index e25902423..8b02bf556 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2008, 2009, 2010 Nicira Networks +/* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ #include "aes128.h" #include "entropy.h" #include "sha1.h" +#include "timeval.h" #include "util.h" static struct aes128 key; @@ -212,9 +213,7 @@ do_init(void) /* Get seed data. */ get_entropy_or_die(random_seed, sizeof random_seed); - if (gettimeofday(&now, NULL)) { - ovs_fatal(errno, "gettimeofday failed"); - } + xgettimeofday(&now); pid = getpid(); ppid = getppid(); uid = getuid(); diff --git a/lib/vlog.c b/lib/vlog.c index 2598111a4..4f6523f3d 100644 --- a/lib/vlog.c +++ b/lib/vlog.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010 Nicira Networks. + * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -680,6 +680,31 @@ vlog(const struct vlog_module *module, enum vlog_level level, va_end(args); } +void +vlog_fatal_valist(const struct vlog_module *module_, enum vlog_level level, + const char *message, va_list args) +{ + struct vlog_module *module = (struct vlog_module *) module_; + + /* Don't log this message to the console to avoid redundancy with the + * message written by the later ovs_fatal_valist(). */ + module->levels[VLF_CONSOLE] = VLL_EMER; + + vlog_valist(module, level, message, args); + ovs_fatal_valist(0, message, args); +} + +void +vlog_fatal(const struct vlog_module *module, enum vlog_level level, + const char *message, ...) +{ + va_list args; + + va_start(args, message); + vlog_fatal_valist(module, level, message, args); + va_end(args); +} + bool vlog_should_drop(const struct vlog_module *module, enum vlog_level level, struct vlog_rate_limit *rl) diff --git a/lib/vlog.h b/lib/vlog.h index 00d324f15..d982db2bd 100644 --- a/lib/vlog.h +++ b/lib/vlog.h @@ -149,14 +149,24 @@ const char *vlog_get_log_file(void); int vlog_set_log_file(const char *file_name); int vlog_reopen_log_file(void); -/* Function for actual logging. */ +/* Initialization. */ void vlog_init(void); void vlog_exit(void); + +/* Functions for actual logging. */ void vlog(const struct vlog_module *, enum vlog_level, const char *format, ...) PRINTF_FORMAT (3, 4); void vlog_valist(const struct vlog_module *, enum vlog_level, const char *, va_list) PRINTF_FORMAT (3, 0); + +void vlog_fatal(const struct vlog_module *, enum vlog_level, + const char *format, ...) + PRINTF_FORMAT (3, 4) NO_RETURN; +void vlog_fatal_valist(const struct vlog_module *, enum vlog_level, + const char *, va_list) + PRINTF_FORMAT (3, 0) NO_RETURN; + void vlog_rate_limit(const struct vlog_module *, enum vlog_level, struct vlog_rate_limit *, const char *, ...) PRINTF_FORMAT (4, 5); @@ -174,6 +184,7 @@ void vlog_rate_limit(const struct vlog_module *, enum vlog_level, * * Guaranteed to preserve errno. */ +#define VLOG_FATAL(...) vlog_fatal(THIS_MODULE, VLL_ERR, __VA_ARGS__) #define VLOG_EMER(...) VLOG(VLL_EMER, __VA_ARGS__) #define VLOG_ERR(...) VLOG(VLL_ERR, __VA_ARGS__) #define VLOG_WARN(...) VLOG(VLL_WARN, __VA_ARGS__) |
