blob: fbce4bcd4011f826fde592bc86d1a57b524914d9 [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include <stdarg.h>
2#include <sys/socket.h>
3#include <stdio.h>
Rich Felker0b44a032011-02-12 00:22:29 -05004#include <unistd.h>
5#include <syslog.h>
6#include <time.h>
7#include <signal.h>
8#include <string.h>
Rich Felkerd2c604d2011-04-18 21:11:23 -04009#include <pthread.h>
Clément Vasseurda271182014-07-09 14:34:18 +020010#include <errno.h>
Rich Felker781f26b2014-07-11 21:59:49 -040011#include <fcntl.h>
Rich Felker0b44a032011-02-12 00:22:29 -050012#include "libc.h"
Rich Felker427c0ca2013-03-23 18:59:30 -040013#include "atomic.h"
Rich Felker0b44a032011-02-12 00:22:29 -050014
Rich Felker4750cf42012-04-24 16:32:23 -040015static int lock[2];
Rich Felker427c0ca2013-03-23 18:59:30 -040016static char log_ident[32];
Rich Felker0b44a032011-02-12 00:22:29 -050017static int log_opt;
18static int log_facility = LOG_USER;
19static int log_mask = 0xff;
Rich Felker19c18302011-04-13 18:32:33 -040020static int log_fd = -1;
Rich Felker0b44a032011-02-12 00:22:29 -050021
22int setlogmask(int maskpri)
23{
Rich Felker427c0ca2013-03-23 18:59:30 -040024 if (maskpri) return a_swap(&log_mask, maskpri);
25 else return log_mask;
Rich Felker0b44a032011-02-12 00:22:29 -050026}
27
28static const struct {
29 short sun_family;
30 char sun_path[9];
31} log_addr = {
32 AF_UNIX,
33 "/dev/log"
34};
35
36void closelog(void)
37{
Rich Felkerd2c604d2011-04-18 21:11:23 -040038 int cs;
39 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -040040 LOCK(lock);
Rich Felker19c18302011-04-13 18:32:33 -040041 close(log_fd);
42 log_fd = -1;
Rich Felker4750cf42012-04-24 16:32:23 -040043 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -040044 pthread_setcancelstate(cs, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050045}
46
Rich Felker427c0ca2013-03-23 18:59:30 -040047static void __openlog()
Rich Felker0b44a032011-02-12 00:22:29 -050048{
Rich Felkerc5743212015-01-09 00:09:54 -050049 int fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
50 if (fd < 0) return;
51 if (connect(fd, (void *)&log_addr, sizeof log_addr) < 0)
52 close(fd);
53 else
54 log_fd = fd;
Rich Felker0b44a032011-02-12 00:22:29 -050055}
56
57void openlog(const char *ident, int opt, int facility)
58{
Rich Felkerd2c604d2011-04-18 21:11:23 -040059 int cs;
60 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -040061 LOCK(lock);
Rich Felker427c0ca2013-03-23 18:59:30 -040062
63 if (ident) {
64 size_t n = strnlen(ident, sizeof log_ident - 1);
65 memcpy(log_ident, ident, n);
66 log_ident[n] = 0;
67 } else {
68 log_ident[0] = 0;
69 }
70 log_opt = opt;
71 log_facility = facility;
72
73 if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
74
Rich Felker4750cf42012-04-24 16:32:23 -040075 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -040076 pthread_setcancelstate(cs, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050077}
78
Rich Felkerd2c604d2011-04-18 21:11:23 -040079static void _vsyslog(int priority, const char *message, va_list ap)
Rich Felker0b44a032011-02-12 00:22:29 -050080{
Rich Felker0b44a032011-02-12 00:22:29 -050081 char timebuf[16];
82 time_t now;
83 struct tm tm;
Rich Felker19c18302011-04-13 18:32:33 -040084 char buf[256];
Clément Vasseurda271182014-07-09 14:34:18 +020085 int errno_save = errno;
Rich Felker19c18302011-04-13 18:32:33 -040086 int pid;
87 int l, l2;
Rich Felkerb8c4cf62014-07-11 21:20:04 -040088 int hlen;
Rich Felker781f26b2014-07-11 21:59:49 -040089 int fd;
Rich Felker0b44a032011-02-12 00:22:29 -050090
Rich Felkera64a0452014-07-11 21:56:50 -040091 if (log_fd < 0) __openlog();
Rich Felker0b44a032011-02-12 00:22:29 -050092
Rich Felker427c0ca2013-03-23 18:59:30 -040093 if (!(priority & LOG_FACMASK)) priority |= log_facility;
94
Rich Felker0b44a032011-02-12 00:22:29 -050095 now = time(NULL);
96 gmtime_r(&now, &tm);
97 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
98
Rich Felker19c18302011-04-13 18:32:33 -040099 pid = (log_opt & LOG_PID) ? getpid() : 0;
Rich Felkerb8c4cf62014-07-11 21:20:04 -0400100 l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
101 priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
Clément Vasseurda271182014-07-09 14:34:18 +0200102 errno = errno_save;
Rich Felker19c18302011-04-13 18:32:33 -0400103 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
104 if (l2 >= 0) {
Rich Felker427c0ca2013-03-23 18:59:30 -0400105 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
106 else l += l2;
Rich Felker19c18302011-04-13 18:32:33 -0400107 if (buf[l-1] != '\n') buf[l++] = '\n';
Rich Felker781f26b2014-07-11 21:59:49 -0400108 if (send(log_fd, buf, l, 0) < 0 && (log_opt & LOG_CONS)) {
109 fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
110 if (fd >= 0) {
111 dprintf(fd, "%.*s", l-hlen, buf+hlen);
112 close(fd);
113 }
114 }
Rich Felkerb8c4cf62014-07-11 21:20:04 -0400115 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
Rich Felker19c18302011-04-13 18:32:33 -0400116 }
Rich Felker0b44a032011-02-12 00:22:29 -0500117}
Rich Felker19c18302011-04-13 18:32:33 -0400118
Rich Felkerd2c604d2011-04-18 21:11:23 -0400119void __vsyslog(int priority, const char *message, va_list ap)
120{
121 int cs;
122 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
123 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -0400124 LOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -0400125 _vsyslog(priority, message, ap);
Rich Felker4750cf42012-04-24 16:32:23 -0400126 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -0400127 pthread_setcancelstate(cs, 0);
128}
129
Rich Felker19c18302011-04-13 18:32:33 -0400130void syslog(int priority, const char *message, ...)
131{
132 va_list ap;
133 va_start(ap, message);
134 __vsyslog(priority, message, ap);
135 va_end(ap);
136}
137
138weak_alias(__vsyslog, vsyslog);