[gvfs/wip/oholy/debug: 1/10] daemon: Allow toggling debug output with SIGUSR2



commit d100fa98b82ec6fd4e125df20addac4f1f7b0742
Author: Ross Lagerwall <rosslagerwall gmail com>
Date:   Wed Nov 5 22:49:23 2014 +0000

    daemon: Allow toggling debug output with SIGUSR2
    
    With dbus hooked up to systemd, it can be useful to toggle debug output
    on/off for a running daemon.  Respond to SIGUSR2 by toggling debug
    output.
    
    E.g. to toggle and then watch debug output on a running sftp mount:
    $ pkill -USR2 gvfsd-sftp
    $ journalctl -f
    
    https://bugzilla.gnome.org/show_bug.cgi?id=740660

 common/gvfsutils.c   |   39 +++++++++++++++++++++++++++++++++++++++
 common/gvfsutils.h   |    4 ++++
 daemon/daemon-main.c |   10 ++++++----
 3 files changed, 49 insertions(+), 4 deletions(-)
---
diff --git a/common/gvfsutils.c b/common/gvfsutils.c
index 47c3117..1c5c2e3 100644
--- a/common/gvfsutils.c
+++ b/common/gvfsutils.c
@@ -22,6 +22,7 @@
 #include <string.h>
 #include <glib.h>
 #include <glib/gstdio.h>
+#include <signal.h>
 #include "gvfsutils.h"
 
 #ifdef G_OS_UNIX
@@ -30,6 +31,9 @@
 #include <unistd.h>
 #endif
 
+/* Indicates whether debug output is enabled. */
+static gboolean debugging = FALSE;
+
 /**
  * gvfs_randomize_string:
  * @str: the string to randomize
@@ -86,3 +90,38 @@ out:
   return FALSE;
 #endif
 }
+
+gboolean
+gvfs_get_debug (void)
+{
+  return debugging;
+}
+
+void
+gvfs_set_debug (gboolean debugging_)
+{
+  debugging = debugging_;
+}
+
+static void
+toggle_debugging (int signum)
+{
+  debugging = !debugging;
+}
+
+/**
+ * gvfs_setup_debugging_handler:
+ *
+ * Sets up a handler for SIGUSR2 that toggles the debugging flag when the
+ * signal is received.
+ **/
+void
+gvfs_setup_debug_handler (void)
+{
+  struct sigaction sa;
+
+  sigemptyset (&sa.sa_mask);
+  sa.sa_handler = toggle_debugging;
+  sa.sa_flags = 0;
+  sigaction (SIGUSR2, &sa, NULL);
+}
diff --git a/common/gvfsutils.h b/common/gvfsutils.h
index edac0b0..2c2146c 100644
--- a/common/gvfsutils.h
+++ b/common/gvfsutils.h
@@ -26,6 +26,10 @@ void         gvfs_randomize_string                  (char             *str,
                                                      int               len);
 gboolean     gvfs_have_session_bus                  (void);
 
+gboolean     gvfs_get_debug                         (void);
+void         gvfs_set_debug                         (gboolean          debugging);
+void         gvfs_setup_debug_handler               (void);
+
 G_END_DECLS
 
 #endif /* __G_VFS_UTILS_H__ */
diff --git a/daemon/daemon-main.c b/daemon/daemon-main.c
index 600921c..a81313d 100644
--- a/daemon/daemon-main.c
+++ b/daemon/daemon-main.c
@@ -33,11 +33,11 @@
 #include <gvfsdaemon.h>
 #include <gvfsbackend.h>
 #include <gvfsdbus.h>
+#include <gvfsutils.h>
 
 static char *spawner_id = NULL;
 static char *spawner_path = NULL;
 
-static gboolean print_debug = FALSE;
 static gboolean already_acquired = FALSE;
 static int process_result = 0;
 
@@ -50,7 +50,7 @@ log_debug (const gchar   *log_domain,
           const gchar   *message,
           gpointer           unused_data)
 {
-  if (print_debug)
+  if (gvfs_get_debug ())
     g_print ("%s", message);
 }
 
@@ -68,6 +68,8 @@ daemon_init (void)
   
   g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, log_debug, NULL);
 
+  gvfs_setup_debug_handler ();
+
 #ifdef SIGPIPE
   /* Ignore SIGPIPE to avoid killing daemons on cancelled transfer *
    * See https://bugzilla.gnome.org/show_bug.cgi?id=649041         *
@@ -217,13 +219,13 @@ daemon_parse_args (int argc, char *argv[], const char *default_type)
 
   if (argc > 1 && strcmp (argv[1], "--debug") == 0)
     {
-      print_debug = TRUE;
+      gvfs_set_debug (TRUE);
       argc--;
       argv++;
     }
   else if (g_getenv ("GVFS_DEBUG"))
     {
-      print_debug = TRUE;
+      gvfs_set_debug (TRUE);
     }
   
   mount_spec = NULL;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]