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