make all objects used with atomic operations volatile
[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 volatile int lock[2];
16 static char log_ident[32];
17 static int log_opt;
18 static int log_facility = LOG_USER;
19 static volatile 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         int fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
50         if (fd < 0) return;
51         if (connect(fd, (void *)&log_addr, sizeof log_addr) < 0)
52                 close(fd);
53         else
54                 log_fd = fd;
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
63         if (ident) {
64                 size_t n = strnlen(ident, sizeof log_ident - 1);
65                 memcpy(log_ident, ident, n);
66                 log_ident[n] = 0;
67         } else {
68                 log_ident[0] = 0;
69         }
70         log_opt = opt;
71         log_facility = facility;
72
73         if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
74
75         UNLOCK(lock);
76         pthread_setcancelstate(cs, 0);
77 }
78
79 static void _vsyslog(int priority, const char *message, va_list ap)
80 {
81         char timebuf[16];
82         time_t now;
83         struct tm tm;
84         char buf[1024];
85         int errno_save = errno;
86         int pid;
87         int l, l2;
88         int hlen;
89         int fd;
90
91         if (log_fd < 0) __openlog();
92
93         if (!(priority & LOG_FACMASK)) priority |= log_facility;
94
95         now = time(NULL);
96         gmtime_r(&now, &tm);
97         strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
98
99         pid = (log_opt & LOG_PID) ? getpid() : 0;
100         l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
101                 priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
102         errno = errno_save;
103         l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
104         if (l2 >= 0) {
105                 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
106                 else l += l2;
107                 if (buf[l-1] != '\n') buf[l++] = '\n';
108                 if (send(log_fd, buf, l, 0) < 0 && (log_opt & LOG_CONS)) {
109                         fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
110                         if (fd >= 0) {
111                                 dprintf(fd, "%.*s", l-hlen, buf+hlen);
112                                 close(fd);
113                         }
114                 }
115                 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
116         }
117 }
118
119 void __vsyslog(int priority, const char *message, va_list ap)
120 {
121         int cs;
122         if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
123         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
124         LOCK(lock);
125         _vsyslog(priority, message, ap);
126         UNLOCK(lock);
127         pthread_setcancelstate(cs, 0);
128 }
129
130 void syslog(int priority, const char *message, ...)
131 {
132         va_list ap;
133         va_start(ap, message);
134         __vsyslog(priority, message, ap);
135         va_end(ap);
136 }
137
138 weak_alias(__vsyslog, vsyslog);