syslog.c 2.6 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>
R
Rich Felker 已提交
11
#include "libc.h"
12
#include "atomic.h"
R
Rich Felker 已提交
13

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

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

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

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

46
static void __openlog()
R
Rich Felker 已提交
47
{
48
	log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
49
	if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
R
Rich Felker 已提交
50 51 52 53
}

void openlog(const char *ident, int opt, int facility)
{
54 55
	int cs;
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
56
	LOCK(lock);
57 58 59 60 61 62 63 64 65 66 67 68 69

	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();

70
	UNLOCK(lock);
71
	pthread_setcancelstate(cs, 0);
R
Rich Felker 已提交
72 73
}

74
static void _vsyslog(int priority, const char *message, va_list ap)
R
Rich Felker 已提交
75 76 77 78
{
	char timebuf[16];
	time_t now;
	struct tm tm;
79
	char buf[256];
80
	int errno_save = errno;
81 82
	int pid;
	int l, l2;
R
Rich Felker 已提交
83

84
	if (log_fd < 0) {
85 86
		__openlog();
		if (log_fd < 0) return;
R
Rich Felker 已提交
87 88
	}

89 90
	if (!(priority & LOG_FACMASK)) priority |= log_facility;

R
Rich Felker 已提交
91 92 93 94
	now = time(NULL);
	gmtime_r(&now, &tm);
	strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);

95 96
	pid = (log_opt & LOG_PID) ? getpid() : 0;
	l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ",
97
		priority, timebuf, log_ident, "["+!pid, pid, "]"+!pid);
98
	errno = errno_save;
99 100
	l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
	if (l2 >= 0) {
101 102
		if (l2 >= sizeof buf - l) l = sizeof buf - 1;
		else l += l2;
103
		if (buf[l-1] != '\n') buf[l++] = '\n';
104
		send(log_fd, buf, l, 0);
105 106
	}
}
R
Rich Felker 已提交
107

108 109 110 111 112
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);
113
	LOCK(lock);
114
	_vsyslog(priority, message, ap);
115
	UNLOCK(lock);
116 117 118
	pthread_setcancelstate(cs, 0);
}

119 120 121
void syslog(int priority, const char *message, ...)
{
	va_list ap;
R
Rich Felker 已提交
122
	va_start(ap, message);
123
	__vsyslog(priority, message, ap);
R
Rich Felker 已提交
124 125
	va_end(ap);
}
126 127

weak_alias(__vsyslog, vsyslog);