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