Debian Codegraph @

src/manager.c

raw
1 /*
2 * /net/reactivated/Fprint/Manager object implementation
3 * Copyright (C) 2008 Daniel Drake <[email protected]>
4 * Copyright (C) 2020 Marco Trevisan <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <glib.h>
24 #include <glib/gi18n.h>
25 #include <fprint.h>
26 #include <glib-object.h>
27 #include <gio/gunixfdlist.h>
28
29 #include "fprintd.h"
30
31 #define LOGIND_BUS_NAME "org.freedesktop.login1"
32 #define LOGIND_IFACE_NAME "org.freedesktop.login1.Manager"
33 #define LOGIND_OBJ_PATH "/org/freedesktop/login1"
34
35 static void fprint_manager_constructed (GObject *object);
36 static gboolean fprint_manager_get_devices (FprintManager *manager,
37 GPtrArray **devices,
38 GError **error);
39 static gboolean fprint_manager_get_default_device (FprintManager *manager,
40 const char **device,
41 GError **error);
42
43 typedef struct
44 {
45 GDBusConnection *connection;
46 GDBusObjectManager *object_manager;
47 FprintDBusManager *dbus_manager;
48 FpContext *context;
49 gboolean no_timeout;
50 guint timeout_id;
51 gint prepare_for_sleep_pending;
52 guint prepare_for_sleep_id;
53 gint sleep_inhibit_fd;
54 } FprintManagerPrivate;
55
56 G_DEFINE_TYPE_WITH_CODE (FprintManager, fprint_manager, G_TYPE_OBJECT, G_ADD_PRIVATE (FprintManager))
57
58 enum {
59 PROP_0,
60 FPRINT_MANAGER_CONNECTION,
61 N_PROPS
62 };
63
64 static GParamSpec *properties[N_PROPS];
65
66 static void
67 fprint_manager_finalize (GObject *object)
68 {
69 FprintManagerPrivate *priv = fprint_manager_get_instance_private (FPRINT_MANAGER (object));
70
71 if (priv->prepare_for_sleep_id)
72 g_dbus_connection_signal_unsubscribe (priv->connection,
73 priv->prepare_for_sleep_id);
74
75 g_clear_object (&priv->object_manager);
76 g_clear_object (&priv->dbus_manager);
77 g_clear_object (&priv->connection);
78 g_clear_object (&priv->context);
79
80 G_OBJECT_CLASS (fprint_manager_parent_class)->finalize (object);
81 }
82
83 static FprintDevice *
84 fprint_dbus_object_skeleton_get_device (FprintDBusObjectSkeleton *object)
85 {
86 FprintDevice *rdev;
87
88 g_object_get (object, "device", &rdev, NULL);
89 return rdev;
90 }
91
92 static void
93 fprint_manager_set_property (GObject *object, guint property_id,
94 const GValue *value, GParamSpec *pspec)
95 {
96 FprintManager *self = FPRINT_MANAGER (object);
97 FprintManagerPrivate *priv = fprint_manager_get_instance_private (self);
98
99 switch (property_id)
100 {
101 case FPRINT_MANAGER_CONNECTION:
102 priv->connection = g_value_dup_object (value);
103 break;
104
105 default:
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
107 break;
108 }
109 }
110
111 static void
112 fprint_manager_get_property (GObject *object, guint property_id,
113 GValue *value, GParamSpec *pspec)
114 {
115 FprintManager *self = FPRINT_MANAGER (object);
116 FprintManagerPrivate *priv = fprint_manager_get_instance_private (self);
117
118 switch (property_id)
119 {
120 case FPRINT_MANAGER_CONNECTION:
121 g_value_set_object (value, priv->connection);
122 break;
123
124 default:
125 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
126 break;
127 }
128 }
129
130 static void
131 fprint_manager_class_init (FprintManagerClass *klass)
132 {
133 GObjectClass *object_class = G_OBJECT_CLASS (klass);
134
135 object_class->constructed = fprint_manager_constructed;
136 object_class->set_property = fprint_manager_set_property;
137 object_class->get_property = fprint_manager_get_property;
138 object_class->finalize = fprint_manager_finalize;
139
140 properties[FPRINT_MANAGER_CONNECTION] =
141 g_param_spec_object ("connection",
142 "Connection",
143 "Set GDBus connection property",
144 G_TYPE_DBUS_CONNECTION,
145 G_PARAM_CONSTRUCT_ONLY |
146 G_PARAM_READWRITE);
147
148 g_object_class_install_properties (object_class, N_PROPS, properties);
149 }
150
151 static gchar *
152 get_device_path (FprintDevice *rdev)
153 {
154 return g_strdup_printf (FPRINT_SERVICE_PATH "/Device/%d",
155 _fprint_device_get_id (rdev));
156 }
157
158 static gboolean
159 fprint_manager_timeout_cb (FprintManager *manager)
160 {
161 //FIXME kill all the devices
162 exit (0);
163 return FALSE;
164 }
165
166 static void
167 fprint_manager_busy_notified (FprintDevice *rdev, GParamSpec *spec, FprintManager *manager)
168 {
169 FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager);
170 guint num_devices_busy = 0;
171
172 g_autolist (GDBusObject) devices = NULL;
173 GList *l;
174 gboolean busy;
175
176 if (priv->timeout_id > 0)
177 {
178 g_source_remove (priv->timeout_id);
179 priv->timeout_id = 0;
180 }
181 if (priv->no_timeout)
182 return;
183
184 devices = g_dbus_object_manager_get_objects (priv->object_manager);
185
186 for (l = devices; l != NULL; l = l->next)
187 {
188 g_autoptr(FprintDevice) dev = NULL;
189 FprintDBusObjectSkeleton *object = l->data;
190
191 dev = fprint_dbus_object_skeleton_get_device (object);
192 g_object_get (G_OBJECT (dev), "busy", &busy, NULL);
193 if (busy != FALSE)
194 num_devices_busy++;
195 }
196
197 if (num_devices_busy == 0)
198 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, (GSourceFunc) fprint_manager_timeout_cb, manager);
199 }
200
201 static gboolean
202 handle_get_devices (FprintManager *manager, GDBusMethodInvocation *invocation,
203 FprintDBusManager *skeleton)
204 {
205 g_autoptr(GPtrArray) devices = NULL;
206 g_autoptr(GError) error = NULL;
207
208 if (!fprint_manager_get_devices (manager, &devices, &error))
209 {
210 g_dbus_method_invocation_return_gerror (invocation, error);
211 return TRUE;
212 }
213
214 fprint_dbus_manager_complete_get_devices (skeleton, invocation,
215 (const gchar *const *)
216 devices->pdata);
217
218 return TRUE;
219 }
220
221 static gboolean
222 handle_get_default_device (FprintManager *manager,
223 GDBusMethodInvocation *invocation,
224 FprintDBusManager *skeleton)
225 {
226 const gchar *device;
227
228 g_autoptr(GError) error = NULL;
229
230 if (!fprint_manager_get_default_device (manager, &device, &error))
231 {
232 g_dbus_method_invocation_return_gerror (invocation, error);
233 return TRUE;
234 }
235
236 fprint_dbus_manager_complete_get_default_device (skeleton, invocation,
237 device);
238
239 return TRUE;
240 }
241
242 static void
243 fprint_device_suspend_cb (GObject *source_object,
244 GAsyncResult *res,
245 gpointer user_data)
246 {
247 g_autoptr(GError) error = NULL;
248 FprintManager *manager = FPRINT_MANAGER (user_data);
249 FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager);
250
251 /* Fetch the result (except for the NULL dummy call). */
252 if (source_object != NULL)
253 {
254 fprint_device_suspend_finish (FPRINT_DEVICE (source_object), res, &error);
255 if (error)
256 {
257 if (!g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_NOT_OPEN) &&
258 !g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_NOT_SUPPORTED))
259 g_message ("Unexpected error while suspending device: %s", error->message);
260 }
261 }
262
263 priv->prepare_for_sleep_pending -= 1;
264
265 /* Close FD when all devices are prepared for sleeping. */
266 if (priv->prepare_for_sleep_pending == 0)
267 {
268 if (priv->sleep_inhibit_fd >= 0)
269 close (priv->sleep_inhibit_fd);
270 priv->sleep_inhibit_fd = -1;
271 g_debug ("Released delay inhibitor for sleep.");
272 }
273
274 g_object_unref (manager);
275 }
276
277 static void
278 logind_sleep_inhibit_cb (GObject *source_object,
279 GAsyncResult *res,
280 gpointer user_data)
281 {
282 g_autoptr(GVariant) data = NULL;
283 g_autoptr(GError) error = NULL;
284 g_autoptr(GUnixFDList) out_fd_list = NULL;
285 g_autoptr(FprintManager) manager = FPRINT_MANAGER (user_data);
286 FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager);
287 gint fd_offset;
288
289 data = g_dbus_connection_call_with_unix_fd_list_finish (priv->connection, &out_fd_list, res, &error);
290
291 if (!data)
292 {
293 g_warning ("Failed to install a sleep delay inhibitor: %s", error->message);
294 return;
295 }
296
297 if (priv->sleep_inhibit_fd >= 0)
298 close (priv->sleep_inhibit_fd);
299
300 g_debug ("Got delay inhibitor for sleep.");
301
302 g_variant_get (data, "(h)", &fd_offset);
303 priv->sleep_inhibit_fd = g_unix_fd_list_get (out_fd_list, fd_offset, NULL);
304 }
305
306 static void
307 handle_prepare_for_sleep_signal (GDBusConnection *connection,
308 const gchar *sender_name,
309 const gchar *object_path,
310 const gchar *interface_name,
311 const gchar *signal_name,
312 GVariant *parameters,
313 gpointer user_data)
314 {
315 g_autolist (GDBusObject) devices = NULL;
316 FprintManager *manager = FPRINT_MANAGER (user_data);
317 FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager);
318 gboolean prepare_for_sleep;
319 GList *l;
320
321 if (!g_variant_check_format_string (parameters, "(b)", FALSE))
322 {
323 g_warning ("Received incorrect parameter for PrepareForSleep signal");
324 return;
325 }
326
327 g_variant_get (parameters, "(b)", &prepare_for_sleep);
328
329 /* called one more time to handle the case of no devices */
330 if (prepare_for_sleep)
331 priv->prepare_for_sleep_pending = 1;
332
333 devices = g_dbus_object_manager_get_objects (priv->object_manager);
334
335 g_debug ("Preparing devices for %s", prepare_for_sleep ? "sleep" : "resume");
336
337 for (l = devices; l != NULL; l = l->next)
338 {
339 g_autoptr(FprintDevice) dev = NULL;
340 FprintDBusObjectSkeleton *object = l->data;
341
342 dev = fprint_dbus_object_skeleton_get_device (object);
343
344 if (prepare_for_sleep)
345 {
346 priv->prepare_for_sleep_pending += 1;
347 g_object_ref (manager);
348 fprint_device_suspend (dev, fprint_device_suspend_cb, manager);
349 }
350 else
351 {
352 fprint_device_resume (dev, NULL, NULL);
353 }
354 }
355
356 if (prepare_for_sleep)
357 {
358 /* "Notify" the initial dummy device we added, handling no devices that suspending */
359 g_object_ref (manager);
360 fprint_device_suspend_cb (NULL, NULL, manager);
361 }
362 else
363 {
364 GVariant *arg = NULL;
365
366 arg = g_variant_new ("(ssss)",
367 "sleep",
368 "net.reactivated.Fprint",
369 "Suspend fingerprint readers",
370 "delay");
371
372 /* Grab a sleep inhibitor. */
373 g_dbus_connection_call_with_unix_fd_list (priv->connection,
374 LOGIND_BUS_NAME,
375 LOGIND_OBJ_PATH,
376 LOGIND_IFACE_NAME,
377 "Inhibit",
378 arg,
379 G_VARIANT_TYPE ("(h)"),
380 G_DBUS_CALL_FLAGS_NONE,
381 -1,
382 NULL,
383 NULL,
384 logind_sleep_inhibit_cb,
385 g_object_ref (manager));
386 }
387 }
388
389 static void
390 device_added_cb (FprintManager *manager, FpDevice *device, FpContext *context)
391 {
392 FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager);
393
394 g_autoptr(FprintDBusObjectSkeleton) object = NULL;
395 g_autoptr(FprintDevice) rdev = NULL;
396 g_autofree gchar *path = NULL;
397
398 rdev = fprint_device_new (device);
399
400 g_signal_connect (G_OBJECT (rdev), "notify::busy",
401 G_CALLBACK (fprint_manager_busy_notified), manager);
402
403 path = get_device_path (rdev);
404
405 object = fprint_dbus_object_skeleton_new (path);
406 fprint_dbus_object_skeleton_set_device (object,
407 FPRINT_DBUS_DEVICE (rdev));
408 g_dbus_object_manager_server_export (
409 G_DBUS_OBJECT_MANAGER_SERVER (priv->object_manager),
410 G_DBUS_OBJECT_SKELETON (object));
411 }
412
413 static void
414 device_removed_cb (FprintManager *manager, FpDevice *device, FpContext *context)
415 {
416 FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager);
417
418 g_autolist (FprintDBusObjectSkeleton) objects = NULL;
419 GList *item;
420
421 objects = g_dbus_object_manager_get_objects (priv->object_manager);
422
423 for (item = objects; item; item = item->next)
424 {
425 g_autoptr(FprintDevice) rdev = NULL;
426 g_autoptr(FpDevice) dev = NULL;
427 FprintDBusObjectSkeleton *object = item->data;
428
429 rdev = fprint_dbus_object_skeleton_get_device (object);
430 g_object_get (rdev, "dev", &dev, NULL);
431 if (dev != device)
432 continue;
433
434 g_dbus_object_manager_server_unexport (
435 G_DBUS_OBJECT_MANAGER_SERVER (priv->object_manager),
436 g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (rdev)));
437
438 g_signal_handlers_disconnect_by_data (rdev, manager);
439
440 /* We cannot continue to iterate at this point, but we don't need to either */
441 break;
442 }
443
444 /* The device that disappeared might have been busy.
445 * Do we need to do anything else in this case to clean up more gracefully? */
446 fprint_manager_busy_notified (NULL, NULL, manager);
447 }
448
449 static void
450 fprint_manager_constructed (GObject *object)
451 {
452 g_autoptr(GVariant) param_false = NULL;
453 FprintManager *manager = FPRINT_MANAGER (object);
454 FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager);
455 GDBusObjectManagerServer *object_manager_server;
456
457 object_manager_server =
458 g_dbus_object_manager_server_new (FPRINT_SERVICE_PATH);
459
460 priv->object_manager = G_DBUS_OBJECT_MANAGER (object_manager_server);
461 priv->dbus_manager = fprint_dbus_manager_skeleton_new ();
462 priv->context = fp_context_new ();
463
464 g_signal_connect_object (priv->dbus_manager,
465 "handle-get-devices",
466 G_CALLBACK (handle_get_devices),
467 manager,
468 G_CONNECT_SWAPPED);
469 g_signal_connect_object (priv->dbus_manager,
470 "handle-get-default-device",
471 G_CALLBACK (handle_get_default_device),
472 manager,
473 G_CONNECT_SWAPPED);
474
475 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (priv->dbus_manager),
476 priv->connection,
477 FPRINT_SERVICE_PATH "/Manager", NULL);
478
479 g_dbus_object_manager_server_set_connection (object_manager_server,
480 priv->connection);
481
482 priv->prepare_for_sleep_id = g_dbus_connection_signal_subscribe (priv->connection,
483 LOGIND_BUS_NAME,
484 LOGIND_IFACE_NAME,
485 "PrepareForSleep",
486 LOGIND_OBJ_PATH,
487 NULL,
488 G_DBUS_SIGNAL_FLAGS_NONE,
489 handle_prepare_for_sleep_signal,
490 manager,
491 NULL);
492 /* Fake a resume as that triggers the inhibitor to be taken. */
493 param_false = g_variant_new ("(b)", FALSE);
494 handle_prepare_for_sleep_signal (priv->connection, NULL, NULL, NULL, NULL, param_false, manager);
495
496 /* And register the signals for initial enumeration and hotplug. */
497 g_signal_connect_object (priv->context,
498 "device-added",
499 (GCallback) device_added_cb,
500 manager,
501 G_CONNECT_SWAPPED);
502
503 g_signal_connect_object (priv->context,
504 "device-removed",
505 (GCallback) device_removed_cb,
506 manager,
507 G_CONNECT_SWAPPED);
508
509 /* Prepare everything by enumerating all devices.
510 * This blocks the main loop until the existing devices are enumerated
511 */
512 fp_context_enumerate (priv->context);
513
514 G_OBJECT_CLASS (fprint_manager_parent_class)->constructed (object);
515 }
516
517 static void
518 fprint_manager_init (FprintManager *manager)
519 {
520 }
521
522 FprintManager *
523 fprint_manager_new (GDBusConnection *connection, gboolean no_timeout)
524 {
525 FprintManagerPrivate *priv;
526 GObject *object;
527
528 object = g_object_new (FPRINT_TYPE_MANAGER, "connection", connection, NULL);
529 priv = fprint_manager_get_instance_private (FPRINT_MANAGER (object));
530 priv->no_timeout = no_timeout;
531
532 if (!priv->no_timeout)
533 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, (GSourceFunc) fprint_manager_timeout_cb, object);
534
535 return FPRINT_MANAGER (object);
536 }
537
538 static gboolean
539 fprint_manager_get_devices (FprintManager *manager,
540 GPtrArray **devices, GError **error)
541 {
542 FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager);
543
544 g_autolist (FprintDBusObjectSkeleton) objects = NULL;
545 GList *l;
546 int num_open;
547 GPtrArray *devs;
548
549 objects = g_dbus_object_manager_get_objects (priv->object_manager);
550 objects = g_list_reverse (objects);
551
552 num_open = g_list_length (objects);
553 devs = g_ptr_array_sized_new (num_open);
554
555 if (num_open > 0)
556 {
557 for (l = objects; l != NULL; l = l->next)
558 {
559 g_autoptr(FprintDevice) rdev = NULL;
560 FprintDBusObjectSkeleton *object = l->data;
561 const char *path;
562
563 rdev = fprint_dbus_object_skeleton_get_device (object);
564 path = g_dbus_interface_skeleton_get_object_path (
565 G_DBUS_INTERFACE_SKELETON (rdev));
566 g_ptr_array_add (devs, (char *) path);
567 }
568 }
569 g_ptr_array_add (devs, NULL);
570
571 *devices = devs;
572 return TRUE;
573 }
574
575 static gboolean
576 fprint_manager_get_default_device (FprintManager *manager,
577 const char **device, GError **error)
578 {
579 FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager);
580
581 g_autolist (FprintDBusObjectSkeleton) objects = NULL;
582 int num_open;
583
584 objects = g_dbus_object_manager_get_objects (priv->object_manager);
585 num_open = g_list_length (objects);
586
587 if (num_open > 0)
588 {
589 g_autoptr(FprintDevice) rdev = NULL;
590 FprintDBusObjectSkeleton *object = g_list_last (objects)->data;
591
592 rdev = fprint_dbus_object_skeleton_get_device (object);
593 *device = g_dbus_interface_skeleton_get_object_path (
594 G_DBUS_INTERFACE_SKELETON (rdev));
595 return TRUE;
596 }
597 else
598 {
599 g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_NO_SUCH_DEVICE,
600 "No devices available");
601 *device = NULL;
602 return FALSE;
603 }
604 }
605
606 GQuark
607 fprint_error_quark (void)
608 {
609 static gsize quark = 0;
610
611 if (g_once_init_enter (&quark))
612 {
613 g_autoptr(GEnumClass) errors_enum = NULL;
614 GQuark domain;
615 unsigned i;
616
617 domain = g_quark_from_static_string ("fprintd-error-quark");
618 errors_enum = g_type_class_ref (FPRINT_TYPE_ERROR);
619
620 for (i = 0; i < errors_enum->n_values; ++i)
621 {
622 GEnumValue *value = &errors_enum->values[i];
623
624 g_dbus_error_register_error (domain, value->value,
625 value->value_nick);
626 }
627
628 g_once_init_leave (&quark, domain);
629 }
630 return (GQuark) quark;
631 }