4 Copyright (C) Andrew Tridgell 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "../include/ctdb_client.h"
22 #include "../include/ctdb_private.h"
23 #include "system/syslog.h"
24 #include "system/time.h"
25 #include "system/filesys.h"
27 struct syslog_message {
34 struct ctdb_syslog_state {
39 static int syslogd_is_started = 0;
42 /* called when child is finished
43 * this is for the syslog daemon, we can not use DEBUG here
45 static void ctdb_syslog_handler(struct event_context *ev, struct fd_event *fde,
46 uint16_t flags, void *p)
48 struct ctdb_syslog_state *state = talloc_get_type(p, struct ctdb_syslog_state);
52 struct syslog_message *msg;
58 count = recv(state->syslog_fd, str, sizeof(str), 0);
59 if (count < sizeof(struct syslog_message)) {
62 msg = (struct syslog_message *)str;
64 syslog(msg->level, "%s", msg->message);
68 /* called when the pipe from the main daemon has closed
69 * this is for the syslog daemon, we can not use DEBUG here
71 static void ctdb_syslog_terminate_handler(struct event_context *ev, struct fd_event *fde,
72 uint16_t flags, void *p)
74 syslog(LOG_ERR, "Shutting down SYSLOG daemon with pid:%d", (int)getpid());
81 * this is for the syslog daemon, we can not use DEBUG here
83 int start_syslog_daemon(struct ctdb_context *ctdb)
85 struct sockaddr_in syslog_sin;
86 struct ctdb_syslog_state *state;
87 struct tevent_fd *fde;
91 state = talloc(ctdb, struct ctdb_syslog_state);
92 CTDB_NO_MEMORY(ctdb, state);
94 if (pipe(state->fd) != 0) {
95 printf("Failed to create syslog pipe\n");
100 if (pipe(startup_fd) != 0) {
101 printf("Failed to create syslog startup pipe\n");
108 ctdb->syslogd_pid = ctdb_fork(ctdb);
109 if (ctdb->syslogd_pid == (pid_t)-1) {
110 printf("Failed to create syslog child process\n");
113 close(startup_fd[0]);
114 close(startup_fd[1]);
119 if (ctdb->syslogd_pid != 0) {
123 DEBUG(DEBUG_ERR,("Starting SYSLOG child process with pid:%d\n", (int)ctdb->syslogd_pid));
126 set_close_on_exec(state->fd[0]);
128 close(startup_fd[1]);
129 n = read(startup_fd[0], &dummy, sizeof(dummy));
130 close(startup_fd[0]);
131 if (n < sizeof(dummy)) {
135 syslogd_is_started = 1;
139 debug_extra = talloc_asprintf(NULL, "syslogd:");
140 talloc_free(ctdb->ev);
141 ctdb->ev = event_context_init(NULL);
143 syslog(LOG_ERR, "Starting SYSLOG daemon with pid:%d", (int)getpid());
144 ctdb_set_process_name("ctdb_syslogd");
147 close(startup_fd[0]);
148 set_close_on_exec(state->fd[1]);
149 set_close_on_exec(startup_fd[1]);
150 fde = event_add_fd(ctdb->ev, state, state->fd[1], EVENT_FD_READ,
151 ctdb_syslog_terminate_handler, state);
152 tevent_fd_set_auto_close(fde);
154 state->syslog_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
155 if (state->syslog_fd == -1) {
156 printf("Failed to create syslog socket\n");
157 close(startup_fd[1]);
161 set_close_on_exec(state->syslog_fd);
163 syslog_sin.sin_family = AF_INET;
164 syslog_sin.sin_port = htons(CTDB_PORT);
165 syslog_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
167 if (bind(state->syslog_fd, (struct sockaddr *)&syslog_sin,
168 sizeof(syslog_sin)) == -1)
170 printf("syslog daemon failed to bind to socket. errno:%d(%s)\n", errno, strerror(errno));
171 close(startup_fd[1]);
176 fde = event_add_fd(ctdb->ev, state, state->syslog_fd, EVENT_FD_READ,
177 ctdb_syslog_handler, state);
178 tevent_fd_set_auto_close(fde);
180 /* Tell parent that we're up */
182 write(startup_fd[1], &ret, sizeof(ret));
183 close(startup_fd[1]);
185 event_loop_wait(ctdb->ev);
187 /* this should not happen */
191 struct ctdb_log_state {
192 struct ctdb_context *ctdb;
198 void (*logfn)(const char *, uint16_t, void *);
202 /* we need this global to keep the DEBUG() syntax */
203 static struct ctdb_log_state *log_state;
206 syslog logging function
208 static void ctdb_syslog_log(const char *format, va_list ap)
210 struct syslog_message *msg;
211 int level = LOG_DEBUG;
215 struct sockaddr_in syslog_sin;
217 ret = vasprintf(&s, format, ap);
222 switch (this_log_level) {
249 len = offsetof(struct syslog_message, message) + strlen(debug_extra) + strlen(s) + 1;
256 msg->len = strlen(debug_extra) + strlen(s);
257 strcpy(msg->message, debug_extra);
258 strcat(msg->message, s);
260 if (syslogd_is_started == 0) {
261 syslog(msg->level, "%s", msg->message);
263 syslog_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
264 if (syslog_fd == -1) {
265 printf("Failed to create syslog socket\n");
271 syslog_sin.sin_family = AF_INET;
272 syslog_sin.sin_port = htons(CTDB_PORT);
273 syslog_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
275 ret = sendto(syslog_fd, msg, len, 0,
276 (struct sockaddr *)&syslog_sin,
278 /* no point in checking here since we cant log an error */
289 log file logging function
291 static void ctdb_logfile_log(const char *format, va_list ap)
300 ret = vasprintf(&s, format, ap);
302 const char *errstr = "vasprintf failed\n";
304 write(log_state->fd, errstr, strlen(errstr));
308 t = timeval_current();
309 tm = localtime(&t.tv_sec);
311 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
313 ret = asprintf(&s2, "%s.%06u [%s%5u]: %s",
314 tbuf, (unsigned)t.tv_usec,
315 debug_extra, (unsigned)getpid(), s);
318 const char *errstr = "asprintf failed\n";
319 write(log_state->fd, errstr, strlen(errstr));
323 write(log_state->fd, s2, strlen(s2));
328 static void ctdb_logfile_log_add(const char *format, va_list ap)
333 ret = vasprintf(&s, format, ap);
335 const char *errstr = "vasprintf failed\n";
337 write(log_state->fd, errstr, strlen(errstr));
342 write(log_state->fd, s, strlen(s));
350 choose the logfile location
352 int ctdb_set_logfile(struct ctdb_context *ctdb, const char *logfile, bool use_syslog)
356 ctdb->log = talloc_zero(ctdb, struct ctdb_log_state);
357 if (ctdb->log == NULL) {
358 printf("talloc_zero failed\n");
362 ctdb->log->ctdb = ctdb;
363 log_state = ctdb->log;
366 do_debug_v = ctdb_syslog_log;
367 do_debug_add_v = ctdb_syslog_log;
368 ctdb->log->use_syslog = true;
369 } else if (logfile == NULL || strcmp(logfile, "-") == 0) {
370 do_debug_v = ctdb_logfile_log;
371 do_debug_add_v = ctdb_logfile_log_add;
373 /* also catch stderr of subcommands to stdout */
376 printf("dup2 failed: %s\n", strerror(errno));
380 do_debug_v = ctdb_logfile_log;
381 do_debug_add_v = ctdb_logfile_log_add;
383 ctdb->log->fd = open(logfile, O_WRONLY|O_APPEND|O_CREAT, 0666);
384 if (ctdb->log->fd == -1) {
385 printf("Failed to open logfile %s\n", logfile);
393 /* Note that do_debug always uses the global log state. */
394 static void write_to_log(struct ctdb_log_state *log,
395 const char *buf, unsigned int len)
397 if (script_log_level <= LogLevel) {
398 if (log != NULL && log->prefix != NULL) {
399 do_debug("%s: %*.*s\n", log->prefix, len, len, buf);
401 do_debug("%*.*s\n", len, len, buf);
403 /* log it in the eventsystem as well */
405 log->logfn(log->buf, len, log->logfn_private);
410 called when log data comes in from a child process
412 static void ctdb_log_handler(struct event_context *ev, struct fd_event *fde,
413 uint16_t flags, void *private)
415 struct ctdb_log_state *log = talloc_get_type(private, struct ctdb_log_state);
419 if (!(flags & EVENT_FD_READ)) {
423 n = read(log->pfd, &log->buf[log->buf_used],
424 sizeof(log->buf) - log->buf_used);
428 if (log != log_state) {
434 this_log_level = script_log_level;
436 while (log->buf_used > 0 &&
437 (p = memchr(log->buf, '\n', log->buf_used)) != NULL) {
438 int n1 = (p - log->buf)+1;
440 /* swallow \r from child processes */
441 if (n2 > 0 && log->buf[n2-1] == '\r') {
444 write_to_log(log, log->buf, n2);
445 memmove(log->buf, p+1, sizeof(log->buf) - n1);
449 /* the buffer could have completely filled - unfortunately we have
450 no choice but to dump it out straight away */
451 if (log->buf_used == sizeof(log->buf)) {
452 write_to_log(log, log->buf, log->buf_used);
457 static int log_context_destructor(struct ctdb_log_state *log)
459 /* Flush buffer in case it wasn't \n-terminated. */
460 if (log->buf_used > 0) {
461 this_log_level = script_log_level;
462 write_to_log(log, log->buf, log->buf_used);
468 fork(), redirecting child output to logging and specified callback.
470 struct ctdb_log_state *ctdb_fork_with_logging(TALLOC_CTX *mem_ctx,
471 struct ctdb_context *ctdb,
472 const char *log_prefix,
473 void (*logfn)(const char *, uint16_t, void *),
474 void *logfn_private, pid_t *pid)
477 struct ctdb_log_state *log;
478 struct tevent_fd *fde;
480 log = talloc_zero(mem_ctx, struct ctdb_log_state);
481 CTDB_NO_MEMORY_NULL(ctdb, log);
483 log->prefix = log_prefix;
485 log->logfn_private = (void *)logfn_private;
488 DEBUG(DEBUG_ERR,(__location__ " Failed to setup for child logging pipe\n"));
492 *pid = ctdb_fork(ctdb);
496 close(STDOUT_FILENO);
497 close(STDERR_FILENO);
498 dup2(p[1], STDOUT_FILENO);
499 dup2(p[1], STDERR_FILENO);
508 DEBUG(DEBUG_ERR, (__location__ " fork failed for child process\n"));
514 set_close_on_exec(log->pfd);
515 talloc_set_destructor(log, log_context_destructor);
516 fde = event_add_fd(ctdb->ev, log, log->pfd,
517 EVENT_FD_READ, ctdb_log_handler, log);
518 tevent_fd_set_auto_close(fde);
528 setup for logging of child process stdout
530 int ctdb_set_child_logging(struct ctdb_context *ctdb)
533 int old_stdout, old_stderr;
534 struct tevent_fd *fde;
536 if (ctdb->log->fd == STDOUT_FILENO) {
537 /* not needed for stdout logging */
541 /* setup a pipe to catch IO from subprocesses */
543 DEBUG(DEBUG_ERR,(__location__ " Failed to setup for child logging pipe\n"));
547 /* We'll fail if stderr/stdout not already open; it's simpler. */
548 old_stdout = dup(STDOUT_FILENO);
549 old_stderr = dup(STDERR_FILENO);
550 if (old_stdout < 0 || old_stderr < 0) {
551 DEBUG(DEBUG_ERR, ("Failed to dup stdout/stderr for child logging\n"));
554 if (dup2(p[1], STDOUT_FILENO) < 0 || dup2(p[1], STDERR_FILENO) < 0) {
555 int saved_errno = errno;
556 dup2(old_stdout, STDOUT_FILENO);
557 dup2(old_stderr, STDERR_FILENO);
564 printf(__location__ " dup2 failed: %s\n",
572 fde = event_add_fd(ctdb->ev, ctdb->log, p[0],
573 EVENT_FD_READ, ctdb_log_handler, ctdb->log);
574 tevent_fd_set_auto_close(fde);
576 ctdb->log->pfd = p[0];
578 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d for logging\n", p[0]));
585 * set up a log handler to catch logging from TEVENT
587 static void ctdb_tevent_logging(void *private_data,
588 enum tevent_debug_level level,
592 enum debug_level lvl = DEBUG_CRIT;
595 case TEVENT_DEBUG_FATAL:
598 case TEVENT_DEBUG_ERROR:
601 case TEVENT_DEBUG_WARNING:
604 case TEVENT_DEBUG_TRACE:
609 if (lvl <= LogLevel) {
610 this_log_level = lvl;
615 int ctdb_init_tevent_logging(struct ctdb_context *ctdb)
619 ret = tevent_set_debug(ctdb->ev,