Add pidfile creation for lttng-sessiond
authorDavid Goulet <dgoulet@efficios.com>
Fri, 11 Jan 2013 20:12:12 +0000 (15:12 -0500)
committerDavid Goulet <dgoulet@efficios.com>
Fri, 11 Jan 2013 21:10:33 +0000 (16:10 -0500)
This also adds a pidfile function to common/utils.c so it could be used
by the all binaries needing this feature.

Fixes #276

Signed-off-by: David Goulet <dgoulet@efficios.com>
src/bin/lttng-sessiond/main.c
src/common/defaults.h
src/common/utils.c
src/common/utils.h

index 237a2cf8d75a3f3ee8dc1a68c7c63dfccc1526b2..e5a22460fc3c6fe2342bf82d015d6da84a85ffd2 100644 (file)
@@ -72,6 +72,7 @@ const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
 
 const char *progname;
 const char *opt_tracing_group;
+static const char *opt_pidfile;
 static int opt_sig_parent;
 static int opt_verbose_consumer;
 static int opt_daemon;
@@ -404,6 +405,17 @@ static void cleanup(void)
        /* First thing first, stop all threads */
        utils_close_pipe(thread_quit_pipe);
 
+       /*
+        * If opt_pidfile is undefined, the default file will be wiped when
+        * removing the rundir.
+        */
+       if (opt_pidfile) {
+               ret = remove(opt_pidfile);
+               if (ret < 0) {
+                       PERROR("remove pidfile %s", opt_pidfile);
+               }
+       }
+
        DBG("Removing %s directory", rundir);
        ret = asprintf(&cmd, "rm -rf %s", rundir);
        if (ret < 0) {
@@ -3406,6 +3418,7 @@ static void usage(void)
        fprintf(stderr, "  -S, --sig-parent                   Send SIGCHLD to parent pid to notify readiness.\n");
        fprintf(stderr, "  -q, --quiet                        No output at all.\n");
        fprintf(stderr, "  -v, --verbose                      Verbose mode. Activate DBG() macro.\n");
+       fprintf(stderr, "  -p, --pidfile FILE                 Write a pid to FILE name overriding the default value.\n");
        fprintf(stderr, "      --verbose-consumer             Verbose mode for consumer. Activate DBG() macro.\n");
        fprintf(stderr, "      --no-kernel                    Disable kernel tracer\n");
 }
@@ -3439,12 +3452,13 @@ static int parse_args(int argc, char **argv)
                { "verbose", 0, 0, 'v' },
                { "verbose-consumer", 0, 0, 'Z' },
                { "no-kernel", 0, 0, 'N' },
+               { "pidfile", 1, 0, 'p' },
                { NULL, 0, 0, 0 }
        };
 
        while (1) {
                int option_index = 0;
-               c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
+               c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:",
                                long_options, &option_index);
                if (c == -1) {
                        break;
@@ -3521,6 +3535,9 @@ static int parse_args(int argc, char **argv)
                case 'T':
                        consumerd64_libdir = optarg;
                        break;
+               case 'p':
+                       opt_pidfile = optarg;
+                       break;
                default:
                        /* Unknown option or other error.
                         * Error is printed by getopt, just return */
@@ -3847,6 +3864,38 @@ static void set_ulimit(void)
        }
 }
 
+/*
+ * Write pidfile using the rundir and opt_pidfile.
+ */
+static void write_pidfile(void)
+{
+       int ret;
+       char pidfile_path[PATH_MAX];
+
+       assert(rundir);
+
+       if (opt_pidfile) {
+               strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
+       } else {
+               /* Build pidfile path from rundir and opt_pidfile. */
+               ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
+                               DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
+               if (ret < 0) {
+                       PERROR("snprintf pidfile path");
+                       goto error;
+               }
+       }
+
+       /*
+        * Create pid file in rundir. Return value is of no importance. The
+        * execution will continue even though we are not able to write the file.
+        */
+       (void) utils_create_pid_file(getpid(), pidfile_path);
+
+error:
+       return;
+}
+
 /*
  * main
  */
@@ -4144,6 +4193,8 @@ int main(int argc, char **argv)
                app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
        }
 
+       write_pidfile();
+
        /* Create thread to manage the client socket */
        ret = pthread_create(&health_thread, NULL,
                        thread_manage_health, (void *) NULL);
index efcd7924b1ab16f38752eecca3ddfe6dabb05a92..0dde5796470f975b3ede10544eb6318384422746 100644 (file)
@@ -72,6 +72,7 @@
 /* Default lttng run directory */
 #define DEFAULT_LTTNG_RUNDIR                    "/var/run/lttng"
 #define DEFAULT_LTTNG_HOME_RUNDIR               "%s/.lttng"
+#define DEFAULT_LTTNG_SESSIOND_PIDFILE          "lttng-sessiond.pid"
 
 /* Default unix socket path */
 #define DEFAULT_GLOBAL_CLIENT_UNIX_SOCK         DEFAULT_LTTNG_RUNDIR "/client-lttng-sessiond"
index 1a0e47ec4ed97b1601cbc3498126a760b5e02895..2f114934fd78c654a43a9b5563f6929bc7422ec6 100644 (file)
@@ -16,6 +16,7 @@
  */
 
 #define _GNU_SOURCE
+#include <assert.h>
 #include <ctype.h>
 #include <fcntl.h>
 #include <limits.h>
@@ -202,3 +203,32 @@ int utils_set_fd_cloexec(int fd)
 end:
        return ret;
 }
+
+/*
+ * Create pid file to the given path and filename.
+ */
+__attribute__((visibility("hidden")))
+int utils_create_pid_file(pid_t pid, const char *filepath)
+{
+       int ret;
+       FILE *fp;
+
+       assert(filepath);
+
+       fp = fopen(filepath, "w");
+       if (fp == NULL) {
+               PERROR("open pid file %s", filepath);
+               ret = -1;
+               goto error;
+       }
+
+       ret = fprintf(fp, "%d\n", pid);
+       if (ret < 0) {
+               PERROR("fprintf pid file");
+       }
+
+       fclose(fp);
+       DBG("Pid %d written in file %s", pid, filepath);
+error:
+       return ret;
+}
index c5723aa1d364b66e55fe7e5d606f52688a57c2f8..95e1b670732a7d0f0c70a5b0893ba3837b1de91a 100644 (file)
@@ -24,5 +24,6 @@ int utils_create_pipe_cloexec(int *dst);
 void utils_close_pipe(int *src);
 char *utils_strdupdelim(const char *begin, const char *end);
 int utils_set_fd_cloexec(int fd);
+int utils_create_pid_file(pid_t pid, const char *filepath);
 
 #endif /* _COMMON_UTILS_H */
This page took 0.029656 seconds and 4 git commands to generate.