1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
/* $Id: error.c,v 1.26 2005/03/18 20:21:55 gerhard Exp $ */
/* Copyright Gerhard Rieger 2001-2005 */
/* Published under the GNU General Public License V.2, see file COPYING */
/* the logging subsystem */
#include "config.h"
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#if HAVE_SYSLOG_H
#include <syslog.h>
#endif
#include <time.h> /* time_t, strftime() */
#include <sys/time.h> /* gettimeofday() */
#include <stdio.h>
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "mytypes.h"
#include "compat.h"
#include "utils.h"
#include "error.h"
/* translate MSG level to SYSLOG level */
int syslevel[] = {
LOG_DEBUG,
LOG_INFO,
LOG_NOTICE,
LOG_WARNING,
LOG_ERR,
LOG_CRIT };
struct diag_opts {
const char *progname;
int msglevel;
int exitlevel;
int logstderr;
int syslog;
FILE *logfile;
int logfacility;
bool micros;
int exitstatus; /* pass signal number to error exit */
} ;
struct diag_opts diagopts =
{ NULL, E_ERROR, E_ERROR, 1, 0, NULL, LOG_DAEMON, false, 0 } ;
static void _msg(int level, const char *buff, const char *syslp);
static struct wordent facilitynames[] = {
{"auth", (void *)LOG_AUTH},
#ifdef LOG_AUTHPRIV
{"authpriv", (void *)LOG_AUTHPRIV},
#endif
#ifdef LOG_CONSOLE
{"console", (void *)LOG_CONSOLE},
#endif
{"cron", (void *)LOG_CRON},
{"daemon", (void *)LOG_DAEMON},
#ifdef LOG_FTP
{"ftp", (void *)LOG_FTP},
#endif
{"kern", (void *)LOG_KERN},
{"local0", (void *)LOG_LOCAL0},
{"local1", (void *)LOG_LOCAL1},
{"local2", (void *)LOG_LOCAL2},
{"local3", (void *)LOG_LOCAL3},
{"local4", (void *)LOG_LOCAL4},
{"local5", (void *)LOG_LOCAL5},
{"local6", (void *)LOG_LOCAL6},
{"local7", (void *)LOG_LOCAL7},
{"lpr", (void *)LOG_LPR},
{"mail", (void *)LOG_MAIL},
{"news", (void *)LOG_NEWS},
#ifdef LOG_SECURITY
{"security", (void *)LOG_SECURITY},
#endif
{"syslog", (void *)LOG_SYSLOG},
{"user", (void *)LOG_USER},
{"uucp", (void *)LOG_UUCP}
} ;
void diag_set(char what, const char *arg) {
switch (what) {
const struct wordent *keywd;
case 'y': diagopts.syslog = true;
if (arg && arg[0]) {
if ((keywd =
keyw(facilitynames, arg,
sizeof(facilitynames)/sizeof(struct wordent))) == NULL) {
Error1("unknown syslog facility \"%s\"", arg);
} else {
diagopts.logfacility = (int)keywd->desc;
}
}
openlog(diagopts.progname, LOG_PID, diagopts.logfacility);
diagopts.logstderr = false; break;
case 'f': if ((diagopts.logfile = fopen(arg, "a")) == NULL) {
Error2("cannot open log file \"%s\": %s", arg, strerror(errno));
break;
} else {
diagopts.logstderr = false; break;
}
case 's': diagopts.logstderr = true; break; /* logging to stderr is default */
case 'p': diagopts.progname = arg;
openlog(diagopts.progname, LOG_PID, diagopts.logfacility);
break;
case 'd': --diagopts.msglevel; break;
case 'u': diagopts.micros = true; break;
default: msg(E_ERROR, "unknown diagnostic option %c", what);
}
}
void diag_set_int(char what, int arg) {
switch (what) {
case 'D': diagopts.msglevel = arg; break;
case 'e': diagopts.exitlevel = arg; break;
case 'x': diagopts.exitstatus = arg; break;
default: msg(E_ERROR, "unknown diagnostic option %c", what);
}
}
int diag_get_int(char what) {
switch (what) {
case 'y': return diagopts.syslog;
case 's': return diagopts.logstderr;
case 'd': case 'D': return diagopts.msglevel;
case 'e': return diagopts.exitlevel;
}
return -1;
}
const char *diag_get_string(char what) {
switch (what) {
case 'p': return diagopts.progname;
}
return NULL;
}
/* Linux and AIX syslog format:
Oct 4 17:10:37 hostname socat[52798]: D signal(13, 1)
*/
void msg(int level, const char *format, ...) {
#if HAVE_GETTIMEOFDAY || 1
struct timeval now;
int result;
time_t nowt;
#else /* !HAVE_GETTIMEOFDAY */
time_t now;
#endif /* !HAVE_GETTIMEOFDAY */
#define BUFLEN 256
char buff[BUFLEN], *bufp, *syslp;
size_t bytes;
va_list ap;
if (level < diagopts.msglevel) return;
va_start(ap, format);
#if HAVE_GETTIMEOFDAY || 1
result = gettimeofday(&now, NULL);
if (result < 0) {
/* invoking msg() might create endless recursion; by hand instead */
sprintf(buff, "cannot read time: %s["F_pid"] E %s",
diagopts.progname, getpid(), strerror(errno));
_msg(LOG_ERR, buff, strstr(buff, " E "+1));
strcpy(buff, "unknown time "); bytes = 20;
} else {
nowt = now.tv_sec;
#if HAVE_STRFTIME
if (diagopts.micros) {
bytes = strftime(buff, 20, "%Y/%m/%d %H:%M:%S", localtime(&nowt));
bytes += sprintf(buff+19, ".%06lu ", now.tv_usec);
} else {
bytes =
strftime(buff, 21, "%Y/%m/%d %H:%M:%S ", localtime(&nowt));
}
#else
strcpy(buff, ctime(&nowt));
bytes = strlen(buff);
#endif
}
#else /* !HAVE_GETTIMEOFDAY */
now = time(NULL); if (now == (time_t)-1) {
/* invoking msg() might create endless recursion; by hand instead */
sprintf(buff, "cannot read time: %s["F_pid"] E %s",
diagopts.progname, getpid(), strerror(errno));
_msg(LOG_ERR, buff, strstr(buff, " E "+1));
strcpy(buff, "unknown time "); bytes = 20;
} else {
#if HAVE_STRFTIME
bytes = strftime(buff, 21, "%Y/%m/%d %H:%M:%S ", localtime(&now));
#else
strcpy(buff, ctime(&now));
bytes = strlen(buff);
#endif
}
#endif /* !HAVE_GETTIMEOFDAY */
bufp = buff + bytes;
bytes = sprintf(bufp, "%s["F_pid"] ", diagopts.progname, getpid());
bufp += bytes;
syslp = bufp;
*bufp++ = "DINWEF"[level];
*bufp++ = ' ';
vsnprintf(bufp, BUFLEN-(bufp-buff)-1, format, ap);
strcat(bufp, "\n");
_msg(level, buff, syslp);
if (level >= diagopts.exitlevel) {
va_end(ap);
if (E_NOTICE >= diagopts.msglevel) {
sprintf(syslp, "N exit(1)\n");
_msg(E_NOTICE, buff, syslp);
}
exit(diagopts.exitstatus ? diagopts.exitstatus : 1);
}
va_end(ap);
}
static void _msg(int level, const char *buff, const char *syslp) {
if (diagopts.logstderr) {
fputs(buff, stderr); fflush(stderr);
}
if (diagopts.syslog) {
/* prevent format string attacks (thanks to CoKi) */
syslog(syslevel[level], "%s", syslp);
}
if (diagopts.logfile) {
fputs(buff, diagopts.logfile); fflush(diagopts.logfile);
}
}
|