blob: 8de34f8db0c923af46ede6680cda07c7ea28df86 [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include <stdarg.h>
2#include <sys/socket.h>
3#include <stdio.h>
4#include <fcntl.h>
5#include <unistd.h>
6#include <syslog.h>
7#include <time.h>
8#include <signal.h>
9#include <string.h>
Rich Felkerd2c604d2011-04-18 21:11:23 -040010#include <pthread.h>
Rich Felker0b44a032011-02-12 00:22:29 -050011#include "libc.h"
12
Rich Felker4750cf42012-04-24 16:32:23 -040013static int lock[2];
Rich Felker0b44a032011-02-12 00:22:29 -050014static const char *log_ident;
15static int log_opt;
16static int log_facility = LOG_USER;
17static int log_mask = 0xff;
Rich Felker19c18302011-04-13 18:32:33 -040018static int log_fd = -1;
Rich Felker0b44a032011-02-12 00:22:29 -050019
20int setlogmask(int maskpri)
21{
22 int old = log_mask;
23 if (maskpri) log_mask = maskpri;
24 return old;
25}
26
27static const struct {
28 short sun_family;
29 char sun_path[9];
30} log_addr = {
31 AF_UNIX,
32 "/dev/log"
33};
34
35void closelog(void)
36{
Rich Felkerd2c604d2011-04-18 21:11:23 -040037 int cs;
38 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -040039 LOCK(lock);
Rich Felker19c18302011-04-13 18:32:33 -040040 close(log_fd);
41 log_fd = -1;
Rich Felker4750cf42012-04-24 16:32:23 -040042 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -040043 pthread_setcancelstate(cs, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050044}
45
46static void __openlog(const char *ident, int opt, int facility)
47{
Rich Felker0b44a032011-02-12 00:22:29 -050048 log_ident = ident;
49 log_opt = opt;
50 log_facility = facility;
51
Rich Felker19c18302011-04-13 18:32:33 -040052 if (!(opt & LOG_NDELAY) || log_fd>=0) return;
Rich Felker0b44a032011-02-12 00:22:29 -050053
Rich Felker79a5e732012-09-29 17:36:27 -040054 log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050055}
56
57void openlog(const char *ident, int opt, int facility)
58{
Rich Felkerd2c604d2011-04-18 21:11:23 -040059 int cs;
60 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -040061 LOCK(lock);
Rich Felker0b44a032011-02-12 00:22:29 -050062 __openlog(ident, opt, facility);
Rich Felker4750cf42012-04-24 16:32:23 -040063 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -040064 pthread_setcancelstate(cs, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050065}
66
Rich Felkerd2c604d2011-04-18 21:11:23 -040067static void _vsyslog(int priority, const char *message, va_list ap)
Rich Felker0b44a032011-02-12 00:22:29 -050068{
Rich Felker0b44a032011-02-12 00:22:29 -050069 char timebuf[16];
70 time_t now;
71 struct tm tm;
Rich Felker19c18302011-04-13 18:32:33 -040072 char buf[256];
73 int pid;
74 int l, l2;
Rich Felker0b44a032011-02-12 00:22:29 -050075
Rich Felker19c18302011-04-13 18:32:33 -040076 if (log_fd < 0) {
77 __openlog(log_ident, log_opt | LOG_NDELAY, log_facility);
78 if (log_fd < 0) {
Rich Felker4750cf42012-04-24 16:32:23 -040079 UNLOCK(lock);
Rich Felker19c18302011-04-13 18:32:33 -040080 return;
81 }
Rich Felker0b44a032011-02-12 00:22:29 -050082 }
83
Rich Felker0b44a032011-02-12 00:22:29 -050084 now = time(NULL);
85 gmtime_r(&now, &tm);
86 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
87
Rich Felker19c18302011-04-13 18:32:33 -040088 pid = (log_opt & LOG_PID) ? getpid() : 0;
89 l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ",
90 priority, timebuf,
91 log_ident ? log_ident : "",
92 "["+!pid, pid, "]"+!pid);
93 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
94 if (l2 >= 0) {
95 l += l2;
96 if (buf[l-1] != '\n') buf[l++] = '\n';
97 sendto(log_fd, buf, l, 0, (void *)&log_addr, 11);
98 }
Rich Felker0b44a032011-02-12 00:22:29 -050099
Rich Felker4750cf42012-04-24 16:32:23 -0400100 UNLOCK(lock);
Rich Felker0b44a032011-02-12 00:22:29 -0500101}
Rich Felker19c18302011-04-13 18:32:33 -0400102
Rich Felkerd2c604d2011-04-18 21:11:23 -0400103void __vsyslog(int priority, const char *message, va_list ap)
104{
105 int cs;
106 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
107 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -0400108 LOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -0400109 _vsyslog(priority, message, ap);
Rich Felker4750cf42012-04-24 16:32:23 -0400110 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -0400111 pthread_setcancelstate(cs, 0);
112}
113
Rich Felker19c18302011-04-13 18:32:33 -0400114void syslog(int priority, const char *message, ...)
115{
116 va_list ap;
117 va_start(ap, message);
118 __vsyslog(priority, message, ap);
119 va_end(ap);
120}
121
122weak_alias(__vsyslog, vsyslog);