blob: 1cd61ce444b2663d3f46f667f1976cb0a9479fd9 [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>
Rich Felker0b44a032011-02-12 00:22:29 -050010#include "libc.h"
Rich Felker427c0ca2013-03-23 18:59:30 -040011#include "atomic.h"
Rich Felker0b44a032011-02-12 00:22:29 -050012
Rich Felker4750cf42012-04-24 16:32:23 -040013static int lock[2];
Rich Felker427c0ca2013-03-23 18:59:30 -040014static char log_ident[32];
Rich Felker0b44a032011-02-12 00:22:29 -050015static int log_opt;
16static int log_facility = LOG_USER;
17static int log_mask = 0xff;
Rich Felker19c18302011-04-13 18:32:33 -040018static int log_fd = -1;
Rich Felker0b44a032011-02-12 00:22:29 -050019
20int setlogmask(int maskpri)
21{
Rich Felker427c0ca2013-03-23 18:59:30 -040022 if (maskpri) return a_swap(&log_mask, maskpri);
23 else return log_mask;
Rich Felker0b44a032011-02-12 00:22:29 -050024}
25
26static const struct {
27 short sun_family;
28 char sun_path[9];
29} log_addr = {
30 AF_UNIX,
31 "/dev/log"
32};
33
34void closelog(void)
35{
Rich Felkerd2c604d2011-04-18 21:11:23 -040036 int cs;
37 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -040038 LOCK(lock);
Rich Felker19c18302011-04-13 18:32:33 -040039 close(log_fd);
40 log_fd = -1;
Rich Felker4750cf42012-04-24 16:32:23 -040041 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -040042 pthread_setcancelstate(cs, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050043}
44
Rich Felker427c0ca2013-03-23 18:59:30 -040045static void __openlog()
Rich Felker0b44a032011-02-12 00:22:29 -050046{
Rich Felker79a5e732012-09-29 17:36:27 -040047 log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
Rich Felker427c0ca2013-03-23 18:59:30 -040048 if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
Rich Felker0b44a032011-02-12 00:22:29 -050049}
50
51void openlog(const char *ident, int opt, int facility)
52{
Rich Felkerd2c604d2011-04-18 21:11:23 -040053 int cs;
54 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -040055 LOCK(lock);
Rich Felker427c0ca2013-03-23 18:59:30 -040056
57 if (ident) {
58 size_t n = strnlen(ident, sizeof log_ident - 1);
59 memcpy(log_ident, ident, n);
60 log_ident[n] = 0;
61 } else {
62 log_ident[0] = 0;
63 }
64 log_opt = opt;
65 log_facility = facility;
66
67 if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
68
Rich Felker4750cf42012-04-24 16:32:23 -040069 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -040070 pthread_setcancelstate(cs, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050071}
72
Rich Felkerd2c604d2011-04-18 21:11:23 -040073static void _vsyslog(int priority, const char *message, va_list ap)
Rich Felker0b44a032011-02-12 00:22:29 -050074{
Rich Felker0b44a032011-02-12 00:22:29 -050075 char timebuf[16];
76 time_t now;
77 struct tm tm;
Rich Felker19c18302011-04-13 18:32:33 -040078 char buf[256];
79 int pid;
80 int l, l2;
Rich Felker0b44a032011-02-12 00:22:29 -050081
Rich Felker19c18302011-04-13 18:32:33 -040082 if (log_fd < 0) {
Rich Felker427c0ca2013-03-23 18:59:30 -040083 __openlog();
84 if (log_fd < 0) return;
Rich Felker0b44a032011-02-12 00:22:29 -050085 }
86
Rich Felker427c0ca2013-03-23 18:59:30 -040087 if (!(priority & LOG_FACMASK)) priority |= log_facility;
88
Rich Felker0b44a032011-02-12 00:22:29 -050089 now = time(NULL);
90 gmtime_r(&now, &tm);
91 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
92
Rich Felker19c18302011-04-13 18:32:33 -040093 pid = (log_opt & LOG_PID) ? getpid() : 0;
94 l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ",
Rich Felker427c0ca2013-03-23 18:59:30 -040095 priority, timebuf, log_ident, "["+!pid, pid, "]"+!pid);
Rich Felker19c18302011-04-13 18:32:33 -040096 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
97 if (l2 >= 0) {
Rich Felker427c0ca2013-03-23 18:59:30 -040098 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
99 else l += l2;
Rich Felker19c18302011-04-13 18:32:33 -0400100 if (buf[l-1] != '\n') buf[l++] = '\n';
Rich Felker427c0ca2013-03-23 18:59:30 -0400101 send(log_fd, buf, l, 0);
Rich Felker19c18302011-04-13 18:32:33 -0400102 }
Rich Felker0b44a032011-02-12 00:22:29 -0500103}
Rich Felker19c18302011-04-13 18:32:33 -0400104
Rich Felkerd2c604d2011-04-18 21:11:23 -0400105void __vsyslog(int priority, const char *message, va_list ap)
106{
107 int cs;
108 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
109 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker4750cf42012-04-24 16:32:23 -0400110 LOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -0400111 _vsyslog(priority, message, ap);
Rich Felker4750cf42012-04-24 16:32:23 -0400112 UNLOCK(lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -0400113 pthread_setcancelstate(cs, 0);
114}
115
Rich Felker19c18302011-04-13 18:32:33 -0400116void syslog(int priority, const char *message, ...)
117{
118 va_list ap;
119 va_start(ap, message);
120 __vsyslog(priority, message, ap);
121 va_end(ap);
122}
123
124weak_alias(__vsyslog, vsyslog);