summaryrefslogtreecommitdiff
path: root/accel-pppd/logs/log_tcp.c
blob: b89b7d65fe367bb1cabfa1659b159bc0b5364f55 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

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

#include "memdebug.h"

struct tcp_target_t
{
	struct log_target_t target;
	struct list_head entry;
	struct triton_md_handler_t hnd;
	struct triton_timer_t conn_timer;
  struct sockaddr_in addr;
	char *buf;
	int buf_size;
	int buf_pos;
	spinlock_t lock;
	struct list_head queue;
	int queue_len;
	int connected:1;
	int wait:1;
};

static int conf_connect_interval = 5;
static int conf_queue_len = 1000;

static struct triton_context_t tcp_ctx;

static const char* level_name[]={"  msg", "error", " warn", " info", " info", "debug"};

static void start_connect(struct tcp_target_t *t);

static LIST_HEAD(targets);

static void disconnect(struct tcp_target_t *t)
{
	triton_md_unregister_handler(&t->hnd);
	close(t->hnd.fd);

	start_connect(t);
}

static void unpack_msg(struct tcp_target_t *t, struct log_msg_t *msg)
{
	struct log_chunk_t *chunk;
	int pos = strlen(msg->hdr->msg);

	strcpy(t->buf, msg->hdr->msg);

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

	t->buf_size = pos;
	t->buf_pos = 0;
}

static int send_log(struct tcp_target_t *t)
{
	struct log_msg_t *msg;
	int n;

	while (1) {
		spin_lock(&t->lock);
		if (!t->queue_len) {
			t->wait = 0;
			spin_unlock(&t->lock);
			return 0;
		}
		msg = list_entry(t->queue.next, typeof(*msg), entry);
		list_del(&msg->entry);
		t->queue_len--;
		spin_unlock(&t->lock);

		unpack_msg(t, msg);

		log_free_msg(msg);

		while (t->buf_pos != t->buf_size) {
			n = write(t->hnd.fd, t->buf + t->buf_pos, t->buf_size - t->buf_pos);
			if (n < 0) {
				if (errno == EAGAIN)
					return 1;
				if (errno != EPIPE)
					log_emerg("log-tcp: write: %s\n", strerror(errno));
				disconnect(t);
				return 0;
			}
			t->buf_pos += n;
		}
	}
}

static void queue_log(struct tcp_target_t *t, struct log_msg_t *msg)
{
	int r;

	spin_lock(&t->lock);
	if (t->queue_len == conf_queue_len) {
		spin_unlock(&t->lock);
		log_free_msg(msg);
		return;
	}
	list_add_tail(&msg->entry, &t->queue);
	t->queue_len++;
	if (t->connected) {
		r = t->wait;
		t->wait = 1;
	} else
		r = 1;
	spin_unlock(&t->lock);

	if (!r) {
		if (send_log(t))
			triton_md_enable_handler(&t->hnd, MD_MODE_WRITE);
	}
}

static void set_hdr(struct log_msg_t *msg, struct ap_session *ses)
{
	struct tm tm;
	char timestamp[32];

	localtime_r(&msg->timestamp.tv_sec, &tm);

	strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", &tm);
	sprintf(msg->hdr->msg, "[%s]: %s: %s: ", timestamp, level_name[msg->level],	ses ? ses->ifname : "");
	msg->hdr->len = strlen(msg->hdr->msg);
}

static void general_log(struct log_target_t *lt, struct log_msg_t *msg, struct ap_session *ses)
{
	struct tcp_target_t *t = container_of(lt, typeof(*t), target);

	set_hdr(msg, ses);
	queue_log(t, msg);
}

static int log_tcp_write(struct triton_md_handler_t *h)
{
	struct tcp_target_t *t = container_of(h, typeof(*t), hnd);

	if (!send_log(t))
		triton_md_disable_handler(h, MD_MODE_WRITE);

	return 0;
}

