blob: eb7829842ae7643113378b533e37a9eb2ec4a8d8 [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 Felker0b44a032011-02-12 00:22:29 -050011#include "libc.h"
Rich Felker427c0ca2013-03-23 18:59:30 -040012#include "atomic.h"
Rich Felker0b44a032011-02-12 00:22:29 -050013
Rich Felker4750cf42012-04-24 16:32:23 -040014static int lock[2];
Rich Felker427c0ca2013-03-23 18:59:30 -040015static char log_ident[32];
Rich Felker0b44a032011-02-12 00:22:29 -050016static int log_opt;
17static int log_facility = LOG_USER;
18static int log_mask = 0xff;
Rich Felker19c18302011-04-13 18:32:33 -040019static int log_fd = -1;
Rich Felker0b44a032011-02-12 00:22:29 -050020
21int setlogmask(int maskpri)
22{
Rich Felker427c0ca2013-03-23 18:59:30 -040023 if (maskpri) return a_swap(&log_mask, maskpri);
24 else return log_mask;
Rich Felker0b44a032011-02-12 00:22:29 -050025}
26
27static const struct {
28 short sun_family;
29 char sun_path[9];
30} log_addr = {
31 AF_UNIX,
32 "/dev/log"
33};
34
35void closelog(void)
36{
Rich Felkerd2c604d2011-04-18 21:11:23 -040037 int cs;
38 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -040039 LOCK(lock);
Rich Felker19c18302011-04-13 18:32:33 -040040 close(log_fd);
41 log_fd = -1;
Rich Felker4750cf42012-04-24 16:32:23 -040042 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -040043 pthread_setcancelstate(cs, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050044}
45
Rich Felker427c0ca2013-03-23 18:59:30 -040046static void __openlog()
Rich Felker0b44a032011-02-12 00:22:29 -050047{
Rich Felker79a5e732012-09-29 17:36:27 -040048 log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
Rich Felker427c0ca2013-03-23 18:59:30 -040049 if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
Rich Felker0b44a032011-02-12 00:22:29 -050050}
51
52void openlog(const char *ident, int opt, int facility)
53{
Rich Felkerd2c604d2011-04-18 21:11:23 -040054 int cs;
55 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -040056 LOCK(lock);
Rich Felker427c0ca2013-03-23 18:59:30 -040057
58 if (ident) {
59 size_t n = strnlen(ident, sizeof log_ident - 1);
60 memcpy(log_ident, ident, n);
61 log_ident[n] = 0;
62 } else {
63 log_ident[0] = 0;
64 }
65 log_opt = opt;
66 log_facility = facility;
67
68 if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
69
Rich Felker4750cf42012-04-24 16:32:23 -040070 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -040071 pthread_setcancelstate(cs, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050072}
73
Rich Felkerd2c604d2011-04-18 21:11:23 -040074static void _vsyslog(int priority, const char *message, va_list ap)
Rich Felker0b44a032011-02-12 00:22:29 -050075{
Rich Felker0b44a032011-02-12 00:22:29 -050076 char timebuf[16];
77 time_t now;
78 struct tm tm;
Rich Felker19c18302011-04-13 18:32:33 -040079 char buf[256];
Clément Vasseurda271182014-07-09 14:34:18 +020080 int errno_save = errno;
Rich Felker19c18302011-04-13 18:32:33 -040081 int pid;
82 int l, l2;
Rich Felkerb8c4cf62014-07-11 21:20:04 -040083 int hlen;
Rich Felker0b44a032011-02-12 00:22:29 -050084
Rich Felker19c18302011-04-13 18:32:33 -040085 if (log_fd < 0) {
Rich Felker427c0ca2013-03-23 18:59:30 -040086 __openlog();
87 if (log_fd < 0) return;
Rich Felker0b44a032011-02-12 00:22:29 -050088 }
89
Rich Felker427c0ca2013-03-23 18:59:30 -040090 if (!(priority & LOG_FACMASK)) priority |= log_facility;
91
Rich Felker0b44a032011-02-12 00:22:29 -050092 now = time(NULL);
93 gmtime_r(&now, &tm);
94 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
95
Rich Felker19c18302011-04-13 18:32:33 -040096 pid = (log_opt & LOG_PID) ? getpid() : 0;
Rich Felkerb8c4cf62014-07-11 21:20:04 -040097 l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
98 priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
Clément Vasseurda271182014-07-09 14:34:18 +020099 errno = errno_save;
Rich Felker19c18302011-04-13 18:32:33 -0400100 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
101 if (l2 >= 0) {
Rich Felker427c0ca2013-03-23 18:59:30 -0400102 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
103 else l += l2;
Rich Felker19c18302011-04-13 18:32:33 -0400104 if (buf[l-1] != '\n') buf[l++] = '\n';
Rich Felker427c0ca2013-03-23 18:59:30 -0400105 send(log_fd, buf, l, 0);
Rich Felkerb8c4cf62014-07-11 21:20:04 -0400106 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
Rich Felker19c18302011-04-13 18:32:33 -0400107 }
Rich Felker0b44a032011-02-12 00:22:29 -0500108}
Rich Felker19c18302011-04-13 18:32:33 -0400109
Rich Felkerd2c604d2011-04-18 21:11:23 -0400110void __vsyslog(int priority, const char *message, va_list ap)
111{
112 int cs;
113 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
114 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -0400115 LOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -0400116 _vsyslog(priority, message, ap);
Rich Felker4750cf42012-04-24 16:32:23 -0400117 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -0400118 pthread_setcancelstate(cs, 0);
119}
120
Rich Felker19c18302011-04-13 18:32:33 -0400121void syslog(int priority, const char *message, ...)
122{
123 va_list ap;
124 va_start(ap, message);
125 __vsyslog(priority, message, ap);
126 va_end(ap);
127}
128
129weak_alias(__vsyslog, vsyslog);