/[gentoo-x86]/net-misc/networkmanager/files/0001-core-runtime-detect-logind-and-ConsoleKit-3.patch
Gentoo

Contents of /net-misc/networkmanager/files/0001-core-runtime-detect-logind-and-ConsoleKit-3.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations) (download)
Mon Jul 15 18:31:50 2013 UTC (14 months ago) by pacho
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
FILE REMOVED
Revision bump dropping runtime detect of logind as we won't be able to support logind without systemd being running, and this keeps us more close to upstream (preventing us from hitting any hypothetical problems that could arise from it until accepted by upstream). Also force dhcpcd < 6 until upstream confirms updating configure check is enough to properly work with newer versions.

(Portage version: 2.1.12.13/cvs/Linux x86_64, signed Manifest commit with key A188FBD4)

1 From b8b49850cace3e0fca84d34f7eb5b1eb3f3c7bc8 Mon Sep 17 00:00:00 2001
2 From: Fabio Erculiani <lxnay@sabayon.org>
3 Date: Mon, 15 Apr 2013 23:25:59 +0100
4 Subject: [PATCH] core: runtime detect logind and ConsoleKit
5
6 If --with-session-tracking=systemd, but logind is not currently running, the code
7 now falls back to ConsoleKit.
8 ---
9 configure.ac | 2 +
10 src/Makefile.am | 10 +-
11 src/nm-session-monitor-systemd.c | 268 ----------------
12 src/nm-session-monitor.c | 641 +++++++++++++++++++++++++++++++++++++++
13 src/nm-session-monitor.h | 6 +
14 5 files changed, 652 insertions(+), 275 deletions(-)
15 delete mode 100644 src/nm-session-monitor-systemd.c
16 create mode 100644 src/nm-session-monitor.c
17
18 diff --git a/configure.ac b/configure.ac
19 index 5e4a2eb..a99a453 100644
20 --- a/configure.ac
21 +++ b/configure.ac
22 @@ -285,10 +285,12 @@ AS_IF([! (echo "$with_session_tracking" | grep -q -E "^(systemd|consolekit|no)$"
23 # add conditionals and subtitutions
24 AM_CONDITIONAL(SESSION_TRACKING_CK, test "$with_session_tracking" = "consolekit")
25 AM_CONDITIONAL(SESSION_TRACKING_SYSTEMD, test "$with_session_tracking" = "systemd")
26 +AM_CONDITIONAL(SESSION_TRACKING_NULL, test "$with_session_tracking" = "no")
27 if test "$with_session_tracking" = "systemd"; then
28 PKG_CHECK_MODULES(SYSTEMD_LOGIN, [libsystemd-login])
29 AC_SUBST(SYSTEMD_LOGIN_CFLAGS)
30 AC_SUBST(SYSTEMD_LOGIN_LIBS)
31 + AC_DEFINE([SESSION_TRACKING_SYSTEMD], 1, [Define to 1 if libsystemd-login is available])
32 fi
33 if test "$with_session_tracking" = "consolekit"; then
34 AC_SUBST(CKDB_PATH, /var/run/ConsoleKit/database)
35 diff --git a/src/Makefile.am b/src/Makefile.am
36 index 9d306a7..6ec3c80 100644
37 --- a/src/Makefile.am
38 +++ b/src/Makefile.am
39 @@ -204,14 +204,10 @@ if WITH_CONCHECK
40 NetworkManager_SOURCES += nm-connectivity.c nm-connectivity.h
41 endif
42
43 -if SESSION_TRACKING_SYSTEMD
44 -NetworkManager_SOURCES += nm-session-monitor-systemd.c
45 -else
46 -if SESSION_TRACKING_CK
47 -NetworkManager_SOURCES += nm-session-monitor-ck.c
48 -else
49 +if SESSION_TRACKING_NULL
50 NetworkManager_SOURCES += nm-session-monitor-null.c
51 -endif
52 +else
53 +NetworkManager_SOURCES += nm-session-monitor.c
54 endif
55
56 if SUSPEND_RESUME_SYSTEMD
57 diff --git a/src/nm-session-monitor-systemd.c b/src/nm-session-monitor-systemd.c
58 deleted file mode 100644
59 index f9fb075..0000000
60 --- a/src/nm-session-monitor-systemd.c
61 +++ /dev/null
62 @@ -1,268 +0,0 @@
63 -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
64 -/*
65 - * Copyright (C) 2011 Red Hat, Inc.
66 - *
67 - * This library is free software; you can redistribute it and/or
68 - * modify it under the terms of the GNU Lesser General Public
69 - * License as published by the Free Software Foundation; either
70 - * version 2 of the License, or (at your option) any later version.
71 - *
72 - * This library is distributed in the hope that it will be useful,
73 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
74 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
75 - * Lesser General Public License for more details.
76 - *
77 - * You should have received a copy of the GNU Lesser General
78 - * Public License along with this library; if not, write to the
79 - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
80 - * Boston, MA 02110-1301, USA.
81 - *
82 - * Author: Matthias Clasen
83 - */
84 -
85 -#include "config.h"
86 -#include <errno.h>
87 -#include <pwd.h>
88 -#include <grp.h>
89 -#include <string.h>
90 -#include <glib/gstdio.h>
91 -#include <systemd/sd-login.h>
92 -#include <stdlib.h>
93 -
94 -#include "nm-session-utils.h"
95 -#include "nm-session-monitor.h"
96 -#include "nm-logging.h"
97 -
98 -/********************************************************************/
99 -
100 -typedef struct {
101 - GSource source;
102 - GPollFD pollfd;
103 - sd_login_monitor *monitor;
104 -} SdSource;
105 -
106 -static gboolean
107 -sd_source_prepare (GSource *source, gint *timeout)
108 -{
109 - *timeout = -1;
110 - return FALSE;
111 -}
112 -
113 -static gboolean
114 -sd_source_check (GSource *source)
115 -{
116 - SdSource *sd_source = (SdSource *) source;
117 -
118 - return sd_source->pollfd.revents != 0;
119 -}
120 -
121 -static gboolean
122 -sd_source_dispatch (GSource *source,
123 - GSourceFunc callback,
124 - gpointer user_data)
125 -
126 -{
127 - SdSource *sd_source = (SdSource *)source;
128 - gboolean ret;
129 -
130 - g_warn_if_fail (callback != NULL);
131 - ret = (*callback) (user_data);
132 - sd_login_monitor_flush (sd_source->monitor);
133 - return ret;
134 -}
135 -
136 -static void
137 -sd_source_finalize (GSource *source)
138 -{
139 - SdSource *sd_source = (SdSource*) source;
140 -
141 - sd_login_monitor_unref (sd_source->monitor);
142 -}
143 -
144 -static GSourceFuncs sd_source_funcs = {
145 - sd_source_prepare,
146 - sd_source_check,
147 - sd_source_dispatch,
148 - sd_source_finalize
149 -};
150 -
151 -static GSource *
152 -sd_source_new (void)
153 -{
154 - GSource *source;
155 - SdSource *sd_source;
156 - int ret;
157 -
158 - source = g_source_new (&sd_source_funcs, sizeof (SdSource));
159 - sd_source = (SdSource *)source;
160 -
161 - ret = sd_login_monitor_new (NULL, &sd_source->monitor);
162 - if (ret < 0)
163 - g_printerr ("Error getting login monitor: %d", ret);
164 - else {
165 - sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
166 - sd_source->pollfd.events = G_IO_IN;
167 - g_source_add_poll (source, &sd_source->pollfd);
168 - }
169 -
170 - return source;
171 -}
172 -
173 -struct _NMSessionMonitor {
174 - GObject parent_instance;
175 -
176 - GSource *sd_source;
177 -};
178 -
179 -struct _NMSessionMonitorClass {
180 - GObjectClass parent_class;
181 -
182 - void (*changed) (NMSessionMonitor *monitor);
183 -};
184 -
185 -
186 -enum {
187 - CHANGED_SIGNAL,
188 - LAST_SIGNAL,
189 -};
190 -static guint signals[LAST_SIGNAL] = {0};
191 -
192 -G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
193 -
194 -/* ---------------------------------------------------------------------------------------------------- */
195 -
196 -static gboolean
197 -sessions_changed (gpointer user_data)
198 -{
199 - NMSessionMonitor *monitor = NM_SESSION_MONITOR (user_data);
200 -
201 - g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
202 - return TRUE;
203 -}
204 -
205 -
206 -static void
207 -nm_session_monitor_init (NMSessionMonitor *monitor)
208 -{
209 - monitor->sd_source = sd_source_new ();
210 - g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
211 - g_source_attach (monitor->sd_source, NULL);
212 -}
213 -
214 -static void
215 -nm_session_monitor_finalize (GObject *object)
216 -{
217 - NMSessionMonitor *monitor = NM_SESSION_MONITOR (object);
218 -
219 - if (monitor->sd_source != NULL) {
220 - g_source_destroy (monitor->sd_source);
221 - g_source_unref (monitor->sd_source);
222 - }
223 -
224 - if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
225 - G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
226 -}
227 -
228 -static void
229 -nm_session_monitor_class_init (NMSessionMonitorClass *klass)
230 -{
231 - GObjectClass *gobject_class;
232 -
233 - gobject_class = G_OBJECT_CLASS (klass);
234 - gobject_class->finalize = nm_session_monitor_finalize;
235 -
236 - /**
237 - * NMSessionMonitor::changed:
238 - * @monitor: A #NMSessionMonitor
239 - *
240 - * Emitted when something changes.
241 - */
242 - signals[CHANGED_SIGNAL] = g_signal_new ("changed",
243 - NM_TYPE_SESSION_MONITOR,
244 - G_SIGNAL_RUN_LAST,
245 - G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
246 - NULL, /* accumulator */
247 - NULL, /* accumulator data */
248 - g_cclosure_marshal_VOID__VOID,
249 - G_TYPE_NONE,
250 - 0);
251 -}
252 -
253 -NMSessionMonitor *
254 -nm_session_monitor_get (void)
255 -{
256 - static NMSessionMonitor *singleton = NULL;
257 -
258 - if (singleton)
259 - return g_object_ref (singleton);
260 -
261 - singleton = NM_SESSION_MONITOR (g_object_new (NM_TYPE_SESSION_MONITOR, NULL));
262 - g_assert (singleton);
263 - return singleton;
264 -}
265 -
266 -gboolean
267 -nm_session_monitor_user_has_session (NMSessionMonitor *monitor,
268 - const char *username,
269 - uid_t *out_uid,
270 - GError **error)
271 -{
272 - uid_t uid;
273 -
274 - if (!nm_session_user_to_uid (username, &uid, error))
275 - return FALSE;
276 -
277 - if (out_uid)
278 - *out_uid = uid;
279 -
280 - return nm_session_monitor_uid_has_session (monitor, uid, NULL, error);
281 -}
282 -
283 -gboolean
284 -nm_session_monitor_user_active (NMSessionMonitor *monitor,
285 - const char *username,
286 - GError **error)
287 -{
288 - uid_t uid;
289 -
290 - if (!nm_session_user_to_uid (username, &uid, error))
291 - return FALSE;
292 -
293 - return nm_session_monitor_uid_active (monitor, uid, error);
294 -}
295 -
296 -gboolean
297 -nm_session_monitor_uid_has_session (NMSessionMonitor *monitor,
298 - uid_t uid,
299 - const char **out_user,
300 - GError **error)
301 -{
302 - int ret;
303 -
304 - if (!nm_session_uid_to_user (uid, out_user, error))
305 - return FALSE;
306 -
307 - ret = sd_uid_get_sessions (uid, FALSE, NULL) > 0;
308 - if (ret < 0) {
309 - nm_log_warn (LOGD_CORE, "Failed to get systemd sessions for uid %d: %d",
310 - uid, ret);
311 - return FALSE;
312 - }
313 - return ret > 0 ? TRUE : FALSE;
314 -}
315 -
316 -gboolean
317 -nm_session_monitor_uid_active (NMSessionMonitor *monitor,
318 - uid_t uid,
319 - GError **error)
320 -{
321 - int ret;
322 -
323 - ret = sd_uid_get_sessions (uid, TRUE, NULL) > 0;
324 - if (ret < 0) {
325 - nm_log_warn (LOGD_CORE, "Failed to get active systemd sessions for uid %d: %d",
326 - uid, ret);
327 - return FALSE;
328 - }
329 - return ret > 0 ? TRUE : FALSE;
330 -}
331 diff --git a/src/nm-session-monitor.c b/src/nm-session-monitor.c
332 new file mode 100644
333 index 0000000..26e66ce
334 --- /dev/null
335 +++ b/src/nm-session-monitor.c
336 @@ -0,0 +1,641 @@
337 +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
338 +/*
339 + * Copyright (C) 2011 Red Hat, Inc.
340 + *
341 + * This library is free software; you can redistribute it and/or
342 + * modify it under the terms of the GNU Lesser General Public
343 + * License as published by the Free Software Foundation; either
344 + * version 2 of the License, or (at your option) any later version.
345 + *
346 + * This library is distributed in the hope that it will be useful,
347 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
348 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
349 + * Lesser General Public License for more details.
350 + *
351 + * You should have received a copy of the GNU Lesser General
352 + * Public License along with this library; if not, write to the
353 + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
354 + * Boston, MA 02110-1301, USA.
355 + *
356 + * Author: Matthias Clasen
357 + */
358 +
359 +#include "config.h"
360 +#include <errno.h>
361 +#include <pwd.h>
362 +#include <grp.h>
363 +#include <string.h>
364 +#include <sys/stat.h>
365 +#include <glib/gstdio.h>
366 +#include <gio/gio.h>
367 +#ifdef SESSION_TRACKING_SYSTEMD
368 +#include <systemd/sd-login.h>
369 +#endif
370 +#include <stdlib.h>
371 +
372 +#include "nm-session-utils.h"
373 +#include "nm-session-monitor.h"
374 +#include "nm-logging.h"
375 +
376 +#define CKDB_PATH "/var/run/ConsoleKit/database"
377 +
378 +/********************************************************************/
379 +
380 +#ifdef SESSION_TRACKING_SYSTEMD
381 +typedef struct {
382 + GSource source;
383 + GPollFD pollfd;
384 + sd_login_monitor *monitor;
385 +} SdSource;
386 +
387 +static gboolean
388 +sd_source_prepare (GSource *source, gint *timeout)
389 +{
390 + *timeout = -1;
391 + return FALSE;
392 +}
393 +
394 +static gboolean
395 +sd_source_check (GSource *source)
396 +{
397 + SdSource *sd_source = (SdSource *) source;
398 +
399 + return sd_source->pollfd.revents != 0;
400 +}
401 +
402 +static gboolean
403 +sd_source_dispatch (GSource *source,
404 + GSourceFunc callback,
405 + gpointer user_data)
406 +
407 +{
408 + SdSource *sd_source = (SdSource *)source;
409 + gboolean ret;
410 +
411 + g_warn_if_fail (callback != NULL);
412 + ret = (*callback) (user_data);
413 + sd_login_monitor_flush (sd_source->monitor);
414 + return ret;
415 +}
416 +
417 +static void
418 +sd_source_finalize (GSource *source)
419 +{
420 + SdSource *sd_source = (SdSource*) source;
421 +
422 + sd_login_monitor_unref (sd_source->monitor);
423 +}
424 +
425 +static GSourceFuncs sd_source_funcs = {
426 + sd_source_prepare,
427 + sd_source_check,
428 + sd_source_dispatch,
429 + sd_source_finalize
430 +};
431 +
432 +static GSource *
433 +sd_source_new (void)
434 +{
435 + GSource *source;
436 + SdSource *sd_source;
437 + int ret;
438 +
439 + source = g_source_new (&sd_source_funcs, sizeof (SdSource));
440 + sd_source = (SdSource *)source;
441 +
442 + ret = sd_login_monitor_new (NULL, &sd_source->monitor);
443 + if (ret < 0)
444 + g_printerr ("Error getting login monitor: %d", ret);
445 + else {
446 + sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
447 + sd_source->pollfd.events = G_IO_IN;
448 + g_source_add_poll (source, &sd_source->pollfd);
449 + }
450 +
451 + return source;
452 +}
453 +#endif /* SESSION_TRACKING_SYSTEMD */
454 +
455 +struct _NMSessionMonitor {
456 + GObject parent_instance;
457 +
458 + GKeyFile *database;
459 + GFileMonitor *database_monitor;
460 + time_t database_mtime;
461 + GHashTable *sessions_by_uid;
462 + GHashTable *sessions_by_user;
463 +
464 + GSource *sd_source;
465 +};
466 +
467 +struct _NMSessionMonitorClass {
468 + GObjectClass parent_class;
469 +
470 + void (*changed) (NMSessionMonitor *monitor);
471 +};
472 +
473 +
474 +enum {
475 + CHANGED_SIGNAL,
476 + LAST_SIGNAL,
477 +};
478 +static guint signals[LAST_SIGNAL] = {0};
479 +
480 +G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
481 +
482 +/* ---------------------------------------------------------------------------------------------------- */
483 +
484 +typedef struct {
485 + char *user;
486 + uid_t uid;
487 + gboolean local;
488 + gboolean active;
489 +} Session;
490 +
491 +static void
492 +session_free (Session *s)
493 +{
494 + g_free (s->user);
495 + memset (s, 0, sizeof (Session));
496 + g_free (s);
497 +}
498 +
499 +static gboolean
500 +check_key (GKeyFile *keyfile, const char *group, const char *key, GError **error)
501 +{
502 + if (g_key_file_has_key (keyfile, group, key, error))
503 + return TRUE;
504 +
505 + if (!error) {
506 + g_set_error (error,
507 + NM_SESSION_MONITOR_ERROR,
508 + NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE,
509 + "ConsoleKit database " CKDB_PATH " group '%s' had no '%s' key",
510 + group, key);
511 + }
512 + return FALSE;
513 +}
514 +
515 +static Session *
516 +session_new (GKeyFile *keyfile, const char *group, GError **error)
517 +{
518 + GError *local = NULL;
519 + Session *s;
520 + const char *uname = NULL;
521 +
522 + s = g_new0 (Session, 1);
523 + g_assert (s);
524 +
525 + s->uid = G_MAXUINT; /* paranoia */
526 + if (!check_key (keyfile, group, "uid", &local))
527 + goto error;
528 + s->uid = (uid_t) g_key_file_get_integer (keyfile, group, "uid", &local);
529 + if (local)
530 + goto error;
531 +
532 + if (!check_key (keyfile, group, "is_active", &local))
533 + goto error;
534 + s->active = g_key_file_get_boolean (keyfile, group, "is_active", &local);
535 + if (local)
536 + goto error;
537 +
538 + if (!check_key (keyfile, group, "is_local", &local))
539 + goto error;
540 + s->local = g_key_file_get_boolean (keyfile, group, "is_local", &local);
541 + if (local)
542 + goto error;
543 +
544 + if (!nm_session_uid_to_user (s->uid, &uname, error))
545 + return FALSE;
546 + s->user = g_strdup (uname);
547 +
548 + return s;
549 +
550 +error:
551 + session_free (s);
552 + g_propagate_error (error, local);
553 + return NULL;
554 +}
555 +
556 +static void
557 +session_merge (Session *src, Session *dest)
558 +{
559 + g_return_if_fail (src != NULL);
560 + g_return_if_fail (dest != NULL);
561 +
562 + g_warn_if_fail (g_strcmp0 (src->user, dest->user) == 0);
563 + g_warn_if_fail (src->uid == dest->uid);
564 +
565 + dest->local = (dest->local || src->local);
566 + dest->active = (dest->active || src->active);
567 +}
568 +
569 +static void
570 +free_database (NMSessionMonitor *self)
571 +{
572 + if (self->database != NULL) {
573 + g_key_file_free (self->database);
574 + self->database = NULL;
575 + }
576 +
577 + g_hash_table_remove_all (self->sessions_by_uid);
578 + g_hash_table_remove_all (self->sessions_by_user);
579 +}
580 +
581 +static gboolean
582 +reload_database (NMSessionMonitor *self, GError **error)
583 +{
584 + struct stat statbuf;
585 + char **groups = NULL;
586 + gsize len = 0, i;
587 + Session *s;
588 +
589 + free_database (self);
590 +
591 + errno = 0;
592 + if (stat (CKDB_PATH, &statbuf) != 0) {
593 + g_set_error (error,
594 + NM_SESSION_MONITOR_ERROR,
595 + errno == ENOENT ? NM_SESSION_MONITOR_ERROR_NO_DATABASE : NM_SESSION_MONITOR_ERROR_IO_ERROR,
596 + "Error statting file " CKDB_PATH ": %s",
597 + strerror (errno));
598 + goto error;
599 + }
600 + self->database_mtime = statbuf.st_mtime;
601 +
602 + self->database = g_key_file_new ();
603 + if (!g_key_file_load_from_file (self->database, CKDB_PATH, G_KEY_FILE_NONE, error))
604 + goto error;
605 +
606 + groups = g_key_file_get_groups (self->database, &len);
607 + if (!groups) {
608 + g_set_error_literal (error,
609 + NM_SESSION_MONITOR_ERROR,
610 + NM_SESSION_MONITOR_ERROR_IO_ERROR,
611 + "Could not load groups from " CKDB_PATH "");
612 + goto error;
613 + }
614 +
615 + for (i = 0; i < len; i++) {
616 + Session *found;
617 +
618 + if (!g_str_has_prefix (groups[i], "Session "))
619 + continue;
620 +
621 + s = session_new (self->database, groups[i], error);
622 + if (!s)
623 + goto error;
624 +
625 + found = g_hash_table_lookup (self->sessions_by_user, (gpointer) s->user);
626 + if (found) {
627 + session_merge (s, found);
628 + session_free (s);
629 + } else {
630 + /* Entirely new user */
631 + g_hash_table_insert (self->sessions_by_user, (gpointer) s->user, s);
632 + g_hash_table_insert (self->sessions_by_uid, GUINT_TO_POINTER (s->uid), s);
633 + }
634 + }
635 +
636 + g_strfreev (groups);
637 + return TRUE;
638 +
639 +error:
640 + if (groups)
641 + g_strfreev (groups);
642 + free_database (self);
643 + return FALSE;
644 +}
645 +
646 +static gboolean
647 +ensure_database (NMSessionMonitor *self, GError **error)
648 +{
649 + gboolean ret = FALSE;
650 +
651 + if (self->database != NULL) {
652 + struct stat statbuf;
653 +
654 + errno = 0;
655 + if (stat (CKDB_PATH, &statbuf) != 0) {
656 + g_set_error (error,
657 + NM_SESSION_MONITOR_ERROR,
658 + errno == ENOENT ? NM_SESSION_MONITOR_ERROR_NO_DATABASE : NM_SESSION_MONITOR_ERROR_IO_ERROR,
659 + "Error statting file " CKDB_PATH " to check timestamp: %s",
660 + strerror (errno));
661 + goto out;
662 + }
663 +
664 + if (statbuf.st_mtime == self->database_mtime) {
665 + ret = TRUE;
666 + goto out;
667 + }
668 + }
669 +
670 + ret = reload_database (self, error);
671 +
672 +out:
673 + return ret;
674 +}
675 +
676 +static void
677 +on_file_monitor_changed (GFileMonitor * file_monitor,
678 + GFile * file,
679 + GFile * other_file,
680 + GFileMonitorEvent event_type,
681 + gpointer user_data)
682 +{
683 + NMSessionMonitor *self = NM_SESSION_MONITOR (user_data);
684 +
685 + /* throw away cache */
686 + free_database (self);
687 +
688 + g_signal_emit (self, signals[CHANGED_SIGNAL], 0);
689 +}
690 +
691 +#ifdef SESSION_TRACKING_SYSTEMD
692 +static gboolean
693 +sessions_changed (gpointer user_data)
694 +{
695 + NMSessionMonitor *monitor = NM_SESSION_MONITOR (user_data);
696 +
697 + g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
698 + return TRUE;
699 +}
700 +#endif /* SESSION_TRACKING_SYSTEMD */
701 +
702 +
703 +static void
704 +nm_session_monitor_init (NMSessionMonitor *monitor)
705 +{
706 + GError *error;
707 + GFile *file;
708 +
709 + monitor->sd_source = NULL;
710 + monitor->database = NULL;
711 + monitor->database_monitor = NULL;
712 + monitor->sessions_by_uid = NULL;
713 + monitor->sessions_by_user = NULL;
714 +
715 +#ifdef SESSION_TRACKING_SYSTEMD
716 + if (LOGIND_RUNNING())
717 + {
718 + monitor->sd_source = sd_source_new ();
719 + g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
720 + g_source_attach (monitor->sd_source, NULL);
721 + return;
722 + }
723 + /* fall through */
724 +#endif /* SESSION_TRACKING_SYSTEMD */
725 +
726 + error = NULL;
727 +
728 + /* Sessions-by-user is responsible for destroying the Session objects */
729 + monitor->sessions_by_user = g_hash_table_new_full (g_str_hash, g_str_equal,
730 + NULL, (GDestroyNotify) session_free);
731 + monitor->sessions_by_uid = g_hash_table_new (g_direct_hash, g_direct_equal);
732 +
733 +
734 + error = NULL;
735 + if (!ensure_database (monitor, &error)) {
736 + /* Ignore the first error if the CK database isn't found yet */
737 + if (g_error_matches (error,
738 + NM_SESSION_MONITOR_ERROR,
739 + NM_SESSION_MONITOR_ERROR_NO_DATABASE) == FALSE) {
740 + nm_log_err (LOGD_CORE, "Error loading " CKDB_PATH ": %s", error->message);
741 + }
742 + g_error_free (error);
743 + }
744 +
745 + error = NULL;
746 + file = g_file_new_for_path (CKDB_PATH);
747 + monitor->database_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
748 + g_object_unref (file);
749 + if (monitor->database_monitor == NULL) {
750 + nm_log_err (LOGD_CORE, "Error monitoring " CKDB_PATH ": %s", error->message);
751 + g_error_free (error);
752 + } else {
753 + g_signal_connect (monitor->database_monitor,
754 + "changed",
755 + G_CALLBACK (on_file_monitor_changed),
756 + monitor);
757 + }
758 +}
759 +
760 +static void
761 +nm_session_monitor_finalize (GObject *object)
762 +{
763 + NMSessionMonitor *monitor = NM_SESSION_MONITOR (object);
764 +
765 + if (monitor->sd_source != NULL) {
766 + g_source_destroy (monitor->sd_source);
767 + g_source_unref (monitor->sd_source);
768 + }
769 +
770 + if (monitor->database_monitor != NULL)
771 + g_object_unref (monitor->database_monitor);
772 +
773 + free_database (monitor);
774 +
775 + if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
776 + G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
777 +}
778 +
779 +static void
780 +nm_session_monitor_class_init (NMSessionMonitorClass *klass)
781 +{
782 + GObjectClass *gobject_class;
783 +
784 + gobject_class = G_OBJECT_CLASS (klass);
785 + gobject_class->finalize = nm_session_monitor_finalize;
786 +
787 + /**
788 + * NMSessionMonitor::changed:
789 + * @monitor: A #NMSessionMonitor
790 + *
791 + * Emitted when something changes.
792 + */
793 + signals[CHANGED_SIGNAL] = g_signal_new (NM_SESSION_MONITOR_CHANGED,
794 + NM_TYPE_SESSION_MONITOR,
795 + G_SIGNAL_RUN_LAST,
796 + G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
797 + NULL, /* accumulator */
798 + NULL, /* accumulator data */
799 + g_cclosure_marshal_VOID__VOID,
800 + G_TYPE_NONE,
801 + 0);
802 +}
803 +
804 +NMSessionMonitor *
805 +nm_session_monitor_get (void)
806 +{
807 + static NMSessionMonitor *singleton = NULL;
808 +
809 + if (singleton)
810 + return g_object_ref (singleton);
811 +
812 + singleton = NM_SESSION_MONITOR (g_object_new (NM_TYPE_SESSION_MONITOR, NULL));
813 + g_assert (singleton);
814 + return singleton;
815 +}
816 +
817 +gboolean
818 +nm_session_monitor_user_has_session (NMSessionMonitor *monitor,
819 + const char *username,
820 + uid_t *out_uid,
821 + GError **error)
822 +{
823 +#ifdef SESSION_TRACKING_SYSTEMD
824 + if (LOGIND_RUNNING())
825 + {
826 + uid_t uid;
827 +
828 + if (!nm_session_user_to_uid (username, &uid, error))
829 + return FALSE;
830 +
831 + if (out_uid)
832 + *out_uid = uid;
833 +
834 + return nm_session_monitor_uid_has_session (monitor, uid, NULL, error);
835 + }
836 + /* fall through */
837 +#endif /* SESSION_TRACKING_SYSTEMD */
838 +
839 + Session *s;
840 +
841 + if (!ensure_database (monitor, error))
842 + return FALSE;
843 +
844 + s = g_hash_table_lookup (monitor->sessions_by_user, (gpointer) username);
845 + if (!s) {
846 + g_set_error (error,
847 + NM_SESSION_MONITOR_ERROR,
848 + NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
849 + "No session found for user '%s'",
850 + username);
851 + return FALSE;
852 + }
853 +
854 + if (out_uid)
855 + *out_uid = s->uid;
856 + return TRUE;
857 +}
858 +
859 +gboolean
860 +nm_session_monitor_user_active (NMSessionMonitor *monitor,
861 + const char *username,
862 + GError **error)
863 +{
864 +#ifdef SESSION_TRACKING_SYSTEMD
865 + if (LOGIND_RUNNING())
866 + {
867 + uid_t uid;
868 +
869 + if (!nm_session_user_to_uid (username, &uid, error))
870 + return FALSE;
871 +
872 + return nm_session_monitor_uid_active (monitor, uid, error);
873 + }
874 + /* fall through */
875 +#endif
876 +
877 + Session *s;
878 +
879 + if (!ensure_database (monitor, error))
880 + return FALSE;
881 +
882 + s = g_hash_table_lookup (monitor->sessions_by_user, (gpointer) username);
883 + if (!s) {
884 + g_set_error (error,
885 + NM_SESSION_MONITOR_ERROR,
886 + NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
887 + "No session found for user '%s'",
888 + username);
889 + return FALSE;
890 + }
891 +
892 + return s->active;
893 +}
894 +
895 +gboolean
896 +nm_session_monitor_uid_has_session (NMSessionMonitor *monitor,
897 + uid_t uid,
898 + const char **out_user,
899 + GError **error)
900 +{
901 +#ifdef SESSION_TRACKING_SYSTEMD
902 + if (LOGIND_RUNNING())
903 + {
904 + int ret;
905 +
906 + if (!nm_session_uid_to_user (uid, out_user, error))
907 + return FALSE;
908 +
909 + ret = sd_uid_get_sessions (uid, FALSE, NULL) > 0;
910 + if (ret < 0) {
911 + nm_log_warn (LOGD_CORE, "Failed to get systemd sessions for uid %d: %d",
912 + uid, ret);
913 + return FALSE;
914 + }
915 + return ret > 0 ? TRUE : FALSE;
916 + }
917 + /* fall through */
918 +#endif
919 +
920 + Session *s;
921 +
922 + if (!ensure_database (monitor, error))
923 + return FALSE;
924 +
925 + s = g_hash_table_lookup (monitor->sessions_by_uid, GUINT_TO_POINTER (uid));
926 + if (!s) {
927 + g_set_error (error,
928 + NM_SESSION_MONITOR_ERROR,
929 + NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
930 + "No session found for uid %d",
931 + uid);
932 + return FALSE;
933 + }
934 +
935 + if (out_user)
936 + *out_user = s->user;
937 + return TRUE;
938 +}
939 +
940 +gboolean
941 +nm_session_monitor_uid_active (NMSessionMonitor *monitor,
942 + uid_t uid,
943 + GError **error)
944 +{
945 +#ifdef SESSION_TRACKING_SYSTEMD
946 + if (LOGIND_RUNNING())
947 + {
948 + int ret;
949 +
950 + ret = sd_uid_get_sessions (uid, TRUE, NULL) > 0;
951 + if (ret < 0) {
952 + nm_log_warn (LOGD_CORE, "Failed to get active systemd sessions for uid %d: %d",
953 + uid, ret);
954 + return FALSE;
955 + }
956 + return ret > 0 ? TRUE : FALSE;
957 + }
958 + /* fall through */
959 +#endif
960 +
961 + Session *s;
962 +
963 + if (!ensure_database (monitor, error))
964 + return FALSE;
965 +
966 + s = g_hash_table_lookup (monitor->sessions_by_uid, GUINT_TO_POINTER (uid));
967 + if (!s) {
968 + g_set_error (error,
969 + NM_SESSION_MONITOR_ERROR,
970 + NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
971 + "No session found for uid '%d'",
972 + uid);
973 + return FALSE;
974 + }
975 +
976 + return s->active;
977 +}
978 diff --git a/src/nm-session-monitor.h b/src/nm-session-monitor.h
979 index 77ea9a0..ac3dec5 100644
980 --- a/src/nm-session-monitor.h
981 +++ b/src/nm-session-monitor.h
982 @@ -21,6 +21,7 @@
983 #ifndef NM_SESSION_MONITOR_H
984 #define NM_SESSION_MONITOR_H
985
986 +#include <unistd.h>
987 #include <glib-object.h>
988
989 G_BEGIN_DECLS
990 @@ -34,6 +35,11 @@ G_BEGIN_DECLS
991
992 #define NM_SESSION_MONITOR_CHANGED "changed"
993
994 +/* check if logind is running
995 + * thanks to: https://bugzilla.gnome.org/show_bug.cgi?id=696266
996 + */
997 +#define LOGIND_RUNNING() (access("/run/systemd/seats/", F_OK) >= 0)
998 +
999 typedef struct _NMSessionMonitor NMSessionMonitor;
1000 typedef struct _NMSessionMonitorClass NMSessionMonitorClass;
1001
1002 --
1003 1.8.2.1

  ViewVC Help
Powered by ViewVC 1.1.20