summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/log.c
diff options
context:
space:
mode:
authorReyk Floeter <reyk@cvs.openbsd.org>2017-01-09 09:53:24 +0000
committerReyk Floeter <reyk@cvs.openbsd.org>2017-01-09 09:53:24 +0000
commit6c3279b5b9b31380828faf80eb546bda8bcab5d2 (patch)
treef8974f1760d6583ae631b13b6863fee8f08f8289 /usr.sbin/smtpd/log.c
parent9f83f3e0aeaffc9b01c3f76790a08bfdbd33f830 (diff)
smtpd joins the 7 other daemons that share the same log.c file.
The only major difference was the "log_trace" concept that is only used by smtpd - move it from log.c into util.c and make it a local concept. This also needed to rename the global "verbose" variable to "tracing" in a few places. OK krw@ gilles@ eric@
Diffstat (limited to 'usr.sbin/smtpd/log.c')
-rw-r--r--usr.sbin/smtpd/log.c139
1 files changed, 74 insertions, 65 deletions
diff --git a/usr.sbin/smtpd/log.c b/usr.sbin/smtpd/log.c
index ec30321df32..b4094db8030 100644
--- a/usr.sbin/smtpd/log.c
+++ b/usr.sbin/smtpd/log.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: log.c,v 1.17 2015/12/28 22:08:30 jung Exp $ */
+/* $OpenBSD: log.c,v 1.18 2017/01/09 09:53:23 reyk Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -11,50 +11,66 @@
* 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.
+ * 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 <sys/types.h>
-#include <sys/queue.h>
-#include <sys/tree.h>
-#include <sys/socket.h>
-
-#include <errno.h>
-#include <pwd.h>
-#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <string.h>
#include <syslog.h>
+#include <errno.h>
#include <time.h>
-#include "log.h"
-
-#define TRACE_DEBUG 0x1
-
-static int foreground;
-static int verbose;
-
-void vlog(int, const char *, va_list);
-void logit(int, const char *, ...)
- __attribute__((format (printf, 2, 3)));
-
+int debug;
+int verbose;
+const char *log_procname;
+
+void log_init(int, int);
+void log_procinit(const char *);
+void log_verbose(int);
+void log_warn(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void log_warnx(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void log_info(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+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)));
void
-log_init(int n_foreground)
+log_init(int n_debug, int facility)
{
extern char *__progname;
- foreground = n_foreground;
- if (!foreground)
- openlog(__progname, LOG_PID | LOG_NDELAY, LOG_MAIL);
+ debug = n_debug;
+ verbose = n_debug;
+ log_procinit(__progname);
+
+ if (!debug)
+ 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;
@@ -74,8 +90,9 @@ void
vlog(int pri, const char *fmt, va_list ap)
{
char *nfmt;
+ int saved_errno = errno;
- if (foreground) {
+ if (debug) {
/* best effort in out of mem situations */
if (asprintf(&nfmt, "%s\n", fmt) == -1) {
vfprintf(stderr, fmt, ap);
@@ -87,31 +104,36 @@ vlog(int pri, const char *fmt, va_list ap)
fflush(stderr);
} else
vsyslog(pri, fmt, ap);
-}
+ errno = saved_errno;
+}
void
log_warn(const char *emsg, ...)
{
- char *nfmt;
- va_list ap;
+ char *nfmt;
+ va_list ap;
+ int saved_errno = errno;
/* best effort to even work in out of memory situations */
if (emsg == NULL)
- logit(LOG_CRIT, "%s", strerror(errno));
+ logit(LOG_CRIT, "%s", strerror(saved_errno));
else {
va_start(ap, emsg);
- if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
+ if (asprintf(&nfmt, "%s: %s", emsg,
+ strerror(saved_errno)) == -1) {
/* we tried it... */
vlog(LOG_CRIT, emsg, ap);
- logit(LOG_CRIT, "%s", strerror(errno));
+ logit(LOG_CRIT, "%s", strerror(saved_errno));
} else {
vlog(LOG_CRIT, nfmt, ap);
free(nfmt);
}
va_end(ap);
}
+
+ errno = saved_errno;
}
void
@@ -139,19 +161,7 @@ log_debug(const char *emsg, ...)
{
va_list ap;
- if (verbose & TRACE_DEBUG) {
- va_start(ap, emsg);
- vlog(LOG_DEBUG, emsg, ap);
- va_end(ap);
- }
-}
-
-void
-log_trace(int mask, const char *emsg, ...)
-{
- va_list ap;
-
- if (verbose & mask) {
+ if (verbose > 1) {
va_start(ap, emsg);
vlog(LOG_DEBUG, emsg, ap);
va_end(ap);
@@ -159,23 +169,23 @@ log_trace(int mask, const char *emsg, ...)
}
static void
-fatal_arg(const char *emsg, va_list ap)
+vfatalc(int code, const char *emsg, va_list ap)
{
-#define FATALBUFSIZE 1024
- static char ebuffer[FATALBUFSIZE];
-
- if (emsg == NULL)
- (void)strlcpy(ebuffer, strerror(errno), sizeof ebuffer);
- else {
- if (errno) {
- (void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap);
- (void)strlcat(ebuffer, ": ", sizeof ebuffer);
- (void)strlcat(ebuffer, strerror(errno), sizeof ebuffer);
- }
- else
- (void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap);
+ static char s[BUFSIZ];
+ const char *sep;
+
+ if (emsg != NULL) {
+ (void)vsnprintf(s, sizeof(s), emsg, ap);
+ sep = ": ";
+ } else {
+ s[0] = '\0';
+ sep = "";
}
- logit(LOG_CRIT, "fatal: %s", ebuffer);
+ if (code)
+ logit(LOG_CRIT, "%s: %s%s%s",
+ log_procname, s, sep, strerror(code));
+ else
+ logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
}
void
@@ -184,7 +194,7 @@ fatal(const char *emsg, ...)
va_list ap;
va_start(ap, emsg);
- fatal_arg(emsg, ap);
+ vfatalc(errno, emsg, ap);
va_end(ap);
exit(1);
}
@@ -194,9 +204,8 @@ fatalx(const char *emsg, ...)
{
va_list ap;
- errno = 0;
va_start(ap, emsg);
- fatal_arg(emsg, ap);
+ vfatalc(0, emsg, ap);
va_end(ap);
exit(1);
}