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