ditch the priority inheritance locks; use malloc's version of lock
[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, 0);
55         fcntl(log_fd, F_SETFD, FD_CLOEXEC);
56 }
57
58 void openlog(const char *ident, int opt, int facility)
59 {
60         int cs;
61         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
62         LOCK(lock);
63         __openlog(ident, opt, facility);
64         UNLOCK(lock);
65         pthread_setcancelstate(cs, 0);
66 }
67
68 static void _vsyslog(int priority, const char *message, va_list ap)
69 {
70         char timebuf[16];
71         time_t now;
72         struct tm tm;
73         char buf[256];
74         int pid;
75         int l, l2;
76
77         if (log_fd < 0) {
78                 __openlog(log_ident, log_opt | LOG_NDELAY, log_facility);
79                 if (log_fd < 0) {
80                         UNLOCK(lock);
81                         return;
82                 }
83         }
84
85         now = time(NULL);
86         gmtime_r(&now, &tm);
87         strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
88
89         pid = (log_opt & LOG_PID) ? getpid() : 0;
90         l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ",
91                 priority, timebuf,
92                 log_ident ? log_ident : "",
93                 "["+!pid, pid, "]"+!pid);
94         l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
95         if (l2 >= 0) {
96                 l += l2;
97                 if (buf[l-1] != '\n') buf[l++] = '\n';
98                 sendto(log_fd, buf, l, 0, (void *)&log_addr, 11);
99         }
100
101         UNLOCK(lock);
102 }
103
104 void __vsyslog(int priority, const char *message, va_list ap)
105 {
106         int cs;
107         if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
108         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
109         LOCK(lock);
110         _vsyslog(priority, message, ap);
111         UNLOCK(lock);
112         pthread_setcancelstate(cs, 0);
113 }
114
115 void syslog(int priority, const char *message, ...)
116 {
117         va_list ap;
118         va_start(ap, message);
119         __vsyslog(priority, message, ap);
120         va_end(ap);
121 }
122
123 weak_alias(__vsyslog, vsyslog);