diff options
author | Reyk Floeter <reyk@cvs.openbsd.org> | 2015-11-22 13:27:14 +0000 |
---|---|---|
committer | Reyk Floeter <reyk@cvs.openbsd.org> | 2015-11-22 13:27:14 +0000 |
commit | ae00fbc8a4af662a3b1361075ec3b2936485b844 (patch) | |
tree | 0cd66f8f60cb1ab7408bbb02fea2efa55d935312 /usr.sbin | |
parent | f06a2f825b72cc678ad244ed598777a44190f891 (diff) |
Update log.c: change fatal() and fatalx() into variadic functions,
include the process name, and replace all calls of fatal*(NULL) with
fatal(__func__) for better debugging.
OK benno@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/httpd/httpd.c | 9 | ||||
-rw-r--r-- | usr.sbin/httpd/httpd.h | 11 | ||||
-rw-r--r-- | usr.sbin/httpd/log.c | 71 | ||||
-rw-r--r-- | usr.sbin/httpd/parse.y | 8 | ||||
-rw-r--r-- | usr.sbin/httpd/proc.c | 20 | ||||
-rw-r--r-- | usr.sbin/relayd/log.c | 71 | ||||
-rw-r--r-- | usr.sbin/relayd/parse.y | 8 | ||||
-rw-r--r-- | usr.sbin/relayd/proc.c | 20 | ||||
-rw-r--r-- | usr.sbin/relayd/relayd.c | 9 | ||||
-rw-r--r-- | usr.sbin/relayd/relayd.h | 11 | ||||
-rw-r--r-- | usr.sbin/snmpd/log.c | 71 | ||||
-rw-r--r-- | usr.sbin/snmpd/parse.y | 12 | ||||
-rw-r--r-- | usr.sbin/snmpd/proc.c | 27 | ||||
-rw-r--r-- | usr.sbin/snmpd/snmpd.c | 11 | ||||
-rw-r--r-- | usr.sbin/snmpd/snmpd.h | 24 | ||||
-rw-r--r-- | usr.sbin/snmpd/traphandler.c | 7 |
16 files changed, 259 insertions, 131 deletions
diff --git a/usr.sbin/httpd/httpd.c b/usr.sbin/httpd/httpd.c index 2aa87776af4..bc11849ed68 100644 --- a/usr.sbin/httpd/httpd.c +++ b/usr.sbin/httpd/httpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: httpd.c,v 1.48 2015/11/21 12:40:59 reyk Exp $ */ +/* $OpenBSD: httpd.c,v 1.49 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org> @@ -38,6 +38,7 @@ #include <err.h> #include <errno.h> #include <event.h> +#include <syslog.h> #include <unistd.h> #include <ctype.h> #include <pwd.h> @@ -190,7 +191,8 @@ main(int argc, char *argv[]) } } - log_init(debug ? debug : 1); /* log to stderr until daemonized */ + /* log to stderr until daemonized */ + log_init(debug ? debug : 1, LOG_DAEMON); argc -= optind; if (argc > 0) @@ -219,7 +221,7 @@ main(int argc, char *argv[]) /* Configure the control socket */ ps->ps_csock.cs_name = NULL; - log_init(debug); + log_init(debug, LOG_DAEMON); log_verbose(verbose); if (!debug && daemon(1, 0) == -1) @@ -247,6 +249,7 @@ main(int argc, char *argv[]) proc_init(ps, procs, nitems(procs)); setproctitle("parent"); + log_procinit("parent"); if (pledge("stdio rpath wpath cpath inet proc ioctl sendfd", NULL) == -1) diff --git a/usr.sbin/httpd/httpd.h b/usr.sbin/httpd/httpd.h index 0c1a48309e1..1ed9c45b35b 100644 --- a/usr.sbin/httpd/httpd.h +++ b/usr.sbin/httpd/httpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: httpd.h,v 1.99 2015/11/21 12:40:59 reyk Exp $ */ +/* $OpenBSD: httpd.h,v 1.100 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2006 - 2015 Reyk Floeter <reyk@openbsd.org> @@ -670,7 +670,8 @@ const char *printb_flags(const uint32_t, const char *); void getmonotime(struct timeval *); /* log.c */ -void log_init(int); +void log_init(int, int); +void log_procinit(const char *); void log_verbose(int); void log_warn(const char *, ...) __attribute__((__format__ (printf, 1, 2))); @@ -684,8 +685,10 @@ void logit(int, const char *, ...) __attribute__((__format__ (printf, 2, 3))); void vlog(int, const char *, va_list) __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *); -__dead void fatalx(const char *); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); /* proc.c */ void proc_init(struct privsep *, struct privsep_proc *, unsigned int); diff --git a/usr.sbin/httpd/log.c b/usr.sbin/httpd/log.c index 124f0af65f9..543c467d2ac 100644 --- a/usr.sbin/httpd/log.c +++ b/usr.sbin/httpd/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.8 2015/11/21 13:46:29 reyk Exp $ */ +/* $OpenBSD: log.c,v 1.9 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -24,10 +24,12 @@ #include <errno.h> #include <time.h> -int debug; -int verbose; +int debug; +int verbose; +const char *log_procname; -void log_init(int); +void log_init(int, int); +void log_procinit(const char *); void log_verbose(int); void log_warn(const char *, ...) __attribute__((__format__ (printf, 1, 2))); @@ -41,24 +43,34 @@ void logit(int, const char *, ...) __attribute__((__format__ (printf, 2, 3))); void vlog(int, const char *, va_list) __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *); -__dead void fatalx(const char *); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); void -log_init(int n_debug) +log_init(int n_debug, int facility) { extern char *__progname; debug = n_debug; verbose = n_debug; + log_procinit(__progname); if (!debug) - openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); + openlog(__progname, LOG_PID | LOG_NDELAY, facility); tzset(); } void +log_procinit(const char *procname) +{ + if (procname != NULL) + log_procname = procname; +} + +void log_verbose(int v) { verbose = v; @@ -150,24 +162,45 @@ log_debug(const char *emsg, ...) } } -void -fatal(const char *emsg) +static void +vfatal(const char *emsg, va_list ap) { - if (emsg == NULL) - logit(LOG_CRIT, "fatal: %s", strerror(errno)); + static char s[BUFSIZ]; + const char *sep; + + if (emsg != NULL) { + (void)vsnprintf(s, sizeof(s), emsg, ap); + sep = ": "; + } else { + s[0] = '\0'; + sep = ""; + } + if (errno) + logit(LOG_CRIT, "fatal in %s: %s%s%s", + log_procname, s, sep, strerror(errno)); else - if (errno) - logit(LOG_CRIT, "fatal: %s: %s", - emsg, strerror(errno)); - else - logit(LOG_CRIT, "fatal: %s", emsg); + logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s); +} +void +fatal(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfatal(emsg, ap); + va_end(ap); exit(1); } void -fatalx(const char *emsg) +fatalx(const char *emsg, ...) { + va_list ap; + errno = 0; - fatal(emsg); + va_start(ap, emsg); + vfatal(emsg, ap); + va_end(ap); + exit(1); } diff --git a/usr.sbin/httpd/parse.y b/usr.sbin/httpd/parse.y index 8622d84b055..080f3192511 100644 --- a/usr.sbin/httpd/parse.y +++ b/usr.sbin/httpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.76 2015/08/20 22:39:29 deraadt Exp $ */ +/* $OpenBSD: parse.y,v 1.77 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2007 - 2015 Reyk Floeter <reyk@openbsd.org> @@ -1723,7 +1723,7 @@ host_v4(const char *s) return (NULL); if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(NULL); + fatal(__func__); sain = (struct sockaddr_in *)&h->ss; sain->sin_len = sizeof(struct sockaddr_in); sain->sin_family = AF_INET; @@ -1748,7 +1748,7 @@ host_v6(const char *s) hints.ai_flags = AI_NUMERICHOST; if (getaddrinfo(s, "0", &hints, &res) == 0) { if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(NULL); + fatal(__func__); sa_in6 = (struct sockaddr_in6 *)&h->ss; sa_in6->sin6_len = sizeof(struct sockaddr_in6); sa_in6->sin6_family = AF_INET6; @@ -1799,7 +1799,7 @@ host_dns(const char *s, struct addresslist *al, int max, res->ai_family != AF_INET6) continue; if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(NULL); + fatal(__func__); if (port != NULL) memcpy(&h->port, port, sizeof(h->port)); diff --git a/usr.sbin/httpd/proc.c b/usr.sbin/httpd/proc.c index 9ba7bbefe6d..8d25801b1e7 100644 --- a/usr.sbin/httpd/proc.c +++ b/usr.sbin/httpd/proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.c,v 1.9 2015/08/20 13:00:23 reyk Exp $ */ +/* $OpenBSD: proc.c,v 1.10 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2010 - 2014 Reyk Floeter <reyk@openbsd.org> @@ -346,6 +346,8 @@ proc_run(struct privsep *ps, struct privsep_proc *p, case -1: fatal("proc_run: cannot fork"); case 0: + log_procinit(p->p_title); + /* Set the process group of the current process */ setpgid(0, 0); break; @@ -357,10 +359,10 @@ proc_run(struct privsep *ps, struct privsep_proc *p, if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) { if (control_init(ps, &ps->ps_csock) == -1) - fatalx(p->p_title); + fatalx(__func__); TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry) if (control_init(ps, rcs) == -1) - fatalx(p->p_title); + fatalx(__func__); } /* Change root directory */ @@ -417,10 +419,10 @@ proc_run(struct privsep *ps, struct privsep_proc *p, if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) { TAILQ_INIT(&ctl_conns); if (control_listen(&ps->ps_csock) == -1) - fatalx(p->p_title); + fatalx(__func__); TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry) if (control_listen(rcs) == -1) - fatalx(p->p_title); + fatalx(__func__); } if (init != NULL) @@ -450,7 +452,7 @@ proc_dispatch(int fd, short event, void *arg) if (event & EV_READ) { if ((n = imsg_read(ibuf)) == -1) - fatal(title); + fatal(__func__); if (n == 0) { /* this pipe is dead, so remove the event handler */ event_del(&iev->ev); @@ -461,12 +463,12 @@ proc_dispatch(int fd, short event, void *arg) if (event & EV_WRITE) { if (msgbuf_write(&ibuf->w) <= 0 && errno != EAGAIN) - fatal(title); + fatal(__func__); } for (;;) { if ((n = imsg_get(ibuf, &imsg)) == -1) - fatal(title); + fatal(__func__); if (n == 0) break; @@ -498,7 +500,7 @@ proc_dispatch(int fd, short event, void *arg) log_warnx("%s: %s %d got invalid imsg %d from %s %d", __func__, title, ps->ps_instance + 1, imsg.hdr.type, p->p_title, p->p_instance); - fatalx(title); + fatalx(__func__); } imsg_free(&imsg); } diff --git a/usr.sbin/relayd/log.c b/usr.sbin/relayd/log.c index c137bdf7446..070065ac2f7 100644 --- a/usr.sbin/relayd/log.c +++ b/usr.sbin/relayd/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.29 2015/11/21 13:46:29 reyk Exp $ */ +/* $OpenBSD: log.c,v 1.30 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -24,10 +24,12 @@ #include <errno.h> #include <time.h> -int debug; -int verbose; +int debug; +int verbose; +const char *log_procname; -void log_init(int); +void log_init(int, int); +void log_procinit(const char *); void log_verbose(int); void log_warn(const char *, ...) __attribute__((__format__ (printf, 1, 2))); @@ -41,24 +43,34 @@ void logit(int, const char *, ...) __attribute__((__format__ (printf, 2, 3))); void vlog(int, const char *, va_list) __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *); -__dead void fatalx(const char *); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); void -log_init(int n_debug) +log_init(int n_debug, int facility) { extern char *__progname; debug = n_debug; verbose = n_debug; + log_procinit(__progname); if (!debug) - openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); + openlog(__progname, LOG_PID | LOG_NDELAY, facility); tzset(); } void +log_procinit(const char *procname) +{ + if (procname != NULL) + log_procname = procname; +} + +void log_verbose(int v) { verbose = v; @@ -150,24 +162,45 @@ log_debug(const char *emsg, ...) } } -void -fatal(const char *emsg) +static void +vfatal(const char *emsg, va_list ap) { - if (emsg == NULL) - logit(LOG_CRIT, "fatal: %s", strerror(errno)); + static char s[BUFSIZ]; + const char *sep; + + if (emsg != NULL) { + (void)vsnprintf(s, sizeof(s), emsg, ap); + sep = ": "; + } else { + s[0] = '\0'; + sep = ""; + } + if (errno) + logit(LOG_CRIT, "fatal in %s: %s%s%s", + log_procname, s, sep, strerror(errno)); else - if (errno) - logit(LOG_CRIT, "fatal: %s: %s", - emsg, strerror(errno)); - else - logit(LOG_CRIT, "fatal: %s", emsg); + logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s); +} +void +fatal(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfatal(emsg, ap); + va_end(ap); exit(1); } void -fatalx(const char *emsg) +fatalx(const char *emsg, ...) { + va_list ap; + errno = 0; - fatal(emsg); + va_start(ap, emsg); + vfatal(emsg, ap); + va_end(ap); + exit(1); } diff --git a/usr.sbin/relayd/parse.y b/usr.sbin/relayd/parse.y index c6c9d7b3f74..a6bf3908f3c 100644 --- a/usr.sbin/relayd/parse.y +++ b/usr.sbin/relayd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.205 2015/08/20 22:39:29 deraadt Exp $ */ +/* $OpenBSD: parse.y,v 1.206 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2007 - 2014 Reyk Floeter <reyk@openbsd.org> @@ -2835,7 +2835,7 @@ host_v4(const char *s) return (NULL); if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(NULL); + fatal(__func__); sain = (struct sockaddr_in *)&h->ss; sain->sin_len = sizeof(struct sockaddr_in); sain->sin_family = AF_INET; @@ -2857,7 +2857,7 @@ host_v6(const char *s) hints.ai_flags = AI_NUMERICHOST; if (getaddrinfo(s, "0", &hints, &res) == 0) { if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(NULL); + fatal(__func__); sa_in6 = (struct sockaddr_in6 *)&h->ss; sa_in6->sin6_len = sizeof(struct sockaddr_in6); sa_in6->sin6_family = AF_INET6; @@ -2904,7 +2904,7 @@ host_dns(const char *s, struct addresslist *al, int max, res->ai_family != AF_INET6) continue; if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(NULL); + fatal(__func__); if (port != NULL) bcopy(port, &h->port, sizeof(h->port)); diff --git a/usr.sbin/relayd/proc.c b/usr.sbin/relayd/proc.c index c796944827d..53c42df3bce 100644 --- a/usr.sbin/relayd/proc.c +++ b/usr.sbin/relayd/proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.c,v 1.21 2015/10/01 12:16:21 reyk Exp $ */ +/* $OpenBSD: proc.c,v 1.22 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2010 - 2014 Reyk Floeter <reyk@openbsd.org> @@ -346,6 +346,8 @@ proc_run(struct privsep *ps, struct privsep_proc *p, case -1: fatal("proc_run: cannot fork"); case 0: + log_procinit(p->p_title); + /* Set the process group of the current process */ setpgid(0, 0); break; @@ -357,10 +359,10 @@ proc_run(struct privsep *ps, struct privsep_proc *p, if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) { if (control_init(ps, &ps->ps_csock) == -1) - fatalx(p->p_title); + fatalx(__func__); TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry) if (control_init(ps, rcs) == -1) - fatalx(p->p_title); + fatalx(__func__); } /* Change root directory */ @@ -417,10 +419,10 @@ proc_run(struct privsep *ps, struct privsep_proc *p, if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) { TAILQ_INIT(&ctl_conns); if (control_listen(&ps->ps_csock) == -1) - fatalx(p->p_title); + fatalx(__func__); TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry) if (control_listen(rcs) == -1) - fatalx(p->p_title); + fatalx(__func__); } if (init != NULL) @@ -450,7 +452,7 @@ proc_dispatch(int fd, short event, void *arg) if (event & EV_READ) { if ((n = imsg_read(ibuf)) == -1) - fatal(title); + fatal(__func__); if (n == 0) { /* this pipe is dead, so remove the event handler */ event_del(&iev->ev); @@ -461,12 +463,12 @@ proc_dispatch(int fd, short event, void *arg) if (event & EV_WRITE) { if (msgbuf_write(&ibuf->w) <= 0 && errno != EAGAIN) - fatal(title); + fatal(__func__); } for (;;) { if ((n = imsg_get(ibuf, &imsg)) == -1) - fatal(title); + fatal(__func__); if (n == 0) break; @@ -498,7 +500,7 @@ proc_dispatch(int fd, short event, void *arg) log_warnx("%s: %s %d got invalid imsg %d from %s %d", __func__, title, ps->ps_instance + 1, imsg.hdr.type, p->p_title, p->p_instance); - fatalx(title); + fatalx(__func__); } imsg_free(&imsg); } diff --git a/usr.sbin/relayd/relayd.c b/usr.sbin/relayd/relayd.c index e1881b4010f..b190faf1a5a 100644 --- a/usr.sbin/relayd/relayd.c +++ b/usr.sbin/relayd/relayd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.c,v 1.145 2015/11/19 21:32:53 mmcc Exp $ */ +/* $OpenBSD: relayd.c,v 1.146 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2007 - 2014 Reyk Floeter <reyk@openbsd.org> @@ -33,6 +33,7 @@ #include <fcntl.h> #include <getopt.h> #include <fnmatch.h> +#include <syslog.h> #include <err.h> #include <errno.h> #include <event.h> @@ -191,7 +192,8 @@ main(int argc, char *argv[]) } } - log_init(debug ? debug : 1); /* log to stderr until daemonized */ + /* log to stderr until daemonized */ + log_init(debug ? debug : 1, LOG_DAEMON); argc -= optind; if (argc > 0) @@ -225,7 +227,7 @@ main(int argc, char *argv[]) /* Configure the control socket */ ps->ps_csock.cs_name = RELAYD_SOCKET; - log_init(debug); + log_init(debug, LOG_DAEMON); log_verbose(verbose); if (!debug && daemon(1, 0) == -1) @@ -243,6 +245,7 @@ main(int argc, char *argv[]) proc_init(ps, procs, nitems(procs)); setproctitle("parent"); + log_procinit("parent"); event_init(); diff --git a/usr.sbin/relayd/relayd.h b/usr.sbin/relayd/relayd.h index e0f7cb24800..0492e24d2eb 100644 --- a/usr.sbin/relayd/relayd.h +++ b/usr.sbin/relayd/relayd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.h,v 1.216 2015/11/21 12:37:42 reyk Exp $ */ +/* $OpenBSD: relayd.h,v 1.217 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2006 - 2015 Reyk Floeter <reyk@openbsd.org> @@ -1332,7 +1332,8 @@ void shuffle_init(struct shuffle *); u_int16_t shuffle_generate16(struct shuffle *); /* log.c */ -void log_init(int); +void log_init(int, int); +void log_procinit(const char *); void log_verbose(int); void log_warn(const char *, ...) __attribute__((__format__ (printf, 1, 2))); @@ -1346,8 +1347,10 @@ void logit(int, const char *, ...) __attribute__((__format__ (printf, 2, 3))); void vlog(int, const char *, va_list) __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *); -__dead void fatalx(const char *); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); /* proc.c */ void proc_init(struct privsep *, struct privsep_proc *, u_int); diff --git a/usr.sbin/snmpd/log.c b/usr.sbin/snmpd/log.c index 83d82225348..2ebd17d0dfe 100644 --- a/usr.sbin/snmpd/log.c +++ b/usr.sbin/snmpd/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.10 2015/11/21 13:46:29 reyk Exp $ */ +/* $OpenBSD: log.c,v 1.11 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -24,10 +24,12 @@ #include <errno.h> #include <time.h> -int debug; -int verbose; +int debug; +int verbose; +const char *log_procname; -void log_init(int); +void log_init(int, int); +void log_procinit(const char *); void log_verbose(int); void log_warn(const char *, ...) __attribute__((__format__ (printf, 1, 2))); @@ -41,24 +43,34 @@ void logit(int, const char *, ...) __attribute__((__format__ (printf, 2, 3))); void vlog(int, const char *, va_list) __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *); -__dead void fatalx(const char *); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); void -log_init(int n_debug) +log_init(int n_debug, int facility) { extern char *__progname; debug = n_debug; verbose = n_debug; + log_procinit(__progname); if (!debug) - openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); + openlog(__progname, LOG_PID | LOG_NDELAY, facility); tzset(); } void +log_procinit(const char *procname) +{ + if (procname != NULL) + log_procname = procname; +} + +void log_verbose(int v) { verbose = v; @@ -150,24 +162,45 @@ log_debug(const char *emsg, ...) } } -void -fatal(const char *emsg) +static void +vfatal(const char *emsg, va_list ap) { - if (emsg == NULL) - logit(LOG_CRIT, "fatal: %s", strerror(errno)); + static char s[BUFSIZ]; + const char *sep; + + if (emsg != NULL) { + (void)vsnprintf(s, sizeof(s), emsg, ap); + sep = ": "; + } else { + s[0] = '\0'; + sep = ""; + } + if (errno) + logit(LOG_CRIT, "fatal in %s: %s%s%s", + log_procname, s, sep, strerror(errno)); else - if (errno) - logit(LOG_CRIT, "fatal: %s: %s", - emsg, strerror(errno)); - else - logit(LOG_CRIT, "fatal: %s", emsg); + logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s); +} +void +fatal(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfatal(emsg, ap); + va_end(ap); exit(1); } void -fatalx(const char *emsg) +fatalx(const char *emsg, ...) { + va_list ap; + errno = 0; - fatal(emsg); + va_start(ap, emsg); + vfatal(emsg, ap); + va_end(ap); + exit(1); } diff --git a/usr.sbin/snmpd/parse.y b/usr.sbin/snmpd/parse.y index d3d3fbba19e..5e288eee9a0 100644 --- a/usr.sbin/snmpd/parse.y +++ b/usr.sbin/snmpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.37 2015/02/08 04:50:32 reyk Exp $ */ +/* $OpenBSD: parse.y,v 1.38 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org> @@ -1103,7 +1103,7 @@ host_v4(const char *s) return (NULL); if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(NULL); + fatal(__func__); sain = (struct sockaddr_in *)&h->ss; sain->sin_len = sizeof(struct sockaddr_in); sain->sin_family = AF_INET; @@ -1125,7 +1125,7 @@ host_v6(const char *s) hints.ai_flags = AI_NUMERICHOST; if (getaddrinfo(s, "0", &hints, &res) == 0) { if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(NULL); + fatal(__func__); sa_in6 = (struct sockaddr_in6 *)&h->ss; sa_in6->sin6_len = sizeof(struct sockaddr_in6); sa_in6->sin6_family = AF_INET6; @@ -1169,17 +1169,17 @@ host_dns(const char *s, struct addresslist *al, int max, res->ai_family != AF_INET6) continue; if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(NULL); + fatal(__func__); h->port = port; if (oid != NULL) { if ((h->sa_oid = calloc(1, sizeof(*oid))) == NULL) - fatal(NULL); + fatal(__func__); bcopy(oid, h->sa_oid, sizeof(*oid)); } if (cmn != NULL) { if ((h->sa_community = strdup(cmn)) == NULL) - fatal(NULL); + fatal(__func__); } h->ss.ss_family = res->ai_family; diff --git a/usr.sbin/snmpd/proc.c b/usr.sbin/snmpd/proc.c index 70ad0087863..3dcde635592 100644 --- a/usr.sbin/snmpd/proc.c +++ b/usr.sbin/snmpd/proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.c,v 1.14 2015/10/14 14:51:57 reyk Exp $ */ +/* $OpenBSD: proc.c,v 1.15 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2010 - 2014 Reyk Floeter <reyk@openbsd.org> @@ -21,12 +21,6 @@ #include <sys/queue.h> #include <sys/socket.h> #include <sys/wait.h> -#include <sys/tree.h> - -#include <net/if.h> -#include <netinet/in.h> -#include <netinet/ip.h> -#include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> @@ -36,6 +30,7 @@ #include <signal.h> #include <pwd.h> #include <event.h> +#include <imsg.h> #include "snmpd.h" @@ -351,6 +346,8 @@ proc_run(struct privsep *ps, struct privsep_proc *p, case -1: fatal("proc_run: cannot fork"); case 0: + log_procinit(p->p_title); + /* Set the process group of the current process */ setpgid(0, 0); break; @@ -362,10 +359,10 @@ proc_run(struct privsep *ps, struct privsep_proc *p, if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) { if (control_init(ps, &ps->ps_csock) == -1) - fatalx(p->p_title); + fatalx(__func__); TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry) if (control_init(ps, rcs) == -1) - fatalx(p->p_title); + fatalx(__func__); } /* Change root directory */ @@ -422,10 +419,10 @@ proc_run(struct privsep *ps, struct privsep_proc *p, if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) { TAILQ_INIT(&ctl_conns); if (control_listen(&ps->ps_csock) == -1) - fatalx(p->p_title); + fatalx(__func__); TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry) if (control_listen(rcs) == -1) - fatalx(p->p_title); + fatalx(__func__); } if (init != NULL) @@ -455,7 +452,7 @@ proc_dispatch(int fd, short event, void *arg) if (event & EV_READ) { if ((n = imsg_read(ibuf)) == -1) - fatal(title); + fatal(__func__); if (n == 0) { /* this pipe is dead, so remove the event handler */ event_del(&iev->ev); @@ -466,12 +463,12 @@ proc_dispatch(int fd, short event, void *arg) if (event & EV_WRITE) { if (msgbuf_write(&ibuf->w) <= 0 && errno != EAGAIN) - fatal(title); + fatal(__func__); } for (;;) { if ((n = imsg_get(ibuf, &imsg)) == -1) - fatal(title); + fatal(__func__); if (n == 0) break; @@ -503,7 +500,7 @@ proc_dispatch(int fd, short event, void *arg) log_warnx("%s: %s %d got invalid imsg %d from %s %d", __func__, title, ps->ps_instance + 1, imsg.hdr.type, p->p_title, p->p_instance); - fatalx(title); + fatalx(__func__); } imsg_free(&imsg); } diff --git a/usr.sbin/snmpd/snmpd.c b/usr.sbin/snmpd/snmpd.c index f2df1ee7f68..e176b20bcd8 100644 --- a/usr.sbin/snmpd/snmpd.c +++ b/usr.sbin/snmpd/snmpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpd.c,v 1.29 2015/06/03 02:24:36 millert Exp $ */ +/* $OpenBSD: snmpd.c,v 1.30 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org> @@ -33,6 +33,7 @@ #include <errno.h> #include <event.h> #include <signal.h> +#include <syslog.h> #include <unistd.h> #include <fcntl.h> #include <pwd.h> @@ -146,7 +147,9 @@ main(int argc, char *argv[]) struct privsep *ps; smi_init(); - log_init(1); /* log to stderr until daemonized */ + + /* log to stderr until daemonized */ + log_init(1, LOG_DAEMON); while ((c = getopt(argc, argv, "dD:nNf:v")) != -1) { switch (c) { @@ -199,7 +202,7 @@ main(int argc, char *argv[]) if ((ps->ps_pw = getpwnam(SNMPD_USER)) == NULL) errx(1, "unknown user %s", SNMPD_USER); - log_init(debug); + log_init(debug, LOG_DAEMON); log_verbose(verbose); if (!debug && daemon(0, 0) == -1) @@ -215,6 +218,8 @@ main(int argc, char *argv[]) proc_init(ps, procs, nitems(procs)); setproctitle("parent"); + log_procinit("parent"); + log_info("startup"); event_init(); diff --git a/usr.sbin/snmpd/snmpd.h b/usr.sbin/snmpd/snmpd.h index e0296b37f66..d98905ea2cf 100644 --- a/usr.sbin/snmpd/snmpd.h +++ b/usr.sbin/snmpd/snmpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpd.h,v 1.63 2015/11/21 13:06:22 reyk Exp $ */ +/* $OpenBSD: snmpd.h,v 1.64 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org> @@ -20,17 +20,22 @@ #ifndef SNMPD_H #define SNMPD_H +#include <net/if.h> +#include <net/if_dl.h> #include <netinet/in.h> #include <netinet/if_ether.h> -#include <net/if_dl.h> +#include <netinet/in.h> +#include <netinet/ip.h> +#include <arpa/inet.h> #include <net/pfvar.h> #include <net/route.h> -#include "ber.h" -#include <snmp.h> - +#include <stdio.h> #include <imsg.h> +#include "ber.h" +#include "snmp.h" + /* * common definitions for snmpd */ @@ -587,7 +592,8 @@ struct snmpd *parse_config(const char *, u_int); int cmdline_symset(char *); /* log.c */ -void log_init(int); +void log_init(int, int); +void log_procinit(const char *); void log_verbose(int); void log_warn(const char *, ...) __attribute__((__format__ (printf, 1, 2))); @@ -601,8 +607,10 @@ void logit(int, const char *, ...) __attribute__((__format__ (printf, 2, 3))); void vlog(int, const char *, va_list) __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *); -__dead void fatalx(const char *); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); /* kroute.c */ void kr_init(void); diff --git a/usr.sbin/snmpd/traphandler.c b/usr.sbin/snmpd/traphandler.c index 2a2f2c94a0b..18451f3a699 100644 --- a/usr.sbin/snmpd/traphandler.c +++ b/usr.sbin/snmpd/traphandler.c @@ -1,4 +1,4 @@ -/* $OpenBSD: traphandler.c,v 1.2 2015/01/16 00:05:13 deraadt Exp $ */ +/* $OpenBSD: traphandler.c,v 1.3 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2014 Bret Stephen Lambert <blambert@openbsd.org> * @@ -35,6 +35,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <syslog.h> #include <unistd.h> #include <pwd.h> @@ -295,7 +296,9 @@ traphandler_fork_handler(struct privsep_proc *p, struct imsg *imsg) fatal("traphandler_fork_handler: cannot drop privileges"); closefrom(STDERR_FILENO + 1); - log_init(debug); + + log_init(debug, LOG_DAEMON); + log_procinit(p->p_title); n = IMSG_DATA_SIZE(imsg); |