This patch adds support for generating and removing a PID file on
start-up and shutdown to make integration with daemon monitoring tools
such as monit, easier.

---
 src/main.c |   38 ++++++++++++++++++++++++++++++++++----
 1 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/src/main.c b/src/main.c
index 18b6bc3..6d85cf9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -101,6 +101,7 @@ static void disconnect_callback(DBusConnection *conn, void 
*user_data)
 
 static gchar *option_debug = NULL;
 static gchar *option_device = NULL;
+static gchar *option_pidfile = NULL;
 static gchar *option_plugin = NULL;
 static gchar *option_nodevice = NULL;
 static gchar *option_noplugin = NULL;
@@ -141,6 +142,8 @@ static GOptionEntry options[] = {
        { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
                                G_OPTION_ARG_NONE, &option_dnsproxy,
                                "Don't enable DNS Proxy" },
+       { "pidfile", 0, 0, G_OPTION_ARG_STRING, &option_pidfile,
+                               "Path to PID file", "PIDFILE" },
        { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
                                "(obsolete)" },
        { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
@@ -168,6 +171,31 @@ connman_bool_t connman_setting_get_bool(const char *key)
        return FALSE;
 }
 
+static void connman_daemon_init(const char *pidfile)
+{
+       FILE *file;
+
+       if (daemon(0, 0)) {
+               perror("Can't start daemon");
+               exit(EXIT_FAILURE);
+       }
+
+       if (pidfile != NULL) {
+               file = fopen(pidfile, "w");
+               if (file != NULL) {
+                       fprintf(file, "%u\n", getpid());
+                       fclose(file);
+               }
+       }
+}
+
+static void connman_daemon_cleanup(const char *pidfile)
+{
+       if (pidfile != NULL) {
+               unlink(pidfile);
+       }
+}
+
 int main(int argc, char *argv[])
 {
        GOptionContext *context;
@@ -207,10 +235,7 @@ int main(int argc, char *argv[])
        }
 
        if (option_detach == TRUE) {
-               if (daemon(0, 0)) {
-                       perror("Can't start daemon");
-                       exit(1);
-               }
+               connman_daemon_init(option_pidfile);
        }
 
        if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
@@ -339,5 +364,10 @@ int main(int argc, char *argv[])
        if (config)
                g_key_file_free(config);
 
+       connman_daemon_cleanup(option_pidfile);
+
+       if (option_pidfile != NULL)
+               g_free(option_pidfile);
+
        return 0;
 }
-- 
1.7.4.2

_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman

Reply via email to