From 7d8234d9e6f162ee642cdbec911f46c29b012c3d Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Mon, 8 Aug 2011 01:22:00 -0400 Subject: [PATCH] Fix handling of sessiond respawn after a SIGKILL Checking if the sessiond responds to connect() lets lttng / ltt-sessiond check correctly if the socket files are stale files or actively used. Signed-off-by: Mathieu Desnoyers --- liblttngctl/liblttngctl.c | 11 ++++++++++- liblttsessiondcomm/liblttsessiondcomm.c | 2 ++ ltt-sessiond/Makefile.am | 4 +++- ltt-sessiond/main.c | 23 ++++++++++++----------- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/liblttngctl/liblttngctl.c b/liblttngctl/liblttngctl.c index 4faf8414f..2464bce3b 100644 --- a/liblttngctl/liblttngctl.c +++ b/liblttngctl/liblttngctl.c @@ -608,13 +608,22 @@ int lttng_session_daemon_alive(void) return ret; } - /* If socket exist, we consider the daemon started */ + /* If socket exist, we check if the daemon listens to connect. */ ret = access(sessiond_sock_path, F_OK); if (ret < 0) { /* Not alive */ return 0; } + ret = lttcomm_connect_unix_sock(sessiond_sock_path); + if (ret < 0) { + /* Not alive */ + return 0; + } + ret = lttcomm_close_unix_sock(ret); + if (ret < 0) + perror("lttcomm_close_unix_sock"); + /* Is alive */ return 1; } diff --git a/liblttsessiondcomm/liblttsessiondcomm.c b/liblttsessiondcomm/liblttsessiondcomm.c index 9313a34c5..e7e420433 100644 --- a/liblttsessiondcomm/liblttsessiondcomm.c +++ b/liblttsessiondcomm/liblttsessiondcomm.c @@ -186,6 +186,8 @@ int lttcomm_create_unix_sock(const char *pathname) sun.sun_family = AF_UNIX; strncpy(sun.sun_path, pathname, strlen(pathname)); + /* Unlink the old file if present */ + (void) unlink(pathname); ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); if (ret < 0) { perror("bind"); diff --git a/ltt-sessiond/Makefile.am b/ltt-sessiond/Makefile.am index 048084095..268617350 100644 --- a/ltt-sessiond/Makefile.am +++ b/ltt-sessiond/Makefile.am @@ -11,7 +11,9 @@ ltt_sessiond_SOURCES = utils.c trace.c session.c traceable-app.c ust-ctl.c \ utils.h trace.h session.h traceable-app.h ust-ctl.h \ context.h kernel-ctl.h ltt-sessiond.h +# link on liblttngctl for check if sessiond is already alive. ltt_sessiond_LDADD = \ $(top_builddir)/liblttsessiondcomm/liblttsessiondcomm.la \ $(top_builddir)/libkernelctl/libkernelctl.la \ - $(top_builddir)/libustctl/libustctl.la + $(top_builddir)/libustctl/libustctl.la \ + $(top_builddir)/liblttngctl/liblttngctl.la diff --git a/ltt-sessiond/main.c b/ltt-sessiond/main.c index 1139df8b9..1e2a0831d 100644 --- a/ltt-sessiond/main.c +++ b/ltt-sessiond/main.c @@ -2418,18 +2418,21 @@ end: } /* - * Check if the global socket is available. If yes, error is returned. + * Check if the global socket is available, and if a daemon is answering + * at the other side. If yes, error is returned. */ static int check_existing_daemon(void) { int ret; - ret = access(client_unix_sock_path, F_OK); - if (ret == 0) { - ret = access(apps_unix_sock_path, F_OK); - } - - return ret; + if (access(client_unix_sock_path, F_OK) < 0 && + access(apps_unix_sock_path, F_OK) < 0) + return 0; + /* Is there anybody out there ? */ + if (lttng_session_daemon_alive()) + return -EEXIST; + else + return 0; } /* @@ -2714,11 +2717,9 @@ int main(int argc, char **argv) DBG("Application socket path %s", apps_unix_sock_path); /* - * See if daemon already exist. If any of the two socket needed by the - * daemon are present, this test fails. However, if the daemon is killed - * with a SIGKILL, those unix socket must be unlinked by hand. + * See if daemon already exist. */ - if ((ret = check_existing_daemon()) == 0) { + if ((ret = check_existing_daemon()) < 0) { ERR("Already running daemon.\n"); /* * We do not goto exit because we must not cleanup() -- 2.34.1