comparison mcabber/mcabber/fifo.c @ 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 ee8657ff9aa8
children 33483d3324cf
comparison
equal deleted inserted replaced
1939:932d1b250a55 1940:7eadf86039e6
89 89
90 static void fifo_destroy_callback(gpointer data) 90 static void fifo_destroy_callback(gpointer data)
91 { 91 {
92 GIOChannel *channel = (GIOChannel *)data; 92 GIOChannel *channel = (GIOChannel *)data;
93 g_io_channel_unref(channel); 93 g_io_channel_unref(channel);
94 channel = NULL;
94 } 95 }
95 96
96 static gboolean check_fifo(const char *name) 97 static gboolean check_fifo(const char *name)
97 { 98 {
98 struct stat finfo; 99 struct stat finfo;
138 g_source_attach(source, main_context); 139 g_source_attach(source, main_context);
139 140
140 return TRUE; 141 return TRUE;
141 } 142 }
142 143
143 int fifo_init(const char *fifo_path) 144 void fifo_deinit(void)
145 {
146 unsetenv(FIFO_ENV_NAME);
147
148 if (fifo_channel)
149 g_source_remove_by_user_data(fifo_channel);
150 /* channel itself should be destroyed by destruction callback */
151 /* destroy open fifo */
152 if (fifo_name) {
153 /* well, that may create fifo, and then unlink,
154 * but at least we will not destroy non-fifo data */
155 if (check_fifo(fifo_name))
156 unlink(fifo_name);
157 g_free(fifo_name);
158 fifo_name = NULL;
159 }
160 }
161
162 // fifo_init_internal(path)
163 // If path is NULL, reopen existing fifo, else open anew.
164 static int fifo_init_internal(const char *fifo_path)
144 { 165 {
145 if (fifo_path) { 166 if (fifo_path) {
167 fifo_deinit();
146 fifo_name = expand_filename(fifo_path); 168 fifo_name = expand_filename(fifo_path);
147 169
148 if (!check_fifo(fifo_name)) { 170 if (!check_fifo(fifo_name)) {
149 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Cannot create the FIFO. " 171 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Cannot create the FIFO. "
150 "%s already exists and is not a pipe", fifo_name); 172 "%s already exists and is not a pipe", fifo_name);
151 g_free(fifo_name); 173 g_free(fifo_name);
174 fifo_name = NULL;
152 return -1; 175 return -1;
153 } 176 }
154 } else if (fifo_name) 177 } else if (fifo_name)
155 g_source_remove_by_user_data(fifo_channel); 178 g_source_remove_by_user_data(fifo_channel);
156 else 179 else
165 188
166 scr_LogPrint(LPRINT_LOGNORM, "FIFO initialized (%s)", fifo_path); 189 scr_LogPrint(LPRINT_LOGNORM, "FIFO initialized (%s)", fifo_path);
167 return 1; 190 return 1;
168 } 191 }
169 192
170 void fifo_deinit(void) 193 static gchar *fifo_guard(const gchar *key, const gchar *new_value)
171 { 194 {
172 unsetenv(FIFO_ENV_NAME); 195 if (new_value)
173 196 fifo_init_internal(new_value);
174 /* destroy open fifo */ 197 else
175 unlink(fifo_name); 198 fifo_deinit();
176 g_source_remove_by_user_data(fifo_channel); 199 return g_strdup(new_value);
177 /* channel itself should be destroyed by destruction callback */ 200 }
178 g_free(fifo_name); 201
202 // Returns 1 in case of success, -1 on error
203 int fifo_init(void)
204 {
205 const char *path = settings_opt_get("fifo_name");
206 static gboolean guard_installed = FALSE;
207 if (!guard_installed)
208 if (!(guard_installed = settings_set_guard("fifo_name", fifo_guard)))
209 scr_LogPrint(LPRINT_DEBUG, "fifo: BUG: Cannot install option guard!");
210 if (path)
211 return fifo_init_internal(path);
212 return 1;
179 } 213 }
180 214
181 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */ 215 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */