implement the LOG_PERROR option in syslog
[oweals/musl.git] / src / misc / syslog.c
1 #include <stdarg.h>
2 #include <sys/socket.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <syslog.h>
6 #include <time.h>
7 #include <signal.h>
8 #include <string.h>
9 #include <pthread.h>
10 #include <errno.h>
11 #include "libc.h"
12 #include "atomic.h"
13
14 static int lock[2];
15 static char log_ident[32];
16 static int log_opt;
17 static int log_facility = LOG_USER;
18 static int log_mask = 0xff;
19 static int log_fd = -1;
20
21 int setlogmask(int maskpri)
22 {
23         if (maskpri) return a_swap(&log_mask, maskpri);
24         else return log_mask;
25 }
26
27 static const struct {
28         short sun_family;
29         char sun_path[9];
30 } log_addr = {
31         AF_UNIX,
32         "/dev/log"
33 };
34
35 void closelog(void)
36 {
37         int cs;
38         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
39         LOCK(lock);
40         close(log_fd);
41         log_fd = -1;
42         UNLOCK(lock);
43         pthread_setcancelstate(cs, 0);
44 }
45
46 static void __openlog()
47 {
48         log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
49         if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
50 }
51
52 void openlog(const char *ident, int opt, int facility)
53 {
54         int cs;
55         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
56         LOCK(lock);
57
58         if (ident) {
59                 size_t n = strnlen(ident, sizeof log_ident - 1);
60                 memcpy(log_ident, ident, n);
61                 log_ident[n] = 0;
62         } else {
63                 log_ident[0] = 0;
64         }
65         log_opt = opt;
66         log_facility = facility;
67
68         if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
69
70         UNLOCK(lock);
71         pthread_setcancelstate(cs, 0);
72 }
73
74 static void _vsyslog(int priority, const char *message, va_list ap)
75 {
76         char timebuf[16];
77         time_t now;
78         struct tm tm;
79         char buf[256];
80         int errno_save = errno;
81         int pid;
82         int l, l2;
83         int hlen;
84
85         if (log_fd < 0) {
86                 __openlog();
87                 if (log_fd < 0) return;
88         }
89
90         if (!(priority & LOG_FACMASK)) priority |= log_facility;
91
92         now = time(NULL);
93         gmtime_r(&now, &tm);
94         strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
95
96         pid = (log_opt & LOG_PID) ? getpid() : 0;
97         l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
98                 priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
99         errno = errno_save;
100         l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
101         if (l2 >= 0) {
102                 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
103                 else l += l2;
104                 if (buf[l-1] != '\n') buf[l++] = '\n';
105                 send(log_fd, buf, l, 0);
106                 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
107         }
108 }
109
110 void __vsyslog(int priority, const char *message, va_list ap)
111 {
112         int cs;
113         if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
114         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
115         LOCK(lock);
116         _vsyslog(priority, message, ap);
117         UNLOCK(lock);
118         pthread_setcancelstate(cs, 0);
119 }
120
121 void syslog(int priority, const char *message, ...)
122 {
123         va_list ap;
124         va_start(ap, message);
125         __vsyslog(priority, message, ap);
126         va_end(ap);
127 }
128
129 weak_alias(__vsyslog, vsyslog);