summaryrefslogtreecommitdiff
path: root/ctdb/common/logging.c
blob: dc8c4f7505896007bfee30833fb6c53d6ca65604 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*
   Logging utilities

   Copyright (C) Andrew Tridgell  2008
   Copyright (C) Martin Schwenke  2014
   Copyright (C) Amitay Isaacs  2015

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/network.h"
#include "system/locale.h"
#include "system/time.h"
#include "system/filesys.h"
#include "system/syslog.h"
#include "system/dir.h"

#include "lib/util/time_basic.h"
#include "lib/util/sys_rw.h"
#include "lib/util/debug.h"
#include "lib/util/blocking.h"
#include "lib/util/samba_util.h" /* get_myname() */

#include "common/logging.h"

struct {
	int log_level;
	const char *log_string;
} log_string_map[] = {
	{ DEBUG_ERR,     "ERROR" },
	{ DEBUG_WARNING, "WARNING" },
	{ 2,             "WARNING" },
	{ DEBUG_NOTICE,  "NOTICE" },
	{ 4,             "NOTICE" },
	{ DEBUG_INFO,    "INFO" },
	{ 6,             "INFO" },
	{ 7,             "INFO" },
	{ 8,             "INFO" },
	{ 9,             "INFO" },
	{ DEBUG_DEBUG,   "DEBUG" },
};

bool debug_level_parse(const char *log_string, int *log_level)
{
	int i;

	if (log_string == NULL) {
		return false;
	}

	if (isdigit(log_string[0])) {
		int level = atoi(log_string);

		if (level >= 0 && level < ARRAY_SIZE(log_string_map)) {
			*log_level = level;
			return true;
		}
		return false;
	}

	for (i=0; i<ARRAY_SIZE(log_string_map); i++) {
		if (strncasecmp(log_string_map[i].log_string,
				log_string, strlen(log_string)) == 0) {
			*log_level = log_string_map[i].log_level;
			return true;
		}
	}

	return false;
}

const char *debug_level_to_string(int log_level)
{
	int i;

	for (i=0; ARRAY_SIZE(log_string_map); i++) {
		if (log_string_map[i].log_level == log_level) {
			return log_string_map[i].log_string;
		}
	}
	return "UNKNOWN";
}

int debug_level_from_string(const char *log_string)
{
	bool found;
	int log_level;

	found = debug_level_parse(log_string, &log_level);
	if (found) {
		return log_level;
	}

	/* Default debug level */
	return DEBUG_ERR;
}

/*
 * file logging backend
 */

struct file_log_state {
	const char *app_name;
	int fd;
	char buffer[1024];
};

static void file_log(void *private_data, int level, const char *msg)
{
	struct file_log_state *state = talloc_get_type_abort(
		private_data, struct file_log_state);
	struct timeval tv;
	struct timeval_buf tvbuf;
	int ret;

	if (state->fd == STDERR_FILENO) {
		ret = snprintf(state->buffer, sizeof(state->buffer),
			       "%s[%u]: %s\n",
			       state->app_name, (unsigned)getpid(), msg);
	} else {
		GetTimeOfDay(&tv);
		timeval_str_buf(&tv, false, true, &tvbuf);

		ret = snprintf(state->buffer, sizeof(state->buffer),
			       "%s %s[%u]: %s\n", tvbuf.buf,
			       state->app_name, (unsigned)getpid(), msg);
	}
	if (ret < 0) {
		return;
	}

	state->buffer[sizeof(state->buffer)-1] = '\0';

	sys_write_v(state->fd, state->buffer, strlen(state->buffer));
}

static int file_log_state_destructor(struct file_log_state *state)
{
	if (state->fd != -1 && state->fd != STDERR_FILENO) {
		close(state->fd);
		state->fd = -1;
	}
	return 0;
}

static bool file_log_validate(const char *option)
{
	char *t, *dir;
	struct stat st;
	int ret;

	if (option == NULL || strcmp(option, "-") == 0) {
		return true;
	}

	t = strdup(option);
	if (t == NULL) {
		return false;
	}

	dir = dirname(t);

	ret = stat(dir, &st);
	free(t);
	if (ret != 0) {
		return false;
	}

	if (! S_ISDIR(st.st_mode)) {
		return false;
	}

	return true;
}

