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