Debian Codegraph @

libc-top-half/musl/src/misc/syslog.c

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