~ubuntu-branches/ubuntu/saucy/totem/saucy-proposed

« back to all changes in this revision

Viewing changes to src/bacon-message-connection.c

  • Committer: Bazaar Package Importer
  • Author(s): Loic Minier, Sebastien Bacher, Loic Minier
  • Date: 2007-03-08 14:51:55 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070308145155-cnu1r0s1z4ffcxza
Tags: 2.16.5-3
[ Sebastien Bacher ]
* debian/patches/30_dlopen_noremove_dbus_glib.dpatch:
  - fix "crash because NPPVpluginKeepLibraryInMemory is broken in gecko",
    patch from Alexander Sack (GNOME bug #415389)

[ Loic Minier ]
* Urgency medium.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include <sys/types.h>
21
21
#include <sys/stat.h>
22
22
#include <fcntl.h>
 
23
#include <stdio.h>
23
24
#include <string.h>
24
25
#include <unistd.h>
25
26
#include <sys/types.h>
26
27
#include <sys/socket.h>
27
28
#include <sys/un.h>
 
29
#include <errno.h>
28
30
 
29
31
#include "bacon-message-connection.h"
30
32
 
33
35
#endif
34
36
 
35
37
struct BaconMessageConnection {
 
38
        /* A server accepts connections */
36
39
        gboolean is_server;
 
40
 
 
41
        /* The socket path itself */
 
42
        char *path;
 
43
 
 
44
        /* File descriptor of the socket */
37
45
        int fd;
38
 
        char *path;
 
46
        /* Channel to watch */
39
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
 
40
54
        /* callback */
41
55
        void (*func) (const char *message, gpointer user_data);
42
56
        gpointer data;
56
70
        return FALSE;
57
71
}
58
72
 
59
 
#define BUF_SIZE 1024
 
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
}
60
128
 
61
129
static gboolean
62
130
server_cb (GIOChannel *source, GIOCondition condition, gpointer data)
63
131
{
64
132
        BaconMessageConnection *conn = (BaconMessageConnection *)data;
65
 
        char *message, *subs, buf[BUF_SIZE];
66
 
        int cd, alen, rc, offset;
 
133
        char *message, *subs, buf;
 
134
        int cd, rc, offset;
67
135
        gboolean finished;
68
136
 
69
 
        message = NULL;
70
137
        offset = 0;
71
 
        cd = accept (g_io_channel_unix_get_fd (source), NULL, &alen);
72
 
 
73
 
        memset (buf, sizeof (buf), '\0');
74
 
        rc = read (cd, buf, BUF_SIZE);
75
 
//FIXME test for the actual errno
76
 
        while (rc != 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')
77
146
        {
78
 
                message = g_realloc (message, rc + offset);
79
 
                memcpy (message + offset, buf, MIN(rc, BUF_SIZE));
80
 
 
 
147
                message = g_realloc (message, rc + offset + 1);
 
148
                message[offset] = buf;
81
149
                offset = offset + rc;
82
 
                memset (buf, sizeof (buf), '\0');
83
 
                rc = read (cd, buf, BUF_SIZE);
84
 
        }
 
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';
85
164
 
86
165
        subs = message;
87
166
        finished = FALSE;
88
167
 
89
 
        while (subs != '\0' && finished == FALSE)
 
168
        while (finished == FALSE && *subs != '\0')
90
169
        {
91
 
                if (message != NULL && conn->func != NULL)
 
170
                if (conn->func != NULL)
92
171
                        (*conn->func) (subs, conn->data);
93
172
 
94
173
                subs += strlen (subs) + 1;
101
180
        return TRUE;
102
181
}
103
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
 
104
248
static gboolean
105
249
try_server (BaconMessageConnection *conn)
106
250
{
107
251
        struct sockaddr_un uaddr;
108
 
        GError *err = NULL;
109
252
 
110
253
        uaddr.sun_family = AF_UNIX;
111
254
        strncpy (uaddr.sun_path, conn->path,
116
259
                conn->fd = -1;
117
260
                return FALSE;
118
261
        }
119
 
 
120
262
        listen (conn->fd, 5);
121
 
        conn->chan = g_io_channel_unix_new (conn->fd);
122
 
        if (conn->chan == NULL || err != NULL)
123
 
        {
124
 
                conn->fd = -1;
 
263
 
 
264
        if (!setup_connection (conn))
125
265
                return FALSE;
126
 
        }
127
 
 
128
266
        return TRUE;
129
267
}
130
268
 
144
282
                return FALSE;
145
283
        }
146
284
 
147
 
        return TRUE;
 
285
        return setup_connection (conn);
148
286
}
149
287
 
150
288
BaconMessageConnection *
151
289
bacon_message_connection_new (const char *prefix)
152
290
{
153
291
        BaconMessageConnection *conn;
154
 
        char *filename, *path;
155
292
 
156
293
        g_return_val_if_fail (prefix != NULL, NULL);
157
294
 
158
 
        filename = g_strdup_printf (".%s.%s", prefix, g_get_user_name ());
159
 
        path = g_build_filename (g_get_home_dir (), filename, NULL);
160
 
        g_free (filename);
161
 
 
162
295
        conn = g_new0 (BaconMessageConnection, 1);
163
 
        conn->path = path;
 
296
        conn->path = socket_filename (prefix);
164
297
 
165
298
        if (test_is_socket (conn->path) == FALSE)
166
299
        {
167
 
                try_server (conn);
168
 
                if (conn->fd == -1)
 
300
                if (!try_server (conn))
169
301
                {
170
302
                        bacon_message_connection_free (conn);
171
303
                        return NULL;
177
309
 
178
310
        if (try_client (conn) == FALSE)
179
311
        {
180
 
                unlink (path);
 
312
                unlink (conn->path);
181
313
                try_server (conn);
182
314
                if (conn->fd == -1)
183
315
                {
196
328
void
197
329
bacon_message_connection_free (BaconMessageConnection *conn)
198
330
{
 
331
        GSList *child_conn;
 
332
 
199
333
        g_return_if_fail (conn != NULL);
200
 
        g_return_if_fail (conn->path != NULL);
201
 
 
202
 
        if (conn->is_server != FALSE)
203
 
        {
 
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) {
204
350
                g_io_channel_shutdown (conn->chan, FALSE, NULL);
205
351
                g_io_channel_unref (conn->chan);
 
352
        }
 
353
 
 
354
        if (conn->is_server != FALSE) {
206
355
                unlink (conn->path);
207
 
        } else {
 
356
        }
 
357
        if (conn->fd != -1) {
208
358
                close (conn->fd);
209
359
        }
210
360
 
218
368
                                       gpointer user_data)
219
369
{
220
370
        g_return_if_fail (conn != NULL);
221
 
        g_assert (conn->is_server != FALSE);
222
 
 
223
 
        g_io_add_watch (conn->chan, G_IO_IN, server_cb, conn);
224
371
 
225
372
        conn->func = func;
226
373
        conn->data = user_data;
231
378
                               const char *message)
232
379
{
233
380
        g_return_if_fail (conn != NULL);
234
 
        g_assert (conn->is_server == FALSE);
235
 
//FIXME test short writes
236
 
        write (conn->fd, message, strlen (message) + 1);
 
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);
237
387
}
238
388
 
239
389
gboolean
240
390
bacon_message_connection_get_is_server (BaconMessageConnection *conn)
241
391
{
242
392
        g_return_val_if_fail (conn != NULL, FALSE);
 
393
 
243
394
        return conn->is_server;
244
395
}
245
396