static int log_tcp_connect(struct triton_md_handler_t *h)
{
	struct tcp_target_t *t = container_of(h, typeof(*t), hnd);

	if (connect(t->hnd.fd, &t->addr, sizeof(t->addr))) {
		if (errno == EAGAIN)
			return 0;
		if (errno == EINPROGRESS)
			return 0;
		log_emerg("log-tcp: connect: %s\n", strerror(errno));
		triton_md_unregister_handler(&t->hnd);
		close(t->hnd.fd);
		triton_timer_add(&tcp_ctx, &t->conn_timer, 0);
		return 0;
	}

	t->hnd.write = log_tcp_write;

	triton_md_disable_handler(&t->hnd, MD_MODE_WRITE);

	spin_lock(&t->lock);
	t->connected = 1;
	t->wait = 1;
	spin_unlock(&t->lock);

	if (send_log(t))
		triton_md_enable_handler(&t->hnd, MD_MODE_WRITE);

	return 0;
}

static void connect_timer(struct triton_timer_t *timer)
{
	struct tcp_target_t *t = container_of(timer, typeof(*t), conn_timer);

	triton_timer_del(timer);

	start_connect(t);
}

static void start_connect(struct tcp_target_t *t)
{
	t->hnd.write = log_tcp_connect;
	t->hnd.fd = socket(PF_INET, SOCK_STREAM, 0);

	if (!t->hnd.fd) {
		log_emerg("log-tcp: socket: %s\n", strerror(errno));
		return;
	}
	
	fcntl(t->hnd.fd, F_SETFD, fcntl(t->hnd.fd, F_GETFD) | FD_CLOEXEC);
	
	if (fcntl(t->hnd.fd, F_SETFL, O_NONBLOCK)) {
    log_emerg("log-tcp: failed to set nonblocking mode: %s\n", strerror(errno));
		close(t->hnd.fd);
    return;
	}

	if (connect(t->hnd.fd, &t->addr, sizeof(t->addr))) {
		if (errno != EINPROGRESS) {
			log_emerg("log-tcp: connect: %s\n", strerror(errno));
			close(t->hnd.fd);
			return;
		}
	}

	triton_md_register_handler(&tcp_ctx, &t->hnd);
	triton_md_enable_handler(&t->hnd, MD_MODE_WRITE);
}

static void log_tcp_close(struct triton_context_t *ctx)
{
	struct tcp_target_t *t;

	while (!list_empty(&targets)) {
		t = list_entry(targets.next, typeof(*t), entry);
		list_del(&t->entry);
		if (t->conn_timer.tpd)
			triton_timer_del(&t->conn_timer);
		else {
			t->connected = 0;
			triton_md_unregister_handler(&t->hnd);
			close(t->hnd.fd);
		}
	}

	triton_context_unregister(&tcp_ctx);
}

static int start_log(const char *_opt)
{
	struct tcp_target_t *t;
	char *opt = strdup(_opt);
	int port;
	char *d;

	d = strchr(opt, ':');
	if (!d)
		goto err;

	*d = 0;

	port = atoi(d + 1);
	if (port <= 0)
		goto err;

	t = _malloc(sizeof(*t));
	memset(t, 0, sizeof(*t));

	t->buf = _malloc(LOG_MAX_SIZE + 64);

	t->conn_timer.expire_tv.tv_sec = conf_connect_interval;
	t->conn_timer.expire = connect_timer;

	t->target.log = general_log;

	memset(&t->addr, 0, sizeof(t->addr));
  t->addr.sin_family = AF_INET;
  t->addr.sin_port = htons(port);
	t->addr.sin_addr.s_addr = inet_addr(opt);

	INIT_LIST_HEAD(&t->queue);

	spinlock_init(&t->lock);

	start_connect(t);

	log_register_target(&t->target);

	list_add_tail(&t->entry, &targets);

	return 0;

err:
	free(opt);
	return -1;
}

static struct triton_context_t tcp_ctx ={
	.close = log_tcp_close,
	.before_switch = log_switch,
};

static void init(void)
{
	struct conf_sect_t *s =	conf_get_section("log");
	struct conf_option_t *opt;
	
	if (!s)
		return;

	triton_context_register(&tcp_ctx, NULL);

	list_for_each_entry(opt, &s->items, entry) {
		if (strcmp(opt->name, "log-tcp"))
			continue;
		if (!opt->val || start_log(opt->val))
			log_emerg("log: log-tcp: invalid format: '%s'\n", opt->val);
	}

	triton_context_wakeup(&tcp_ctx);
}

DEFINE_INIT(1, init);