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