logging: give a unique logging name to each forked child.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 19 Jul 2010 09:59:09 +0000 (19:29 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 20 Jul 2010 05:29:59 +0000 (14:59 +0930)
This means we can distinguish which child is logging, esp. via syslog where we have no pid.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
15 files changed:
client/ctdb_client.c
common/ctdb_logging.c
include/ctdb.h
lib/util/debug.c
lib/util/debug.h
server/ctdb_freeze.c
server/ctdb_lockwait.c
server/ctdb_logging.c
server/ctdb_monitor.c
server/ctdb_persistent.c
server/ctdb_recover.c
server/ctdb_recoverd.c
server/ctdb_traverse.c
server/ctdb_vacuum.c
server/eventscript.c

index 1e0a80dc81efc7600b1de82124640334d5f84979..7caa5cbe69b891eabba8873539d8fece624656c0 100644 (file)
@@ -3654,9 +3654,15 @@ int ctdb_ctrl_recd_ping(struct ctdb_context *ctdb)
  * to the daemon as a client process, this function can be used to change
  * the ctdb context from daemon into client mode
  */
-int switch_from_server_to_client(struct ctdb_context *ctdb)
+int switch_from_server_to_client(struct ctdb_context *ctdb, const char *fmt, ...)
 {
        int ret;
+       va_list ap;
+
+       /* Add extra information so we can identify this in the logs */
+       va_start(ap, fmt);
+       debug_extra = talloc_append_string(NULL, talloc_vasprintf(NULL, fmt, ap), ":");
+       va_end(ap);
 
        /* shutdown the transport */
        if (ctdb->methods) {
index 64507f4a20faa80299570678e20e422a96bac182..1b011b65c9ba8af6b1ca862992b354ac426872a5 100644 (file)
@@ -151,7 +151,7 @@ int32_t ctdb_control_get_log(struct ctdb_context *ctdb, TDB_DATA addr)
        }
 
        if (child == 0) {
-               if (switch_from_server_to_client(ctdb) != 0) {
+               if (switch_from_server_to_client(ctdb, "log-collector") != 0) {
                        DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch log collector child into client mode.\n"));
                        _exit(1);
                }
index c380c3d33a3fb2bde867eb521514f0e002fd40f5..cc83495acdc283877bfdbbef85d01cc921c084cb 100644 (file)
@@ -663,7 +663,7 @@ int ctdb_transaction_commit(struct ctdb_transaction_handle *h);
 
 int ctdb_ctrl_recd_ping(struct ctdb_context *ctdb);
 
-int switch_from_server_to_client(struct ctdb_context *ctdb);
+int switch_from_server_to_client(struct ctdb_context *ctdb, const char *fmt, ...);
 
 #define MONITOR_SCRIPT_OK      0
 #define MONITOR_SCRIPT_TIMEOUT 1
index d4d3bd643e54835c64e38b87a2bc717658184828..a0c3fe94b040fa8095d25748d63c0de4a8c156b7 100644 (file)
@@ -42,13 +42,15 @@ static void _do_debug_v(const char *format, va_list ap)
 
        strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
 
-       fprintf(stderr, "%s.%06u [%5u]: %s", tbuf, (unsigned)t.tv_usec, (unsigned)getpid(), s);
+       fprintf(stderr, "%s.%06u [%s%5u]: %s", tbuf, (unsigned)t.tv_usec,
+               debug_extra, (unsigned)getpid(), s);
        fflush(stderr);
        free(s);
 }
 
 /* default logging function */
 void (*do_debug_v)(const char *, va_list ap) = _do_debug_v;
+const char *debug_extra = "";
 
 void do_debug(const char *format, ...)
 {
index d91f43b4b42d0b26bd8a93842746499f3d307fd7..b81a09749a853acb5f5cf28d4d9c02f6e3c4acbd 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 void (*do_debug_v)(const char *, va_list ap);
+const char *debug_extra;
 void (*do_debug_add_v)(const char *, va_list ap);
 void log_ringbuffer(const char *format, ...);
 void do_debug(const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
index e641ef3ae68c5e756250416441630e8b153b1b6d..bf4c83e88b191f7d65ba388f2c2ff4a997768fd4 100644 (file)
@@ -202,6 +202,7 @@ static struct ctdb_freeze_handle *ctdb_freeze_lock(struct ctdb_context *ctdb, ui
                /* in the child */
                close(fd[0]);
 
+               debug_extra = talloc_asprintf(NULL, "freeze_lock-%u:", priority);
                ret = ctdb_lock_all_databases(ctdb, priority);
                if (ret != 0) {
                        _exit(0);
index afbb921439e3fd51bf822568cd5dc2bbf503a6a1..b7ae55c6c935c4dde56f1db658b30a64d619ac77 100644 (file)
@@ -136,6 +136,7 @@ struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
        if (result->child == 0) {
                char c = 0;
                close(result->fd[0]);
+               debug_extra = talloc_asprintf(NULL, "chainlock-%s:", ctdb_db->db_name);
                tdb_chainlock(ctdb_db->ltdb->tdb, key);
                write(result->fd[1], &c, 1);
                /* make sure we die when our parent dies */
index a7ca1a1df99aeba36437779e5872aa8622dcee2a..4e3595bd21ddbdc24f7684dc522c434d8981725f 100644 (file)
@@ -115,6 +115,7 @@ 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);
 
@@ -213,15 +214,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 +277,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";
index 729895c68a612117b0a19b8f69b32b3847b97fdf..cd1d5b94d5ea36501551fa4559a85448a0637305 100644 (file)
@@ -91,6 +91,7 @@ static void ctdb_run_notification_script(struct ctdb_context *ctdb, const char *
        if (child == 0) {
                int ret;
 
+               debug_extra = talloc_asprintf(NULL, "notification-%s:", event);
                ret = ctdb_run_notification_script_child(ctdb, event);
                if (ret != 0) {
                        DEBUG(DEBUG_ERR,(__location__ " Notification script failed\n"));
index d38aa8d9e775539522ba610cfc8c4eadd1ca5863..4401bcda6b0bbf31b3b9c82580183c30d8e493d8 100644 (file)
@@ -543,6 +543,7 @@ struct childwrite_handle *ctdb_childwrite(struct ctdb_db_context *ctdb_db,
                char c = 0;
 
                close(result->fd[0]);
+               debug_extra = talloc_asprintf(NULL, "childwrite-%s:", ctdb_db->db_name);
                ret = ctdb_persistent_store(state);
                if (ret != 0) {
                        DEBUG(DEBUG_ERR, (__location__ " Failed to write persistent data\n"));
index 22e48986f41d4a1828b397dd1fce13edb582dfe8..f61b6e7b6f63b1b11ec292c0e6b29ab172ff6b74 100644 (file)
@@ -725,6 +725,7 @@ int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb,
                char cc = 0;
                close(state->fd[0]);
 
+               debug_extra = talloc_asprintf(NULL, "set_recmode:");
                /* we should not be able to get the lock on the reclock file, 
                  as it should  be held by the recovery master 
                */
index dfef71634f468bd84e315dbe55412020f2d5c99f..fbc5eecaf2ba11b9701268347f493362ce94060d 100644 (file)
@@ -2711,6 +2711,7 @@ static int check_recovery_lock(struct ctdb_context *ctdb)
                close(state->fd[0]);
                state->fd[0] = -1;
 
+               debug_extra = talloc_asprintf(NULL, "recovery-lock:");
                if (pread(ctdb->recovery_lock_fd, &cc, 1, 0) == -1) {
                        DEBUG(DEBUG_CRIT,("failed read from recovery_lock_fd - %s\n", strerror(errno)));
                        cc = RECLOCK_FAILED;
@@ -3527,7 +3528,7 @@ int ctdb_start_recoverd(struct ctdb_context *ctdb)
 
        srandom(getpid() ^ time(NULL));
 
-       if (switch_from_server_to_client(ctdb) != 0) {
+       if (switch_from_server_to_client(ctdb, "recoverd") != 0) {
                DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch recovery daemon into client mode. shutting down.\n"));
                exit(1);
        }
index 2c33387d690a71db83649baf42b5a3a37188f149..6bfa1d4e484b896c6721292587b8ec03039a2f9a 100644 (file)
@@ -171,6 +171,8 @@ static struct ctdb_traverse_local_handle *ctdb_traverse_local(struct ctdb_db_con
        if (h->child == 0) {
                /* start the traverse in the child */
                close(h->fd[0]);
+               debug_extra = talloc_asprintf(NULL, "traverse_local-%s:",
+                                             ctdb_db->db_name);
                tdb_traverse_read(ctdb_db->ltdb->tdb, ctdb_traverse_local_fn, h);
                _exit(0);
        }
index 580686e2ba3445ec3684ee4dd65b03e3b5f5d680..2f751b326d1eb5180b7de605334c9d45ba87076b 100644 (file)
@@ -841,7 +841,7 @@ ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
 
                DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
        
-               if (switch_from_server_to_client(ctdb) != 0) {
+               if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
                        DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
                        _exit(1);
                }
index 8a6d779a8802e944dfb33dca621da61bac636b8c..712733c63cab35703b77c9a4f23db0d0ee0750f8 100644 (file)
@@ -407,6 +407,9 @@ static int fork_child_for_script(struct ctdb_context *ctdb,
        if (state->child == 0) {
                int rt;
 
+               debug_extra = talloc_asprintf(NULL, "eventscript-%s-%s:",
+                                             current->name,
+                                             ctdb_eventscript_call_names[state->call]);
                close(state->fd[0]);
                set_close_on_exec(state->fd[1]);