50.samba run the smbcontrol in the background. no need to block waiting for it.
[sahlberg/ctdb.git] / server / ctdb_logging.c
index 807bb179e0b152353a2c329354c7504616a21060..27b990ef6768b09967b4e8139f0bbf19f9d1f5d8 100644 (file)
@@ -18,7 +18,7 @@
 */
 
 #include "includes.h"
-#include "lib/events/events.h"
+#include "lib/tevent/tevent.h"
 #include "../include/ctdb_client.h"
 #include "../include/ctdb_private.h"
 #include "system/syslog.h"
@@ -85,6 +85,7 @@ int start_syslog_daemon(struct ctdb_context *ctdb)
 {
        struct sockaddr_in syslog_sin;
        struct ctdb_syslog_state *state;
+       struct tevent_fd *fde;
 
        state = talloc(ctdb, struct ctdb_syslog_state);
        CTDB_NO_MEMORY(ctdb, state);
@@ -95,7 +96,7 @@ int start_syslog_daemon(struct ctdb_context *ctdb)
                return -1;
        }
        
-       ctdb->syslogd_pid = fork();
+       ctdb->syslogd_pid = ctdb_fork(ctdb);
        if (ctdb->syslogd_pid == (pid_t)-1) {
                printf("Failed to create syslog child process\n");
                close(state->fd[0]);
@@ -115,15 +116,18 @@ int start_syslog_daemon(struct ctdb_context *ctdb)
                return 0;
        }
 
+       debug_extra = talloc_asprintf(NULL, "syslogd:");
        talloc_free(ctdb->ev);
        ctdb->ev = event_context_init(NULL);
+       tevent_loop_allow_nesting(ctdb->ev);
 
        syslog(LOG_ERR, "Starting SYSLOG daemon with pid:%d", (int)getpid());
 
        close(state->fd[0]);
        set_close_on_exec(state->fd[1]);
