/*
- * 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.
#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
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]);
}
\f
/* Process title. */
int fds[2];
pid_t pid;
- if (pipe(fds) < 0) {
- ovs_fatal(errno, "pipe failed");
- }
+ xpipe(fds);
pid = fork();
if (pid > 0) {
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;
lockfile_postfork();
*fdp = fds[1];
} else {
- ovs_fatal(errno, "could not fork");
+ VLOG_FATAL("fork failed (%s)", strerror(errno));
}
return pid;
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);
} 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)) {
-/* 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.
{
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));
}
}
#include "poll-loop.h"
#include "shash.h"
#include "sset.h"
+#include "signals.h"
#include "socket-util.h"
#include "util.h"
#include "vlog.h"
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]);
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);
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]);
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 *
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);
}
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);
}
/*
- * 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.
#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",
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;
#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
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]);
}
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);
}
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));
+ }
+}
/*
- * 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.
#ifndef SIGNALS_H
#define SIGNALS_H 1
+#include <signal.h>
#include <stdbool.h>
void signal_init(void);
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 */
}
}
+void
+xpipe(int fds[2])
+{
+ if (pipe(fds)) {
+ VLOG_FATAL("failed to create pipe (%s)", strerror(errno));
+ }
+}
/*
- * 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.
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 */
#include <unistd.h>
#include "coverage.h"
#include "fatal-signal.h"
+#include "signals.h"
#include "util.h"
#include "vlog.h"
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
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;
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));
}
}
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
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)
{
/*
- * 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.
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
-/* 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.
#include "aes128.h"
#include "entropy.h"
#include "sha1.h"
+#include "timeval.h"
#include "util.h"
static struct aes128 key;
/* 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();
/*
- * 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.
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)
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);
*
* 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__)
/*
- * Copyright (c) 2009, 2010 Nicira Networks.
+ * Copyright (c) 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.
{
struct timeval tv;
- assert(!gettimeofday(&tv, NULL));
+ xgettimeofday(&tv);
return timeval_to_msec(&tv);
}
OFPT_ECHO_REQUEST, &request);
random_bytes(rq_hdr + 1, payload);
- gettimeofday(&start, NULL);
+ xgettimeofday(&start);
run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
- gettimeofday(&end, NULL);
+ xgettimeofday(&end);
rpy_hdr = reply->data;
if (reply->size != request->size
count, message_size, count * message_size);
open_vconn(argv[1], &vconn);
- gettimeofday(&start, NULL);
+ xgettimeofday(&start);
for (i = 0; i < count; i++) {
struct ofpbuf *request, *reply;
struct ofp_header *rq_hdr;
run(vconn_transact(vconn, request, &reply), "transact");
ofpbuf_delete(reply);
}
- gettimeofday(&end, NULL);
+ xgettimeofday(&end);
vconn_close(vconn);
duration = ((1000*(double)(end.tv_sec - start.tv_sec))
error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
if (error) {
- ovs_fatal(error, "could not create datapath");
+ VLOG_FATAL("could not create datapath (%s)", strerror(error));
}
/* Add ports to the datapath if requested by the user. */
error = netdev_open_default(port, &netdev);
if (error) {
- ovs_fatal(error, "%s: failed to open network device", port);
+ VLOG_FATAL("%s: failed to open network device (%s)",
+ port, strerror(error));
}
error = dpif_port_add(dpif, netdev, NULL);
if (error) {
- ovs_fatal(error, "failed to add %s as a port", port);
+ VLOG_FATAL("failed to add %s as a port (%s)",
+ port, strerror(error));
}
netdev_close(netdev);
/* Start OpenFlow processing. */
error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
if (error) {
- ovs_fatal(error, "could not initialize openflow switch");
+ VLOG_FATAL("could not initialize openflow switch (%s)",
+ strerror(error));
}
if (s.datapath_id) {
ofproto_set_datapath_id(ofproto, s.datapath_id);
s.serial_desc, s.dp_desc);
error = ofproto_set_snoops(ofproto, &s.snoops);
if (error) {
- ovs_fatal(error,
- "failed to configure controller snooping connections");
+ VLOG_FATAL("failed to configure controller snooping connections (%s)",
+ strerror(error));
}
memset(&nf_options, 0, sizeof nf_options);
nf_options.collectors = s.netflow;
error = ofproto_set_netflow(ofproto, &nf_options);
if (error) {
- ovs_fatal(error, "failed to configure NetFlow collectors");
+ VLOG_FATAL("failed to configure NetFlow collectors (%s)",
+ strerror(error));
}
ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
ofproto_set_fail_mode(ofproto, s.fail_mode);
while (!exiting && (s.run_forever || ofproto_is_alive(ofproto))) {
error = ofproto_run(ofproto);
if (error) {
- ovs_fatal(error, "unrecoverable datapath error");
+ VLOG_FATAL("unrecoverable datapath error (%s)", strerror(error));
}
unixctl_server_run(unixctl);
dp_run();
switch (c) {
case OPT_DATAPATH_ID:
if (!dpid_from_string(optarg, &s->datapath_id)) {
- ovs_fatal(0, "argument to --datapath-id must be "
- "exactly 16 hex digits and may not be all-zero");
+ VLOG_FATAL("argument to --datapath-id must be exactly 16 hex "
+ "digits and may not be all-zero");
}
break;
|| !strcmp(optarg, "secure")) {
s->fail_mode = OFPROTO_FAIL_SECURE;
} else {
- ovs_fatal(0, "--fail argument must be \"standalone\" "
- "or \"secure\"");
+ VLOG_FATAL("--fail argument must be \"standalone\" "
+ "or \"secure\"");
}
break;
case OPT_INACTIVITY_PROBE:
controller_opts.probe_interval = atoi(optarg);
if (controller_opts.probe_interval < 5) {
- ovs_fatal(0, "--inactivity-probe argument must be at least 5");
+ VLOG_FATAL("--inactivity-probe argument must be at least 5");
}
break;
} else {
s->max_idle = atoi(optarg);
if (s->max_idle < 1 || s->max_idle > 65535) {
- ovs_fatal(0, "--max-idle argument must be between 1 and "
- "65535 or the word 'permanent'");
+ VLOG_FATAL("--max-idle argument must be between 1 and "
+ "65535 or the word 'permanent'");
}
}
break;
case OPT_MAX_BACKOFF:
controller_opts.max_backoff = atoi(optarg);
if (controller_opts.max_backoff < 1) {
- ovs_fatal(0, "--max-backoff argument must be at least 1");
+ VLOG_FATAL("--max-backoff argument must be at least 1");
} else if (controller_opts.max_backoff > 3600) {
controller_opts.max_backoff = 3600;
}
if (optarg) {
controller_opts.rate_limit = atoi(optarg);
if (controller_opts.rate_limit < 1) {
- ovs_fatal(0, "--rate-limit argument must be at least 1");
+ VLOG_FATAL("--rate-limit argument must be at least 1");
}
} else {
controller_opts.rate_limit = 1000;
case OPT_BURST_LIMIT:
controller_opts.burst_limit = atoi(optarg);
if (controller_opts.burst_limit < 1) {
- ovs_fatal(0, "--burst-limit argument must be at least 1");
+ VLOG_FATAL("--burst-limit argument must be at least 1");
}
break;
argc -= optind;
argv += optind;
if (argc < 2) {
- ovs_fatal(0, "need at least two non-option arguments; "
- "use --help for usage");
+ VLOG_FATAL("need at least two non-option arguments; "
+ "use --help for usage");
}
/* Rate limiting. */
}
if (brc_open(&brc_sock)) {
- ovs_fatal(0, "could not open brcompat socket. Check "
- "\"brcompat\" kernel module.");
+ VLOG_FATAL("could not open brcompat socket. Check "
+ "\"brcompat\" kernel module.");
}
if (prune_timeout) {
error = nl_sock_create(NETLINK_ROUTE, &rtnl_sock);
if (error) {
- ovs_fatal(error, "could not create rtnetlink socket");
+ VLOG_FATAL("could not create rtnetlink socket (%s)",
+ strerror(error));
}
error = nl_sock_join_mcgroup(rtnl_sock, RTNLGRP_LINK);
if (error) {
- ovs_fatal(error, "could not join RTNLGRP_LINK multicast group");
+ VLOG_FATAL("could not join RTNLGRP_LINK multicast group (%s)",
+ strerror(error));
}
}
} else if (p[1] == 's') {
n++;
} else {
- ovs_fatal(0, "only '%%s' and '%%%%' allowed in --appctl-command");
+ VLOG_FATAL("only '%%s' and '%%%%' allowed in --appctl-command");
}
}
if (n != 1) {
- ovs_fatal(0, "'%%s' must appear exactly once in --appctl-command");
+ VLOG_FATAL("'%%s' must appear exactly once in --appctl-command");
}
}
argv += optind;
if (argc != 1) {
- ovs_fatal(0, "database socket is non-option argument; "
- "use --help for usage");
+ VLOG_FATAL("database socket is non-option argument; "
+ "use --help for usage");
}
return argv[0];
argv += optind;
if (argc != 1) {
- ovs_fatal(0, "database socket is only non-option argument; "
- "use --help for usage");
+ VLOG_FATAL("database socket is only non-option argument; "
+ "use --help for usage");
}
return argv[0];