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