chiark / gitweb /
core: always create /dev/kdbus/ns (and make it private 0700) after setting up the...
[elogind.git] / src / gudev / gudevclient.c
1 /* -*- Mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2  *
3  * Copyright (C) 2008-2010 David Zeuthen <davidz@redhat.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "gudevclient.h"
28 #include "gudevdevice.h"
29 #include "gudevmarshal.h"
30 #include "gudevprivate.h"
31
32 /**
33  * SECTION:gudevclient
34  * @short_description: Query devices and listen to uevents
35  *
36  * #GUdevClient is used to query information about devices on a Linux
37  * system from the Linux kernel and the udev device
38  * manager.
39  *
40  * Device information is retrieved from the kernel (through the
41  * <literal>sysfs</literal> filesystem) and the udev daemon (through a
42  * <literal>tmpfs</literal> filesystem) and presented through
43  * #GUdevDevice objects. This means that no blocking IO ever happens
44  * (in both cases, we are essentially just reading data from kernel
45  * memory) and as such there are no asynchronous versions of the
46  * provided methods.
47  *
48  * To get #GUdevDevice objects, use
49  * g_udev_client_query_by_subsystem(),
50  * g_udev_client_query_by_device_number(),
51  * g_udev_client_query_by_device_file(),
52  * g_udev_client_query_by_sysfs_path(),
53  * g_udev_client_query_by_subsystem_and_name()
54  * or the #GUdevEnumerator type.
55  *
56  * To listen to uevents, connect to the #GUdevClient::uevent signal.
57  */
58
59 struct _GUdevClientPrivate
60 {
61   GSource *watch_source;
62   struct udev *udev;
63   struct udev_monitor *monitor;
64
65   gchar **subsystems;
66 };
67
68 enum
69 {
70   PROP_0,
71   PROP_SUBSYSTEMS,
72 };
73
74 enum
75 {
76   UEVENT_SIGNAL,
77   LAST_SIGNAL,
78 };
79
80 static guint signals[LAST_SIGNAL] = { 0 };
81
82 G_DEFINE_TYPE (GUdevClient, g_udev_client, G_TYPE_OBJECT)
83
84 /* ---------------------------------------------------------------------------------------------------- */
85
86 static gboolean
87 monitor_event (GIOChannel *source,
88                GIOCondition condition,
89                gpointer data)
90 {
91   GUdevClient *client = (GUdevClient *) data;
92   GUdevDevice *device;
93   struct udev_device *udevice;
94
95   if (client->priv->monitor == NULL)
96     goto out;
97   udevice = udev_monitor_receive_device (client->priv->monitor);
98   if (udevice == NULL)
99     goto out;
100
101   device = _g_udev_device_new (udevice);
102   udev_device_unref (udevice);
103   g_signal_emit (client,
104                  signals[UEVENT_SIGNAL],
105                  0,
106                  g_udev_device_get_action (device),
107                  device);
108   g_object_unref (device);
109
110  out:
111   return TRUE;
112 }
113
114 static void
115 g_udev_client_finalize (GObject *object)
116 {
117   GUdevClient *client = G_UDEV_CLIENT (object);
118
119   if (client->priv->watch_source != NULL)
120     {
121       g_source_destroy (client->priv->watch_source);
122       client->priv->watch_source = NULL;
123     }
124
125   if (client->priv->monitor != NULL)
126     {
127       udev_monitor_unref (client->priv->monitor);
128       client->priv->monitor = NULL;
129     }
130
131   if (client->priv->udev != NULL)
132     {
133       udev_unref (client->priv->udev);
134       client->priv->udev = NULL;
135     }
136
137   g_strfreev (client->priv->subsystems);
138
139   if (G_OBJECT_CLASS (g_udev_client_parent_class)->finalize != NULL)
140     G_OBJECT_CLASS (g_udev_client_parent_class)->finalize (object);
141 }
142
143 static void
144 g_udev_client_set_property (GObject      *object,
145                             guint         prop_id,
146                             const GValue *value,
147                             GParamSpec   *pspec)
148 {
149   GUdevClient *client = G_UDEV_CLIENT (object);
150
151   switch (prop_id)
152     {
153     case PROP_SUBSYSTEMS:
154       if (client->priv->subsystems != NULL)
155         g_strfreev (client->priv->subsystems);
156       client->priv->subsystems = g_strdupv (g_value_get_boxed (value));
157       break;
158
159     default:
160       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161       break;
162     }
163 }
164
165 static void
166 g_udev_client_get_property (GObject     *object,
167                             guint        prop_id,
168                             GValue      *value,
169                             GParamSpec  *pspec)
170 {
171   GUdevClient *client = G_UDEV_CLIENT (object);
172
173   switch (prop_id)
174     {
175     case PROP_SUBSYSTEMS:
176       g_value_set_boxed (value, client->priv->subsystems);
177       break;
178
179     default:
180       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181       break;
182     }
183 }
184
185 static void
186 g_udev_client_constructed (GObject *object)
187 {
188   GUdevClient *client = G_UDEV_CLIENT (object);
189   GIOChannel *channel;
190   guint n;
191
192   client->priv->udev = udev_new ();
193
194   /* connect to event source */
195   client->priv->monitor = udev_monitor_new_from_netlink (client->priv->udev, "udev");
196
197   //g_debug ("ss = %p", client->priv->subsystems);
198
199   if (client->priv->subsystems != NULL)
200     {
201       /* install subsystem filters to only wake up for certain events */
202       for (n = 0; client->priv->subsystems[n] != NULL; n++)
203         {
204           gchar *subsystem;
205           gchar *devtype;
206           gchar *s;
207
208           subsystem = g_strdup (client->priv->subsystems[n]);
209           devtype = NULL;
210
211           //g_debug ("s = '%s'", subsystem);
212
213           s = strstr (subsystem, "/");
214           if (s != NULL)
215             {
216               devtype = s + 1;
217               *s = '\0';
218             }
219
220           if (client->priv->monitor != NULL)
221               udev_monitor_filter_add_match_subsystem_devtype (client->priv->monitor, subsystem, devtype);
222
223           g_free (subsystem);
224         }
225
226       /* listen to events, and buffer them */
227       if (client->priv->monitor != NULL)
228         {
229           udev_monitor_enable_receiving (client->priv->monitor);
230           channel = g_io_channel_unix_new (udev_monitor_get_fd (client->priv->monitor));
231           client->priv->watch_source = g_io_create_watch (channel, G_IO_IN);
232           g_io_channel_unref (channel);
233           g_source_set_callback (client->priv->watch_source, (GSourceFunc) monitor_event, client, NULL);
234           g_source_attach (client->priv->watch_source, g_main_context_get_thread_default ());
235           g_source_unref (client->priv->watch_source);
236         }
237       else
238         {
239           client->priv->watch_source = NULL;
240         }
241     }
242
243   if (G_OBJECT_CLASS (g_udev_client_parent_class)->constructed != NULL)
244     G_OBJECT_CLASS (g_udev_client_parent_class)->constructed (object);
245 }
246
247
248 static void
249 g_udev_client_class_init (GUdevClientClass *klass)
250 {
251   GObjectClass *gobject_class = (GObjectClass *) klass;
252
253   gobject_class->constructed  = g_udev_client_constructed;
254   gobject_class->set_property = g_udev_client_set_property;
255   gobject_class->get_property = g_udev_client_get_property;
256   gobject_class->finalize     = g_udev_client_finalize;
257
258   /**
259    * GUdevClient:subsystems:
260    *
261    * The subsystems to listen for uevents on.
262    *
263    * To listen for only a specific DEVTYPE for a given SUBSYSTEM, use
264    * "subsystem/devtype". For example, to only listen for uevents
265    * where SUBSYSTEM is usb and DEVTYPE is usb_interface, use
266    * "usb/usb_interface".
267    *
268    * If this property is %NULL, then no events will be reported. If
269    * it's the empty array, events from all subsystems will be
270    * reported.
271    */
272   g_object_class_install_property (gobject_class,
273                                    PROP_SUBSYSTEMS,
274                                    g_param_spec_boxed ("subsystems",
275                                                        "The subsystems to listen for changes on",
276                                                        "The subsystems to listen for changes on",
277                                                        G_TYPE_STRV,
278                                                        G_PARAM_CONSTRUCT_ONLY |
279                                                        G_PARAM_READWRITE));
280
281   /**
282    * GUdevClient::uevent:
283    * @client: The #GUdevClient receiving the event.
284    * @action: The action for the uevent e.g. "add", "remove", "change", "move", etc.
285    * @device: Details about the #GUdevDevice the event is for.
286    *
287    * Emitted when @client receives an uevent.
288    *
289    * This signal is emitted in the
290    * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
291    * of the thread that @client was created in.
292    */
293   signals[UEVENT_SIGNAL] = g_signal_new ("uevent",
294                                          G_TYPE_FROM_CLASS (klass),
295                                          G_SIGNAL_RUN_LAST,
296                                          G_STRUCT_OFFSET (GUdevClientClass, uevent),
297                                          NULL,
298                                          NULL,
299                                          g_udev_marshal_VOID__STRING_OBJECT,
300                                          G_TYPE_NONE,
301                                          2,
302                                          G_TYPE_STRING,
303                                          G_UDEV_TYPE_DEVICE);
304
305   g_type_class_add_private (klass, sizeof (GUdevClientPrivate));
306 }
307
308 static void
309 g_udev_client_init (GUdevClient *client)
310 {
311   client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
312                                               G_UDEV_TYPE_CLIENT,
313                                               GUdevClientPrivate);
314 }
315
316 /**
317  * g_udev_client_new:
318  * @subsystems: (array zero-terminated=1) (element-type utf8) (transfer none) (allow-none): A %NULL terminated string array of subsystems to listen for uevents on, %NULL to not listen on uevents at all, or an empty array to listen to uevents on all subsystems. See the documentation for the #GUdevClient:subsystems property for details on this parameter.
319  *
320  * Constructs a #GUdevClient object that can be used to query
321  * information about devices. Connect to the #GUdevClient::uevent
322  * signal to listen for uevents. Note that signals are emitted in the
323  * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
324  * of the thread that you call this constructor from.
325  *
326  * Returns: A new #GUdevClient object. Free with g_object_unref().
327  */
328 GUdevClient *
329 g_udev_client_new (const gchar * const *subsystems)
330 {
331   return G_UDEV_CLIENT (g_object_new (G_UDEV_TYPE_CLIENT, "subsystems", subsystems, NULL));
332 }
333
334 /**
335  * g_udev_client_query_by_subsystem:
336  * @client: A #GUdevClient.
337  * @subsystem: (allow-none): The subsystem to get devices for or %NULL to get all devices.
338  *
339  * Gets all devices belonging to @subsystem.
340  *
341  * Returns: (element-type GUdevDevice) (transfer full): A list of #GUdevDevice objects. The caller should free the result by using g_object_unref() on each element in the list and then g_list_free() on the list.
342  */
343 GList *
344 g_udev_client_query_by_subsystem (GUdevClient  *client,
345                                   const gchar  *subsystem)
346 {
347   struct udev_enumerate *enumerate;
348   struct udev_list_entry *l, *devices;
349   GList *ret;
350
351   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
352
353   ret = NULL;
354
355   /* prepare a device scan */
356   enumerate = udev_enumerate_new (client->priv->udev);
357
358   /* filter for subsystem */
359   if (subsystem != NULL)
360     udev_enumerate_add_match_subsystem (enumerate, subsystem);
361   /* retrieve the list */
362   udev_enumerate_scan_devices (enumerate);
363
364   /* add devices to the list */
365   devices = udev_enumerate_get_list_entry (enumerate);
366   for (l = devices; l != NULL; l = udev_list_entry_get_next (l))
367     {
368       struct udev_device *udevice;
369       GUdevDevice *device;
370
371       udevice = udev_device_new_from_syspath (udev_enumerate_get_udev (enumerate),
372                                               udev_list_entry_get_name (l));
373       if (udevice == NULL)
374         continue;
375       device = _g_udev_device_new (udevice);
376       udev_device_unref (udevice);
377       ret = g_list_prepend (ret, device);
378     }
379   udev_enumerate_unref (enumerate);
380
381   ret = g_list_reverse (ret);
382
383   return ret;
384 }
385
386 /**
387  * g_udev_client_query_by_device_number:
388  * @client: A #GUdevClient.
389  * @type: A value from the #GUdevDeviceType enumeration.
390  * @number: A device number.
391  *
392  * Looks up a device for a type and device number.
393  *
394  * Returns: (transfer full): A #GUdevDevice object or %NULL if the device was not found. Free with g_object_unref().
395  */
396 GUdevDevice *
397 g_udev_client_query_by_device_number (GUdevClient      *client,
398                                       GUdevDeviceType   type,
399                                       GUdevDeviceNumber number)
400 {
401   struct udev_device *udevice;
402   GUdevDevice *device;
403
404   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
405
406   device = NULL;
407   udevice = udev_device_new_from_devnum (client->priv->udev, type, number);
408
409   if (udevice == NULL)
410     goto out;
411
412   device = _g_udev_device_new (udevice);
413   udev_device_unref (udevice);
414
415  out:
416   return device;
417 }
418
419 /**
420  * g_udev_client_query_by_device_file:
421  * @client: A #GUdevClient.
422  * @device_file: A device file.
423  *
424  * Looks up a device for a device file.
425  *
426  * Returns: (transfer full): A #GUdevDevice object or %NULL if the device was not found. Free with g_object_unref().
427  */
428 GUdevDevice *
429 g_udev_client_query_by_device_file (GUdevClient  *client,
430                                     const gchar  *device_file)
431 {
432   struct stat stat_buf;
433   GUdevDevice *device;
434
435   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
436   g_return_val_if_fail (device_file != NULL, NULL);
437
438   device = NULL;
439
440   if (stat (device_file, &stat_buf) != 0)
441     goto out;
442
443   if (stat_buf.st_rdev == 0)
444     goto out;
445
446   if (S_ISBLK (stat_buf.st_mode))
447     device = g_udev_client_query_by_device_number (client, G_UDEV_DEVICE_TYPE_BLOCK, stat_buf.st_rdev);
448   else if (S_ISCHR (stat_buf.st_mode))
449     device = g_udev_client_query_by_device_number (client, G_UDEV_DEVICE_TYPE_CHAR, stat_buf.st_rdev);
450
451  out:
452   return device;
453 }
454
455 /**
456  * g_udev_client_query_by_sysfs_path:
457  * @client: A #GUdevClient.
458  * @sysfs_path: A sysfs path.
459  *
460  * Looks up a device for a sysfs path.
461  *
462  * Returns: (transfer full): A #GUdevDevice object or %NULL if the device was not found. Free with g_object_unref().
463  */
464 GUdevDevice *
465 g_udev_client_query_by_sysfs_path (GUdevClient  *client,
466                                    const gchar  *sysfs_path)
467 {
468   struct udev_device *udevice;
469   GUdevDevice *device;
470
471   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
472   g_return_val_if_fail (sysfs_path != NULL, NULL);
473
474   device = NULL;
475   udevice = udev_device_new_from_syspath (client->priv->udev, sysfs_path);
476   if (udevice == NULL)
477     goto out;
478
479   device = _g_udev_device_new (udevice);
480   udev_device_unref (udevice);
481
482  out:
483   return device;
484 }
485
486 /**
487  * g_udev_client_query_by_subsystem_and_name:
488  * @client: A #GUdevClient.
489  * @subsystem: A subsystem name.
490  * @name: The name of the device.
491  *
492  * Looks up a device for a subsystem and name.
493  *
494  * Returns: (transfer full): A #GUdevDevice object or %NULL if the device was not found. Free with g_object_unref().
495  */
496 GUdevDevice *
497 g_udev_client_query_by_subsystem_and_name (GUdevClient  *client,
498                                            const gchar  *subsystem,
499                                            const gchar  *name)
500 {
501   struct udev_device *udevice;
502   GUdevDevice *device;
503
504   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
505   g_return_val_if_fail (subsystem != NULL, NULL);
506   g_return_val_if_fail (name != NULL, NULL);
507
508   device = NULL;
509   udevice = udev_device_new_from_subsystem_sysname (client->priv->udev, subsystem, name);
510   if (udevice == NULL)
511     goto out;
512
513   device = _g_udev_device_new (udevice);
514   udev_device_unref (udevice);
515
516  out:
517   return device;
518 }
519
520 struct udev *
521 _g_udev_client_get_udev (GUdevClient *client)
522 {
523   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
524   return client->priv->udev;
525 }