50.samba run the smbcontrol in the background. no need to block waiting for it.
[sahlberg/ctdb.git] / server / ctdb_logging.c
index 4680c32b076d0fc098323eaa206d5c99c027f705..27b990ef6768b09967b4e8139f0bbf19f9d1f5d8 100644 (file)
@@ -96,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]);
@@ -116,8 +116,10 @@ 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());
 
@@ -216,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);
@@ -278,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";
@@ -450,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) {
@@ -543,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;
+}