[Xfce4-commits] <xfce4-appfinder:master> Daemonize the process if dbus service.

Nick Schermer noreply at xfce.org
Sat Jul 9 16:18:06 CEST 2011


Updating branch refs/heads/master
         to de066886a15375a5c27b446615af6cfa3bd49f71 (commit)
       from 67bdc2185bb143913226af21f62abae6c4025824 (commit)

commit de066886a15375a5c27b446615af6cfa3bd49f71
Author: Nick Schermer <nick at xfce.org>
Date:   Mon Jun 13 18:24:15 2011 +0200

    Daemonize the process if dbus service.

 configure.ac.in       |    3 +-
 src/appfinder-model.c |    3 +-
 src/main.c            |   53 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/configure.ac.in b/configure.ac.in
index 62fd416..4ea3179 100644
--- a/configure.ac.in
+++ b/configure.ac.in
@@ -60,7 +60,8 @@ dnl **********************************
 dnl *** Check for standard headers ***
 dnl **********************************
 AC_HEADER_STDC()
-AC_CHECK_HEADERS([stdlib.h string.h])
+AC_CHECK_HEADERS([stdlib.h string.h errno.h unistd.h])
+AC_CHECK_FUNCS([daemon setsid])
 
 dnl ******************************
 dnl *** Check for i18n support ***
diff --git a/src/appfinder-model.c b/src/appfinder-model.c
index 6d038f3..1613d32 100644
--- a/src/appfinder-model.c
+++ b/src/appfinder-model.c
@@ -934,7 +934,7 @@ xfce_appfinder_model_menu_changed (GarconMenu         *menu,
   g_return_if_fail (GARCON_IS_MENU (menu));
   g_return_if_fail (XFCE_IS_APPFINDER_MODEL (model));
 
-
+  /* TODO */
 }
 
 
@@ -1161,6 +1161,7 @@ xfce_appfinder_model_execute (XfceAppfinderModel  *model,
 }
 
 
+
 GdkPixbuf *
 xfce_appfinder_model_load_pixbuf (const gchar *icon_name,
                                   gint         size)
diff --git a/src/main.c b/src/main.c
index 0e5cd8f..ce590f4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -23,9 +23,16 @@
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
 
 #include <gtk/gtk.h>
 #include <libxfce4util/libxfce4util.h>
+#include <libxfce4ui/libxfce4ui.h>
 #include <garcon/garcon.h>
 #include <xfconf/xfconf.h>
 
@@ -51,6 +58,7 @@ static gboolean  opt_version = FALSE;
 static gboolean  opt_replace = FALSE;
 static gboolean  opt_quit = FALSE;
 static gchar    *opt_filename = NULL;
+static gboolean  opt_no_daemon = FALSE;
 static GSList   *windows = NULL;
 static gboolean  service_owner = FALSE;
 
@@ -62,6 +70,7 @@ static GOptionEntry option_entries[] =
   { "version", 'V', 0, G_OPTION_ARG_NONE, &opt_version, N_("Print version information and exit"), NULL },
   { "replace", 'r', 0, G_OPTION_ARG_NONE, &opt_replace, N_("Replace the existing service"), NULL },
   { "quit", 'q', 0, G_OPTION_ARG_NONE, &opt_quit, N_("Quit all instances"), NULL },
+  { "no-daemon", 0, 0, G_OPTION_ARG_NONE, &opt_no_daemon, N_("Do not fork to the background"), NULL },
   { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME, &opt_filename, NULL, NULL },
   { NULL }
 };
@@ -264,6 +273,32 @@ appfinder_dbus_unregister (DBusConnection *dbus_connection)
 
 
 
+static gint
+appfinder_daemonize (void)
+{
+#ifdef HAVE_DAEMON
+  return daemon (1, 1);
+#else
+  pid_t pid;
+
+  pid = fork ();
+  if (pid < 0)
+    return -1;
+
+  if (pid > 0)
+    _exit (EXIT_SUCCESS);
+
+#ifdef HAVE_SETSID
+  if (setsid () < 0)
+    return -1;
+#endif
+
+  return 0;
+#endif
+}
+
+
+
 gint
 main (gint argc, gchar **argv)
 {
@@ -355,6 +390,24 @@ main (gint argc, gchar **argv)
 
               /* successfully registered the service */
               service_owner = TRUE;
+
+              if (!opt_no_daemon)
+                {
+                  /* fork to the background */
+                  if (appfinder_daemonize () == -1)
+                    {
+                      xfce_message_dialog (NULL, _("Application Finder"),
+                                           GTK_STOCK_DIALOG_ERROR,
+                                           _("Unable to daemonize the process"),
+                                           g_strerror (errno),
+                                           GTK_STOCK_QUIT, GTK_RESPONSE_ACCEPT,
+                                           NULL);
+
+                      return EXIT_FAILURE;
+                    }
+
+                  APPFINDER_DEBUG ("daemonized the process");
+                }
             }
           else
             {



More information about the Xfce4-commits mailing list