1 /* -*- Mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
3 * Copyright (C) 2008-2010 David Zeuthen <davidz@redhat.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser 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.
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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
29 #include "gudevclient.h"
30 #include "gudevdevice.h"
31 #include "gudevmarshal.h"
32 #include "gudevprivate.h"
36 * @short_description: Query devices and listen to uevents
38 * #GUdevClient is used to query information about devices on a Linux
39 * system from the Linux kernel and the udev device
42 * Device information is retrieved from the kernel (through the
43 * <literal>sysfs</literal> filesystem) and the udev daemon (through a
44 * <literal>tmpfs</literal> filesystem) and presented through
45 * #GUdevDevice objects. This means that no blocking IO ever happens
46 * (in both cases, we are essentially just reading data from kernel
47 * memory) and as such there are no asynchronous versions of the
50 * To get information about a device, use
51 * g_udev_client_query_by_subsystem(),
52 * g_udev_client_query_by_device_number(),
53 * g_udev_client_query_by_device_file(),
54 * g_udev_client_query_by_sysfs_path() or
55 * g_udev_client_query_by_subsystem_and_name().
57 * To listen to uevents, connect to the #GUdevClient::uevent signal.
60 struct _GUdevClientPrivate
62 GSource *watch_source;
64 struct udev_monitor *monitor;
81 static guint signals[LAST_SIGNAL] = { 0 };
83 G_DEFINE_TYPE (GUdevClient, g_udev_client, G_TYPE_OBJECT)
85 /* ---------------------------------------------------------------------------------------------------- */
88 monitor_event (GIOChannel *source,
89 GIOCondition condition,
92 GUdevClient *client = (GUdevClient *) data;
94 struct udev_device *udevice;
96 if (client->priv->monitor == NULL)
98 udevice = udev_monitor_receive_device (client->priv->monitor);
102 device = _g_udev_device_new (udevice);
103 udev_device_unref (udevice);
104 g_signal_emit (client,
105 signals[UEVENT_SIGNAL],
107 g_udev_device_get_action (device),
109 g_object_unref (device);
116 g_udev_client_finalize (GObject *object)
118 GUdevClient *client = G_UDEV_CLIENT (object);
120 if (client->priv->watch_source != NULL)
122 g_source_destroy (client->priv->watch_source);
123 client->priv->watch_source = NULL;
126 if (client->priv->monitor != NULL)
128 udev_monitor_unref (client->priv->monitor);
129 client->priv->monitor = NULL;
132 if (client->priv->udev != NULL)
134 udev_unref (client->priv->udev);
135 client->priv->udev = NULL;
138 g_strfreev (client->priv->subsystems);
140 if (G_OBJECT_CLASS (g_udev_client_parent_class)->finalize != NULL)
141 G_OBJECT_CLASS (g_udev_client_parent_class)->finalize (object);
145 g_udev_client_set_property (GObject *object,
150 GUdevClient *client = G_UDEV_CLIENT (object);
154 case PROP_SUBSYSTEMS:
155 if (client->priv->subsystems != NULL)
156 g_strfreev (client->priv->subsystems);
157 client->priv->subsystems = g_strdupv (g_value_get_boxed (value));
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 g_udev_client_get_property (GObject *object,
172 GUdevClient *client = G_UDEV_CLIENT (object);
176 case PROP_SUBSYSTEMS:
177 g_value_set_boxed (value, client->priv->subsystems);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187 g_udev_client_constructed (GObject *object)
189 GUdevClient *client = G_UDEV_CLIENT (object);
193 client->priv->udev = udev_new ();
195 /* connect to event source */
196 client->priv->monitor = udev_monitor_new_from_netlink (client->priv->udev, "udev");
198 //g_debug ("ss = %p", client->priv->subsystems);
200 if (client->priv->subsystems != NULL)
202 /* install subsystem filters to only wake up for certain events */
203 for (n = 0; client->priv->subsystems[n] != NULL; n++)
209 subsystem = g_strdup (client->priv->subsystems[n]);
212 //g_debug ("s = '%s'", subsystem);
214 s = strstr (subsystem, "/");
221 if (client->priv->monitor != NULL)
222 udev_monitor_filter_add_match_subsystem_devtype (client->priv->monitor, subsystem, devtype);
227 /* listen to events, and buffer them */
228 if (client->priv->monitor != NULL)
230 udev_monitor_enable_receiving (client->priv->monitor);
231 channel = g_io_channel_unix_new (udev_monitor_get_fd (client->priv->monitor));
232 client->priv->watch_source = g_io_create_watch (channel, G_IO_IN);
233 g_io_channel_unref (channel);
234 g_source_set_callback (client->priv->watch_source, (GSourceFunc) monitor_event, client, NULL);
235 g_source_attach (client->priv->watch_source, g_main_context_get_thread_default ());
236 g_source_unref (client->priv->watch_source);
240 client->priv->watch_source = NULL;
244 if (G_OBJECT_CLASS (g_udev_client_parent_class)->constructed != NULL)
245 G_OBJECT_CLASS (g_udev_client_parent_class)->constructed (object);
250 g_udev_client_class_init (GUdevClientClass *klass)
252 GObjectClass *gobject_class = (GObjectClass *) klass;
254 gobject_class->constructed = g_udev_client_constructed;
255 gobject_class->set_property = g_udev_client_set_property;
256 gobject_class->get_property = g_udev_client_get_property;
257 gobject_class->finalize = g_udev_client_finalize;
260 * GUdevClient:subsystems:
262 * The subsystems to listen for uevents on.
264 * To listen for only a specific DEVTYPE for a given SUBSYSTEM, use
265 * "subsystem/devtype". For example, to only listen for uevents
266 * where SUBSYSTEM is usb and DEVTYPE is usb_interface, use
267 * "usb/usb_interface".
269 * If this property is %NULL, then no events will be reported. If
270 * it's the empty array, events from all subsystems will be
273 g_object_class_install_property (gobject_class,
275 g_param_spec_boxed ("subsystems",
276 "The subsystems to listen for changes on",
277 "The subsystems to listen for changes on",
279 G_PARAM_CONSTRUCT_ONLY |
283 * GUdevClient::uevent:
284 * @client: The #GUdevClient receiving the event.
285 * @action: The action for the uevent e.g. "add", "remove", "change", "move", etc.
286 * @device: Details about the #GUdevDevice the event is for.
288 * Emitted when @client receives an uevent.
290 * This signal is emitted in the
291 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
292 * of the thread that @client was created in.
294 signals[UEVENT_SIGNAL] = g_signal_new ("uevent",
295 G_TYPE_FROM_CLASS (klass),
297 G_STRUCT_OFFSET (GUdevClientClass, uevent),
300 g_udev_marshal_VOID__STRING_OBJECT,
306 g_type_class_add_private (klass, sizeof (GUdevClientPrivate));
310 g_udev_client_init (GUdevClient *client)
312 client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
319 * @subsystems: (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.
321 * Constructs a #GUdevClient object that can be used to query
322 * information about devices. Connect to the #GUdevClient::uevent
323 * signal to listen for uevents. Note that signals are emitted in the
324 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
325 * of the thread that you call this constructor from.
327 * Returns: A new #GUdevClient object. Free with g_object_unref().
330 g_udev_client_new (const gchar * const *subsystems)
332 return G_UDEV_CLIENT (g_object_new (G_UDEV_TYPE_CLIENT, "subsystems", subsystems, NULL));
336 * g_udev_client_query_by_subsystem:
337 * @client: A #GUdevClient.
338 * @subsystem: (allow-none): The subsystem to get devices for or %NULL to get all devices.
340 * Gets all devices belonging to @subsystem.
342 * Returns: (element-type GUdevDevice): 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.
345 g_udev_client_query_by_subsystem (GUdevClient *client,
346 const gchar *subsystem)
348 struct udev_enumerate *enumerate;
349 struct udev_list_entry *l, *devices;
352 g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
356 /* prepare a device scan */
357 enumerate = udev_enumerate_new (client->priv->udev);
359 /* filter for subsystem */
360 if (subsystem != NULL)
361 udev_enumerate_add_match_subsystem (enumerate, subsystem);
362 /* retrieve the list */
363 udev_enumerate_scan_devices (enumerate);
365 /* add devices to the list */
366 devices = udev_enumerate_get_list_entry (enumerate);
367 for (l = devices; l != NULL; l = udev_list_entry_get_next (l))
369 struct udev_device *udevice;
372 udevice = udev_device_new_from_syspath (udev_enumerate_get_udev (enumerate),
373 udev_list_entry_get_name (l));
376 device = _g_udev_device_new (udevice);
377 udev_device_unref (udevice);
378 ret = g_list_prepend (ret, device);
380 udev_enumerate_unref (enumerate);
382 ret = g_list_reverse (ret);
388 * g_udev_client_query_by_device_number:
389 * @client: A #GUdevClient.
390 * @type: A value from the #GUdevDeviceType enumeration.
391 * @number: A device number.
393 * Looks up a device for a type and device number.
395 * Returns: A #GUdevDevice object or %NULL if the device was not found. Free with g_object_unref().
398 g_udev_client_query_by_device_number (GUdevClient *client,
399 GUdevDeviceType type,
400 GUdevDeviceNumber number)
402 struct udev_device *udevice;
405 g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
408 udevice = udev_device_new_from_devnum (client->priv->udev, type, number);
413 device = _g_udev_device_new (udevice);
414 udev_device_unref (udevice);
421 * g_udev_client_query_by_device_file:
422 * @client: A #GUdevClient.
423 * @device_file: A device file.
425 * Looks up a device for a device file.
427 * Returns: A #GUdevDevice object or %NULL if the device was not found. Free with g_object_unref().
430 g_udev_client_query_by_device_file (GUdevClient *client,
431 const gchar *device_file)
433 struct stat stat_buf;
436 g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
437 g_return_val_if_fail (device_file != NULL, NULL);
441 if (stat (device_file, &stat_buf) != 0)
444 if (stat_buf.st_rdev == 0)
447 if (S_ISBLK (stat_buf.st_mode))
448 device = g_udev_client_query_by_device_number (client, G_UDEV_DEVICE_TYPE_BLOCK, stat_buf.st_rdev);
449 else if (S_ISCHR (stat_buf.st_mode))
450 device = g_udev_client_query_by_device_number (client, G_UDEV_DEVICE_TYPE_CHAR, stat_buf.st_rdev);
457 * g_udev_client_query_by_sysfs_path:
458 * @client: A #GUdevClient.
459 * @sysfs_path: A sysfs path.
461 * Looks up a device for a sysfs path.
463 * Returns: A #GUdevDevice object or %NULL if the device was not found. Free with g_object_unref().
466 g_udev_client_query_by_sysfs_path (GUdevClient *client,
467 const gchar *sysfs_path)
469 struct udev_device *udevice;
472 g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
473 g_return_val_if_fail (sysfs_path != NULL, NULL);
476 udevice = udev_device_new_from_syspath (client->priv->udev, sysfs_path);
480 device = _g_udev_device_new (udevice);
481 udev_device_unref (udevice);
488 * g_udev_client_query_by_subsystem_and_name:
489 * @client: A #GUdevClient.
490 * @subsystem: A subsystem name.
491 * @name: The name of the device.
493 * Looks up a device for a subsystem and name.
495 * Returns: A #GUdevDevice object or %NULL if the device was not found. Free with g_object_unref().
498 g_udev_client_query_by_subsystem_and_name (GUdevClient *client,
499 const gchar *subsystem,
502 struct udev_device *udevice;
505 g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
506 g_return_val_if_fail (subsystem != NULL, NULL);
507 g_return_val_if_fail (name != NULL, NULL);
510 udevice = udev_device_new_from_subsystem_sysname (client->priv->udev, subsystem, name);
514 device = _g_udev_device_new (udevice);
515 udev_device_unref (udevice);