diff options
author | Pierre-Yves Ritschard <pyr@cvs.openbsd.org> | 2007-10-25 06:03:32 +0000 |
---|---|---|
committer | Pierre-Yves Ritschard <pyr@cvs.openbsd.org> | 2007-10-25 06:03:32 +0000 |
commit | d0af1f2e22b9df78076ce52956f0eb9987400e9e (patch) | |
tree | 60f18ef493c7fe150a178ee1384d917333b7f00a /usr.sbin/ifstated | |
parent | 5eb6c1d31d9ae99c3a273c38fa757a7dce7729cf (diff) |
sync logging with most other daemons.
ok mcbride@, mpf@
Diffstat (limited to 'usr.sbin/ifstated')
-rw-r--r-- | usr.sbin/ifstated/Makefile | 4 | ||||
-rw-r--r-- | usr.sbin/ifstated/ifstated.c | 77 | ||||
-rw-r--r-- | usr.sbin/ifstated/ifstated.h | 17 | ||||
-rw-r--r-- | usr.sbin/ifstated/log.c | 158 | ||||
-rw-r--r-- | usr.sbin/ifstated/parse.y | 19 |
5 files changed, 191 insertions, 84 deletions
diff --git a/usr.sbin/ifstated/Makefile b/usr.sbin/ifstated/Makefile index 53d105ea514..59e31e5e05b 100644 --- a/usr.sbin/ifstated/Makefile +++ b/usr.sbin/ifstated/Makefile @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile,v 1.8 2006/11/26 11:31:13 deraadt Exp $ +# $OpenBSD: Makefile,v 1.9 2007/10/25 06:03:31 pyr Exp $ PROG= ifstated -SRCS= ifstated.c parse.y +SRCS= ifstated.c log.c parse.y CFLAGS+= -Wall -I${.CURDIR} CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes CFLAGS+= -Wmissing-declarations -Wredundant-decls diff --git a/usr.sbin/ifstated/ifstated.c b/usr.sbin/ifstated/ifstated.c index 5fed575a991..32ba24e428a 100644 --- a/usr.sbin/ifstated/ifstated.c +++ b/usr.sbin/ifstated/ifstated.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ifstated.c,v 1.30 2006/11/28 19:28:16 reyk Exp $ */ +/* $OpenBSD: ifstated.c,v 1.31 2007/10/25 06:03:31 pyr Exp $ */ /* * Copyright (c) 2004 Marco Pfatschbacher <mpf@openbsd.org> @@ -50,7 +50,6 @@ struct ifsd_config *conf = NULL, *newconf = NULL; int opts = 0; -int opt_debug = 0; int opt_inhibit = 0; char *configfile = "/etc/ifstated.conf"; struct event rt_msg_ev, sighup_ev, startup_ev, sigchld_ev; @@ -75,8 +74,6 @@ int state_change(void); void do_action(struct ifsd_action *); void remove_action(struct ifsd_action *, struct ifsd_state *); void remove_expression(struct ifsd_expression *, struct ifsd_state *); -void log_init(int); -void logit(int, const char *, ...); void usage(void) @@ -93,11 +90,14 @@ main(int argc, char *argv[]) { struct timeval tv; int ch; + int debug = 0; + + log_init(1); while ((ch = getopt(argc, argv, "dD:f:hniv")) != -1) { switch (ch) { case 'd': - opt_debug = 1; + debug = 1; break; case 'D': if (cmdline_symset(optarg) < 0) @@ -133,13 +133,13 @@ main(int argc, char *argv[]) exit(0); } - if (!opt_debug) { + if (!debug) { daemon(1, 0); setproctitle(NULL); } event_init(); - log_init(opt_debug); + log_init(debug); signal_set(&sigchld_ev, SIGCHLD, sigchld_handler, NULL); signal_add(&sigchld_ev, NULL); @@ -163,7 +163,7 @@ startup_handler(int fd, short event, void *arg) err(1, "no routing socket"); if (load_config() != 0) { - logit(IFSD_LOG_QUIET, "unable to load config"); + log_warnx("unable to load config"); exit(1); } @@ -173,15 +173,15 @@ startup_handler(int fd, short event, void *arg) signal_set(&sighup_ev, SIGHUP, sighup_handler, NULL); signal_add(&sighup_ev, NULL); - logit(IFSD_LOG_NORMAL, "started"); + log_info("started"); } void sighup_handler(int fd, short event, void *arg) { - logit(IFSD_LOG_NORMAL, "reloading config"); + log_info("reloading config"); if (load_config() != 0) - logit(IFSD_LOG_QUIET, "unable to reload config"); + log_warnx("unable to reload config"); } int @@ -198,8 +198,7 @@ load_config(void) adjust_external_expressions(&conf->always); eval_state(&conf->always); if (conf->curstate != NULL) { - logit(IFSD_LOG_NORMAL, - "initial state: %s", conf->curstate->name); + log_info("initial state: %s", conf->curstate->name); conf->curstate->entered = time(NULL); conf->nextstate = conf->curstate; conf->curstate = NULL; @@ -265,8 +264,7 @@ external_exec(struct ifsd_external *external, int async) int s; if (external->pid > 0) { - logit(IFSD_LOG_NORMAL, - "previous command %s [%d] still running, killing it", + log_info("previous command %s [%d] still running, killing it", external->command, external->pid); kill(external->pid, SIGKILL); waitpid(external->pid, &s, 0); @@ -274,10 +272,10 @@ external_exec(struct ifsd_external *external, int async) } argp[2] = external->command; - logit(IFSD_LOG_VERBOSE, "running %s", external->command); + log_debug("running %s", external->command); pid = fork(); if (pid < 0) { - logit(IFSD_LOG_QUIET, "fork error"); + log_warn("fork error"); } else if (pid == 0) { execv("/bin/sh", argp); _exit(1); @@ -340,8 +338,7 @@ check_external_status(struct ifsd_state *state) if (WIFEXITED(s)) status = WEXITSTATUS(s); else { - logit(IFSD_LOG_QUIET, - "%s exited abnormally", external->command); + log_warnx("%s exited abnormally", external->command); goto loop; } @@ -525,8 +522,7 @@ int state_change(void) { if (conf->nextstate != NULL && conf->curstate != conf->nextstate) { - logit(IFSD_LOG_NORMAL, "changing state to %s", - conf->nextstate->name); + log_info("changing state to %s", conf->nextstate->name); if (conf->curstate != NULL) { evtimer_del(&conf->curstate->ev); external_evtimer_setup(conf->curstate, @@ -553,7 +549,7 @@ do_action(struct ifsd_action *action) switch (action->type) { case IFSD_ACTION_COMMAND: - logit(IFSD_LOG_NORMAL, "running %s", action->act.command); + log_info("running %s", action->act.command); system(action->act.command); break; case IFSD_ACTION_CHANGESTATE: @@ -569,8 +565,7 @@ do_action(struct ifsd_action *action) } break; default: - logit(IFSD_LOG_DEBUG, "do_action: unknown action %d", - action->type); + log_debug("do_action: unknown action %d", action->type); break; } } @@ -698,37 +693,3 @@ remove_expression(struct ifsd_expression *expression, } free(expression); } - -void -log_init(int n_debug) -{ - extern char *__progname; - - if (!n_debug) - openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); -} - -void -logit(int level, const char *fmt, ...) -{ - va_list ap; - char *nfmt; - - if (conf != NULL && level > conf->loglevel) - return; - - va_start(ap, fmt); - if (opt_debug) { - /* best effort in out of mem situations */ - if (asprintf(&nfmt, "ifstated: %s\n", fmt) != -1) { - vfprintf(stderr, nfmt, ap); - free(nfmt); - } else { - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); - } - } else - vsyslog(LOG_NOTICE, fmt, ap); - - va_end(ap); -} diff --git a/usr.sbin/ifstated/ifstated.h b/usr.sbin/ifstated/ifstated.h index ba46132cd6e..85dace2ead3 100644 --- a/usr.sbin/ifstated/ifstated.h +++ b/usr.sbin/ifstated/ifstated.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ifstated.h,v 1.4 2004/03/10 00:13:38 deraadt Exp $ */ +/* $OpenBSD: ifstated.h,v 1.5 2007/10/25 06:03:31 pyr Exp $ */ /* * Copyright (c) 2004 Ryan McBride @@ -129,15 +129,18 @@ struct ifsd_config { #define IFSD_OPT_VERBOSE2 0x00000002 #define IFSD_OPT_NOACTION 0x00000004 int maxdepth; - u_int8_t loglevel; -#define IFSD_LOG_NONE 0 -#define IFSD_LOG_QUIET 1 -#define IFSD_LOG_NORMAL 2 -#define IFSD_LOG_VERBOSE 3 -#define IFSD_LOG_DEBUG 4 }; enum { IFSD_EVTIMER_ADD, IFSD_EVTIMER_DEL }; struct ifsd_config *parse_config(char *, int); int cmdline_symset(char *); void clear_config(struct ifsd_config *); + +/* log.c */ +void log_init(int); +void log_warn(const char *, ...); +void log_warnx(const char *, ...); +void log_info(const char *, ...); +void log_debug(const char *, ...); +__dead void fatal(const char *); +__dead void fatalx(const char *); diff --git a/usr.sbin/ifstated/log.c b/usr.sbin/ifstated/log.c new file mode 100644 index 00000000000..f9148d84160 --- /dev/null +++ b/usr.sbin/ifstated/log.c @@ -0,0 +1,158 @@ +/* $OpenBSD: log.c,v 1.1 2007/10/25 06:03:31 pyr Exp $ */ + +/* + * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syslog.h> + +void log_init(int); +void log_warn(const char *, ...); +void log_warnx(const char *, ...); +void log_info(const char *, ...); +void log_debug(const char *, ...); +__dead void fatal(const char *); +__dead void fatalx(const char *); + +int debug; + +void vlog(int, const char *, va_list); +void logit(int, const char *, ...); + +void +log_init(int n_debug) +{ + extern char *__progname; + + debug = n_debug; + + if (!debug) + openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); + + tzset(); +} + +void +logit(int pri, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlog(pri, fmt, ap); + va_end(ap); +} + +void +vlog(int pri, const char *fmt, va_list ap) +{ + char *nfmt; + + if (debug) { + /* best effort in out of mem situations */ + if (asprintf(&nfmt, "%s\n", fmt) == -1) { + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + } else { + vfprintf(stderr, nfmt, ap); + free(nfmt); + } + fflush(stderr); + } else + vsyslog(pri, fmt, ap); +} + + +void +log_warn(const char *emsg, ...) +{ + char *nfmt; + va_list ap; + + /* best effort to even work in out of memory situations */ + if (emsg == NULL) + logit(LOG_CRIT, "%s", strerror(errno)); + else { + va_start(ap, emsg); + + if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { + /* we tried it... */ + vlog(LOG_CRIT, emsg, ap); + logit(LOG_CRIT, "%s", strerror(errno)); + } else { + vlog(LOG_CRIT, nfmt, ap); + free(nfmt); + } + va_end(ap); + } +} + +void +log_warnx(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vlog(LOG_CRIT, emsg, ap); + va_end(ap); +} + +void +log_info(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vlog(LOG_INFO, emsg, ap); + va_end(ap); +} + +void +log_debug(const char *emsg, ...) +{ + va_list ap; + + if (debug) { + va_start(ap, emsg); + vlog(LOG_DEBUG, emsg, ap); + va_end(ap); + } +} + +void +fatal(const char *emsg) +{ + if (emsg == NULL) + logit(LOG_CRIT, "fatal: %s", strerror(errno)); + else + if (errno) + logit(LOG_CRIT, "fatal: %s: %s", + emsg, strerror(errno)); + else + logit(LOG_CRIT, "fatal: %s", emsg); + + exit(1); +} + +void +fatalx(const char *emsg) +{ + errno = 0; + fatal(emsg); +} diff --git a/usr.sbin/ifstated/parse.y b/usr.sbin/ifstated/parse.y index 345469c03ed..5f54814dff2 100644 --- a/usr.sbin/ifstated/parse.y +++ b/usr.sbin/ifstated/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.23 2007/10/21 08:29:34 pyr Exp $ */ +/* $OpenBSD: parse.y,v 1.24 2007/10/25 06:03:31 pyr Exp $ */ /* * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org> @@ -104,7 +104,7 @@ typedef struct { %token STATE INITSTATE %token LINK UP DOWN UNKNOWN ADDED REMOVED -%token IF RUN SETSTATE EVERY INIT LOGLEVEL +%token IF RUN SETSTATE EVERY INIT %left AND OR %left UNARY %token ERROR @@ -156,19 +156,6 @@ varset : STRING '=' string { conf_main : INITSTATE STRING { start_state = $2; } - | LOGLEVEL STRING { - if (!strcmp($2, "none")) - conf->loglevel = IFSD_LOG_NONE; - else if (!strcmp($2, "quiet")) - conf->loglevel = IFSD_LOG_QUIET; - else if (!strcmp($2, "normal")) - conf->loglevel = IFSD_LOG_NORMAL; - else if (!strcmp($2, "verbose")) - conf->loglevel = IFSD_LOG_VERBOSE; - else if (!strcmp($2, "debug")) - conf->loglevel = IFSD_LOG_DEBUG; - free($2); - } ; interface : STRING { @@ -397,7 +384,6 @@ lookup(char *s) { "init", INIT}, { "init-state", INITSTATE}, { "link", LINK}, - { "loglevel", LOGLEVEL}, { "removed", REMOVED}, { "run", RUN}, { "set-state", SETSTATE}, @@ -746,7 +732,6 @@ parse_config(char *filename, int opts) init_state(&conf->always); curaction = conf->always.always; - conf->loglevel = IFSD_LOG_NORMAL; conf->opts = opts; yyparse(); |