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 <unsupported_api.h>
13 #include "lock.h"
14
15 static volatile int lock[1];
16 static char log_ident[32];
17 static int log_opt;
18 static int log_facility = LOG_USER;
19 static int log_mask = 0xff;
20 static int log_fd = -1;
21
setlogmask(int maskpri)22 int setlogmask(int maskpri)
23 {
24 unsupported_api(__FUNCTION__);
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
closelog(void)40 void closelog(void)
41 {
42 int cs;
43 unsupported_api(__FUNCTION__);
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
__openlog()52 static 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
openlog(const char * ident,int opt,int facility)58 void openlog(const char *ident, int opt, int facility)
59 {
60 int cs;
61 unsupported_api(__FUNCTION__);
62 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
63 LOCK(lock);
64
65 if (ident) {
66 size_t n = strnlen(ident, sizeof log_ident - 1);
67 memcpy(log_ident, ident, n);
68 log_ident[n] = 0;
69 } else {
70 log_ident[0] = 0;
71 }
72 log_opt = opt;
73 log_facility = facility;
74
75 if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
76
77 UNLOCK(lock);
78 pthread_setcancelstate(cs, 0);
79 }
80
is_lost_conn(int e)81 static int is_lost_conn(int e)
82 {
83 return e==ECONNREFUSED || e==ECONNRESET || e==ENOTCONN || e==EPIPE;
84 }
85
_vsyslog(int priority,const char * message,va_list ap)86 static void _vsyslog(int priority, const char *message, va_list ap)
87 {
88 char timebuf[16];
89 time_t now;
90 struct tm tm;
91 char buf[1024];
92 int errno_save = errno;
93 int pid;
94 int l, l2;
95 int hlen;
96 int fd;
97
98 if (log_fd < 0) __openlog();
99
100 if (!(priority & LOG_FACMASK)) priority |= log_facility;
101
102 now = time(NULL);
103 gmtime_r(&now, &tm);
104 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
105
106 pid = (log_opt & LOG_PID) ? getpid() : 0;
107 l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
108 priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
109 errno = errno_save;
110 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
111 if (l2 >= 0) {
112 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
113 else l += l2;
114 if (buf[l-1] != '\n') buf[l++] = '\n';
115 if (send(log_fd, buf, l, 0) < 0 && (!is_lost_conn(errno)
116 || connect(log_fd, (void *)&log_addr, sizeof log_addr) < 0
117 || send(log_fd, buf, l, 0) < 0)
118 && (log_opt & LOG_CONS)) {
119 fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
120 if (fd >= 0) {
121 dprintf(fd, "%.*s", l-hlen, buf+hlen);
122 close(fd);
123 }
124 }
125 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
126 }
127 }
128
__vsyslog(int priority,const char * message,va_list ap)129 static void __vsyslog(int priority, const char *message, va_list ap)
130 {
131 int cs;
132 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
133 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
134 LOCK(lock);
135 _vsyslog(priority, message, ap);
136 UNLOCK(lock);
137 pthread_setcancelstate(cs, 0);
138 }
139
syslog(int priority,const char * message,...)140 void syslog(int priority, const char *message, ...)
141 {
142 unsupported_api(__FUNCTION__);
143 va_list ap;
144 va_start(ap, message);
145 __vsyslog(priority, message, ap);
146 va_end(ap);
147 }
148
149 weak_alias(__vsyslog, vsyslog);
150