blob: cbe65209627df54d5dc24459ad26b4a00c031998 [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include <stdarg.h>
2#include <sys/socket.h>
3#include <stdio.h>
4#include <fcntl.h>
5#include <unistd.h>
6#include <syslog.h>
7#include <time.h>
8#include <signal.h>
9#include <string.h>
Rich Felkerd2c604d2011-04-18 21:11:23 -040010#include <pthread.h>
Rich Felker0b44a032011-02-12 00:22:29 -050011#include "libc.h"
12
13static int lock;
14static const char *log_ident;
15static 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{
22 int old = log_mask;
23 if (maskpri) log_mask = maskpri;
24 return old;
25}
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 Felker0b44a032011-02-12 00:22:29 -050039 LOCK(&lock);
Rich Felker19c18302011-04-13 18:32:33 -040040 close(log_fd);
41 log_fd = -1;
Rich Felker0b44a032011-02-12 00:22:29 -050042 UNLOCK(&lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -040043 pthread_setcancelstate(cs, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050044}
45
46static void __openlog(const char *ident, int opt, int facility)
47{
Rich Felker0b44a032011-02-12 00:22:29 -050048 log_ident = ident;
49 log_opt = opt;
50 log_facility = facility;
51
Rich Felker19c18302011-04-13 18:32:33 -040052 if (!(opt & LOG_NDELAY) || log_fd>=0) return;
Rich Felker0b44a032011-02-12 00:22:29 -050053
Rich Felker19c18302011-04-13 18:32:33 -040054 log_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
55 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
Rich Felker0b44a032011-02-12 00:22:29 -050056}
57
58void openlog(const char *ident, int opt, int facility)
59{
Rich Felkerd2c604d2011-04-18 21:11:23 -040060 int cs;
61 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker0b44a032011-02-12 00:22:29 -050062 LOCK(&lock);
63 __openlog(ident, opt, facility);
64 UNLOCK(&lock);
Rich Felkerd2c604d2011-04-18 21:11:23 -040065 pthread_setcancelstate(cs, 0);
Rich Felker0b44a032011-02-12 00:22:29 -050066}
67
Rich Felkerd2c604d2011-04-18 21:11:23 -040068static void _vsyslog(int priority, const char *message, va_list ap)
Rich Felker0b44a032011-02-12 00:22:29 -050069{
Rich Felker0b44a032011-02-12 00:22:29 -050070 char timebuf[16];
71 time_t now;
72 struct tm tm;
Rich Felker19c18302011-04-13 18:32:33 -040073 char buf[256];
74 int pid;
75 int l, l2;
Rich Felker0b44a032011-02-12 00:22:29 -050076
Rich Felker19c18302011-04-13 18:32:33 -040077 if (log_fd < 0) {
78 __openlog(log_ident, log_opt | LOG_NDELAY, log_facility);
79 if (log_fd < 0) {
80 UNLOCK(&lock);
81 return;
82 }
Rich Felker0b44a032011-02-12 00:22:29 -050083 }
84
Rich Felker0b44a032011-02-12 00:22:29 -050085 now = time(NULL);
86 gmtime_r(&now, &tm);
87 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
88
Rich Felker19c18302011-04-13 18:32:33 -040089 pid = (log_opt & LOG_PID) ? getpid() : 0;
90 l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ",
91 priority, timebuf,
92 log_ident ? log_ident : "",
93 "["+!pid, pid, "]"+!pid);
94 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
95 if (l2 >= 0) {
96 l += l2;
97 if (buf[l-1] != '\n') buf[l++] = '\n';
98 sendto(log_fd, buf, l, 0, (void *)&log_addr, 11);
99 }
Rich Felker0b44a032011-02-12 00:22:29 -0500100
Rich Felker0b44a032011-02-12 00:22:29 -0500101 UNLOCK(&lock);
102}
Rich Felker19c18302011-04-13 18:32:33 -0400103
Rich Felkerd2c604d2011-04-18 21:11:23 -0400104void __vsyslog(int priority, const char *message, va_list ap)
105{
106 int cs;
107 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
108 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
109 LOCK(&lock);
110 _vsyslog(priority, message, ap);
111 UNLOCK(&lock);
112 pthread_setcancelstate(cs, 0);
113}
114
Rich Felker19c18302011-04-13 18:32:33 -0400115void syslog(int priority, const char *message, ...)
116{
117 va_list ap;
118 va_start(ap, message);
119 __vsyslog(priority, message, ap);
120 va_end(ap);
121}
122
123weak_alias(__vsyslog, vsyslog);