Updated Spanish translation and uploaded figures
[empathy-mirror.git] / src / bacon-message-connection.c
blobc9fda4aeb77dafdecdaa7963a27d2cfcdf8a5845
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The Totem project hereby grant permission for non-gpl compatible GStreamer
19 * plugins to be used and distributed together with GStreamer and Totem. This
20 * permission are above and beyond the permissions granted by the GPL license
21 * Totem is covered by.
23 * Monday 7th February 2005: Christian Schaller: Add excemption clause.
24 * See license_change file for details.
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 #include <errno.h>
39 #include "bacon-message-connection.h"
41 #ifndef UNIX_PATH_MAX
42 #define UNIX_PATH_MAX 108
43 #endif
45 struct BaconMessageConnection {
46 /* A server accepts connections */
47 gboolean is_server;
49 /* The socket path itself */
50 char *path;
52 /* File descriptor of the socket */
53 int fd;
54 /* Channel to watch */
55 GIOChannel *chan;
56 /* Event id returned by g_io_add_watch() */
57 int conn_id;
59 /* Connections accepted by this connection */
60 GSList *accepted_connections;
62 /* callback */
63 void (*func) (const char *message, gpointer user_data);
64 gpointer data;
67 static gboolean
68 test_is_socket (const char *path)
70 struct stat s;
72 if (stat (path, &s) == -1)
73 return FALSE;
75 if (S_ISSOCK (s.st_mode))
76 return TRUE;
78 return FALSE;
81 static gboolean
82 is_owned_by_user_and_socket (const char *path)
84 struct stat s;
86 if (stat (path, &s) == -1)
87 return FALSE;
89 if (s.st_uid != geteuid ())
90 return FALSE;
92 if ((s.st_mode & S_IFSOCK) != S_IFSOCK)
93 return FALSE;
95 return TRUE;
98 static gboolean server_cb (GIOChannel *source,
99 GIOCondition condition, gpointer data);
101 static gboolean
102 setup_connection (BaconMessageConnection *conn)
104 int fdflags;
106 g_return_val_if_fail (conn->chan == NULL, FALSE);
108 /* Add CLOEXEC flag on the fd to make sure the socket get closed
109 * if exec is called. */
110 fdflags = fcntl (conn->fd, F_GETFD, 0);
111 if (fdflags >= 0) {
112 fdflags |= FD_CLOEXEC;
113 fcntl (conn->fd, F_SETFD, fdflags);
116 conn->chan = g_io_channel_unix_new (conn->fd);
117 if (!conn->chan) {
118 return FALSE;
120 g_io_channel_set_line_term (conn->chan, "\n", 1);
121 conn->conn_id = g_io_add_watch (conn->chan, G_IO_IN, server_cb, conn);
123 return TRUE;
126 static void
127 accept_new_connection (BaconMessageConnection *server_conn)
129 BaconMessageConnection *conn;
130 int alen;
132 g_return_if_fail (server_conn->is_server);
134 conn = g_new0 (BaconMessageConnection, 1);
135 conn->is_server = FALSE;
136 conn->func = server_conn->func;
137 conn->data = server_conn->data;
139 conn->fd = accept (server_conn->fd, NULL, (guint *)&alen);
141 server_conn->accepted_connections =
142 g_slist_prepend (server_conn->accepted_connections, conn);
144 setup_connection (conn);
147 static gboolean
148 server_cb (GIOChannel *source, GIOCondition condition, gpointer data)
150 BaconMessageConnection *conn = (BaconMessageConnection *)data;
151 char *message, *subs, buf;
152 int cd, rc, offset;
153 gboolean finished;
155 offset = 0;
156 if (conn->is_server && conn->fd == g_io_channel_unix_get_fd (source)) {
157 accept_new_connection (conn);
158 return TRUE;
160 message = g_malloc (1);
161 cd = conn->fd;
162 rc = read (cd, &buf, 1);
163 while (rc > 0 && buf != '\n')
165 message = g_realloc (message, rc + offset + 1);
166 message[offset] = buf;
167 offset = offset + rc;
168 rc = read (cd, &buf, 1);
170 if (rc <= 0) {
171 g_io_channel_shutdown (conn->chan, FALSE, NULL);
172 g_io_channel_unref (conn->chan);
173 conn->chan = NULL;
174 close (conn->fd);
175 conn->fd = -1;
176 g_free (message);
177 conn->conn_id = 0;
179 return FALSE;
181 message[offset] = '\0';
183 subs = message;
184 finished = FALSE;
186 while (finished == FALSE && *subs != '\0')
188 if (conn->func != NULL)
189 (*conn->func) (subs, conn->data);
191 subs += strlen (subs) + 1;
192 if (subs - message >= offset)
193 finished = TRUE;
196 g_free (message);
198 return TRUE;
201 static char *
202 find_file_with_pattern (const char *dir, const char *pattern)
204 GDir *filedir;
205 char *found_filename;
206 const char *filename;
207 GPatternSpec *pat;
209 filedir = g_dir_open (dir, 0, NULL);
210 if (filedir == NULL)
211 return NULL;
213 pat = g_pattern_spec_new (pattern);
214 if (pat == NULL)
216 g_dir_close (filedir);
217 return NULL;
220 found_filename = NULL;
222 while ((filename = g_dir_read_name (filedir)))
224 if (g_pattern_match_string (pat, filename))
226 char *tmp = g_build_filename (dir, filename, NULL);
227 if (is_owned_by_user_and_socket (tmp))
228 found_filename = g_strdup (filename);
229 g_free (tmp);
232 if (found_filename != NULL)
233 break;
236 g_pattern_spec_free (pat);
237 g_dir_close (filedir);
239 return found_filename;
242 static char *
243 socket_filename (const char *prefix)
245 char *pattern, *newfile, *path, *filename;
246 const char *tmpdir;
248 pattern = g_strdup_printf ("%s.%s.*", prefix, g_get_user_name ());
249 tmpdir = g_get_tmp_dir ();
250 filename = find_file_with_pattern (tmpdir, pattern);
251 if (filename == NULL)
253 newfile = g_strdup_printf ("%s.%s.%u", prefix,
254 g_get_user_name (), g_random_int ());
255 path = g_build_filename (tmpdir, newfile, NULL);
256 g_free (newfile);
257 } else {
258 path = g_build_filename (tmpdir, filename, NULL);
259 g_free (filename);
262 g_free (pattern);
263 return path;
266 static gboolean
267 try_server (BaconMessageConnection *conn)
269 struct sockaddr_un uaddr;
271 uaddr.sun_family = AF_UNIX;
272 strncpy (uaddr.sun_path, conn->path,
273 MIN (strlen(conn->path)+1, UNIX_PATH_MAX));
274 conn->fd = socket (PF_UNIX, SOCK_STREAM, 0);
275 if (bind (conn->fd, (struct sockaddr *) &uaddr, sizeof (uaddr)) == -1)
277 conn->fd = -1;
278 return FALSE;
280 listen (conn->fd, 5);
282 return setup_connection (conn);
285 static gboolean
286 try_client (BaconMessageConnection *conn)
288 struct sockaddr_un uaddr;
290 uaddr.sun_family = AF_UNIX;
291 strncpy (uaddr.sun_path, conn->path,
292 MIN(strlen(conn->path)+1, UNIX_PATH_MAX));
293 conn->fd = socket (PF_UNIX, SOCK_STREAM, 0);
294 if (connect (conn->fd, (struct sockaddr *) &uaddr,
295 sizeof (uaddr)) == -1)
297 conn->fd = -1;
298 return FALSE;
301 return setup_connection (conn);
304 BaconMessageConnection *
305 bacon_message_connection_new (const char *prefix)
307 BaconMessageConnection *conn;
309 g_return_val_if_fail (prefix != NULL, NULL);
311 conn = g_new0 (BaconMessageConnection, 1);
312 conn->path = socket_filename (prefix);
314 if (test_is_socket (conn->path) == FALSE)
316 if (!try_server (conn))
318 bacon_message_connection_free (conn);
319 return NULL;
322 conn->is_server = TRUE;
323 return conn;
326 if (try_client (conn) == FALSE)
328 unlink (conn->path);
329 try_server (conn);
330 if (conn->fd == -1)
332 bacon_message_connection_free (conn);
333 return NULL;
336 conn->is_server = TRUE;
337 return conn;
340 conn->is_server = FALSE;
341 return conn;
344 void
345 bacon_message_connection_free (BaconMessageConnection *conn)
347 GSList *child_conn;
349 g_return_if_fail (conn != NULL);
350 /* Only servers can accept other connections */
351 g_return_if_fail (conn->is_server != FALSE ||
352 conn->accepted_connections == NULL);
354 child_conn = conn->accepted_connections;
355 while (child_conn != NULL) {
356 bacon_message_connection_free (child_conn->data);
357 child_conn = g_slist_next (child_conn);
359 g_slist_free (conn->accepted_connections);
361 if (conn->conn_id) {
362 g_source_remove (conn->conn_id);
363 conn->conn_id = 0;
365 if (conn->chan) {
366 g_io_channel_shutdown (conn->chan, FALSE, NULL);
367 g_io_channel_unref (conn->chan);
370 if (conn->is_server != FALSE) {
371 unlink (conn->path);
373 if (conn->fd != -1) {
374 close (conn->fd);
377 g_free (conn->path);
378 g_free (conn);
381 void
382 bacon_message_connection_set_callback (BaconMessageConnection *conn,
383 BaconMessageReceivedFunc func,
384 gpointer user_data)
386 g_return_if_fail (conn != NULL);
388 conn->func = func;
389 conn->data = user_data;
392 void
393 bacon_message_connection_send (BaconMessageConnection *conn,
394 const char *message)
396 g_return_if_fail (conn != NULL);
397 g_return_if_fail (message != NULL);
399 g_io_channel_write_chars (conn->chan, message, strlen (message),
400 NULL, NULL);
401 g_io_channel_write_chars (conn->chan, "\n", 1, NULL, NULL);
402 g_io_channel_flush (conn->chan, NULL);
405 gboolean
406 bacon_message_connection_get_is_server (BaconMessageConnection *conn)
408 g_return_val_if_fail (conn != NULL, FALSE);
410 return conn->is_server;