-       event_add_fd(ctdb->ev, state, state->fd[1], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
+       fde = event_add_fd(ctdb->ev, state, state->fd[1], EVENT_FD_READ,
                     ctdb_syslog_terminate_handler, state);
+       tevent_fd_set_auto_close(fde);
 
        state->syslog_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (state->syslog_fd == -1) {
@@ -147,8 +151,9 @@ int start_syslog_daemon(struct ctdb_context *ctdb)
        }
 
 
-       event_add_fd(ctdb->ev, state, state->syslog_fd, EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
+       fde = event_add_fd(ctdb->ev, state, state->syslog_fd, EVENT_FD_READ,
                     ctdb_syslog_handler, state);
+       tevent_fd_set_auto_close(fde);
 
        event_loop_wait(ctdb->ev);
 
@@ -213,15 +218,16 @@ static void ctdb_syslog_log(const char *format, va_list ap)
                break;          
        }
 
-       len = offsetof(struct syslog_message, message) + strlen(s) + 1;
+       len = offsetof(struct syslog_message, message) + strlen(debug_extra) + strlen(s) + 1;
        msg = malloc(len);
        if (msg == NULL) {
                free(s);
                return;
        }
        msg->level = level;
-       msg->len   = strlen(s);
-       strcpy(msg->message, s);
+       msg->len   = strlen(debug_extra) + strlen(s);
+       strcpy(msg->message, debug_extra);
+       strcat(msg->message, s);
 
        if (syslogd_is_started == 0) {
                syslog(msg->level, "%s", msg->message);
@@ -275,8 +281,9 @@ static void ctdb_logfile_log(const char *format, va_list ap)
 
        strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
 
-       ret = asprintf(&s2, "%s.%06u [%5u]: %s",
-                tbuf, (unsigned)t.tv_usec, (unsigned)getpid(), s);
+       ret = asprintf(&s2, "%s.%06u [%s%5u]: %s",
+                      tbuf, (unsigned)t.tv_usec,
+                      debug_extra, (unsigned)getpid(), s);
        free(s);
        if (ret == -1) {
                const char *errstr = "asprintf failed\n";
@@ -434,6 +441,7 @@ struct ctdb_log_state *ctdb_fork_with_logging(TALLOC_CTX *mem_ctx,
 {
        int p[2];
        struct ctdb_log_state *log;
+       struct tevent_fd *fde;
 
        log = talloc_zero(mem_ctx, struct ctdb_log_state);
        CTDB_NO_MEMORY_NULL(ctdb, log);
@@ -446,7 +454,7 @@ struct ctdb_log_state *ctdb_fork_with_logging(TALLOC_CTX *mem_ctx,
                goto free_log;
        }
 
-       *pid = fork();
+       *pid = ctdb_fork(ctdb);
 
        /* Child? */
        if (*pid == 0) {
@@ -470,9 +478,10 @@ struct ctdb_log_state *ctdb_fork_with_logging(TALLOC_CTX *mem_ctx,
        log->pfd = p[0];
        set_close_on_exec(log->pfd);
        talloc_set_destructor(log, log_context_destructor);
-       event_add_fd(ctdb->ev, log, log->pfd,
-                    EVENT_FD_READ | EVENT_FD_AUTOCLOSE,
-                    ctdb_log_handler, log);
+       fde = event_add_fd(ctdb->ev, log, log->pfd,
+                          EVENT_FD_READ, ctdb_log_handler, log);
+       tevent_fd_set_auto_close(fde);
+
        return log;
 
 free_log:
@@ -487,6 +496,7 @@ int ctdb_set_child_logging(struct ctdb_context *ctdb)
 {
        int p[2];
        int old_stdout, old_stderr;
+       struct tevent_fd *fde;
 
        if (ctdb->log->fd == STDOUT_FILENO) {
                /* not needed for stdout logging */
@@ -525,9 +535,10 @@ int ctdb_set_child_logging(struct ctdb_context *ctdb)
        set_close_on_exec(STDERR_FILENO);
        set_close_on_exec(p[0]);
 
-       event_add_fd(ctdb->ev, ctdb->log, p[0],
-                    EVENT_FD_READ | EVENT_FD_AUTOCLOSE,
-                    ctdb_log_handler, ctdb->log);
+       fde = event_add_fd(ctdb->ev, ctdb->log, p[0],
+                          EVENT_FD_READ, ctdb_log_handler, ctdb->log);
+       tevent_fd_set_auto_close(fde);
+
        ctdb->log->pfd = p[0];
 
        DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d for logging\n", p[0]));
@@ -536,6 +547,46 @@ int ctdb_set_child_logging(struct ctdb_context *ctdb)
 }
 
 
+/*
+ * set up a log handler to catch logging from TEVENT
+ */
+static void ctdb_tevent_logging(void *private_data,
+                               enum tevent_debug_level level,
+                               const char *fmt,
+                               va_list ap)
+{
+       enum debug_level lvl = DEBUG_EMERG;
+
+       switch (level) {
+       case TEVENT_DEBUG_FATAL:
+               lvl = DEBUG_EMERG;
+               break;
+       case TEVENT_DEBUG_ERROR:
+               lvl = DEBUG_ERR;
+               break;
+       case TEVENT_DEBUG_WARNING:
+               lvl = DEBUG_WARNING;
+               break;
+       case TEVENT_DEBUG_TRACE:
+               lvl = DEBUG_DEBUG;
+               break;
+       }
+
+       if (lvl <= LogLevel) {
+               this_log_level = lvl;
+               do_debug_v(fmt, ap);
+       }
+}
+
+int ctdb_init_tevent_logging(struct ctdb_context *ctdb)
+{
+       int ret;
+
+       ret = tevent_set_debug(ctdb->ev,
+                       ctdb_tevent_logging,
+                       ctdb);
+       return ret;
+}