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 "lib/events/events.h"
22 #include "../include/ctdb_client.h"
23 #include "../include/ctdb_private.h"
24 #include "system/syslog.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
28 struct syslog_message {
35 struct ctdb_syslog_state {
40 static int syslogd_is_started = 0;
43 /* called when child is finished
44 * this is for the syslog daemon, we can not use DEBUG here
46 static void ctdb_syslog_handler(struct event_context *ev, struct fd_event *fde,
47 uint16_t flags, void *p)
49 struct ctdb_syslog_state *state = talloc_get_type(p, struct ctdb_syslog_state);
53 struct syslog_message *msg;
59 count = recv(state->syslog_fd, str, sizeof(str), 0);
60 if (count < sizeof(struct syslog_message)) {
63 msg = (struct syslog_message *)str;
65 syslog(msg->level, "%s", msg->message);
69 /* called when the pipd from the main daemon has closed
70 * this is for the syslog daemon, we can not use DEBUG here
72 static void ctdb_syslog_terminate_handler(struct event_context *ev, struct fd_event *fde,
73 uint16_t flags, void *p)
75 syslog(LOG_ERR, "Shutting down SYSLOG daemon with pid:%d", (int)getpid());
82 * this is for the syslog daemon, we can not use DEBUG here
84 int start_syslog_daemon(struct ctdb_context *ctdb)
86 struct sockaddr_in syslog_sin;
87 struct ctdb_syslog_state *state;
89 state = talloc(ctdb, struct ctdb_syslog_state);
90 CTDB_NO_MEMORY(ctdb, state);
92 if (pipe(state->fd) != 0) {
93 printf("Failed to create syslog pipe\n");
98 ctdb->syslogd_pid = fork();
99 if (ctdb->syslogd_pid == (pid_t)-1) {
100 printf("Failed to create syslog child process\n");
107 syslogd_is_started = 1;
109 if (ctdb->syslogd_pid != 0) {
110 DEBUG(DEBUG_ERR,("Starting SYSLOG child process with pid:%d\n", (int)ctdb->syslogd_pid));
113 set_close_on_exec(state->fd[0]);
118 debug_extra = talloc_asprintf(NULL, "syslogd:");
119 talloc_free(ctdb->ev);
120 ctdb->ev = event_context_init(NULL);
122 syslog(LOG_ERR, "Starting SYSLOG daemon with pid:%d", (int)getpid());
125 set_close_on_exec(state->fd[1]);
126 event_add_fd(ctdb->ev, state, state->fd[1], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
127 ctdb_syslog_terminate_handler, state);
129 state->syslog_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
130 if (state->syslog_fd == -1) {
131 printf("Failed to create syslog socket\n");
135 set_close_on_exec(state->syslog_fd);
137 syslog_sin.sin_family = AF_INET;
138 syslog_sin.sin_port = htons(CTDB_PORT);
139 syslog_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
141 if (bind(state->syslog_fd, &syslog_sin, sizeof(syslog_sin)) == -1) {
142 if (errno == EADDRINUSE) {
143 /* this is ok, we already have a syslog daemon */
146 printf("syslog daemon failed to bind to socket. errno:%d(%s)\n", errno, strerror(errno));
151 event_add_fd(ctdb->ev, state, state->syslog_fd, EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
152 ctdb_syslog_handler, state);
154 event_loop_wait(ctdb->ev);
156 /* this should not happen */
160 struct ctdb_log_state {
161 struct ctdb_context *ctdb;
166 void (*logfn)(const char *, uint16_t, void *);
170 /* we need this global to keep the DEBUG() syntax */
171 static struct ctdb_log_state *log_state;
174 syslog logging function
176 static void ctdb_syslog_log(const char *format, va_list ap)
178 struct syslog_message *msg;
179 int level = LOG_DEBUG;
183 struct sockaddr_in syslog_sin;
185 ret = vasprintf(&s, format, ap);
190 switch (this_log_level) {
217 len = offsetof(struct syslog_message, message) + strlen(debug_extra) + strlen(s) + 1;
224 msg->len = strlen(debug_extra) + strlen(s);
225 strcpy(msg->message, debug_extra);
226 strcat(msg->message, s);
228 if (syslogd_is_started == 0) {
229 syslog(msg->level, "%s", msg->message);
231 syslog_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
232 if (syslog_fd == -1) {
233 printf("Failed to create syslog socket\n");
239 syslog_sin.sin_family = AF_INET;
240 syslog_sin.sin_port = htons(CTDB_PORT);
241 syslog_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
244 ret = sendto(syslog_fd, msg, len, 0, &syslog_sin, sizeof(syslog_sin));
245 /* no point in checking here since we cant log an error */
256 log file logging function
258 static void ctdb_logfile_log(const char *format, va_list ap)
267 ret = vasprintf(&s, format, ap);
269 const char *errstr = "vasprintf failed\n";
271 write(log_state->fd, errstr, strlen(errstr));
275 t = timeval_current();
276 tm = localtime(&t.tv_sec);
278 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
280 ret = asprintf(&s2, "%s.%06u [%s%5u]: %s",
281 tbuf, (unsigned)t.tv_usec,
282 debug_extra, (unsigned)getpid(), s);
285 const char *errstr = "asprintf failed\n";
286 write(log_state->fd, errstr, strlen(errstr));
290 write(log_state->fd, s2, strlen(s2));
295 static void ctdb_logfile_log_add(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));
309 write(log_state->fd, s, strlen(s));
317 choose the logfile location
319 int ctdb_set_logfile(struct ctdb_context *ctdb, const char *logfile, bool use_syslog)
323 ctdb->log = talloc_zero(ctdb, struct ctdb_log_state);
324 if (ctdb->log == NULL) {
325 printf("talloc_zero failed\n");
329 ctdb->log->ctdb = ctdb;
330 log_state = ctdb->log;
333 do_debug_v = ctdb_syslog_log;
334 do_debug_add_v = ctdb_syslog_log;
335 ctdb->log->use_syslog = true;
336 } else if (logfile == NULL || strcmp(logfile, "-") == 0) {
337 do_debug_v = ctdb_logfile_log;
338 do_debug_add_v = ctdb_logfile_log_add;
340 /* also catch stderr of subcommands to stdout */
343 printf("dup2 failed: %s\n", strerror(errno));
347 do_debug_v = ctdb_logfile_log;
348 do_debug_add_v = ctdb_logfile_log_add;
350 ctdb->log->fd = open(logfile, O_WRONLY|O_APPEND|O_CREAT, 0666);
351 if (ctdb->log->fd == -1) {
352 printf("Failed to open logfile %s\n", logfile);
360 /* Note that do_debug always uses the global log state. */
361 static void write_to_log(struct ctdb_log_state *log,
362 const char *buf, unsigned int len)
364 if (script_log_level <= LogLevel) {
365 do_debug("%*.*s\n", len, len, buf);
366 /* log it in the eventsystem as well */
368 log->logfn(log->buf, len, log->logfn_private);
373 called when log data comes in from a child process
375 static void ctdb_log_handler(struct event_context *ev, struct fd_event *fde,
376 uint16_t flags, void *private)
378 struct ctdb_log_state *log = talloc_get_type(private, struct ctdb_log_state);
382 if (!(flags & EVENT_FD_READ)) {
386 n = read(log->pfd, &log->buf[log->buf_used],
387 sizeof(log->buf) - log->buf_used);
391 if (log != log_state) {
397 this_log_level = script_log_level;
399 while (log->buf_used > 0 &&
400 (p = memchr(log->buf, '\n', log->buf_used)) != NULL) {
401 int n1 = (p - log->buf)+1;
403 /* swallow \r from child processes */
404 if (n2 > 0 && log->buf[n2-1] == '\r') {
407 write_to_log(log, log->buf, n2);
408 memmove(log->buf, p+1, sizeof(log->buf) - n1);
412 /* the buffer could have completely filled - unfortunately we have
413 no choice but to dump it out straight away */
414 if (log->buf_used == sizeof(log->buf)) {
415 write_to_log(log, log->buf, log->buf_used);
420 static int log_context_destructor(struct ctdb_log_state *log)
422 /* Flush buffer in case it wasn't \n-terminated. */
423 if (log->buf_used > 0) {
424 this_log_level = script_log_level;
425 write_to_log(log, log->buf, log->buf_used);
431 fork(), redirecting child output to logging and specified callback.
433 struct ctdb_log_state *ctdb_fork_with_logging(TALLOC_CTX *mem_ctx,
434 struct ctdb_context *ctdb,
435 void (*logfn)(const char *, uint16_t, void *),
436 void *logfn_private, pid_t *pid)
439 struct ctdb_log_state *log;
441 log = talloc_zero(mem_ctx, struct ctdb_log_state);
442 CTDB_NO_MEMORY_NULL(ctdb, log);
445 log->logfn_private = (void *)logfn_private;
448 DEBUG(DEBUG_ERR,(__location__ " Failed to setup for child logging pipe\n"));
456 close(STDOUT_FILENO);
457 close(STDERR_FILENO);
458 dup2(p[1], STDOUT_FILENO);
459 dup2(p[1], STDERR_FILENO);
468 DEBUG(DEBUG_ERR, (__location__ " fork failed for child process\n"));
474 set_close_on_exec(log->pfd);
475 talloc_set_destructor(log, log_context_destructor);
476 event_add_fd(ctdb->ev, log, log->pfd,
477 EVENT_FD_READ | EVENT_FD_AUTOCLOSE,
478 ctdb_log_handler, log);
487 setup for logging of child process stdout
489 int ctdb_set_child_logging(struct ctdb_context *ctdb)
492 int old_stdout, old_stderr;
494 if (ctdb->log->fd == STDOUT_FILENO) {
495 /* not needed for stdout logging */
499 /* setup a pipe to catch IO from subprocesses */
501 DEBUG(DEBUG_ERR,(__location__ " Failed to setup for child logging pipe\n"));
505 /* We'll fail if stderr/stdout not already open; it's simpler. */
506 old_stdout = dup(STDOUT_FILENO);
507 old_stderr = dup(STDERR_FILENO);
508 if (dup2(p[1], STDOUT_FILENO) < 0 || dup2(p[1], STDERR_FILENO) < 0) {
509 int saved_errno = errno;
510 dup2(old_stdout, STDOUT_FILENO);
511 dup2(old_stderr, STDERR_FILENO);
518 printf(__location__ " dup2 failed: %s\n",
526 /* Is this correct for STDOUT and STDERR ? */
527 set_close_on_exec(STDOUT_FILENO);
528 set_close_on_exec(STDERR_FILENO);
529 set_close_on_exec(p[0]);
531 event_add_fd(ctdb->ev, ctdb->log, p[0],
532 EVENT_FD_READ | EVENT_FD_AUTOCLOSE,
533 ctdb_log_handler, ctdb->log);
534 ctdb->log->pfd = p[0];
536 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d for logging\n", p[0]));