blob: fdf90ba9fb074ddecb4353496dc12d579d84f601 [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 Felker79a5e732012-09-29 17:36:27 -040049 log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
Rich Felker427c0ca2013-03-23 18:59:30 -040050 if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
Rich Felker0b44a032011-02-12 00:22:29 -050051}
52
53void openlog(const char *ident, int opt, int facility)
54{
Rich Felkerd2c604d2011-04-18 21:11:23 -040055 int cs;
56 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -040057 LOCK(lock);
Rich Felker427c0ca2013-03-23 18:59:30 -040058
59 if (ident) {
60 size_t n = strnlen(ident, sizeof log_ident - 1);
61 memcpy(log_ident, ident, n);
62 log_ident[n] = 0;
63 } else {
64 log_ident[0] = 0;
65 }
66 log_opt = opt;
67 log_facility = facility;
68
69 if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
70
Rich Felker4750cf42012-04-24 16:32:23 -040071 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -040072 pthread_setcancelstate(cs, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050073}
74
Rich Felkerd2c604d2011-04-18 21:11:23 -040075static void _vsyslog(int priority, const char *message, va_list ap)
Rich Felker0b44a032011-02-12 00:22:29 -050076{
Rich Felker0b44a032011-02-12 00:22:29 -050077 char timebuf[16];
78 time_t now;
79 struct tm tm;
Rich Felker19c18302011-04-13 18:32:33 -040080 char buf[256];
Clément Vasseurda271182014-07-09 14:34:18 +020081 int errno_save = errno;
Rich Felker19c18302011-04-13 18:32:33 -040082 int pid;
83 int l, l2;
Rich Felkerb8c4cf62014-07-11 21:20:04 -040084 int hlen;
Rich Felker781f26b2014-07-11 21:59:49 -040085 int fd;
Rich Felker0b44a032011-02-12 00:22:29 -050086
Rich Felkera64a0452014-07-11 21:56:50 -040087 if (log_fd < 0) __openlog();
Rich Felker0b44a032011-02-12 00:22:29 -050088
Rich Felker427c0ca2013-03-23 18:59:30 -040089 if (!(priority & LOG_FACMASK)) priority |= log_facility;
90
Rich Felker0b44a032011-02-12 00:22:29 -050091 now = time(NULL);
92 gmtime_r(&now, &tm);
93 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
94
Rich Felker19c18302011-04-13 18:32:33 -040095 pid = (log_opt & LOG_PID) ? getpid() : 0;
Rich Felkerb8c4cf62014-07-11 21:20:04 -040096 l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
97 priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
Clément Vasseurda271182014-07-09 14:34:18 +020098 errno = errno_save;
Rich Felker19c18302011-04-13 18:32:33 -040099 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
100 if (l2 >= 0) {
Rich Felker427c0ca2013-03-23 18:59:30 -0400101 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
102 else l += l2;
Rich Felker19c18302011-04-13 18:32:33 -0400103 if (buf[l-1] != '\n') buf[l++] = '\n';
Rich Felker781f26b2014-07-11 21:59:49 -0400104 if (send(log_fd, buf, l, 0) < 0 && (log_opt & LOG_CONS)) {
105 fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
106 if (fd >= 0) {
107 dprintf(fd, "%.*s", l-hlen, buf+hlen);
108 close(fd);
109 }
110 }
Rich Felkerb8c4cf62014-07-11 21:20:04 -0400111 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
Rich Felker19c18302011-04-13 18:32:33 -0400112 }
Rich Felker0b44a032011-02-12 00:22:29 -0500113}
Rich Felker19c18302011-04-13 18:32:33 -0400114
Rich Felkerd2c604d2011-04-18 21:11:23 -0400115void __vsyslog(int priority, const char *message, va_list ap)
116{
117 int cs;
118 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
119 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -0400120 LOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -0400121 _vsyslog(priority, message, ap);
Rich Felker4750cf42012-04-24 16:32:23 -0400122 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -0400123 pthread_setcancelstate(cs, 0);
124}
125
Rich Felker19c18302011-04-13 18:32:33 -0400126void syslog(int priority, const char *message, ...)
127{
128 va_list ap;
129 va_start(ap, message);
130 __vsyslog(priority, message, ap);
131 va_end(ap);
132}
133
134weak_alias(__vsyslog, vsyslog);