chiark / gitweb /
resolved: when there's already somebody listening on the LLMNR ports, simple disable...
[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: (nullable) (element-type GUdevDevice) (transfer full): A
342  * list of #GUdevDevice objects. The caller should free the result by
343  * using g_object_unref() on each element in the list and then
344  * g_list_free() on the list.
345  */
346 GList *
347 g_udev_client_query_by_subsystem (GUdevClient  *client,
348                                   const gchar  *subsystem)
349 {
350   struct udev_enumerate *enumerate;
351   struct udev_list_entry *l, *devices;
352   GList *ret;
353
354   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
355
356   ret = NULL;
357
358   /* prepare a device scan */
359   enumerate = udev_enumerate_new (client->priv->udev);
360
361   /* filter for subsystem */
362   if (subsystem != NULL)
363     udev_enumerate_add_match_subsystem (enumerate, subsystem);
364   /* retrieve the list */
365   udev_enumerate_scan_devices (enumerate);
366
367   /* add devices to the list */
368   devices = udev_enumerate_get_list_entry (enumerate);
369   for (l = devices; l != NULL; l = udev_list_entry_get_next (l))
370     {
371       struct udev_device *udevice;
372       GUdevDevice *device;
373
374       udevice = udev_device_new_from_syspath (udev_enumerate_get_udev (enumerate),
375                                               udev_list_entry_get_name (l));
376       if (udevice == NULL)
377         continue;
378       device = _g_udev_device_new (udevice);
379       udev_device_unref (udevice);
380       ret = g_list_prepend (ret, device);
381     }
382   udev_enumerate_unref (enumerate);
383
384   ret = g_list_reverse (ret);
385
386   return ret;
387 }
388
389 /**
390  * g_udev_client_query_by_device_number:
391  * @client: A #GUdevClient.
392  * @type: A value from the #GUdevDeviceType enumeration.
393  * @number: A device number.
394  *
395  * Looks up a device for a type and device number.
396  *
397  * Returns: (nullable) (transfer full): A #GUdevDevice object or %NULL
398  * if the device was not found. Free with g_object_unref().
399  */
400 GUdevDevice *
401 g_udev_client_query_by_device_number (GUdevClient      *client,
402                                       GUdevDeviceType   type,
403                                       GUdevDeviceNumber number)
404 {
405   struct udev_device *udevice;
406   GUdevDevice *device;
407
408   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
409
410   device = NULL;
411   udevice = udev_device_new_from_devnum (client->priv->udev, type, number);
412
413   if (udevice == NULL)
414     goto out;
415
416   device = _g_udev_device_new (udevice);
417   udev_device_unref (udevice);
418
419  out:
420   return device;
421 }
422
423 /**
424  * g_udev_client_query_by_device_file:
425  * @client: A #GUdevClient.
426  * @device_file: A device file.
427  *
428  * Looks up a device for a device file.
429  *
430  * Returns: (nullable) (transfer full): A #GUdevDevice object or %NULL
431  * if the device was not found. Free with g_object_unref().
432  */
433 GUdevDevice *
434 g_udev_client_query_by_device_file (GUdevClient  *client,
435                                     const gchar  *device_file)
436 {
437   struct stat stat_buf;
438   GUdevDevice *device;
439
440   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
441   g_return_val_if_fail (device_file != NULL, NULL);
442
443   device = NULL;
444
445   if (stat (device_file, &stat_buf) != 0)
446     goto out;
447
448   if (stat_buf.st_rdev == 0)
449     goto out;
450
451   if (S_ISBLK (stat_buf.st_mode))
452     device = g_udev_client_query_by_device_number (client, G_UDEV_DEVICE_TYPE_BLOCK, stat_buf.st_rdev);
453   else if (S_ISCHR (stat_buf.st_mode))
454     device = g_udev_client_query_by_device_number (client, G_UDEV_DEVICE_TYPE_CHAR, stat_buf.st_rdev);
455
456  out:
457   return device;
458 }
459
460 /**
461  * g_udev_client_query_by_sysfs_path:
462  * @client: A #GUdevClient.
463  * @sysfs_path: A sysfs path.
464  *
465  * Looks up a device for a sysfs path.
466  *
467  * Returns: (nullable) (transfer full): A #GUdevDevice object or %NULL
468  * if the device was not found. Free with g_object_unref().
469  */
470 GUdevDevice *
471 g_udev_client_query_by_sysfs_path (GUdevClient  *client,
472                                    const gchar  *sysfs_path)
473 {
474   struct udev_device *udevice;
475   GUdevDevice *device;
476
477   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
478   g_return_val_if_fail (sysfs_path != NULL, NULL);
479
480   device = NULL;
481   udevice = udev_device_new_from_syspath (client->priv->udev, sysfs_path);
482   if (udevice == NULL)
483     goto out;
484
485   device = _g_udev_device_new (udevice);
486   udev_device_unref (udevice);
487
488  out:
489   return device;
490 }
491
492 /**
493  * g_udev_client_query_by_subsystem_and_name:
494  * @client: A #GUdevClient.
495  * @subsystem: A subsystem name.
496  * @name: The name of the device.
497  *
498  * Looks up a device for a subsystem and name.
499  *
500  * Returns: (nullable) (transfer full): A #GUdevDevice object or %NULL
501  * if the device was not found. Free with g_object_unref().
502  */
503 GUdevDevice *
504 g_udev_client_query_by_subsystem_and_name (GUdevClient  *client,
505                                            const gchar  *subsystem,
506                                            const gchar  *name)
507 {
508   struct udev_device *udevice;
509   GUdevDevice *device;
510
511   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
512   g_return_val_if_fail (subsystem != NULL, NULL);
513   g_return_val_if_fail (name != NULL, NULL);
514
515   device = NULL;
516   udevice = udev_device_new_from_subsystem_sysname (client->priv->udev, subsystem, name);
517   if (udevice == NULL)
518     goto out;
519
520   device = _g_udev_device_new (udevice);
521   udev_device_unref (udevice);
522
523  out:
524   return device;
525 }
526
527 struct udev *
528 _g_udev_client_get_udev (GUdevClient *client)
529 {
530   g_return_val_if_fail (G_UDEV_IS_CLIENT (client), NULL);
531   return client->priv->udev;
532 }