Add --check-running option.
authorJelmer Vernooij <jelmer@samba.org>
Wed, 6 Feb 2008 16:22:17 +0000 (17:22 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Wed, 6 Feb 2008 16:22:17 +0000 (17:22 +0100)
NEWS
src/main.c
src/util.c

diff --git a/NEWS b/NEWS
index 69f03fd75bfd83a22f20890942928ef8c2d09106..f183ec30a10a8f616ce15e2181876e73ac27122b 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -25,6 +25,8 @@ Ctrlproxy 3.0.6 UNRELEASED
 
        * Avoid data overhead when responding to /NAMES out of cache.
 
+       * Add --check-running option.
+
   BUG FIXES
 
     * Don't "forget" listener password.
index c4387233eb7f2946c9aa63d28bc5cac6f28f8f05..1f747b3010a7a17d531cb298e345bae57e82351a 100644 (file)
@@ -156,9 +156,11 @@ int main(int argc, char **argv)
        gboolean init = FALSE;
        const char *inetd_client = NULL;
        char *pidfile;
+       gboolean check_running = FALSE;
        gboolean version = FALSE;
        GOptionContext *pc;
        GOptionEntry options[] = {
+               {"check-running", 0, 0, G_OPTION_ARG_NONE, &check_running, "Only check whether ctrlproxy is running and exit"},
                {"inetd-client", 'i', 0, G_OPTION_ARG_STRING, &inetd_client, "Communicate with client to NETWORK via stdio", "NETWORK" },
                {"debug-level", 'd', 'd', G_OPTION_ARG_INT, &current_log_level, ("Debug level [0-5]"), "LEVEL" },
                {"no-timestamp", 'n', 0, G_OPTION_ARG_NONE, &no_log_timestamp, "No timestamps in logs" },
@@ -221,8 +223,6 @@ int main(int argc, char **argv)
 
        init_log(logfile);
 
-       log_global(LOG_INFO, "CtrlProxy %s starting", VERSION);
-
        if (gethostname(my_hostname, NI_MAXHOST) != 0) {
                log_global(LOG_WARNING, "Can't figure out hostname of local host!");
                g_free(config_dir);
@@ -260,9 +260,6 @@ int main(int argc, char **argv)
        init_replication();
        help = help_load_file(HELPFILE);
 
-       /* Determine correct modules directory */
-       init_plugins(getenv("CTRLPROXY_MODULESDIR")?getenv("CTRLPROXY_MODULESDIR"):MODULESDIR);
-
        tmp = g_build_filename(config_dir, "config", NULL);
 
        if (!g_file_test(tmp, G_FILE_TEST_EXISTS)) {
@@ -295,10 +292,28 @@ int main(int argc, char **argv)
                return 1;
        }
 
+       if (check_running) {
+               pid_t pid;
+               pidfile = pid_file(my_global);
+               pid = read_pidfile(pidfile);
+               if (pid == -1) {
+                       printf("ctrlproxy is not running\n");
+                       return -1;
+               } else {
+                       printf("ctrlproxy pid is %d\n", pid);
+                       return 0;
+               }
+       }
+
+       log_global(LOG_INFO, "CtrlProxy %s (pid %d) starting", VERSION, getpid());
+
        pidfile = pid_file(my_global);
        write_pidfile(pidfile);
        g_free(pidfile);
 
+       /* Determine correct modules directory */
+       init_plugins(getenv("CTRLPROXY_MODULESDIR")?getenv("CTRLPROXY_MODULESDIR"):MODULESDIR);
+
        start_unix_socket(my_global);
        start_admin_socket(my_global);
        autoconnect_networks(my_global->networks);
index f740d58c71fe0ba500067b5801b882ec53bb072b..bfcb1f42ab83c9c8984ec36ae52cd044a3a75d0b 100644 (file)
@@ -286,7 +286,11 @@ pid_t read_pidfile(const char *path)
        if (!g_file_get_contents(path, &contents, NULL, &error)) 
                return -1;
        pid = atol(contents);
-       /* FIXME: Check if pid is still running */
+
+       /* Check if process is still running */
+       if (kill(pid,0) != 0 && errno == ESRCH)
+               pid = -1;
+
        g_free(contents);
        return pid;
 }