emulate SOCK_CLOEXEC and SOCK_NONBLOCK for old (pre-2.6.27) kernels
[musl] / src / misc / syslog.c
1 #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>
10 #include <pthread.h>
11 #include "libc.h"
12
13 static int lock[2];
14 static const char *log_ident;
15 static int log_opt;
16 static int log_facility = LOG_USER;
17 static int log_mask = 0xff;
18 static int log_fd = -1;
19
20 int setlogmask(int maskpri)
21 {
22         int old = log_mask;
23         if (maskpri) log_mask = maskpri;
24         return old;
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(const char *ident, int opt, int facility)
47 {
48         log_ident = ident;
49         log_opt = opt;
50         log_facility = facility;
51
52         if (!(opt & LOG_NDELAY) || log_fd>=0) return;
53
54         log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
55 }
56
57 void openlog(const char *ident, int opt, int facility)
58 {
59         int cs;
60         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
61         LOCK(lock);
62         __openlog(ident, opt, facility);
63         UNLOCK(lock);
64         pthread_setcancelstate(cs, 0);
65 }
66
67 static void _vsyslog(int priority, const char *message, va_list ap)
68 {
69         char timebuf[16];
70         time_t now;
71         struct tm tm;
72         char buf[256];
73         int pid;
74         int l, l2;
75
76         if (log_fd < 0) {
77                 __openlog(log_ident, log_opt | LOG_NDELAY, log_facility);
78                 if (log_fd < 0) {
79                         UNLOCK(lock);
80                         return;
81                 }
82         }
83
84         now = time(NULL);
85         gmtime_r(&now, &tm);
86         strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
87
88         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         }
99
100         UNLOCK(lock);
101 }
102
103 void __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);
108         LOCK(lock);
109         _vsyslog(priority, message, ap);
110         UNLOCK(lock);
111         pthread_setcancelstate(cs, 0);
112 }
113
114 void 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
122 weak_alias(__vsyslog, vsyslog);