summaryrefslogtreecommitdiff
path: root/accel-pppd/logs/log_syslog.c
blob: 5a922f5733bd19838781cb7c555427e2c54bb5b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <syslog.h>

#include "triton.h"
#include "spinlock.h"
#include "log.h"
#include "list.h"
#include "events.h"
#include "ppp.h"

#include "memdebug.h"

static int conf_queue_max = 1000;

static void syslog_close(struct triton_context_t *ctx);

static struct triton_context_t syslog_ctx = {
	.close = syslog_close,
	.before_switch = log_switch,
};

static LIST_HEAD(msg_queue);
static int queue_size;
static int sleeping = 1;
static spinlock_t queue_lock = SPINLOCK_INITIALIZER;
static char *log_buf;
static int need_close;
static char *ident;
static int prio_map[] = {LOG_INFO, LOG_ERR, LOG_WARNING, LOG_INFO, LOG_INFO, LOG_DEBUG};

static void unpack_msg(struct log_msg_t *msg)
{
	struct log_chunk_t *chunk;
	int pos;

	strcpy(log_buf, msg->hdr->msg);
	pos = strlen(log_buf);

	list_for_each_entry(chunk, msg->chunks, entry) {
		memcpy(log_buf + pos, chunk->msg, chunk->len);
		pos += chunk->len;
	}

	if (pos > 1)
		log_buf[pos - 1] = 0;
	else
		log_buf[0] = 0;
}

static void set_hdr(struct log_msg_t *msg, struct ppp_t *ppp)
{
	if (ppp) {
		if (snprintf(msg->hdr->msg, LOG_CHUNK_SIZE, "%s:%s: ", ppp->ifname, ppp->username ? ppp->username : ""))
			strcpy(msg->hdr->msg + LOG_CHUNK_SIZE - 3, ": ");
	} else 
		msg->hdr->msg[0] = 0;
}

static void do_syslog(void)
{
	struct log_msg_t *msg;

	while (1) {
		spin_lock(&queue_lock);
		if (list_empty(&msg_queue)) {
			sleeping = 1;
			spin_unlock(&queue_lock);
			if (need_close)
				triton_context_unregister(&syslog_ctx);
			return;
		}

		msg = list_entry(msg_queue.next, typeof(*msg), entry);
		list_del(&msg->entry);
		--queue_size;
		spin_unlock(&queue_lock);

		unpack_msg(msg);
		syslog(prio_map[msg->level], "%s", log_buf);
		log_free_msg(msg);
	}
}

static void queue_log(struct log_msg_t *msg)
{
	int r = 0, f = 0;
	spin_lock(&queue_lock);
	if (queue_size < conf_queue_max) {
		list_add_tail(&msg->entry, &msg_queue);
		++queue_size;
		r = sleeping;
		sleeping = 0;
	} else
		f = 1;
	spin_unlock(&queue_lock);

	if (r)
		triton_context_call(&syslog_ctx, (void (*)(void*))do_syslog, NULL);
	else if (f)
		log_free_msg(msg);
}


static void general_log(struct log_target_t *t, struct log_msg_t *msg, struct ppp_t *ppp)
{
	set_hdr(msg, ppp);

	if (syslog_ctx.tpd)
		queue_log(msg);
	else {
		unpack_msg(msg);
		syslog(prio_map[msg->level], "%s", log_buf);
		log_free_msg(msg);
	}
}

static void syslog_close(struct triton_context_t *ctx)
{
	spin_lock(&queue_lock);
	if (sleeping) {
		triton_context_unregister(&syslog_ctx);
	} else
		need_close = 1;	
	spin_unlock(&queue_lock);
}

static struct log_target_t target = {
	.log = general_log,
};

static void parse_opt(const char *opt, char **ident, int *facility)
{
	char *str = _strdup(opt);
	char *ptr, *endptr;
	int n;
	const char *facility_name[] = {"daemon", "local0", "local1", "local2", "local3", "local4", "local5", "local6", "local7"};
	const int facility_num[] = {LOG_DAEMON, LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7};

	ptr = strchr(str, ',');
	if (ptr) {
		*ptr = 0;
		n = strtol(ptr + 1, &endptr, 10);
		if (*endptr) {
			for (n = 0; n < sizeof(facility_name); n++) {
				if (!strcasecmp(ptr + 1, facility_name[n]))
					break;
			}
			if (n == sizeof(facility_name))
				log_emerg("log_syslog: unknown facility name '%s'\n", ptr + 1);
			else
				*facility = facility_num[n];
		} else
			*facility = n;
	}

	*ident = str;
}

static void load_config()
{
	const char *opt;
	int facility = LOG_DAEMON;

	if (ident) {
		closelog();
		_free(ident);
	}

	opt = conf_get_opt("log", "syslog");
	if (opt)
		parse_opt(opt, &ident, &facility);
	else
		ident = _strdup("accel-pppd");

	openlog(ident, 0, facility);
}

static void init(void)
{
	log_buf = malloc(LOG_MAX_SIZE + 1);

	load_config();

	triton_context_register(&syslog_ctx, NULL);
	triton_context_wakeup(&syslog_ctx);

	log_register_target(&target);

	triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);
}

DEFINE_INIT(1, init);