4 Copyright (C) Andrew Tridgell 2008
5 Copyright (C) Martin Schwenke 2014
6 Copyright (C) Amitay Isaacs 2015
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "system/network.h"
24 #include "system/locale.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
27 #include "system/syslog.h"
28 #include "system/dir.h"
30 #include "lib/util/time_basic.h"
31 #include "lib/util/sys_rw.h"
32 #include "lib/util/debug.h"
33 #include "lib/util/blocking.h"
34 #include "lib/util/samba_util.h" /* get_myname() */
36 #include "common/logging.h"
40 const char *log_string;
41 } log_string_map[] = {
42 { DEBUG_ERR, "ERROR" },
43 { DEBUG_WARNING, "WARNING" },
45 { DEBUG_NOTICE, "NOTICE" },
47 { DEBUG_INFO, "INFO" },
52 { DEBUG_DEBUG, "DEBUG" },
55 bool debug_level_parse(const char *log_string, int *log_level)
59 if (log_string == NULL) {
63 if (isdigit(log_string[0])) {
64 int level = atoi(log_string);
66 if (level >= 0 && level < ARRAY_SIZE(log_string_map)) {
73 for (i=0; i<ARRAY_SIZE(log_string_map); i++) {
74 if (strncasecmp(log_string_map[i].log_string,
75 log_string, strlen(log_string)) == 0) {
76 *log_level = log_string_map[i].log_level;
84 const char *debug_level_to_string(int log_level)
88 for (i=0; ARRAY_SIZE(log_string_map); i++) {
89 if (log_string_map[i].log_level == log_level) {
90 return log_string_map[i].log_string;
96 int debug_level_from_string(const char *log_string)
101 found = debug_level_parse(log_string, &log_level);
106 /* Default debug level */
111 * file logging backend
114 struct file_log_state {
115 const char *app_name;
120 static void file_log(void *private_data, int level, const char *msg)
122 struct file_log_state *state = talloc_get_type_abort(
123 private_data, struct file_log_state);
125 struct timeval_buf tvbuf;
128 if (state->fd == STDERR_FILENO) {
129 ret = snprintf(state->buffer, sizeof(state->buffer),
131 state->app_name, (unsigned)getpid(), msg);
134 timeval_str_buf(&tv, false, true, &tvbuf);
136 ret = snprintf(state->buffer, sizeof(state->buffer),
137 "%s %s[%u]: %s\n", tvbuf.buf,
138 state->app_name, (unsigned)getpid(), msg);
144 state->buffer[sizeof(state->buffer)-1] = '\0';
146 sys_write_v(state->fd, state->buffer, strlen(state->buffer));
149 static int file_log_state_destructor(struct file_log_state *state)
151 if (state->fd != -1 && state->fd != STDERR_FILENO) {
158 static bool file_log_validate(const char *option)
164 if (option == NULL || strcmp(option, "-") == 0) {
175 ret = stat(dir, &st);
181 if (! S_ISDIR(st.st_mode)) {
188 static int file_log_setup(TALLOC_CTX *mem_ctx, const char *option,
189 const char *app_name)
191 struct file_log_state *state;
193 state = talloc_zero(mem_ctx, struct file_log_state);
198 state->app_name = app_name;
200 if (option == NULL || strcmp(option, "-") == 0) {
203 state->fd = STDERR_FILENO;
204 ret = dup2(STDERR_FILENO, STDOUT_FILENO);
206 int save_errno = errno;
212 state->fd = open(option, O_WRONLY|O_APPEND|O_CREAT, 0644);
213 if (state->fd == -1) {
214 int save_errno = errno;
219 if (! set_close_on_exec(state->fd)) {
220 int save_errno = errno;
226 talloc_set_destructor(state, file_log_state_destructor);
227 debug_set_callback(state, file_log);
233 * syslog logging backend
236 /* Copied from lib/util/debug.c */
237 static int debug_level_to_priority(int level)
240 * map debug levels to syslog() priorities
242 static const int priority_map[] = {
256 if( level >= ARRAY_SIZE(priority_map) || level < 0)
257 priority = LOG_DEBUG;
259 priority = priority_map[level];
264 struct syslog_log_state {
266 const char *app_name;
267 const char *hostname;
268 int (*format)(int dbglevel, struct syslog_log_state *state,
269 const char *str, char *buf, int bsize);
270 /* RFC3164 says: The total length of the packet MUST be 1024
273 unsigned int dropped_count;
276 /* Format messages as per RFC3164
278 * It appears that some syslog daemon implementations do not allow a
279 * hostname when messages are sent via a Unix domain socket, so omit
280 * it. Similarly, syslogd on FreeBSD does not understand the hostname
281 * part of the header, even when logging via UDP. Note that most
282 * implementations will log messages against "localhost" when logging
283 * via UDP. A timestamp could be sent but rsyslogd on Linux limits
284 * the timestamp logged to the precision that was received on
285 * /dev/log. It seems sane to send degenerate RFC3164 messages
286 * without a header at all, so that the daemon will generate high
287 * resolution timestamps if configured.
289 static int format_rfc3164(int dbglevel, struct syslog_log_state *state,
290 const char *str, char *buf, int bsize)
295 pri = LOG_DAEMON | debug_level_to_priority(dbglevel);
296 len = snprintf(buf, bsize, "<%d>%s[%u]: %s",
297 pri, state->app_name, getpid(), str);
299 len = MIN(len, bsize - 1);
304 /* Format messages as per RFC5424
306 * <165>1 2003-08-24T05:14:15.000003-07:00 192.0.2.1
307 * myproc 8710 - - %% It's time to make the do-nuts.
309 static int format_rfc5424(int dbglevel, struct syslog_log_state *state,
310 const char *str, char *buf, int bsize)
314 struct timeval_buf tvbuf;
318 pri = LOG_DAEMON | debug_level_to_priority(dbglevel);
320 len = snprintf(buf, bsize,
321 "<%d>1 %s %s %s %u - - ",
322 pri, timeval_str_buf(&tv, true, true, &tvbuf),
323 state->hostname, state->app_name, getpid());
324 /* A truncated header is not useful... */
330 s = snprintf(&buf[len], bsize - len, "%s", str);
332 len = MIN(len + s, bsize - 1);
337 static void syslog_log(void *private_data, int level, const char *msg)
339 syslog(debug_level_to_priority(level), "%s", msg);
342 static int syslog_log_sock_maybe(struct syslog_log_state *state,
343 int level, const char *msg)
348 n = state->format(level, state, msg, state->buffer,
349 sizeof(state->buffer));
355 ret = write(state->fd, state->buffer, n);
356 } while (ret == -1 && errno == EINTR);
365 static void syslog_log_sock(void *private_data, int level, const char *msg)
367 struct syslog_log_state *state = talloc_get_type_abort(
368 private_data, struct syslog_log_state);
371 if (state->dropped_count > 0) {
373 snprintf(t, sizeof(t),
374 "[Dropped %u log messages]\n",
375 state->dropped_count);
376 t[sizeof(t)-1] = '\0';
377 ret = syslog_log_sock_maybe(state, level, t);
378 if (ret == EAGAIN || ret == EWOULDBLOCK) {
379 state->dropped_count++;
381 * If above failed then actually drop the
382 * message that would be logged below, since
383 * it would have been dropped anyway and it is
384 * also likely to fail. Falling through and
385 * attempting to log the message also means
386 * that the dropped message count will be
387 * logged out of order.
392 /* Silent failure on any other error */
395 state->dropped_count = 0;
398 ret = syslog_log_sock_maybe(state, level, msg);
399 if (ret == EAGAIN || ret == EWOULDBLOCK) {
400 state->dropped_count++;
404 static int syslog_log_setup_syslog(TALLOC_CTX *mem_ctx, const char *app_name)
406 openlog(app_name, LOG_PID, LOG_DAEMON);
408 debug_set_callback(NULL, syslog_log);
413 static int syslog_log_state_destructor(struct syslog_log_state *state)
415 if (state->fd != -1) {
422 static int syslog_log_setup_common(TALLOC_CTX *mem_ctx, const char *app_name,
423 struct syslog_log_state **result)
425 struct syslog_log_state *state;
427 state = talloc_zero(mem_ctx, struct syslog_log_state);
433 state->app_name = app_name;
434 talloc_set_destructor(state, syslog_log_state_destructor);
441 static int syslog_log_setup_nonblocking(TALLOC_CTX *mem_ctx,
442 const char *app_name)
444 struct syslog_log_state *state = NULL;
445 struct sockaddr_un dest;
448 ret = syslog_log_setup_common(mem_ctx, app_name, &state);
453 state->fd = socket(AF_UNIX, SOCK_DGRAM, 0);
454 if (state->fd == -1) {
455 int save_errno = errno;
460 dest.sun_family = AF_UNIX;
461 strncpy(dest.sun_path, _PATH_LOG, sizeof(dest.sun_path)-1);
462 ret = connect(state->fd,
463 (struct sockaddr *)&dest, sizeof(dest));
465 int save_errno = errno;
470 ret = set_blocking(state->fd, false);
472 int save_errno = errno;
477 if (! set_close_on_exec(state->fd)) {
478 int save_errno = errno;
483 state->hostname = NULL; /* Make this explicit */
484 state->format = format_rfc3164;
486 debug_set_callback(state, syslog_log_sock);
490 #endif /* _PATH_LOG */
492 static int syslog_log_setup_udp(TALLOC_CTX *mem_ctx, const char *app_name,
495 struct syslog_log_state *state = NULL;
496 struct sockaddr_in dest;
499 ret = syslog_log_setup_common(mem_ctx, app_name, &state);
504 state->fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
505 if (state->fd == -1) {
506 int save_errno = errno;
511 dest.sin_family = AF_INET;
512 dest.sin_port = htons(514);
513 dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
514 ret = connect(state->fd,
515 (struct sockaddr *)&dest, sizeof(dest));
517 int save_errno = errno;
522 if (! set_close_on_exec(state->fd)) {
523 int save_errno = errno;
528 state->hostname = get_myname(state);
529 if (state->hostname == NULL) {
530 /* Use a fallback instead of failing initialisation */
531 state->hostname = "localhost";
534 state->format = format_rfc5424;
536 state->format = format_rfc3164;
539 debug_set_callback(state, syslog_log_sock);
544 static bool syslog_log_validate(const char *option)
546 if (option == NULL) {
549 } else if (strcmp(option, "nonblocking") == 0) {
552 } else if (strcmp(option, "udp") == 0) {
554 } else if (strcmp(option, "udp-rfc5424") == 0) {
561 static int syslog_log_setup(TALLOC_CTX *mem_ctx, const char *option,
562 const char *app_name)
564 if (option == NULL) {
565 return syslog_log_setup_syslog(mem_ctx, app_name);
567 } else if (strcmp(option, "nonblocking") == 0) {
568 return syslog_log_setup_nonblocking(mem_ctx, app_name);
570 } else if (strcmp(option, "udp") == 0) {
571 return syslog_log_setup_udp(mem_ctx, app_name, false);
572 } else if (strcmp(option, "udp-rfc5424") == 0) {
573 return syslog_log_setup_udp(mem_ctx, app_name, true);
581 bool (*validate)(const char *option);
582 int (*setup)(TALLOC_CTX *mem_ctx,
584 const char *app_name);
587 static struct log_backend log_backend[] = {
590 .validate = file_log_validate,
591 .setup = file_log_setup,
595 .validate = syslog_log_validate,
596 .setup = syslog_log_setup,
600 static int log_backend_parse(TALLOC_CTX *mem_ctx,
602 struct log_backend **backend,
603 char **backend_option)
605 struct log_backend *b = NULL;
606 char *t, *name, *option;
609 t = talloc_strdup(mem_ctx, logging);
614 name = strtok(t, ":");
619 option = strtok(NULL, ":");
621 for (i=0; i<ARRAY_SIZE(log_backend); i++) {
622 if (strcmp(log_backend[i].name, name) == 0) {
633 if (option != NULL) {
634 *backend_option = talloc_strdup(mem_ctx, option);
635 if (*backend_option == NULL) {
640 *backend_option = NULL;
647 bool logging_validate(const char *logging)
650 struct log_backend *backend;
655 tmp_ctx = talloc_new(NULL);
656 if (tmp_ctx == NULL) {
660 ret = log_backend_parse(tmp_ctx, logging, &backend, &option);
662 talloc_free(tmp_ctx);
666 status = backend->validate(option);
667 talloc_free(tmp_ctx);
671 /* Initialise logging */
672 int logging_init(TALLOC_CTX *mem_ctx, const char *logging,
673 const char *debug_level, const char *app_name)
675 struct log_backend *backend = NULL;
679 setup_logging(app_name, DEBUG_STDERR);
681 if (debug_level == NULL) {
682 debug_level = getenv("CTDB_DEBUGLEVEL");
684 if (! debug_level_parse(debug_level, &DEBUGLEVEL)) {
688 if (logging == NULL) {
689 logging = getenv("CTDB_LOGGING");
691 if (logging == NULL || logging[0] == '\0') {
695 ret = log_backend_parse(mem_ctx, logging, &backend, &option);
698 fprintf(stderr, "Invalid logging option \'%s\'\n",
705 ret = backend->setup(mem_ctx, option, app_name);