master
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"
15
16static volatile int lock[1];
17static char log_ident[32];
18static int log_opt;
19static int log_facility = LOG_USER;
20static int log_mask = 0xff;
21static int log_fd = -1;
22volatile int *const __syslog_lockptr = lock;
23
24int setlogmask(int maskpri)
25{
26 LOCK(lock);
27 int ret = log_mask;
28 if (maskpri) log_mask = maskpri;
29 UNLOCK(lock);
30 return ret;
31}
32
33static const struct {
34 short sun_family;
35 char sun_path[9];
36} log_addr = {
37 AF_UNIX,
38 "/dev/log"
39};
40
41void closelog(void)
42{
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);
50}
51
52static void __openlog()
53{
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);
56}
57
58void openlog(const char *ident, int opt, int facility)
59{
60 int cs;
61 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
62 LOCK(lock);
63
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;
70 }
71 log_opt = opt;
72 log_facility = facility;
73
74 if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
75
76 UNLOCK(lock);
77 pthread_setcancelstate(cs, 0);
78}
79
80static int is_lost_conn(int e)
81{
82 return e==ECONNREFUSED || e==ECONNRESET || e==ENOTCONN || e==EPIPE;
83}
84
85static void _vsyslog(int priority, const char *message, va_list ap)
86{
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;
96
97 if (log_fd < 0) __openlog();
98
99 if (!(priority & LOG_FACMASK)) priority |= log_facility;
100
101 now = time(NULL);
102 gmtime_r(&now, &tm);
103 strftime_l(timebuf, sizeof timebuf, "%b %e %T", &tm, C_LOCALE);
104
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);
122 }
123 }
124 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
125 }
126}
127
128static void __vsyslog(int priority, const char *message, va_list ap)
129{
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);
137}
138
139void syslog(int priority, const char *message, ...)
140{
141 va_list ap;
142 va_start(ap, message);
143 __vsyslog(priority, message, ap);
144 va_end(ap);
145}
146
147weak_alias(__vsyslog, vsyslog);