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.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 talloc_free(ctdb->ev);
119 ctdb->ev = event_context_init(NULL);
121 syslog(LOG_ERR, "Starting SYSLOG daemon with pid:%d", (int)getpid());
124 event_add_fd(ctdb->ev, state, state->fd[1], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
125 ctdb_syslog_terminate_handler, state);
127 state->syslog_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
128 if (state->syslog_fd == -1) {
129 printf("Failed to create syslog socket\n");
133 syslog_sin.sin_family = AF_INET;
134 syslog_sin.sin_port = htons(CTDB_PORT);
135 syslog_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
137 if (bind(state->syslog_fd, &syslog_sin, sizeof(syslog_sin)) == -1) {
138 if (errno == EADDRINUSE) {
139 /* this is ok, we already have a syslog daemon */
142 printf("syslog daemon failed to bind to socket. errno:%d(%s)\n", errno, strerror(errno));
147 event_add_fd(ctdb->ev, state, state->syslog_fd, EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
148 ctdb_syslog_handler, state);
150 event_loop_wait(ctdb->ev);
152 /* this should not happen */
156 struct ctdb_log_state {
163 /* we need this global to keep the DEBUG() syntax */
164 static struct ctdb_log_state *log_state;
167 syslog logging function
169 static void ctdb_syslog_log(const char *format, va_list ap)
171 struct syslog_message *msg;
172 int level = LOG_DEBUG;
176 struct sockaddr_in syslog_sin;
178 ret = vasprintf(&s, format, ap);
183 switch (this_log_level) {
210 len = offsetof(struct syslog_message, message) + strlen(s) + 1;
217 msg->len = strlen(s);
218 strcpy(msg->message, s);
220 if (syslogd_is_started == 0) {
221 syslog(msg->level, "%s", msg->message);
223 syslog_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
224 if (syslog_fd == -1) {
225 printf("Failed to create syslog socket\n");
231 syslog_sin.sin_family = AF_INET;
232 syslog_sin.sin_port = htons(CTDB_PORT);
233 syslog_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
236 ret = sendto(syslog_fd, msg, len, 0, &syslog_sin, sizeof(syslog_sin));
237 /* no point in checking here since we cant log an error */
248 log file logging function
250 static void ctdb_logfile_log(const char *format, va_list ap)
259 ret = vasprintf(&s, format, ap);
261 const char *errstr = "vasprintf failed\n";
263 write(log_state->fd, errstr, strlen(errstr));
267 t = timeval_current();
268 tm = localtime(&t.tv_sec);
270 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
272 ret = asprintf(&s2, "%s.%06u [%5u]: %s",
273 tbuf, (unsigned)t.tv_usec, (unsigned)getpid(), s);
276 const char *errstr = "asprintf failed\n";
277 write(log_state->fd, errstr, strlen(errstr));
281 write(log_state->fd, s2, strlen(s2));
286 static void ctdb_logfile_log_add(const char *format, va_list ap)
291 ret = vasprintf(&s, format, ap);
293 const char *errstr = "vasprintf failed\n";
295 write(log_state->fd, errstr, strlen(errstr));
300 write(log_state->fd, s, strlen(s));
308 choose the logfile location
310 int ctdb_set_logfile(struct ctdb_context *ctdb, const char *logfile, bool use_syslog)
314 ctdb->log = talloc_zero(ctdb, struct ctdb_log_state);
315 if (ctdb->log == NULL) {
316 printf("talloc_zero failed\n");
320 log_state = ctdb->log;
323 do_debug_v = ctdb_syslog_log;
324 do_debug_add_v = ctdb_syslog_log;
325 ctdb->log->use_syslog = true;
326 } else if (logfile == NULL || strcmp(logfile, "-") == 0) {
327 do_debug_v = ctdb_logfile_log;
328 do_debug_add_v = ctdb_logfile_log_add;
330 /* also catch stderr of subcommands to stdout */
333 printf("dup2 failed: %s\n", strerror(errno));
337 do_debug_v = ctdb_logfile_log;
338 do_debug_add_v = ctdb_logfile_log_add;
340 ctdb->log->fd = open(logfile, O_WRONLY|O_APPEND|O_CREAT, 0666);
341 if (ctdb->log->fd == -1) {
342 printf("Failed to open logfile %s\n", logfile);
353 called when log data comes in from a child process
355 static void ctdb_log_handler(struct event_context *ev, struct fd_event *fde,
356 uint16_t flags, void *private)
358 struct ctdb_context *ctdb = talloc_get_type(private, struct ctdb_context);
362 if (!(flags & EVENT_FD_READ)) {
366 n = read(ctdb->log->pfd, &ctdb->log->buf[ctdb->log->buf_used],
367 sizeof(ctdb->log->buf) - ctdb->log->buf_used);
369 ctdb->log->buf_used += n;
372 this_log_level = script_log_level;
374 while (ctdb->log->buf_used > 0 &&
375 (p = memchr(ctdb->log->buf, '\n', ctdb->log->buf_used)) != NULL) {
376 int n1 = (p - ctdb->log->buf)+1;
378 /* swallow \r from child processes */
379 if (n2 > 0 && ctdb->log->buf[n2-1] == '\r') {
382 if (script_log_level <= LogLevel) {
383 do_debug("%*.*s\n", n2, n2, ctdb->log->buf);
384 /* log it in the eventsystem as well */
385 ctdb_log_event_script_output(ctdb, ctdb->log->buf, n2);
387 memmove(ctdb->log->buf, p+1, sizeof(ctdb->log->buf) - n1);
388 ctdb->log->buf_used -= n1;
391 /* the buffer could have completely filled - unfortunately we have
392 no choice but to dump it out straight away */
393 if (ctdb->log->buf_used == sizeof(ctdb->log->buf)) {
394 if (script_log_level <= LogLevel) {
396 (int)ctdb->log->buf_used, (int)ctdb->log->buf_used, ctdb->log->buf);
397 /* log it in the eventsystem as well */
398 ctdb_log_event_script_output(ctdb, ctdb->log->buf, ctdb->log->buf_used);
400 ctdb->log->buf_used = 0;
407 setup for logging of child process stdout
409 int ctdb_set_child_logging(struct ctdb_context *ctdb)
414 if (ctdb->log->fd == 1) {
415 /* not needed for stdout logging */
419 /* setup a pipe to catch IO from subprocesses */
421 DEBUG(DEBUG_ERR,(__location__ " Failed to setup for child logging pipe\n"));
425 event_add_fd(ctdb->ev, ctdb, p[0], EVENT_FD_READ,
426 ctdb_log_handler, ctdb);
427 set_close_on_exec(p[0]);
428 ctdb->log->pfd = p[0];
430 DEBUG(DEBUG_NOTICE, (__location__ " Created PIPE FD:%d for logging\n", p[0]));
437 printf("dup2 failed: %s\n", strerror(errno));
442 /* also catch stderr of subcommands to the log */
445 printf("dup2 failed: %s\n", strerror(errno));