~ubuntu-branches/debian/squeeze/devicekit-disks/squeeze

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 David Zeuthen <david@fubar.dk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#include "devkit-disks-poller.h"
#include "devkit-disks-device.h"
#include "devkit-disks-device-private.h"

#ifdef __linux__
extern char **environ;
static char **argv_buffer = NULL;
static size_t argv_size = 0;
#endif

static void
set_proc_title_init (int argc, char *argv[])
{
#ifdef __linux__
        unsigned int i;
        char **new_environ, *endptr;

        /* This code is really really ugly. We make some memory layout
         * assumptions and reuse the environment array as memory to store
         * our process title in */

        for (i = 0; environ[i] != NULL; i++)
                ;

        endptr = i ? environ[i-1] + strlen (environ[i-1]) : argv[argc-1] + strlen (argv[argc-1]);

        argv_buffer = argv;
        argv_size = endptr - argv_buffer[0];

        /* Make a copy of environ */

        new_environ = malloc (sizeof(char*) * (i + 1));
        for (i = 0; environ[i] != NULL; i++)
                new_environ[i] = strdup (environ[i]);
        new_environ[i] = NULL;

        environ = new_environ;
#endif
}

/* this code borrowed from avahi-daemon's setproctitle.c (LGPL v2) */
static void
set_proc_title (const char *format, ...)
{
#ifdef __linux__
        size_t len;
        va_list ap;

        if (argv_buffer == NULL)
                goto out;

        va_start (ap, format);
        vsnprintf (argv_buffer[0], argv_size, format, ap);
        va_end (ap);

        len = strlen (argv_buffer[0]);

        memset (argv_buffer[0] + len, 0, argv_size - len);
        argv_buffer[1] = NULL;
out:
        ;
#endif
}


static gchar **poller_devices_to_poll = NULL;

static guint poller_timeout_id = 0;

void
devkit_disks_poller_poll_device (const gchar *device_file)
{
        gboolean is_cdrom;
        int fd;

        /* the device file is the canonical device file from udev */
        is_cdrom = (g_str_has_prefix (device_file, "/dev/sr") || g_str_has_prefix (device_file, "/dev/scd"));

        //g_debug ("polling '%s'", device_file);

        if (is_cdrom) {
                /* optical drives need special care
                 *
                 *  - use O_NONBLOCK to avoid closing the door
                 *  - use O_EXCL to avoid interferring with cd burning software / audio playback / etc
                 */
                fd = open (device_file, O_RDONLY | O_NONBLOCK | O_EXCL);
                if (fd != -1) {
                        close (fd);
                }

        } else {
                fd = open (device_file, O_RDONLY);
                if (fd != -1) {
                        close (fd);
                }
        }
}


static gboolean
poller_timeout_cb (gpointer user_data)
{
        guint n;

        for (n = 0; poller_devices_to_poll != NULL && poller_devices_to_poll[n] != NULL; n++) {
                const gchar *device_file = poller_devices_to_poll[n];

                devkit_disks_poller_poll_device (device_file);
        }

        /* don't remove the source */
        return TRUE;
}

static gboolean
poller_have_data (GIOChannel    *channel,
                  GIOCondition   condition,
                  gpointer       user_data)
{
        gchar *line;
        gsize line_length;
        GError *error;
        gint status;

        error = NULL;

        /* Exit if parent dies */
        if (condition == G_IO_HUP) {
                exit (1);
        }

 again:
        status = g_io_channel_read_line (channel,
                                         &line,
                                         &line_length,
                                         NULL,
                                         &error);
        if (error != NULL) {
                g_warning ("Error reading line from daemon: %s", error->message);
                g_error_free (error);
                goto out;
        }
        if (status == G_IO_STATUS_AGAIN) {
                goto again;
        }

        //g_debug ("polling process read '%s'", line);

        if (line[line_length - 1] == '\n')
                line[line_length - 1] = '\0';

        if (line[line_length - 2] == ' ')
                line[line_length - 2] = '\0';

        g_strfreev (poller_devices_to_poll);
        poller_devices_to_poll = g_strsplit (line, " ", 0);

        if (g_strv_length (poller_devices_to_poll) == 0) {
                if (poller_timeout_id > 0) {
                        g_source_remove (poller_timeout_id);
                        poller_timeout_id = 0;
                }

                set_proc_title ("devkit-disks-daemon: not polling any devices");
        } else {
                set_proc_title ("devkit-disks-daemon: polling %s", line);

                if (poller_timeout_id == 0) {
                        poller_timeout_id = g_timeout_add_seconds (2, poller_timeout_cb, NULL);
                }
        }

        g_free (line);

 out:
        /* keep the IOChannel around */
        return TRUE;
}

static void
poller_run (gint fd)
{
        GMainLoop *loop;
        GIOChannel *io_channel;

        loop = g_main_loop_new (NULL, FALSE);

        io_channel = g_io_channel_unix_new (fd);
        g_io_channel_set_flags (io_channel, G_IO_FLAG_NONBLOCK, NULL);
        g_io_add_watch (io_channel, G_IO_IN | G_IO_HUP, poller_have_data, NULL);

        g_main_loop_run (loop);
}

/* ---------------------------------------------------------------------------------------------------- */

static gint poller_daemon_write_end_fd;

gboolean
devkit_disks_poller_setup (int argc, char *argv[])
{
        gint pipefds[2];
        gboolean ret;

        ret = FALSE;

        if (pipe (pipefds) != 0) {
                g_warning ("Couldn't set up polling process, pipe() failed: %m");
                goto out;
        }

        switch (fork ()) {
        case 0:
                /* child */
                close (pipefds[1]); /* close write end */
                set_proc_title_init (argc, argv);
                poller_run (pipefds[0]);
                break;

        default:
                /* parent */
                close (pipefds[0]); /* close read end */
                poller_daemon_write_end_fd = pipefds[1];
                break;

        case -1:
                g_warning ("Couldn't set up polling process, fork() failed: %m");
                goto out;
                break;
        }

        ret = TRUE;

 out:
        return ret;
}

void
devkit_disks_poller_set_devices (GList *devices)
{
        GList *l;
        gchar **device_array;
        guint n;
        gchar *devices_to_poll;
        static gchar *devices_currently_polled = NULL;

        device_array = g_new0 (gchar *, g_list_length (devices) + 2);

        for (l = devices, n = 0; l != NULL; l = l->next) {
                DevkitDisksDevice *device = DEVKIT_DISKS_DEVICE (l->data);

                device_array[n++] = device->priv->device_file;
        }

        g_qsort_with_data (device_array, n, sizeof (gchar *), (GCompareDataFunc) g_strcmp0, NULL);

        device_array[n] = "\n";

        devices_to_poll = g_strjoinv (" ", device_array);
        g_free (device_array);

        /* only poke the polling process if the list of currently polled devices change */
        if (g_strcmp0 (devices_to_poll, devices_currently_polled) != 0) {
                g_free (devices_currently_polled);
                devices_currently_polled = devices_to_poll;

                write (poller_daemon_write_end_fd, devices_currently_polled, strlen (devices_currently_polled));
                //g_debug ("Wanna poll: '%s'", devices_currently_polled);
        } else {
                g_free (devices_to_poll);
        }
}