syslog.c 2.8 KB
Newer Older
R
Rich Felker 已提交
1 2 3 4 5 6 7 8
#include <stdarg.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <time.h>
#include <signal.h>
#include <string.h>
9
#include <pthread.h>
10
#include <errno.h>
11
#include <fcntl.h>
R
Rich Felker 已提交
12
#include "libc.h"
13
#include "atomic.h"
R
Rich Felker 已提交
14

15
static int lock[2];
16
static char log_ident[32];
R
Rich Felker 已提交
17 18 19
static int log_opt;
static int log_facility = LOG_USER;
static int log_mask = 0xff;
20
static int log_fd = -1;
R
Rich Felker 已提交
21 22 23

int setlogmask(int maskpri)
{
24 25
	if (maskpri) return a_swap(&log_mask, maskpri);
	else return log_mask;
R
Rich Felker 已提交
26 27 28 29 30 31 32 33 34 35 36 37
}

static const struct {
	short sun_family;
	char sun_path[9];
} log_addr = {
	AF_UNIX,
	"/dev/log"
};

void closelog(void)
{
38 39
	int cs;
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
40
	LOCK(lock);
41 42
	close(log_fd);
	log_fd = -1;
43
	UNLOCK(lock);
44
	pthread_setcancelstate(cs, 0);
R
Rich Felker 已提交
45 46
}

47
static void __openlog()
R
Rich Felker 已提交
48
{
49 50 51 52 53 54
	int fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
	if (fd < 0) return;
	if (connect(fd, (void *)&log_addr, sizeof log_addr) < 0)
		close(fd);
	else
		log_fd = fd;
R
Rich Felker 已提交
55 56 57 58
}

void openlog(const char *ident, int opt, int facility)
{
59 60
	int cs;
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
61
	LOCK(lock);
62 63 64 65 66 67 68 69 70 71 72 73 74

	if (ident) {
		size_t n = strnlen(ident, sizeof log_ident - 1);
		memcpy(log_ident, ident, n);
		log_ident[n] = 0;
	} else {
		log_ident[0] = 0;
	}
	log_opt = opt;
	log_facility = facility;

	if ((opt & LOG_NDELAY) && log_fd<0) __openlog();

75
	UNLOCK(lock);
76
	pthread_setcancelstate(cs, 0);
R
Rich Felker 已提交
77 78
}

79
static void _vsyslog(int priority, const char *message, va_list ap)
R
Rich Felker 已提交
80 81 82 83
{
	char timebuf[16];
	time_t now;
	struct tm tm;
84
	char buf[256];
85
	int errno_save = errno;
86 87
	int pid;
	int l, l2;
88
	int hlen;
89
	int fd;
R
Rich Felker 已提交
90

91
	if (log_fd < 0) __openlog();
R
Rich Felker 已提交
92

93 94
	if (!(priority & LOG_FACMASK)) priority |= log_facility;

R
Rich Felker 已提交
95 96 97 98
	now = time(NULL);
	gmtime_r(&now, &tm);
	strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);

99
	pid = (log_opt & LOG_PID) ? getpid() : 0;
100 101
	l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
		priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
102
	errno = errno_save;
103 104
	l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
	if (l2 >= 0) {
105 106
		if (l2 >= sizeof buf - l) l = sizeof buf - 1;
		else l += l2;
107
		if (buf[l-1] != '\n') buf[l++] = '\n';
108 109 110 111 112 113 114
		if (send(log_fd, buf, l, 0) < 0 && (log_opt & LOG_CONS)) {
			fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
			if (fd >= 0) {
				dprintf(fd, "%.*s", l-hlen, buf+hlen);
				close(fd);
			}
		}
115
		if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
116 117
	}
}
R
Rich Felker 已提交
118

119 120 121 122 123
void __vsyslog(int priority, const char *message, va_list ap)
{
	int cs;
	if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
124
	LOCK(lock);
125
	_vsyslog(priority, message, ap);
126
	UNLOCK(lock);
127 128 129
	pthread_setcancelstate(cs, 0);
}

130 131 132
void syslog(int priority, const char *message, ...)
{
	va_list ap;
R
Rich Felker 已提交
133
	va_start(ap, message);
134
	__vsyslog(priority, message, ap);
R
Rich Felker 已提交
135 136
	va_end(ap);
}
137 138

weak_alias(__vsyslog, vsyslog);