changeset 1940:7eadf86039e6

Use guard for the fifo system (Myhailo Danylenko) Merge patch from isbear's mcabber-experimental repository.
author Mikael Berthe <mikael@lilotux.net>
date Sun, 27 Feb 2011 17:24:11 +0100
parents 932d1b250a55
children 2256d0626730
files mcabber/mcabber/fifo.c mcabber/mcabber/fifo.h mcabber/mcabber/main.c
diffstat 3 files changed, 44 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/mcabber/fifo.c	Sun Feb 27 16:51:55 2011 +0100
+++ b/mcabber/mcabber/fifo.c	Sun Feb 27 17:24:11 2011 +0100
@@ -91,6 +91,7 @@
 {
   GIOChannel *channel = (GIOChannel *)data;
   g_io_channel_unref(channel);
+  channel = NULL;
 }
 
 static gboolean check_fifo(const char *name)
@@ -140,15 +141,37 @@
   return TRUE;
 }
 
-int fifo_init(const char *fifo_path)
+void fifo_deinit(void)
+{
+  unsetenv(FIFO_ENV_NAME);
+
+  if (fifo_channel)
+    g_source_remove_by_user_data(fifo_channel);
+  /* channel itself should be destroyed by destruction callback */
+  /* destroy open fifo */
+  if (fifo_name) {
+    /* well, that may create fifo, and then unlink,
+     * but at least we will not destroy non-fifo data */
+    if (check_fifo(fifo_name))
+      unlink(fifo_name);
+    g_free(fifo_name);
+    fifo_name = NULL;
+  }
+}
+
+//  fifo_init_internal(path)
+// If path is NULL, reopen existing fifo, else open anew.
+static int fifo_init_internal(const char *fifo_path)
 {
   if (fifo_path) {
+    fifo_deinit();
     fifo_name = expand_filename(fifo_path);
 
     if (!check_fifo(fifo_name)) {
       scr_LogPrint(LPRINT_LOGNORM, "WARNING: Cannot create the FIFO. "
                    "%s already exists and is not a pipe", fifo_name);
       g_free(fifo_name);
+      fifo_name = NULL;
       return -1;
     }
   } else if (fifo_name)
@@ -167,15 +190,26 @@
   return 1;
 }
 
-void fifo_deinit(void)
+static gchar *fifo_guard(const gchar *key, const gchar *new_value)
 {
-  unsetenv(FIFO_ENV_NAME);
+  if (new_value)
+    fifo_init_internal(new_value);
+  else
+    fifo_deinit();
+  return g_strdup(new_value);
+}
 
-  /* destroy open fifo */
-  unlink(fifo_name);
-  g_source_remove_by_user_data(fifo_channel);
-  /* channel itself should be destroyed by destruction callback */
-  g_free(fifo_name);
+// Returns 1 in case of success, -1 on error
+int fifo_init(void)
+{
+  const char *path = settings_opt_get("fifo_name");
+  static gboolean guard_installed = FALSE;
+  if (!guard_installed)
+    if (!(guard_installed = settings_set_guard("fifo_name", fifo_guard)))
+      scr_LogPrint(LPRINT_DEBUG, "fifo: BUG: Cannot install option guard!");
+  if (path)
+    return fifo_init_internal(path);
+  return 1;
 }
 
 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2:  For Vim users... */
--- a/mcabber/mcabber/fifo.h	Sun Feb 27 16:51:55 2011 +0100
+++ b/mcabber/mcabber/fifo.h	Sun Feb 27 17:24:11 2011 +0100
@@ -1,7 +1,7 @@
 #ifndef __MCABBER_FIFO_H__
 #define __MCABBER_FIFO_H__ 1
 
-int  fifo_init(const char *fifo_path);
+int  fifo_init(void);
 void fifo_deinit(void);
 
 #endif /* __MCABBER_FIFO_H__ */
--- a/mcabber/mcabber/main.c	Sun Feb 27 16:51:55 2011 +0100
+++ b/mcabber/mcabber/main.c	Sun Feb 27 17:24:11 2011 +0100
@@ -450,7 +450,7 @@
   chatstates_disabled = settings_opt_get_int("disable_chatstates");
 
   /* Initialize FIFO named pipe */
-  fifo_init(settings_opt_get("fifo_name"));
+  fifo_init();
 
   /* Load previous roster state */
   hlog_load_state();