static int file_log_setup(TALLOC_CTX *mem_ctx, const char *option,
			  const char *app_name)
{
	struct file_log_state *state;

	state = talloc_zero(mem_ctx, struct file_log_state);
	if (state == NULL) {
		return ENOMEM;
	}

	state->app_name = app_name;

	if (option == NULL || strcmp(option, "-") == 0) {
		int ret;

		state->fd = STDERR_FILENO;
		ret = dup2(STDERR_FILENO, STDOUT_FILENO);
		if (ret == -1) {
			int save_errno = errno;
			talloc_free(state);
			return save_errno;
		}

	} else {
		state->fd = open(option, O_WRONLY|O_APPEND|O_CREAT, 0644);
		if (state->fd == -1) {
			int save_errno = errno;
			talloc_free(state);
			return save_errno;
		}

		if (! set_close_on_exec(state->fd)) {
			int save_errno = errno;
			talloc_free(state);
			return save_errno;
		}
	}

	talloc_set_destructor(state, file_log_state_destructor);
	debug_set_callback(state, file_log);

	return 0;
}

/*
 * syslog logging backend
 */

/* Copied from lib/util/debug.c */
static int debug_level_to_priority(int level)
{
        /*
         * map debug levels to syslog() priorities
         */
        static const int priority_map[] = {
                LOG_ERR,     /* 0 */
                LOG_WARNING, /* 1 */
                LOG_NOTICE,  /* 2 */
                LOG_NOTICE,  /* 3 */
                LOG_NOTICE,  /* 4 */
                LOG_NOTICE,  /* 5 */
                LOG_INFO,    /* 6 */
                LOG_INFO,    /* 7 */
                LOG_INFO,    /* 8 */
                LOG_INFO,    /* 9 */
        };
        int priority;

        if( level >= ARRAY_SIZE(priority_map) || level < 0)
                priority = LOG_DEBUG;
        else
                priority = priority_map[level];

        return priority;
}

struct syslog_log_state {
	int fd;
	const char *app_name;
	const char *hostname;
	int (*format)(int dbglevel, struct syslog_log_state *state,
		      const char *str, char *buf, int bsize);
	/* RFC3164 says: The total length of the packet MUST be 1024
	   bytes or less. */
	char buffer[1024];
	unsigned int dropped_count;
};

/* Format messages as per RFC3164
 *
 * It appears that some syslog daemon implementations do not allow a
 * hostname when messages are sent via a Unix domain socket, so omit
 * it.  Similarly, syslogd on FreeBSD does not understand the hostname
 * part of the header, even when logging via UDP.  Note that most
 * implementations will log messages against "localhost" when logging
 * via UDP.  A timestamp could be sent but rsyslogd on Linux limits
 * the timestamp logged to the precision that was received on
 * /dev/log.  It seems sane to send degenerate RFC3164 messages
 * without a header at all, so that the daemon will generate high
 * resolution timestamps if configured.
 */
static int format_rfc3164(int dbglevel, struct syslog_log_state *state,
			  const char *str, char *buf, int bsize)
{
	int pri;
	int len;

	pri = LOG_DAEMON | debug_level_to_priority(dbglevel);
	len = snprintf(buf, bsize, "<%d>%s[%u]: %s",
		       pri, state->app_name, getpid(), str);
	buf[bsize-1] = '\0';
	len = MIN(len, bsize - 1);

	return len;
}

/* Format messages as per RFC5424
 *
 * <165>1 2003-08-24T05:14:15.000003-07:00 192.0.2.1
 *         myproc 8710 - - %% It's time to make the do-nuts.
 */
static int format_rfc5424(int dbglevel, struct syslog_log_state *state,
			  const char *str, char *buf, int bsize)
{
	int pri;
	struct timeval tv;
	struct timeval_buf tvbuf;
	int len, s;

	/* Header */
	pri = LOG_DAEMON | debug_level_to_priority(dbglevel);
	GetTimeOfDay(&tv);
	len = snprintf(buf, bsize,
		       "<%d>1 %s %s %s %u - - ",
		       pri, timeval_str_buf(&tv, true, true, &tvbuf),
		       state->hostname, state->app_name, getpid());
	/* A truncated header is not useful... */
	if (len >= bsize) {
		return -1;
	}

	/* Message */
	s = snprintf(&buf[len], bsize - len, "%s", str);
	buf[bsize-1] = '\0';
	len = MIN(len + s, bsize - 1);

	return len;
}

static void syslog_log(void *private_data, int level, const char *msg)
{
	syslog(debug_level_to_priority(level), "%s", msg);
}

