Updated Spanish translation
[anjuta-git-plugin.git] / src / bacon-message-connection.c
blob6907a66859c113b9e84bbca2b74794580b4d115e
1 /*
2 * Copyright (C) 2003 Bastien Nocera <hadess@hadess.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <errno.h>
31 #include "bacon-message-connection.h"
33 #ifndef UNIX_PATH_MAX
34 #define UNIX_PATH_MAX 108
35 #endif
37 struct BaconMessageConnection {
38 /* A server accepts connections */
39 gboolean is_server;
41 /* The socket path itself */
42 char *path;
44 /* File descriptor of the socket */
45 int fd;
46 /* Channel to watch */
47 GIOChannel *chan;
48 /* Event id returned by g_io_add_watch() */
49 int conn_id;
51 /* Connections accepted by this connection */
52 GSList *accepted_connections;
54 /* callback */
55 void (*func) (const char *message, gpointer user_data);
56 gpointer data;
59 static gboolean
60 test_is_socket (const char *path)
62 struct stat s;
64 if (stat (path, &s) == -1)
65 return FALSE;
67 if (S_ISSOCK (s.st_mode))
68 return TRUE;
70 return FALSE;
73 static gboolean
74 is_owned_by_user_and_socket (const char *path)
76 struct stat s;
78 if (stat (path, &s) == -1)
79 return FALSE;
81 if (s.st_uid != geteuid ())
82 return FALSE;
84 if ((s.st_mode & S_IFSOCK) != S_IFSOCK)
85 return FALSE;
87 return TRUE;
90 static gboolean server_cb (GIOChannel *source,
91 GIOCondition condition, gpointer data);
93 static gboolean
94 setup_connection (BaconMessageConnection *conn)
96 g_return_val_if_fail (conn->chan == NULL, FALSE);
98 conn->chan = g_io_channel_unix_new (conn->fd);
99 if (!conn->chan) {
100 return FALSE;
102 g_io_channel_set_line_term (conn->chan, "\n", 1);
103 conn->conn_id = g_io_add_watch (conn->chan, G_IO_IN, server_cb, conn);
105 return TRUE;
108 static void
109 accept_new_connection (BaconMessageConnection *server_conn)
111 BaconMessageConnection *conn;
112 int alen;
114 g_return_if_fail (server_conn->is_server);
116 conn = g_new0 (BaconMessageConnection, 1);
117 conn->is_server = FALSE;
118 conn->func = server_conn->func;
119 conn->data = server_conn->data;
121 conn->fd = accept (server_conn->fd, NULL, (guint *)&alen);
123 server_conn->accepted_connections =
124 g_slist_prepend (server_conn->accepted_connections, conn);
126 setup_connection (conn);
129 static gboolean
130 server_cb (GIOChannel *source, GIOCondition condition, gpointer data)
132 BaconMessageConnection *conn = (BaconMessageConnection *)data;
133 char *message, *subs, buf;
134 int cd, rc, offset;
135 gboolean finished;
137 offset = 0;
138 if (conn->is_server && conn->fd == g_io_channel_unix_get_fd (source)) {
139 accept_new_connection (conn);
140 return TRUE;
142 message = g_malloc (1);
143 cd = conn->fd;
144 rc = read (cd, &buf, 1);
145 while (rc > 0 && buf != '\n')
147 message = g_realloc (message, rc + offset + 1);
148 message[offset] = buf;
149 offset = offset + rc;
150 rc = read (cd, &buf, 1);
152 if (rc <= 0) {
153 g_io_channel_shutdown (conn->chan, FALSE, NULL);
154 g_io_channel_unref (conn->chan);
155 conn->chan = NULL;
156 close (conn->fd);
157 conn->fd = -1;
158 g_free (message);
159 conn->conn_id = 0;
161 return FALSE;
163 message[offset] = '\0';
165 subs = message;
166 finished = FALSE;
168 while (finished == FALSE && *subs != '\0')
170 if (conn->func != NULL)
171 (*conn->func) (subs, conn->data);
173 subs += strlen (subs) + 1;
174 if (subs - message >= offset)
175 finished = TRUE;
178 g_free (message);
180 return TRUE;
183 static char *
184 find_file_with_pattern (const char *dir, const char *pattern)
186 GDir *filedir;
187 char *found_filename;
188 const char *filename;
189 GPatternSpec *pat;
191 filedir = g_dir_open (dir, 0, NULL);
192 if (filedir == NULL)
193 return NULL;
195 pat = g_pattern_spec_new (pattern);
196 if (pat == NULL)
198 g_dir_close (filedir);
199 return NULL;
202 found_filename = NULL;
204 while ((filename = g_dir_read_name (filedir)))
206 if (g_pattern_match_string (pat, filename))
208 char *tmp = g_build_filename (dir, filename, NULL);
209 if (is_owned_by_user_and_socket (tmp))
210 found_filename = g_strdup (filename);
211 g_free (tmp);
214 if (found_filename != NULL)
215 break;
218 g_pattern_spec_free (pat);
219 g_dir_close (filedir);
221 return found_filename;
224 static char *
225 socket_filename (const char *prefix)
227 char *pattern, *newfile, *path, *filename;
228 const char *tmpdir;
230 pattern = g_strdup_printf ("%s.%s.*", prefix, g_get_user_name ());
231 tmpdir = g_get_tmp_dir ();
232 filename = find_file_with_pattern (tmpdir, pattern);
233 if (filename == NULL)
235 newfile = g_strdup_printf ("%s.%s.%u", prefix,
236 g_get_user_name (), g_random_int ());
237 path = g_build_filename (tmpdir, newfile, NULL);
238 g_free (newfile);
239 } else {
240 path = g_build_filename (tmpdir, filename, NULL);
241 g_free (filename);
244 g_free (pattern);
245 return path;
248 static gboolean
249 try_server (BaconMessageConnection *conn)
251 struct sockaddr_un uaddr;
253 uaddr.sun_family = AF_UNIX;
254 strncpy (uaddr.sun_path, conn->path,
255 MIN (strlen(conn->path)+1, UNIX_PATH_MAX));
256 conn->fd = socket (PF_UNIX, SOCK_STREAM, 0);
257 if (bind (conn->fd, (struct sockaddr *) &uaddr, sizeof (uaddr)) == -1)
259 conn->fd = -1;
260 return FALSE;
262 listen (conn->fd, 5);
264 if (!setup_connection (conn))
265 return FALSE;
266 return TRUE;
269 static gboolean
270 try_client (BaconMessageConnection *conn)
272 struct sockaddr_un uaddr;
274 uaddr.sun_family = AF_UNIX;
275 strncpy (uaddr.sun_path, conn->path,
276 MIN(strlen(conn->path)+1, UNIX_PATH_MAX));
277 conn->fd = socket (PF_UNIX, SOCK_STREAM, 0);
278 if (connect (conn->fd, (struct sockaddr *) &uaddr,
279 sizeof (uaddr)) == -1)
281 conn->fd = -1;
282 return FALSE;
285 return setup_connection (conn);
288 BaconMessageConnection *
289 bacon_message_connection_new (const char *prefix)
291 BaconMessageConnection *conn;
293 g_return_val_if_fail (prefix != NULL, NULL);
295 conn = g_new0 (BaconMessageConnection, 1);
296 conn->path = socket_filename (prefix);
298 if (test_is_socket (conn->path) == FALSE)
300 if (!try_server (conn))
302 bacon_message_connection_free (conn);
303 return NULL;
306 conn->is_server = TRUE;
307 return conn;
310 if (try_client (conn) == FALSE)
312 unlink (conn->path);
313 try_server (conn);
314 if (conn->fd == -1)
316 bacon_message_connection_free (conn);
317 return NULL;
320 conn->is_server = TRUE;
321 return conn;
324 conn->is_server = FALSE;
325 return conn;
328 void
329 bacon_message_connection_free (BaconMessageConnection *conn)
331 GSList *child_conn;
333 g_return_if_fail (conn != NULL);
334 /* Only servers can accept other connections */
335 g_return_if_fail (conn->is_server != FALSE ||
336 conn->accepted_connections == NULL);
338 child_conn = conn->accepted_connections;
339 while (child_conn != NULL) {
340 bacon_message_connection_free (child_conn->data);
341 child_conn = g_slist_next (child_conn);
343 g_slist_free (conn->accepted_connections);
345 if (conn->conn_id) {
346 g_source_remove (conn->conn_id);
347 conn->conn_id = 0;
349 if (conn->chan) {
350 g_io_channel_shutdown (conn->chan, FALSE, NULL);
351 g_io_channel_unref (conn->chan);
354 if (conn->is_server != FALSE) {
355 unlink (conn->path);
357 if (conn->fd != -1) {
358 close (conn->fd);
361 g_free (conn->path);
362 g_free (conn);
365 void
366 bacon_message_connection_set_callback (BaconMessageConnection *conn,
367 BaconMessageReceivedFunc func,
368 gpointer user_data)
370 g_return_if_fail (conn != NULL);
372 conn->func = func;
373 conn->data = user_data;
376 void
377 bacon_message_connection_send (BaconMessageConnection *conn,
378 const char *message)
380 g_return_if_fail (conn != NULL);
381 g_return_if_fail (message != NULL);
383 g_io_channel_write_chars (conn->chan, message, strlen (message),
384 NULL, NULL);
385 g_io_channel_write_chars (conn->chan, "\n", 1, NULL, NULL);
386 g_io_channel_flush (conn->chan, NULL);
389 gboolean
390 bacon_message_connection_get_is_server (BaconMessageConnection *conn)
392 g_return_val_if_fail (conn != NULL, FALSE);
394 return conn->is_server;