]> git.0d.be Git - empathy.git/blob - src/bacon-message-connection.c
Fix includes
[empathy.git] / src / bacon-message-connection.c
1 /*
2  * Copyright (C) 2003 Bastien Nocera <hadess@hadess.net>
3  *
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.
8  *
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.
13  *
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.
17  *
18  */
19
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>
30
31 #include "bacon-message-connection.h"
32
33 #ifndef UNIX_PATH_MAX
34 #define UNIX_PATH_MAX 108
35 #endif
36
37 struct BaconMessageConnection {
38         /* A server accepts connections */
39         gboolean is_server;
40
41         /* The socket path itself */
42         char *path;
43
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;
50
51         /* Connections accepted by this connection */
52         GSList *accepted_connections;
53
54         /* callback */
55         void (*func) (const char *message, gpointer user_data);
56         gpointer data;
57 };
58
59 static gboolean
60 test_is_socket (const char *path)
61 {
62         struct stat s;
63
64         if (stat (path, &s) == -1)
65                 return FALSE;
66
67         if (S_ISSOCK (s.st_mode))
68                 return TRUE;
69
70         return FALSE;
71 }
72
73 static gboolean
74 is_owned_by_user_and_socket (const char *path)
75 {
76         struct stat s;
77
78         if (stat (path, &s) == -1)
79                 return FALSE;
80
81         if (s.st_uid != geteuid ())
82                 return FALSE;
83
84         if ((s.st_mode & S_IFSOCK) != S_IFSOCK)
85                 return FALSE;
86         
87         return TRUE;
88 }
89
90 static gboolean server_cb (GIOChannel *source,
91                            GIOCondition condition, gpointer data);
92
93 static gboolean
94 setup_connection (BaconMessageConnection *conn)
95 {
96         g_return_val_if_fail (conn->chan == NULL, FALSE);
97
98         conn->chan = g_io_channel_unix_new (conn->fd);
99         if (!conn->chan) {
100                 return FALSE;
101         }
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);
104
105         return TRUE;
106 }
107
108 static void
109 accept_new_connection (BaconMessageConnection *server_conn)
110 {
111         BaconMessageConnection *conn;
112         int alen;
113
114         g_return_if_fail (server_conn->is_server);
115
116         conn = g_new0 (BaconMessageConnection, 1);
117         conn->is_server = FALSE;
118         conn->func = server_conn->func;
119         conn->data = server_conn->data;
120
121         conn->fd = accept (server_conn->fd, NULL, (guint *)&alen);
122
123         server_conn->accepted_connections =
124                 g_slist_prepend (server_conn->accepted_connections, conn);
125
126         setup_connection (conn);
127 }
128
129 static gboolean
130 server_cb (GIOChannel *source, GIOCondition condition, gpointer data)
131 {
132         BaconMessageConnection *conn = (BaconMessageConnection *)data;
133         char *message, *subs, buf;
134         int cd, rc, offset;
135         gboolean finished;
136
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;
141         }
142         message = g_malloc (1);
143         cd = conn->fd;
144         rc = read (cd, &buf, 1);
145         while (rc > 0 && buf != '\n')
146         {
147                 message = g_realloc (message, rc + offset + 1);
148                 message[offset] = buf;
149                 offset = offset + rc;
150                 rc = read (cd, &buf, 1);
151         }
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;
160
161                 return FALSE;
162         }
163         message[offset] = '\0';
164
165         subs = message;
166         finished = FALSE;
167
168         while (finished == FALSE && *subs != '\0')
169         {
170                 if (conn->func != NULL)
171                         (*conn->func) (subs, conn->data);
172
173                 subs += strlen (subs) + 1;
174                 if (subs - message >= offset)
175                         finished = TRUE;
176         }
177
178         g_free (message);
179
180         return TRUE;
181 }
182
183 static char *
184 find_file_with_pattern (const char *dir, const char *pattern)
185 {
186         GDir *filedir;
187         char *found_filename;
188         const char *filename;
189         GPatternSpec *pat;
190
191         filedir = g_dir_open (dir, 0, NULL);
192         if (filedir == NULL)
193                 return NULL;
194
195         pat = g_pattern_spec_new (pattern);
196         if (pat == NULL)
197         {
198                 g_dir_close (filedir);
199                 return NULL;
200         }
201
202         found_filename = NULL;
203
204         while ((filename = g_dir_read_name (filedir)))
205         {
206                 if (g_pattern_match_string (pat, filename))
207                 {
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);
212                 }
213
214                 if (found_filename != NULL)
215                         break;
216         }
217
218         g_pattern_spec_free (pat);
219         g_dir_close (filedir);
220
221         return found_filename;
222 }
223
224 static char *
225 socket_filename (const char *prefix)
226 {
227         char *pattern, *newfile, *path, *filename;
228         const char *tmpdir;
229
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)
234         {
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);
242         }
243
244         g_free (pattern);
245         return path;
246 }
247
248 static gboolean
249 try_server (BaconMessageConnection *conn)
250 {
251         struct sockaddr_un uaddr;
252
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)
258         {
259                 conn->fd = -1;
260                 return FALSE;
261         }
262         listen (conn->fd, 5);
263
264         if (!setup_connection (conn))
265                 return FALSE;
266         return TRUE;
267 }
268
269 static gboolean
270 try_client (BaconMessageConnection *conn)
271 {
272         struct sockaddr_un uaddr;
273
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)
280         {
281                 conn->fd = -1;
282                 return FALSE;
283         }
284
285         return setup_connection (conn);
286 }
287
288 BaconMessageConnection *
289 bacon_message_connection_new (const char *prefix)
290 {
291         BaconMessageConnection *conn;
292
293         g_return_val_if_fail (prefix != NULL, NULL);
294
295         conn = g_new0 (BaconMessageConnection, 1);
296         conn->path = socket_filename (prefix);
297
298         if (test_is_socket (conn->path) == FALSE)
299         {
300                 if (!try_server (conn))
301                 {
302                         bacon_message_connection_free (conn);
303                         return NULL;
304                 }
305
306                 conn->is_server = TRUE;
307                 return conn;
308         }
309
310         if (try_client (conn) == FALSE)
311         {
312                 unlink (conn->path);
313                 try_server (conn);
314                 if (conn->fd == -1)
315                 {
316                         bacon_message_connection_free (conn);
317                         return NULL;
318                 }
319
320                 conn->is_server = TRUE;
321                 return conn;
322         }
323
324         conn->is_server = FALSE;
325         return conn;
326 }
327
328 void
329 bacon_message_connection_free (BaconMessageConnection *conn)
330 {
331         GSList *child_conn;
332
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);
337
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);
342         }
343         g_slist_free (conn->accepted_connections);
344
345         if (conn->conn_id) {
346                 g_source_remove (conn->conn_id);
347                 conn->conn_id = 0;
348         }
349         if (conn->chan) {
350                 g_io_channel_shutdown (conn->chan, FALSE, NULL);
351                 g_io_channel_unref (conn->chan);
352         }
353
354         if (conn->is_server != FALSE) {
355                 unlink (conn->path);
356         }
357         if (conn->fd != -1) {
358                 close (conn->fd);
359         }
360
361         g_free (conn->path);
362         g_free (conn);
363 }
364
365 void
366 bacon_message_connection_set_callback (BaconMessageConnection *conn,
367                                        BaconMessageReceivedFunc func,
368                                        gpointer user_data)
369 {
370         g_return_if_fail (conn != NULL);
371
372         conn->func = func;
373         conn->data = user_data;
374 }
375
376 void
377 bacon_message_connection_send (BaconMessageConnection *conn,
378                                const char *message)
379 {
380         g_return_if_fail (conn != NULL);
381         g_return_if_fail (message != NULL);
382
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);
387 }
388
389 gboolean
390 bacon_message_connection_get_is_server (BaconMessageConnection *conn)
391 {
392         g_return_val_if_fail (conn != NULL, FALSE);
393
394         return conn->is_server;
395 }
396