diff options
Diffstat (limited to 'usr.sbin/syslogd')
-rw-r--r-- | usr.sbin/syslogd/Makefile | 5 | ||||
-rw-r--r-- | usr.sbin/syslogd/log.c | 208 | ||||
-rw-r--r-- | usr.sbin/syslogd/log.h | 50 | ||||
-rw-r--r-- | usr.sbin/syslogd/syslogd.c | 24 | ||||
-rw-r--r-- | usr.sbin/syslogd/syslogd.h | 6 |
5 files changed, 285 insertions, 8 deletions
diff --git a/usr.sbin/syslogd/Makefile b/usr.sbin/syslogd/Makefile index 236d683964b..232ea45c4f0 100644 --- a/usr.sbin/syslogd/Makefile +++ b/usr.sbin/syslogd/Makefile @@ -1,7 +1,8 @@ -# $OpenBSD: Makefile,v 1.7 2015/01/18 19:37:59 bluhm Exp $ +# $OpenBSD: Makefile,v 1.8 2017/03/16 23:55:19 bluhm Exp $ PROG= syslogd -SRCS= syslogd.c ttymsg.c privsep.c privsep_fdpass.c ringbuf.c evbuffer_tls.c +SRCS= evbuffer_tls.c log.c privsep.c privsep_fdpass.c ringbuf.c syslogd.c \ + ttymsg.c MAN= syslogd.8 syslog.conf.5 LDADD= -levent -ltls -lssl -lcrypto DPADD= ${LIBEVENT} ${LIBTLS} ${LIBSSL} ${LIBCRYPTO} diff --git a/usr.sbin/syslogd/log.c b/usr.sbin/syslogd/log.c new file mode 100644 index 00000000000..845966792a4 --- /dev/null +++ b/usr.sbin/syslogd/log.c @@ -0,0 +1,208 @@ +/* $OpenBSD: log.c,v 1.1 2017/03/16 23:55:19 bluhm Exp $ */ + +/* + * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> + * Copyright (c) 2017 Alexander Bluhm <bluhm@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 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 <stdio.h> +#include <string.h> +#include <syslog.h> +#include <time.h> + +#include "log.h" +#include "syslogd.h" + +static int debug; +static int verbose; +static int facility; +static const char *log_procname; + +void +log_init(int n_debug, int fac) +{ + extern char *__progname; + + debug = n_debug; + verbose = n_debug; + facility = fac; + log_procinit(__progname); + + tzset(); +} + +void +log_procinit(const char *procname) +{ + if (procname != NULL) + log_procname = procname; +} + +void +log_setdebug(int d) +{ + debug = d; +} + +int +log_getdebug(void) +{ + return (debug); +} + +void +log_setverbose(int v) +{ + verbose = v; +} + +int +log_getverbose(void) +{ + return (verbose); +} + +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 ebuf[ERRBUFSIZE]; + size_t l; + int saved_errno = errno; + + if (debug) { + l = snprintf(ebuf, sizeof(ebuf), "%s: ", log_procname); + if (l < sizeof(ebuf)) + vsnprintf(ebuf+l, sizeof(ebuf)-l, fmt, ap); + fprintf(stderr, "%s\n", ebuf); + fflush(stderr); + } else + vlogmsg(pri, log_procname, fmt, ap); + + errno = saved_errno; +} + +void +log_warn(const char *emsg, ...) +{ + char ebuf[ERRBUFSIZE]; + size_t l; + va_list ap; + int saved_errno = errno; + + /* best effort to even work in out of memory situations */ + if (emsg == NULL) + logit(LOG_ERR, "%s", strerror(saved_errno)); + else { + va_start(ap, emsg); + l = vsnprintf(ebuf, sizeof(ebuf), emsg, ap); + if (l < sizeof(ebuf)) + snprintf(ebuf+l, sizeof(ebuf)-l, ": %s", + strerror(saved_errno)); + logit(LOG_ERR, "%s", ebuf); + va_end(ap); + } + errno = saved_errno; +} + +void +log_warnx(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vlog(LOG_ERR, emsg, ap); + va_end(ap); +} + +void +log_info(int pri, const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vlog(pri, emsg, ap); + va_end(ap); +} + +void +log_debug(const char *emsg, ...) +{ + char ebuf[ERRBUFSIZE]; + va_list ap; + int saved_errno; + + if (verbose) { + saved_errno = errno; + va_start(ap, emsg); + vsnprintf(ebuf, sizeof(ebuf), emsg, ap); + fprintf(stderr, "%s\n", ebuf); + fflush(stderr); + va_end(ap); + errno = saved_errno; + } +} + +static void +vfatalc(int error, const char *emsg, va_list ap) +{ + char ebuf[ERRBUFSIZE]; + const char *sep; + + if (emsg != NULL) { + (void)vsnprintf(ebuf, sizeof(ebuf), emsg, ap); + sep = ": "; + } else { + ebuf[0] = '\0'; + sep = ""; + } + if (error) + logit(LOG_CRIT, "fatal in %s: %s%s%s", + log_procname, ebuf, sep, strerror(error)); + else + logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, ebuf); +} + +void +fatal(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfatalc(errno, emsg, ap); + va_end(ap); + die(0); +} + +void +fatalx(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfatalc(0, emsg, ap); + va_end(ap); + die(0); +} diff --git a/usr.sbin/syslogd/log.h b/usr.sbin/syslogd/log.h new file mode 100644 index 00000000000..aaa98b19d1d --- /dev/null +++ b/usr.sbin/syslogd/log.h @@ -0,0 +1,50 @@ +/* $OpenBSD: log.h,v 1.1 2017/03/16 23:55:19 bluhm Exp $ */ + +/* + * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> + * Copyright (c) 2017 Alexander Bluhm <bluhm@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 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. + */ + +#ifndef LOG_H +#define LOG_H + +#include <sys/cdefs.h> + +#include <stdarg.h> + +void log_init(int, int); +void log_procinit(const char *); +void log_setdebug(int); +int log_getdebug(void); +void log_setverbose(int); +int log_getverbose(void); +void log_warn(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_warnx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_info(int, const char *, ...) + __attribute__((__format__ (printf, 2, 3))); +void log_debug(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +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 *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); + +#endif /* LOG_H */ diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c index 5e4d1afbd4f..2e1c3ef43da 100644 --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: syslogd.c,v 1.229 2017/03/16 17:55:22 bluhm Exp $ */ +/* $OpenBSD: syslogd.c,v 1.230 2017/03/16 23:55:19 bluhm Exp $ */ /* * Copyright (c) 1983, 1988, 1993, 1994 @@ -99,6 +99,7 @@ #define SYSLOG_NAMES #include <sys/syslog.h> +#include "log.h" #include "syslogd.h" #include "evbuffer_tls.h" @@ -316,7 +317,6 @@ void ctlconn_cleanup(void); struct filed *cfline(char *, char *, char *); void cvthname(struct sockaddr *, char *, size_t); int decode(const char *, const CODE *); -void die(int); void markit(void); void fprintlog(struct filed *, int, char *); void init(void); @@ -464,6 +464,9 @@ main(int argc, char *argv[]) if (argc != optind) usage(); + log_init(Debug, LOG_SYSLOG); + log_procinit("syslogd"); + log_setdebug(1); if (Debug) setvbuf(stdout, NULL, _IOLBF, 0); @@ -695,8 +698,6 @@ main(int argc, char *argv[]) logdebug("off & running....\n"); - tzset(); - if (!Debug && !Foreground) { char c; @@ -788,6 +789,7 @@ main(int argc, char *argv[]) init(); + log_setdebug(0); Startup = 0; /* Allocate ctl socket reply buffer if we have a ctl socket */ @@ -1649,6 +1651,18 @@ printsys(char *msg) } } +void +vlogmsg(int pri, const char *proc, const char *fmt, va_list ap) +{ + char msg[ERRBUFSIZE]; + size_t l; + + l = snprintf(msg, sizeof(msg), "%s[%d]: ", proc, getpid()); + if (l < sizeof(msg)); + vsnprintf(msg + l, sizeof(msg) - l, fmt, ap); + logmsg(pri, msg, LocalHostName, ADDDATE); +} + struct timeval now; /* @@ -2265,7 +2279,7 @@ logerror_reason(const char *message, const char *reason) logmsg(LOG_SYSLOG|LOG_ERR, ebuf, LocalHostName, ADDDATE); } -void +__dead void die(int signo) { struct filed *f; diff --git a/usr.sbin/syslogd/syslogd.h b/usr.sbin/syslogd/syslogd.h index 2dd444d117b..c5d33c6b275 100644 --- a/usr.sbin/syslogd/syslogd.h +++ b/usr.sbin/syslogd/syslogd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: syslogd.h,v 1.26 2016/10/17 11:19:55 bluhm Exp $ */ +/* $OpenBSD: syslogd.h,v 1.27 2017/03/16 23:55:19 bluhm Exp $ */ /* * Copyright (c) 2003 Anil Madhavapeddy <anil@recoil.org> @@ -20,6 +20,8 @@ #include <sys/socket.h> #include <sys/uio.h> +#include <stdarg.h> + /* Privilege separation */ void priv_init(int, int, int, char **); __dead void priv_exec(char *, int, int, int, char **); @@ -49,6 +51,8 @@ extern char *path_ctlsock; #define MAXLINE 8192 /* maximum line length */ #define ERRBUFSIZE 256 void logdebug(const char *, ...) __attribute__((__format__ (printf, 1, 2))); +void vlogmsg(int pri, const char *, const char *, va_list); +__dead void die(int); extern int Debug; extern int Startup; |