sys/stat.h: fix typo in statx member name stx_dio_offset_align
[musl.git] / src / misc / syslog.c
blob710202f98057c3d7a4386ed90a88d13e6df01699
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 "lock.h"
13 #include "fork_impl.h"
14 #include "locale_impl.h"
16 static volatile int lock[1];
17 static char log_ident[32];
18 static int log_opt;
19 static int log_facility = LOG_USER;
20 static int log_mask = 0xff;
21 static int log_fd = -1;
22 volatile int *const __syslog_lockptr = lock;
24 int setlogmask(int maskpri)
26 LOCK(lock);
27 int ret = log_mask;
28 if (maskpri) log_mask = maskpri;
29 UNLOCK(lock);
30 return ret;
33 static const struct {
34 short sun_family;
35 char sun_path[9];
36 } log_addr = {
37 AF_UNIX,
38 "/dev/log"
41 void closelog(void)
43 int cs;
44 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
45 LOCK(lock);
46 close(log_fd);
47 log_fd = -1;
48 UNLOCK(lock);
49 pthread_setcancelstate(cs, 0);
52 static void __openlog()
54 log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
55 if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
58 void openlog(const char *ident, int opt, int facility)
60 int cs;
61 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
62 LOCK(lock);
64 if (ident) {
65 size_t n = strnlen(ident, sizeof log_ident - 1);
66 memcpy(log_ident, ident, n);
67 log_ident[n] = 0;
68 } else {
69 log_ident[0] = 0;
71 log_opt = opt;
72 log_facility = facility;
74 if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
76 UNLOCK(lock);
77 pthread_setcancelstate(cs, 0);
80 static int is_lost_conn(int e)
82 return e==ECONNREFUSED || e==ECONNRESET || e==ENOTCONN || e==EPIPE;
85 static void _vsyslog(int priority, const char *message, va_list ap)
87 char timebuf[16];
88 time_t now;
89 struct tm tm;
90 char buf[1024];
91 int errno_save = errno;
92 int pid;
93 int l, l2;
94 int hlen;
95 int fd;
97 if (log_fd < 0) __openlog();
99 if (!(priority & LOG_FACMASK)) priority |= log_facility;
101 now = time(NULL);
102 gmtime_r(&now, &tm);
103 strftime_l(timebuf, sizeof timebuf, "%b %e %T", &tm, C_LOCALE);
105 pid = (log_opt & LOG_PID) ? getpid() : 0;
106 l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
107 priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
108 errno = errno_save;
109 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
110 if (l2 >= 0) {
111 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
112 else l += l2;
113 if (buf[l-1] != '\n') buf[l++] = '\n';
114 if (send(log_fd, buf, l, 0) < 0 && (!is_lost_conn(errno)
115 || connect(log_fd, (void *)&log_addr, sizeof log_addr) < 0
116 || send(log_fd, buf, l, 0) < 0)
117 && (log_opt & LOG_CONS)) {
118 fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
119 if (fd >= 0) {
120 dprintf(fd, "%.*s", l-hlen, buf+hlen);
121 close(fd);
124 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
128 static void __vsyslog(int priority, const char *message, va_list ap)
130 int cs;
131 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
132 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
133 LOCK(lock);
134 _vsyslog(priority, message, ap);
135 UNLOCK(lock);
136 pthread_setcancelstate(cs, 0);
139 void syslog(int priority, const char *message, ...)
141 va_list ap;
142 va_start(ap, message);
143 __vsyslog(priority, message, ap);
144 va_end(ap);
147 weak_alias(__vsyslog, vsyslog);