static int syslog_log_sock_maybe(struct syslog_log_state *state,
				 int level, const char *msg)
{
	int n;
	ssize_t ret;

	n = state->format(level, state, msg, state->buffer,
			  sizeof(state->buffer));
	if (n == -1) {
		return E2BIG;
	}

	do {
		ret = write(state->fd, state->buffer, n);
	} while (ret == -1 && errno == EINTR);

	if (ret == -1) {
		return errno;
	}

	return 0;

}
static void syslog_log_sock(void *private_data, int level, const char *msg)
{
	struct syslog_log_state *state = talloc_get_type_abort(
		private_data, struct syslog_log_state);
	int ret;

	if (state->dropped_count > 0) {
		char t[64] = { 0 };
		snprintf(t, sizeof(t),
			 "[Dropped %u log messages]\n",
			 state->dropped_count);
		t[sizeof(t)-1] = '\0';
		ret = syslog_log_sock_maybe(state, level, t);
		if (ret == EAGAIN || ret == EWOULDBLOCK) {
			state->dropped_count++;
			/*
			 * If above failed then actually drop the
			 * message that would be logged below, since
			 * it would have been dropped anyway and it is
			 * also likely to fail.  Falling through and
			 * attempting to log the message also means
			 * that the dropped message count will be
			 * logged out of order.
			 */
			return;
		}
		if (ret != 0) {
			/* Silent failure on any other error */
			return;
		}
		state->dropped_count = 0;
	}

	ret = syslog_log_sock_maybe(state, level, msg);
	if (ret == EAGAIN || ret == EWOULDBLOCK) {
		state->dropped_count++;
	}
}

static int syslog_log_setup_syslog(TALLOC_CTX *mem_ctx, const char *app_name)
{
	openlog(app_name, LOG_PID, LOG_DAEMON);

	debug_set_callback(NULL, syslog_log);

	return 0;
}

static int syslog_log_state_destructor(struct syslog_log_state *state)
{
	if (state->fd != -1) {
		close(state->fd);
		state->fd = -1;
	}
	return 0;
}

static int syslog_log_setup_common(TALLOC_CTX *mem_ctx, const char *app_name,
				   struct syslog_log_state **result)
{
	struct syslog_log_state *state;

	state = talloc_zero(mem_ctx, struct syslog_log_state);
	if (state == NULL) {
		return ENOMEM;
	}

	state->fd = -1;
	state->app_name = app_name;
	talloc_set_destructor(state, syslog_log_state_destructor);

	*result = state;
	return 0;
}

#ifdef _PATH_LOG
static int syslog_log_setup_nonblocking(TALLOC_CTX *mem_ctx,
					const char *app_name)
{
	struct syslog_log_state *state = NULL;
	struct sockaddr_un dest;
	int ret;

	ret = syslog_log_setup_common(mem_ctx, app_name, &state);
	if (ret != 0) {
		return ret;
	}

	state->fd = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (state->fd == -1) {
		int save_errno = errno;
		talloc_free(state);
		return save_errno;
	}

	dest.sun_family = AF_UNIX;
	strncpy(dest.sun_path, _PATH_LOG, sizeof(dest.sun_path)-1);
	ret = connect(state->fd,
		      (struct sockaddr *)&dest, sizeof(dest));
	if (ret == -1) {
		int save_errno = errno;
		talloc_free(state);
		return save_errno;
	}

	ret = set_blocking(state->fd, false);
	if (ret != 0) {
		int save_errno = errno;
		talloc_free(state);
		return save_errno;
	}

	if (! set_close_on_exec(state->fd)) {
		int save_errno = errno;
		talloc_free(state);
		return save_errno;
	}

	state->hostname = NULL; /* Make this explicit */
	state->format = format_rfc3164;

	debug_set_callback(state, syslog_log_sock);

	return 0;
}
#endif /* _PATH_LOG */

static int syslog_log_setup_udp(TALLOC_CTX *mem_ctx, const char *app_name,
				bool rfc5424)
{
	struct syslog_log_state *state = NULL;
	struct sockaddr_in dest;
	int ret;

	ret = syslog_log_setup_common(mem_ctx, app_name, &state);
	if (ret != 0) {
		return ret;
	}

	state->fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (state->fd == -1) {
		int save_errno = errno;
		talloc_free(state);
		return save_errno;
	}

	dest.sin_family = AF_INET;
	dest.sin_port   = htons(514);
	dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	ret = connect(state->fd,
		      (struct sockaddr *)&dest, sizeof(dest));
	if (ret == -1) {
		int save_errno = errno;
		talloc_free(state);
		return save_errno;
	}

	if (! set_close_on_exec(state->fd)) {
		int save_errno = errno;
		talloc_free(state);
		return save_errno;
	}

	state->hostname = get_myname(state);
	if (state->hostname == NULL) {
		/* Use a fallback instead of failing initialisation */
		state->hostname = "localhost";
	}
	if (rfc5424) {
		state->format = format_rfc5424;
	} else {
		state->format = format_rfc3164;
	}

	debug_set_callback(state, syslog_log_sock);

	return 0;
}

