annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
1 /*
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
2 * fifo.c -- Read commands from a named pipe
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
3 *
1599
dcd5d4c75199 Update/Add headers
Mikael Berthe <mikael@lilotux.net>
parents: 1598
diff changeset
4 * Copyright (C) 2008,2009 Mikael Berthe <mikael@lilotux.net>
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
5 * Copyrigth (C) 2009 Myhailo Danylenko <isbear@ukrpost.net>
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
6 *
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or (at
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
10 * your option) any later version.
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
11 *
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
15 * General Public License for more details.
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
16 *
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
20 * USA
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
21 */
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
22
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1572
diff changeset
23 #include <stdlib.h>
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
24 #include <glib.h>
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
25 #include <sys/types.h>
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
26 #include <sys/stat.h>
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
27 #include <fcntl.h>
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
28 #include <unistd.h>
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
29 #include <errno.h>
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
30 #include <fcntl.h>
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
31
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
32 #include "commands.h"
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
33 #include "logprint.h"
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
34 #include "utils.h"
1427
a8eb9aba2ed2 Add option 'fifo_hide_commands'
Mikael Berthe <mikael@lilotux.net>
parents: 1426
diff changeset
35 #include "settings.h"
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
36 #include "main.h"
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
37
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
38 static char *fifo_name = NULL;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
39 static GIOChannel *fifo_channel = NULL;
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
40
1572
8c0237c8c186 Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
Mikael Berthe <mikael@lilotux.net>
parents: 1526
diff changeset
41 static const char *FIFO_ENV_NAME = "MCABBER_FIFO";
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
42
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
43 static gboolean attach_fifo(const char *name);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
44
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
45 static guint fifo_callback(GIOChannel *channel,
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
46 GIOCondition condition,
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
47 gpointer data)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
48 {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
49 if (condition & (G_IO_IN|G_IO_PRI)) {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
50 GIOStatus chstat;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
51 gchar *buf;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
52 gsize endpos;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
53
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
54 chstat = g_io_channel_read_line(channel, &buf, NULL, &endpos, NULL);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
55 if (chstat == G_IO_STATUS_ERROR || chstat == G_IO_STATUS_EOF) {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
56 if (!attach_fifo(fifo_name))
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
57 scr_LogPrint(LPRINT_LOGNORM,
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
58 "Reopening fifo failed! Fifo will not work from now!");
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
59 return FALSE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
60 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
61 if (buf) {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
62 guint logflag;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
63 guint fifo_ignore = settings_opt_get_int("fifo_ignore");
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
64
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
65 if (endpos)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
66 buf[endpos] = '\0';
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
67
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
68 if (settings_opt_get_int("fifo_hide_commands"))
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
69 logflag = LPRINT_LOG;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
70 else
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
71 logflag = LPRINT_LOGNORM;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
72 scr_LogPrint(logflag, "%s FIFO command: %s",
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
73 (fifo_ignore ? "Ignoring" : "Executing"), buf);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
74 if (!fifo_ignore) {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
75 if (process_command(buf, TRUE) == 255)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
76 mcabber_set_terminate_ui();
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
77 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
78
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
79 g_free(buf);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
80 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
81 } else if (condition & (G_IO_ERR|G_IO_NVAL|G_IO_HUP)) {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
82 if (!attach_fifo(fifo_name))
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
83 scr_LogPrint(LPRINT_LOGNORM,
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
84 "Reopening fifo failed! Fifo will not work from now!");
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
85 return FALSE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
86 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
87 return TRUE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
88 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
89
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
90 static void fifo_destroy_callback(gpointer data)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
91 {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
92 GIOChannel *channel = (GIOChannel *)data;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
93 g_io_channel_unref(channel);
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
94 channel = NULL;
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
95 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
96
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
97 static gboolean check_fifo(const char *name)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
98 {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
99 struct stat finfo;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
100 if (stat(name, &finfo) == -1) {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
101 /* some unknown error */
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
102 if (errno != ENOENT)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
103 return FALSE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
104 /* fifo not yet exists */
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
105 if (mkfifo(name, S_IRUSR|S_IWUSR) != -1)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
106 return check_fifo(name);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
107 else
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
108 return FALSE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
109 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
110
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
111 /* file exists */
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
112 if (S_ISFIFO(finfo.st_mode))
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
113 return TRUE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
114 else
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
115 return FALSE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
116 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
117
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
118 static gboolean attach_fifo(const char *name)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
119 {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
120 GSource *source;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
121 int fd = open (name, O_RDONLY|O_NONBLOCK);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
122 if (fd == -1)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
123 return FALSE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
124
1912
ee8657ff9aa8 Fix two fd leaks
franky
parents: 1811
diff changeset
125 if (fifo_channel)
ee8657ff9aa8 Fix two fd leaks
franky
parents: 1811
diff changeset
126 g_io_channel_unref(fifo_channel);
ee8657ff9aa8 Fix two fd leaks
franky
parents: 1811
diff changeset
127
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
128 fifo_channel = g_io_channel_unix_new(fd);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
129
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
130 g_io_channel_set_flags(fifo_channel, G_IO_FLAG_NONBLOCK, NULL);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
131 g_io_channel_set_encoding(fifo_channel, NULL, NULL);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
132 g_io_channel_set_close_on_unref(fifo_channel, TRUE);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
133
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
134 source = g_io_create_watch(fifo_channel,
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
135 G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
136 g_source_set_callback(source, (GSourceFunc)fifo_callback,
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
137 (gpointer)fifo_channel,
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
138 (GDestroyNotify)fifo_destroy_callback);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
139 g_source_attach(source, main_context);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
140
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
141 return TRUE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
142 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
143
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
144 void fifo_deinit(void)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
145 {
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
146 unsetenv(FIFO_ENV_NAME);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
147
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
148 if (fifo_channel)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
149 g_source_remove_by_user_data(fifo_channel);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
150 /* channel itself should be destroyed by destruction callback */
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
151 /* destroy open fifo */
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
152 if (fifo_name) {
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
153 /* well, that may create fifo, and then unlink,
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
154 * but at least we will not destroy non-fifo data */
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
155 if (check_fifo(fifo_name))
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
156 unlink(fifo_name);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
157 g_free(fifo_name);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
158 fifo_name = NULL;
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
159 }
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
160 }
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
161
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
162 // fifo_init_internal(path)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
163 // If path is NULL, reopen existing fifo, else open anew.
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
164 static int fifo_init_internal(const char *fifo_path)
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
165 {
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
166 if (fifo_path) {
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
167 fifo_deinit();
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
168 fifo_name = expand_filename(fifo_path);
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
169
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
170 if (!check_fifo(fifo_name)) {
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
171 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Cannot create the FIFO. "
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
172 "%s already exists and is not a pipe", fifo_name);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
173 g_free(fifo_name);
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
174 fifo_name = NULL;
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
175 return -1;
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
176 }
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
177 } else if (fifo_name)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
178 g_source_remove_by_user_data(fifo_channel);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
179 else
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
180 return -1;
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
181
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
182 if (!attach_fifo(fifo_name)) {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
183 scr_LogPrint(LPRINT_LOGNORM, "Error: Cannot open fifo");
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
184 return -1;
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
185 }
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
186
1572
8c0237c8c186 Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
Mikael Berthe <mikael@lilotux.net>
parents: 1526
diff changeset
187 setenv(FIFO_ENV_NAME, fifo_name, 1);
8c0237c8c186 Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
Mikael Berthe <mikael@lilotux.net>
parents: 1526
diff changeset
188
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
189 scr_LogPrint(LPRINT_LOGNORM, "FIFO initialized (%s)", fifo_path);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
190 return 1;
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
191 }
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
192
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
193 static gchar *fifo_guard(const gchar *key, const gchar *new_value)
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
194 {
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
195 if (new_value)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
196 fifo_init_internal(new_value);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
197 else
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
198 fifo_deinit();
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
199 return g_strdup(new_value);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
200 }
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
201
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
202 // Returns 1 in case of success, -1 on error
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
203 int fifo_init(void)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
204 {
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
205 const char *path = settings_opt_get("fifo_name");
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
206 static gboolean guard_installed = FALSE;
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
207 if (!guard_installed)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
208 if (!(guard_installed = settings_set_guard("fifo_name", fifo_guard)))
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
209 scr_LogPrint(LPRINT_DEBUG, "fifo: BUG: Cannot install option guard!");
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
210 if (path)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
211 return fifo_init_internal(path);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
212 return 1;
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
213 }
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
214
1811
e6d355e50d7a Update Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 1668
diff changeset
215 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */