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

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

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

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

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

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

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

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

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

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

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

87 88
	if (!(priority & LOG_FACMASK)) priority |= log_facility;

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

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

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

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

weak_alias(__vsyslog, vsyslog);