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>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 #include "bacon-message-connection.h"
42 #define UNIX_PATH_MAX 108
45 struct BaconMessageConnection {
46 /* A server accepts connections */
49 /* The socket path itself */
52 /* File descriptor of the socket */
54 /* Channel to watch */
56 /* Event id returned by g_io_add_watch() */
59 /* Connections accepted by this connection */
60 GSList *accepted_connections;
63 void (*func) (const char *message, gpointer user_data);
68 test_is_socket (const char *path)
72 if (stat (path, &s) == -1)
75 if (S_ISSOCK (s.st_mode))
82 is_owned_by_user_and_socket (const char *path)
86 if (stat (path, &s) == -1)
89 if (s.st_uid != geteuid ())
92 if ((s.st_mode & S_IFSOCK) != S_IFSOCK)
98 static gboolean server_cb (GIOChannel *source,
99 GIOCondition condition, gpointer data);
102 setup_connection (BaconMessageConnection *conn)
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);
112 fdflags |= FD_CLOEXEC;
113 fcntl (conn->fd, F_SETFD, fdflags);
116 conn->chan = g_io_channel_unix_new (conn->fd);
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);
127 accept_new_connection (BaconMessageConnection *server_conn)
129 BaconMessageConnection *conn;
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);
148 server_cb (GIOChannel *source, GIOCondition condition, gpointer data)
150 BaconMessageConnection *conn = (BaconMessageConnection *)data;
151 char *message, *subs, buf;
156 if (conn->is_server && conn->fd == g_io_channel_unix_get_fd (source)) {
157 accept_new_connection (conn);
160 message = g_malloc (1);
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);
171 g_io_channel_shutdown (conn->chan, FALSE, NULL);
172 g_io_channel_unref (conn->chan);
181 message[offset] = '\0';
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)
202 find_file_with_pattern (const char *dir, const char *pattern)
205 char *found_filename;
206 const char *filename;
209 filedir = g_dir_open (dir, 0, NULL);
213 pat = g_pattern_spec_new (pattern);
216 g_dir_close (filedir);
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);
232 if (found_filename != NULL)
236 g_pattern_spec_free (pat);
237 g_dir_close (filedir);
239 return found_filename;
243 socket_filename (const char *prefix)
245 char *pattern, *newfile, *path, *filename;
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);
258 path = g_build_filename (tmpdir, filename, NULL);
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)
280 listen (conn->fd, 5);
282 return setup_connection (conn);
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)
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);
322 conn->is_server = TRUE;
326 if (try_client (conn) == FALSE)
332 bacon_message_connection_free (conn);
336 conn->is_server = TRUE;
340 conn->is_server = FALSE;
345 bacon_message_connection_free (BaconMessageConnection *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);
362 g_source_remove (conn->conn_id);
366 g_io_channel_shutdown (conn->chan, FALSE, NULL);
367 g_io_channel_unref (conn->chan);
370 if (conn->is_server != FALSE) {
373 if (conn->fd != -1) {
382 bacon_message_connection_set_callback (BaconMessageConnection *conn,
383 BaconMessageReceivedFunc func,
386 g_return_if_fail (conn != NULL);
389 conn->data = user_data;
393 bacon_message_connection_send (BaconMessageConnection *conn,
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),
401 g_io_channel_write_chars (conn->chan, "\n", 1, NULL, NULL);
402 g_io_channel_flush (conn->chan, NULL);
406 bacon_message_connection_get_is_server (BaconMessageConnection *conn)
408 g_return_val_if_fail (conn != NULL, FALSE);
410 return conn->is_server;