Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/gen/syslog.c
Show All 30 Lines | |||||
#if defined(LIBC_SCCS) && !defined(lint) | #if defined(LIBC_SCCS) && !defined(lint) | ||||
static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95"; | static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95"; | ||||
#endif /* LIBC_SCCS and not lint */ | #endif /* LIBC_SCCS and not lint */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include "namespace.h" | #include "namespace.h" | ||||
#include <sys/types.h> | #include <sys/param.h> | ||||
#include <sys/socket.h> | #include <sys/socket.h> | ||||
#include <sys/syslog.h> | #include <sys/syslog.h> | ||||
#include <sys/time.h> | |||||
#include <sys/uio.h> | #include <sys/uio.h> | ||||
#include <sys/un.h> | #include <sys/un.h> | ||||
#include <netdb.h> | #include <netdb.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <paths.h> | #include <paths.h> | ||||
#include <pthread.h> | #include <pthread.h> | ||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | syslog(int pri, const char *fmt, ...) | ||||
va_start(ap, fmt); | va_start(ap, fmt); | ||||
vsyslog(pri, fmt, ap); | vsyslog(pri, fmt, ap); | ||||
va_end(ap); | va_end(ap); | ||||
} | } | ||||
static void | static void | ||||
vsyslog1(int pri, const char *fmt, va_list ap) | vsyslog1(int pri, const char *fmt, va_list ap) | ||||
{ | { | ||||
int cnt; | struct timeval now; | ||||
struct tm tm; | |||||
kib: Move 'cnt' declaration into the line with other ints. | |||||
char ch, *p; | char ch, *p; | ||||
time_t now; | long tz_offset; | ||||
int fd, saved_errno; | int cnt, fd, saved_errno; | ||||
char *stdp, tbuf[2048], fmt_cpy[1024], timbuf[26], errstr[64]; | char hostname[MAXHOSTNAMELEN], *stdp, tbuf[2048], fmt_cpy[1024], | ||||
errstr[64], tz_sign; | |||||
FILE *fp, *fmt_fp; | FILE *fp, *fmt_fp; | ||||
struct bufcookie tbuf_cookie; | struct bufcookie tbuf_cookie; | ||||
struct bufcookie fmt_cookie; | struct bufcookie fmt_cookie; | ||||
#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID | #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID | ||||
/* Check for invalid bits. */ | /* Check for invalid bits. */ | ||||
if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { | if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { | ||||
syslog(INTERNALLOG, | syslog(INTERNALLOG, | ||||
Show All 13 Lines | #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID | ||||
/* Create the primary stdio hook */ | /* Create the primary stdio hook */ | ||||
tbuf_cookie.base = tbuf; | tbuf_cookie.base = tbuf; | ||||
tbuf_cookie.left = sizeof(tbuf); | tbuf_cookie.left = sizeof(tbuf); | ||||
fp = fwopen(&tbuf_cookie, writehook); | fp = fwopen(&tbuf_cookie, writehook); | ||||
if (fp == NULL) | if (fp == NULL) | ||||
return; | return; | ||||
/* Build the message. */ | /* Build the message according to RFC 5424. Tag and version. */ | ||||
(void)time(&now); | (void)fprintf(fp, "<%d>1 ", pri); | ||||
(void)fprintf(fp, "<%d>", pri); | /* Timestamp similar to RFC 3339. */ | ||||
(void)fprintf(fp, "%.15s ", ctime_r(&now, timbuf) + 4); | if (gettimeofday(&now, NULL) == 0 && | ||||
localtime_r(&now.tv_sec, &tm) != NULL) { | |||||
if (tm.tm_gmtoff < 0) { | |||||
tz_sign = '-'; | |||||
Done Inline ActionsStyle: no local declarations after the first statement. kib: Style: no local declarations after the first statement. | |||||
tz_offset = -tm.tm_gmtoff; | |||||
} else { | |||||
tz_sign = '+'; | |||||
tz_offset = tm.tm_gmtoff; | |||||
} | |||||
(void)fprintf(fp, | |||||
"%04d-%02d-%02d" /* Date. */ | |||||
"T%02d:%02d:%02d.%06ld" /* Time. */ | |||||
"%c%02ld:%02ld ", /* Time zone offset. */ | |||||
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, | |||||
tm.tm_hour, tm.tm_min, tm.tm_sec, now.tv_usec, | |||||
tz_sign, tz_offset / 3600, (tz_offset % 3600) / 60); | |||||
} else | |||||
(void)fprintf(fp, "- "); | |||||
/* Hostname. */ | |||||
(void)gethostname(hostname, sizeof(hostname)); | |||||
(void)fprintf(fp, "%s ", hostname); | |||||
if (LogStat & LOG_PERROR) { | if (LogStat & LOG_PERROR) { | ||||
/* Transfer to string buffer */ | /* Transfer to string buffer */ | ||||
(void)fflush(fp); | (void)fflush(fp); | ||||
Not Done Inline Actionsfputs(3), even despite compiler can optimize it. kib: fputs(3), even despite compiler can optimize it. | |||||
Not Done Inline ActionsThere's a space at the end of the format: "%s "; not "%s". Using fputs() would remove that. ed: There's a space at the end of the format: `"%s "`; not `"%s"`. Using `fputs()` would remove… | |||||
stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left); | stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left); | ||||
} | } | ||||
/* | |||||
* Application name, process ID, message ID and structured data. | |||||
* Provide the process ID regardless of whether LOG_PID has been | |||||
* specified, as it provides valuable information. Many | |||||
* applications tend not to use this, even though they should. | |||||
*/ | |||||
if (LogTag == NULL) | if (LogTag == NULL) | ||||
LogTag = _getprogname(); | LogTag = _getprogname(); | ||||
if (LogTag != NULL) | (void)fprintf(fp, "%s %d - - ", | ||||
(void)fprintf(fp, "%s", LogTag); | LogTag == NULL ? "-" : LogTag, getpid()); | ||||
if (LogStat & LOG_PID) | |||||
(void)fprintf(fp, "[%d]", getpid()); | |||||
if (LogTag != NULL) { | |||||
(void)fprintf(fp, ": "); | |||||
} | |||||
/* Check to see if we can skip expanding the %m */ | /* Check to see if we can skip expanding the %m */ | ||||
if (strstr(fmt, "%m")) { | if (strstr(fmt, "%m")) { | ||||
/* Create the second stdio hook */ | /* Create the second stdio hook */ | ||||
fmt_cookie.base = fmt_cpy; | fmt_cookie.base = fmt_cpy; | ||||
fmt_cookie.left = sizeof(fmt_cpy) - 1; | fmt_cookie.left = sizeof(fmt_cpy) - 1; | ||||
fmt_fp = fwopen(&fmt_cookie, writehook); | fmt_fp = fwopen(&fmt_cookie, writehook); | ||||
▲ Show 20 Lines • Show All 111 Lines • ▼ Show 20 Lines | #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID | ||||
* Make sure the error reported is the one from the syslogd failure. | * Make sure the error reported is the one from the syslogd failure. | ||||
*/ | */ | ||||
if (LogStat & LOG_CONS && | if (LogStat & LOG_CONS && | ||||
(fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK|O_CLOEXEC, 0)) >= | (fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK|O_CLOEXEC, 0)) >= | ||||
0) { | 0) { | ||||
struct iovec iov[2]; | struct iovec iov[2]; | ||||
struct iovec *v = iov; | struct iovec *v = iov; | ||||
p = strchr(tbuf, '>') + 1; | p = strchr(tbuf, '>') + 3; | ||||
v->iov_base = p; | v->iov_base = p; | ||||
v->iov_len = cnt - (p - tbuf); | v->iov_len = cnt - (p - tbuf); | ||||
++v; | ++v; | ||||
v->iov_base = "\r\n"; | v->iov_base = "\r\n"; | ||||
v->iov_len = 2; | v->iov_len = 2; | ||||
(void)_writev(fd, iov, 2); | (void)_writev(fd, iov, 2); | ||||
(void)_close(fd); | (void)_close(fd); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 139 Lines • Show Last 20 Lines |
Move 'cnt' declaration into the line with other ints.