static bool syslog_log_validate(const char *option)
{
	if (option == NULL) {
		return true;
#ifdef _PATH_LOG
	} else if (strcmp(option, "nonblocking") == 0) {
		return true;
#endif
	} else if (strcmp(option, "udp") == 0) {
		return true;
	} else if (strcmp(option, "udp-rfc5424") == 0) {
		return true;
	}

	return false;
}

static int syslog_log_setup(TALLOC_CTX *mem_ctx, const char *option,
			    const char *app_name)
{
	if (option == NULL) {
		return syslog_log_setup_syslog(mem_ctx, app_name);
#ifdef _PATH_LOG
	} else if (strcmp(option, "nonblocking") == 0) {
		return syslog_log_setup_nonblocking(mem_ctx, app_name);
#endif
	} else if (strcmp(option, "udp") == 0) {
		return syslog_log_setup_udp(mem_ctx, app_name, false);
	} else if (strcmp(option, "udp-rfc5424") == 0) {
		return syslog_log_setup_udp(mem_ctx, app_name, true);
	}

	return EINVAL;
}

struct log_backend {
	const char *name;
	bool (*validate)(const char *option);
	int (*setup)(TALLOC_CTX *mem_ctx,
		     const char *option,
		     const char *app_name);
};

static struct log_backend log_backend[] = {
	{
		.name = "file",
		.validate = file_log_validate,
		.setup = file_log_setup,
	},
	{
		.name = "syslog",
		.validate = syslog_log_validate,
		.setup = syslog_log_setup,
	},
};

static int log_backend_parse(TALLOC_CTX *mem_ctx,
			     const char *logging,
			     struct log_backend **backend,
			     char **backend_option)
{
	struct log_backend *b = NULL;
	char *t, *name, *option;
	int i;

	t = talloc_strdup(mem_ctx, logging);
	if (t == NULL) {
		return ENOMEM;
	}

	name = strtok(t, ":");
	if (name == NULL) {
		talloc_free(t);
		return EINVAL;
	}
	option = strtok(NULL, ":");

	for (i=0; i<ARRAY_SIZE(log_backend); i++) {
		if (strcmp(log_backend[i].name, name) == 0) {
			b = &log_backend[i];
		}
	}

	if (b == NULL) {
		talloc_free(t);
		return ENOENT;
	}

	*backend = b;
	if (option != NULL) {
		*backend_option = talloc_strdup(mem_ctx, option);
		if (*backend_option == NULL) {
			talloc_free(t);
			return ENOMEM;
		}
	} else {
		*backend_option = NULL;
	}

	talloc_free(t);
	return 0;
}

bool logging_validate(const char *logging)
{
	TALLOC_CTX *tmp_ctx;
	struct log_backend *backend;
	char *option;
	int ret;
	bool status;

	tmp_ctx = talloc_new(NULL);
	if (tmp_ctx == NULL) {
		return false;
	}

	ret = log_backend_parse(tmp_ctx, logging, &backend, &option);
	if (ret != 0) {
		talloc_free(tmp_ctx);
		return false;
	}

	status = backend->validate(option);
	talloc_free(tmp_ctx);
	return status;
}

/* Initialise logging */
int logging_init(TALLOC_CTX *mem_ctx, const char *logging,
		 const char *debug_level, const char *app_name)
{
	struct log_backend *backend = NULL;
	char *option = NULL;
	int ret;

	setup_logging(app_name, DEBUG_STDERR);

	if (debug_level == NULL) {
		debug_level = getenv("CTDB_DEBUGLEVEL");
	}
	if (! debug_level_parse(debug_level, &DEBUGLEVEL)) {
		return EINVAL;
	}

	if (logging == NULL) {
		logging = getenv("CTDB_LOGGING");
	}
	if (logging == NULL || logging[0] == '\0') {
		return EINVAL;
	}

	ret = log_backend_parse(mem_ctx, logging, &backend, &option);
	if (ret != 0) {
		if (ret == ENOENT) {
			fprintf(stderr, "Invalid logging option \'%s\'\n",
				logging);
		}
		talloc_free(option);
		return ret;
	}

	ret = backend->setup(mem_ctx, option, app_name);
	talloc_free(option);
	return ret;
}