annotate mcabber/mcabber/fifo_internal.c @ 2223:965e0282c128

Backed out changeset fa516ef22145 Turns out that in this hash the value pointer is the same as the key pointer, so there's no need to free both the key and the value.
author Mikael Berthe <mikael@lilotux.net>
date Fri, 06 Nov 2015 22:31:40 +0100
parents 038c4d601011
children f5402d705f67
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 /*
2079
8da280d34b48 Rename FIFO module file name
Mikael Berthe <mikael@lilotux.net>
parents: 2019
diff changeset
2 * fifo_internal.c -- Read commands from a named pipe
1426
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>
2019
33483d3324cf s/Copyrigth/Copyright/ and so on
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1940
diff changeset
5 * Copyright (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) {
2164
038c4d601011 Simplify handling of command '/quit'
franky
parents: 2079
diff changeset
75 process_command(buf, TRUE);
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
76 }
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 g_free(buf);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
79 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
80 } 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
81 if (!attach_fifo(fifo_name))
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
82 scr_LogPrint(LPRINT_LOGNORM,
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
83 "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
84 return FALSE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
85 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
86 return TRUE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
87 }
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 static void fifo_destroy_callback(gpointer data)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
90 {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
91 GIOChannel *channel = (GIOChannel *)data;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
92 g_io_channel_unref(channel);
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
93 channel = NULL;
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
94 }
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 static gboolean check_fifo(const char *name)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
97 {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
98 struct stat finfo;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
99 if (stat(name, &finfo) == -1) {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
100 /* some unknown error */
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
101 if (errno != ENOENT)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
102 return FALSE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
103 /* fifo not yet exists */
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
104 if (mkfifo(name, S_IRUSR|S_IWUSR) != -1)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
105 return check_fifo(name);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
106 else
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
107 return FALSE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
108 }
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 /* file exists */
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
111 if (S_ISFIFO(finfo.st_mode))
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
112 return TRUE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
113 else
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
114 return FALSE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
115 }
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 static gboolean attach_fifo(const char *name)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
118 {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
119 GSource *source;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
120 int fd = open (name, O_RDONLY|O_NONBLOCK);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
121 if (fd == -1)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
122 return FALSE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
123
1912
ee8657ff9aa8 Fix two fd leaks
franky
parents: 1811
diff changeset
124 if (fifo_channel)
ee8657ff9aa8 Fix two fd leaks
franky
parents: 1811
diff changeset
125 g_io_channel_unref(fifo_channel);
ee8657ff9aa8 Fix two fd leaks
franky
parents: 1811
diff changeset
126
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
127 fifo_channel = g_io_channel_unix_new(fd);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
128
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
129 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
130 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
131 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
132
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
133 source = g_io_create_watch(fifo_channel,
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
134 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
135 g_source_set_callback(source, (GSourceFunc)fifo_callback,
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
136 (gpointer)fifo_channel,
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
137 (GDestroyNotify)fifo_destroy_callback);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
138 g_source_attach(source, main_context);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
139
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
140 return TRUE;
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
141 }
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
142
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
143 void fifo_deinit(void)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
144 {
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
145 unsetenv(FIFO_ENV_NAME);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
146
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
147 if (fifo_channel)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
148 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
149 /* 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
150 /* destroy open fifo */
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
151 if (fifo_name) {
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
152 /* 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
153 * 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
154 if (check_fifo(fifo_name))
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
155 unlink(fifo_name);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
156 g_free(fifo_name);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
157 fifo_name = NULL;
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
158 }
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 // fifo_init_internal(path)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
162 // 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
163 static int fifo_init_internal(const char *fifo_path)
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
164 {
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
165 if (fifo_path) {
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
166 fifo_deinit();
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
167 fifo_name = expand_filename(fifo_path);
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
168
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
169 if (!check_fifo(fifo_name)) {
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
170 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
171 "%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
172 g_free(fifo_name);
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
173 fifo_name = NULL;
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
174 return -1;
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
175 }
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
176 } else if (fifo_name)
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
177 g_source_remove_by_user_data(fifo_channel);
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
178 else
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
179 return -1;
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
180
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
181 if (!attach_fifo(fifo_name)) {
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
182 scr_LogPrint(LPRINT_LOGNORM, "Error: Cannot open fifo");
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
183 return -1;
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
184 }
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
185
1572
8c0237c8c186 Export the FIFO pipe name as an environment variable (Lukas Kuklinek)
Mikael Berthe <mikael@lilotux.net>
parents: 1526
diff changeset
186 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
187
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
188 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
189 return 1;
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
190 }
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
191
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
192 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
193 {
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
194 if (new_value)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
195 fifo_init_internal(new_value);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
196 else
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
197 fifo_deinit();
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
198 return g_strdup(new_value);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
199 }
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
200
1940
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
201 // 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
202 int fifo_init(void)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
203 {
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
204 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
205 static gboolean guard_installed = FALSE;
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
206 if (!guard_installed)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
207 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
208 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
209 if (path)
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
210 return fifo_init_internal(path);
7eadf86039e6 Use guard for the fifo system (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1912
diff changeset
211 return 1;
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
212 }
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
213
1811
e6d355e50d7a Update Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 1668
diff changeset
214 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */