1 diff -ruN polkit-0.113.orig/configure.ac polkit-0.113/configure.ac
2 --- polkit-0.113.orig/configure.ac 2015-06-19 22:31:02.000000000 +0200
3 +++ polkit-0.113/configure.ac 2015-09-26 23:40:21.669982142 +0200
6 dnl if libsystemd is not available, fall back to the older libsystemd-login
7 [PKG_CHECK_MODULES([LIBSYSTEMD_LOGIN],
9 + [libsystemd-login libsystemd-daemon],
12 LIBSYSTEMD_CFLAGS="$LIBSYSTEMD_LOGIN_CFLAGS"
13 diff -ruN polkit-0.113.orig/src/polkit/Makefile.am polkit-0.113/src/polkit/Makefile.am
14 --- polkit-0.113.orig/src/polkit/Makefile.am 2015-06-19 22:31:02.000000000 +0200
15 +++ polkit-0.113/src/polkit/Makefile.am 2015-09-26 23:40:21.669982142 +0200
17 polkitimplicitauthorization.c polkitimplicitauthorization.h \
18 polkittemporaryauthorization.c polkittemporaryauthorization.h \
19 polkitpermission.c polkitpermission.h \
23 -libpolkit_gobject_1_la_SOURCES += \
24 - polkitunixsession-systemd.c polkitunixsession.h
26 -libpolkit_gobject_1_la_SOURCES += \
27 polkitunixsession.c polkitunixsession.h
30 libpolkit_gobject_1_la_CFLAGS = \
31 -D_POLKIT_COMPILATION \
32 --- polkit-0.114/src/polkit/polkitunixsession.c.orig 2018-03-23 16:09:30.000000000 +0100
33 +++ polkit-0.114/src/polkit/polkitunixsession.c 2018-04-12 19:03:50.775644785 +0200
35 #include "polkiterror.h"
36 #include "polkitprivate.h"
38 +#ifdef HAVE_LIBSYSTEMD
40 +# include <systemd/sd-daemon.h>
41 +# include <systemd/sd-login.h>
45 * SECTION:polkitunixsession
46 * @title: PolkitUnixSession
47 @@ -363,35 +369,49 @@ polkit_unix_session_exists_sync (PolkitS
49 PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
50 GDBusConnection *connection;
52 + GVariant *result = NULL;
58 - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
59 - if (connection == NULL)
61 +#ifdef HAVE_LIBSYSTEMD
64 - result = g_dbus_connection_call_sync (connection,
65 - "org.freedesktop.ConsoleKit", /* name */
66 - session->session_id, /* object path */
67 - "org.freedesktop.ConsoleKit.Session", /* interface name */
68 - "GetUser", /* method */
69 - NULL, /* parameters */
70 - G_VARIANT_TYPE ("(u)"),
71 - G_DBUS_CALL_FLAGS_NONE,
77 + if (sd_booted () > 0)
79 + if (sd_session_get_uid (session->session_id, &uid) == 0)
83 +#endif /* HAVE_LIBSYSTEMD */
85 + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
86 + if (connection == NULL)
89 + result = g_dbus_connection_call_sync (connection,
90 + "org.freedesktop.ConsoleKit", /* name */
91 + session->session_id, /* object path */
92 + "org.freedesktop.ConsoleKit.Session", /* interface name */
93 + "GetUser", /* method */
94 + NULL, /* parameters */
95 + G_VARIANT_TYPE ("(u)"),
96 + G_DBUS_CALL_FLAGS_NONE,
100 + if (result == NULL)
104 - g_variant_unref (result);
106 + g_variant_unref (result);
110 if (connection != NULL)
111 g_object_unref (connection);
116 @@ -474,6 +494,7 @@ polkit_unix_session_initable_init (GInit
124 @@ -484,28 +505,65 @@ polkit_unix_session_initable_init (GInit
128 - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
129 - if (connection == NULL)
131 +#ifdef HAVE_LIBSYSTEMD
135 + if (sd_booted () > 0)
137 + if (sd_pid_get_session (session->pid, &s) == 0)
139 + session->session_id = g_strdup (s);
145 + /* Now do process -> uid -> graphical session (systemd version 213)*/
146 + if (sd_pid_get_owner_uid (session->pid, &uid) < 0)
149 - result = g_dbus_connection_call_sync (connection,
150 - "org.freedesktop.ConsoleKit", /* name */
151 - "/org/freedesktop/ConsoleKit/Manager", /* object path */
152 - "org.freedesktop.ConsoleKit.Manager", /* interface name */
153 - "GetSessionForUnixProcess", /* method */
154 - g_variant_new ("(u)", session->pid), /* parameters */
155 - G_VARIANT_TYPE ("(o)"),
156 - G_DBUS_CALL_FLAGS_NONE,
160 - if (result == NULL)
162 + if (sd_uid_get_display (uid, &s) >= 0)
164 + session->session_id = g_strdup (s);
170 - g_variant_get (result, "(o)", &session->session_id);
171 - g_variant_unref (result);
173 + g_set_error (error,
175 + POLKIT_ERROR_FAILED,
176 + "No session for pid %d",
177 + (gint) session->pid);
180 +#endif /* HAVE_LIBSYSTEMD */
182 + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
183 + if (connection == NULL)
186 + result = g_dbus_connection_call_sync (connection,
187 + "org.freedesktop.ConsoleKit", /* name */
188 + "/org/freedesktop/ConsoleKit/Manager", /* object path */
189 + "org.freedesktop.ConsoleKit.Manager", /* interface name */
190 + "GetSessionForUnixProcess", /* method */
191 + g_variant_new ("(u)", session->pid), /* parameters */
192 + G_VARIANT_TYPE ("(o)"),
193 + G_DBUS_CALL_FLAGS_NONE,
197 + if (result == NULL)
201 + g_variant_get (result, "(o)", &session->session_id);
202 + g_variant_unref (result);
208 if (connection != NULL)
209 --- polkit-0.114/src/polkit/polkitunixsession-systemd.c.orig 2018-04-12 18:55:30.995650493 +0200
210 +++ polkit-0.114/src/polkit/polkitunixsession-systemd.c 1970-01-01 01:00:00.000000000 +0100
213 - * Copyright (C) 2011 Red Hat, Inc.
215 - * This library is free software; you can redistribute it and/or
216 - * modify it under the terms of the GNU Lesser General Public
217 - * License as published by the Free Software Foundation; either
218 - * version 2 of the License, or (at your option) any later version.
220 - * This library is distributed in the hope that it will be useful,
221 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
222 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
223 - * Lesser General Public License for more details.
225 - * You should have received a copy of the GNU Lesser General
226 - * Public License along with this library; if not, write to the
227 - * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
228 - * Boston, MA 02111-1307, USA.
230 - * Author: Matthias Clasen
233 -#ifdef HAVE_CONFIG_H
234 -# include "config.h"
239 -#include "polkitunixsession.h"
240 -#include "polkitsubject.h"
241 -#include "polkiterror.h"
242 -#include "polkitprivate.h"
244 -#include <systemd/sd-login.h>
247 - * SECTION:polkitunixsession
248 - * @title: PolkitUnixSession
249 - * @short_description: Unix sessions
251 - * An object that represents an user session.
253 - * The session id is an opaque string obtained from ConsoleKit.
257 - * PolkitUnixSession:
259 - * The #PolkitUnixSession struct should not be accessed directly.
261 -struct _PolkitUnixSession
263 - GObject parent_instance;
270 -struct _PolkitUnixSessionClass
272 - GObjectClass parent_class;
282 -static void subject_iface_init (PolkitSubjectIface *subject_iface);
283 -static void initable_iface_init (GInitableIface *initable_iface);
284 -static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
286 -G_DEFINE_TYPE_WITH_CODE (PolkitUnixSession, polkit_unix_session, G_TYPE_OBJECT,
287 - G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init)
288 - G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
289 - G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
293 -polkit_unix_session_init (PolkitUnixSession *session)
298 -polkit_unix_session_finalize (GObject *object)
300 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
302 - g_free (session->session_id);
304 - if (G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize != NULL)
305 - G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize (object);
309 -polkit_unix_session_get_property (GObject *object,
314 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
318 - case PROP_SESSION_ID:
319 - g_value_set_string (value, session->session_id);
323 - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329 -polkit_unix_session_set_property (GObject *object,
331 - const GValue *value,
334 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
338 - case PROP_SESSION_ID:
339 - polkit_unix_session_set_session_id (session, g_value_get_string (value));
343 - session->pid = g_value_get_int (value);
347 - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
353 -polkit_unix_session_class_init (PolkitUnixSessionClass *klass)
355 - GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
357 - gobject_class->finalize = polkit_unix_session_finalize;
358 - gobject_class->get_property = polkit_unix_session_get_property;
359 - gobject_class->set_property = polkit_unix_session_set_property;
362 - * PolkitUnixSession:session-id:
364 - * The UNIX session id.
366 - g_object_class_install_property (gobject_class,
368 - g_param_spec_string ("session-id",
370 - "The UNIX session ID",
372 - G_PARAM_CONSTRUCT |
373 - G_PARAM_READWRITE |
374 - G_PARAM_STATIC_NAME |
375 - G_PARAM_STATIC_BLURB |
376 - G_PARAM_STATIC_NICK));
380 - * PolkitUnixSession:pid:
382 - * The UNIX process id to look up the session.
384 - g_object_class_install_property (gobject_class,
386 - g_param_spec_int ("pid",
388 - "Process ID to use for looking up the session",
392 - G_PARAM_CONSTRUCT_ONLY |
394 - G_PARAM_STATIC_NAME |
395 - G_PARAM_STATIC_BLURB |
396 - G_PARAM_STATIC_NICK));
401 - * polkit_unix_session_get_session_id:
402 - * @session: A #PolkitUnixSession.
404 - * Gets the session id for @session.
406 - * Returns: The session id for @session. Do not free this string, it
407 - * is owned by @session.
410 -polkit_unix_session_get_session_id (PolkitUnixSession *session)
412 - g_return_val_if_fail (POLKIT_IS_UNIX_SESSION (session), NULL);
413 - return session->session_id;
417 - * polkit_unix_session_set_session_id:
418 - * @session: A #PolkitUnixSession.
419 - * @session_id: The session id.
421 - * Sets the session id for @session to @session_id.
424 -polkit_unix_session_set_session_id (PolkitUnixSession *session,
425 - const gchar *session_id)
427 - g_return_if_fail (POLKIT_IS_UNIX_SESSION (session));
428 - /*g_return_if_fail (session_id != NULL);*/
429 - g_free (session->session_id);
430 - session->session_id = g_strdup (session_id);
434 - * polkit_unix_session_new:
435 - * @session_id: The session id.
437 - * Creates a new #PolkitUnixSession for @session_id.
439 - * Returns: (transfer full): A #PolkitUnixSession. Free with g_object_unref().
442 -polkit_unix_session_new (const gchar *session_id)
444 - return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_UNIX_SESSION,
445 - "session-id", session_id,
450 - * polkit_unix_session_new_for_process:
451 - * @pid: The process id of the process to get the session for.
452 - * @cancellable: (allow-none): A #GCancellable or %NULL.
453 - * @callback: A #GAsyncReadyCallback to call when the request is satisfied
454 - * @user_data: The data to pass to @callback.
456 - * Asynchronously creates a new #PolkitUnixSession object for the
457 - * process with process id @pid.
459 - * When the operation is finished, @callback will be invoked in the
460 - * <link linkend="g-main-context-push-thread-default">thread-default
461 - * main loop</link> of the thread you are calling this method
462 - * from. You can then call
463 - * polkit_unix_session_new_for_process_finish() to get the result of
466 - * This method constructs the object asynchronously, for the synchronous and blocking version
467 - * use polkit_unix_session_new_for_process_sync().
470 -polkit_unix_session_new_for_process (gint pid,
471 - GCancellable *cancellable,
472 - GAsyncReadyCallback callback,
473 - gpointer user_data)
475 - g_async_initable_new_async (POLKIT_TYPE_UNIX_SESSION,
476 - G_PRIORITY_DEFAULT,
485 - * polkit_unix_session_new_for_process_finish:
486 - * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to polkit_unix_session_new_for_process().
487 - * @error: (allow-none): Return location for error.
489 - * Finishes constructing a #PolkitSubject for a process id.
491 - * Returns: (transfer full) (allow-none): A #PolkitUnixSession for the @pid passed to
492 - * polkit_unix_session_new_for_process() or %NULL if @error is
493 - * set. Free with g_object_unref().
496 -polkit_unix_session_new_for_process_finish (GAsyncResult *res,
500 - GObject *source_object;
502 - source_object = g_async_result_get_source_object (res);
503 - g_assert (source_object != NULL);
505 - object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
508 - g_object_unref (source_object);
510 - if (object != NULL)
511 - return POLKIT_SUBJECT (object);
518 - * polkit_unix_session_new_for_process_sync:
519 - * @pid: The process id of the process to get the session for.
520 - * @cancellable: (allow-none): A #GCancellable or %NULL.
521 - * @error: (allow-none): Return location for error.
523 - * Creates a new #PolkitUnixSession for the process with process id @pid.
525 - * This is a synchronous call - the calling thread is blocked until a
526 - * reply is received. For the asynchronous version, see
527 - * polkit_unix_session_new_for_process().
529 - * Returns: (allow-none) (transfer full): A #PolkitUnixSession for
530 - * @pid or %NULL if @error is set. Free with g_object_unref().
533 -polkit_unix_session_new_for_process_sync (gint pid,
534 - GCancellable *cancellable,
537 - return POLKIT_SUBJECT (g_initable_new (POLKIT_TYPE_UNIX_SESSION,
545 -polkit_unix_session_hash (PolkitSubject *subject)
547 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
549 - return g_str_hash (session->session_id);
553 -polkit_unix_session_equal (PolkitSubject *a,
556 - PolkitUnixSession *session_a;
557 - PolkitUnixSession *session_b;
559 - session_a = POLKIT_UNIX_SESSION (a);
560 - session_b = POLKIT_UNIX_SESSION (b);
562 - return g_strcmp0 (session_a->session_id, session_b->session_id) == 0;
566 -polkit_unix_session_to_string (PolkitSubject *subject)
568 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
570 - return g_strdup_printf ("unix-session:%s", session->session_id);
574 -polkit_unix_session_exists_sync (PolkitSubject *subject,
575 - GCancellable *cancellable,
578 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
579 - gboolean ret = FALSE;
582 - if (sd_session_get_uid (session->session_id, &uid) == 0)
589 -exists_in_thread_func (GSimpleAsyncResult *res,
591 - GCancellable *cancellable)
595 - if (!polkit_unix_session_exists_sync (POLKIT_SUBJECT (object),
599 - g_simple_async_result_set_from_error (res, error);
600 - g_error_free (error);
605 -polkit_unix_session_exists (PolkitSubject *subject,
606 - GCancellable *cancellable,
607 - GAsyncReadyCallback callback,
608 - gpointer user_data)
610 - GSimpleAsyncResult *simple;
612 - g_return_if_fail (POLKIT_IS_UNIX_SESSION (subject));
614 - simple = g_simple_async_result_new (G_OBJECT (subject),
617 - polkit_unix_session_exists);
618 - g_simple_async_result_run_in_thread (simple,
619 - exists_in_thread_func,
620 - G_PRIORITY_DEFAULT,
622 - g_object_unref (simple);
626 -polkit_unix_session_exists_finish (PolkitSubject *subject,
630 - GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
633 - g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == polkit_unix_session_exists);
637 - if (g_simple_async_result_propagate_error (simple, error))
640 - ret = g_simple_async_result_get_op_res_gboolean (simple);
647 -subject_iface_init (PolkitSubjectIface *subject_iface)
649 - subject_iface->hash = polkit_unix_session_hash;
650 - subject_iface->equal = polkit_unix_session_equal;
651 - subject_iface->to_string = polkit_unix_session_to_string;
652 - subject_iface->exists = polkit_unix_session_exists;
653 - subject_iface->exists_finish = polkit_unix_session_exists_finish;
654 - subject_iface->exists_sync = polkit_unix_session_exists_sync;
658 -polkit_unix_session_initable_init (GInitable *initable,
659 - GCancellable *cancellable,
662 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (initable);
663 - gboolean ret = FALSE;
667 - if (session->session_id != NULL)
669 - /* already set, nothing to do */
674 - if (sd_pid_get_session (session->pid, &s) == 0)
676 - session->session_id = g_strdup (s);
682 - /* Now do process -> uid -> graphical session (systemd version 213)*/
683 - if (sd_pid_get_owner_uid (session->pid, &uid) < 0)
686 - if (sd_uid_get_display (uid, &s) >= 0)
688 - session->session_id = g_strdup (s);
695 - g_set_error (error,
697 - POLKIT_ERROR_FAILED,
698 - "No session for pid %d",
699 - (gint) session->pid);
706 -initable_iface_init (GInitableIface *initable_iface)
708 - initable_iface->init = polkit_unix_session_initable_init;
712 -async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
714 - /* use default implementation to run GInitable code in a thread */
716 diff -ruN polkit-0.113.orig/src/polkitbackend/Makefile.am polkit-0.113/src/polkitbackend/Makefile.am
717 --- polkit-0.113.orig/src/polkitbackend/Makefile.am 2015-06-19 22:31:02.000000000 +0200
718 +++ polkit-0.113/src/polkitbackend/Makefile.am 2015-09-26 23:40:21.674982125 +0200
720 polkitbackendactionpool.h polkitbackendactionpool.c \
721 polkitbackendconfigsource.h polkitbackendconfigsource.c \
722 polkitbackendactionlookup.h polkitbackendactionlookup.c \
726 -libpolkit_backend_1_la_SOURCES += \
727 - polkitbackendsessionmonitor.h polkitbackendsessionmonitor-systemd.c
729 -libpolkit_backend_1_la_SOURCES += \
730 polkitbackendsessionmonitor.h polkitbackendsessionmonitor.c
733 libpolkit_backend_1_la_CFLAGS = \
734 -D_POLKIT_COMPILATION \
735 --- polkit-0.114.orig/src/polkitbackend/polkitbackendjsauthority.cpp 2015-06-19 22:39:58.000000000 +0200
736 +++ polkit-0.114/src/polkitbackend/polkitbackendjsauthority.cpp 2015-09-26 23:40:21.674982125 +0200
738 #include <polkit/polkitprivate.h>
740 #ifdef HAVE_LIBSYSTEMD
741 +#include <systemd/sd-daemon.h>
742 #include <systemd/sd-login.h>
743 #endif /* HAVE_LIBSYSTEMD */
748 #ifdef HAVE_LIBSYSTEMD
749 + if (sd_booted () > 0)
751 if (sd_pid_get_session (pid, &session_str) == 0)
753 if (sd_session_get_seat (session_str, &seat_str) == 0)
759 #endif /* HAVE_LIBSYSTEMD */
761 g_assert (POLKIT_IS_UNIX_USER (user_for_subject));
762 diff -ruN polkit-0.113.orig/src/polkitbackend/polkitbackendsessionmonitor.c polkit-0.113/src/polkitbackend/polkitbackendsessionmonitor.c
763 --- polkit-0.113.orig/src/polkitbackend/polkitbackendsessionmonitor.c 2015-06-06 01:24:06.000000000 +0200
764 +++ polkit-0.113/src/polkitbackend/polkitbackendsessionmonitor.c 2015-09-26 23:40:39.451918791 +0200
767 #include <glib/gstdio.h>
769 +#ifdef HAVE_LIBSYSTEMD
770 +# include <stdlib.h>
771 +# include <systemd/sd-daemon.h>
772 +# include <systemd/sd-login.h>
773 +#endif /* HAVE_LIBSYSTEMD */
775 #include <polkit/polkit.h>
776 #include "polkitbackendsessionmonitor.h"
779 * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
782 +#ifdef HAVE_LIBSYSTEMD
787 + sd_login_monitor *monitor;
791 +sd_source_prepare (GSource *source,
799 +sd_source_check (GSource *source)
801 + SdSource *sd_source = (SdSource *)source;
803 + return sd_source->pollfd.revents != 0;
807 +sd_source_dispatch (GSource *source,
808 + GSourceFunc callback,
809 + gpointer user_data)
812 + SdSource *sd_source = (SdSource *)source;
815 + g_warn_if_fail (callback != NULL);
817 + ret = (*callback) (user_data);
819 + sd_login_monitor_flush (sd_source->monitor);
825 +sd_source_finalize (GSource *source)
827 + SdSource *sd_source = (SdSource*)source;
829 + sd_login_monitor_unref (sd_source->monitor);
832 +static GSourceFuncs sd_source_funcs = {
835 + sd_source_dispatch,
840 +sd_source_new (void)
843 + SdSource *sd_source;
846 + source = g_source_new (&sd_source_funcs, sizeof (SdSource));
847 + sd_source = (SdSource *)source;
849 + if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
851 + g_printerr ("Error getting login monitor: %d", ret);
855 + sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
856 + sd_source->pollfd.events = G_IO_IN;
857 + g_source_add_poll (source, &sd_source->pollfd);
862 +#endif /* HAVE_LIBSYSTEMD */
864 struct _PolkitBackendSessionMonitor
866 GObject parent_instance;
869 GFileMonitor *database_monitor;
870 time_t database_mtime;
872 +#ifdef HAVE_LIBSYSTEMD
873 + GSource *sd_source;
874 +#endif /* HAVE_LIBSYSTEMD */
877 struct _PolkitBackendSessionMonitorClass
880 /* ---------------------------------------------------------------------------------------------------- */
882 +#ifdef HAVE_LIBSYSTEMD
884 +sessions_changed (gpointer user_data)
886 + PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
888 + g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
892 +#endif /* HAVE_LIBSYSTEMD */
895 reload_database (PolkitBackendSessionMonitor *monitor,
897 @@ -176,31 +280,47 @@
898 g_error_free (error);
902 - if (!ensure_database (monitor, &error))
903 +#ifdef HAVE_LIBSYSTEMD
904 + monitor->sd_source = NULL;
906 + if (sd_booted () > 0)
908 - g_printerr ("Error loading " CKDB_PATH ": %s", error->message);
909 - g_error_free (error);
910 + monitor->sd_source = sd_source_new ();
911 + g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
912 + g_source_attach (monitor->sd_source, NULL);
914 + monitor->database_monitor = NULL;
915 + monitor->database = NULL;
918 +#endif /* HAVE_LIBSYSTEMD */
921 + if (!ensure_database (monitor, &error))
923 + g_printerr ("Error loading " CKDB_PATH ": %s", error->message);
924 + g_error_free (error);
928 - file = g_file_new_for_path (CKDB_PATH);
929 - monitor->database_monitor = g_file_monitor_file (file,
931 + file = g_file_new_for_path (CKDB_PATH);
932 + monitor->database_monitor = g_file_monitor_file (file,
936 - g_object_unref (file);
937 - if (monitor->database_monitor == NULL)
939 - g_printerr ("Error monitoring " CKDB_PATH ": %s", error->message);
940 - g_error_free (error);
944 - g_signal_connect (monitor->database_monitor,
946 - G_CALLBACK (on_file_monitor_changed),
948 + g_object_unref (file);
949 + if (monitor->database_monitor == NULL)
951 + g_printerr ("Error monitoring " CKDB_PATH ": %s", error->message);
952 + g_error_free (error);
956 + g_signal_connect (monitor->database_monitor,
958 + G_CALLBACK (on_file_monitor_changed),
965 if (monitor->database != NULL)
966 g_key_file_free (monitor->database);
968 + if (monitor->sd_source != NULL)
970 + g_source_destroy (monitor->sd_source);
971 + g_source_unref (monitor->sd_source);
974 if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL)
975 G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object);
977 @@ -310,22 +436,38 @@
979 else if (POLKIT_IS_UNIX_SESSION (subject))
981 - if (!ensure_database (monitor, error))
982 +#ifdef HAVE_LIBSYSTEMD
983 + if (monitor->sd_source != NULL)
985 - g_prefix_error (error, "Error getting user for session: Error ensuring CK database at " CKDB_PATH ": ");
987 + if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
989 + g_set_error (error,
991 + POLKIT_ERROR_FAILED,
992 + "Error getting uid for session");
997 - group = g_strdup_printf ("Session %s", polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)));
998 - local_error = NULL;
999 - uid = g_key_file_get_integer (monitor->database, group, "uid", &local_error);
1000 - if (local_error != NULL)
1002 +#endif /* HAVE_LIBSYSTEMD */
1004 - g_propagate_prefixed_error (error, local_error, "Error getting uid using " CKDB_PATH ": ");
1005 + if (!ensure_database (monitor, error))
1007 + g_prefix_error (error, "Error getting user for session: Error ensuring CK database at " CKDB_PATH ": ");
1011 + group = g_strdup_printf ("Session %s", polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)));
1012 + local_error = NULL;
1013 + uid = g_key_file_get_integer (monitor->database, group, "uid", &local_error);
1014 + if (local_error != NULL)
1016 + g_propagate_prefixed_error (error, local_error, "Error getting uid using " CKDB_PATH ": ");
1025 ret = polkit_unix_user_new (uid);
1027 @@ -349,35 +491,26 @@
1028 PolkitSubject *subject,
1031 - PolkitSubject *session;
1034 + PolkitSubject *session = NULL;
1037 if (POLKIT_IS_UNIX_PROCESS (subject))
1039 - const gchar *session_id;
1041 - result = g_dbus_connection_call_sync (monitor->system_bus,
1042 - "org.freedesktop.ConsoleKit",
1043 - "/org/freedesktop/ConsoleKit/Manager",
1044 - "org.freedesktop.ConsoleKit.Manager",
1045 - "GetSessionForUnixProcess",
1046 - g_variant_new ("(u)", polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject))),
1047 - G_VARIANT_TYPE ("(o)"),
1048 - G_DBUS_CALL_FLAGS_NONE,
1049 - -1, /* timeout_msec */
1050 - NULL, /* GCancellable */
1052 - if (result == NULL)
1054 - g_variant_get (result, "(&o)", &session_id);
1055 - session = polkit_unix_session_new (session_id);
1056 - g_variant_unref (result);
1057 + { /* We already have a process; now do process -> pid */
1058 + pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject));
1060 else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1063 - const gchar *session_id;
1064 + { /* Convert bus name to process / pid */
1065 +#ifdef HAVE_LIBSYSTEMD
1066 + if (monitor->sd_source != NULL)
1068 + PolkitUnixProcess *process = (PolkitUnixProcess*)polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME(subject), NULL, error);
1071 + pid = polkit_unix_process_get_pid (process);
1072 + g_object_unref (process);
1078 result = g_dbus_connection_call_sync (monitor->system_bus,
1079 @@ -395,23 +528,7 @@
1081 g_variant_get (result, "(u)", &pid);
1082 g_variant_unref (result);
1084 - result = g_dbus_connection_call_sync (monitor->system_bus,
1085 - "org.freedesktop.ConsoleKit",
1086 - "/org/freedesktop/ConsoleKit/Manager",
1087 - "org.freedesktop.ConsoleKit.Manager",
1088 - "GetSessionForUnixProcess",
1089 - g_variant_new ("(u)", pid),
1090 - G_VARIANT_TYPE ("(o)"),
1091 - G_DBUS_CALL_FLAGS_NONE,
1092 - -1, /* timeout_msec */
1093 - NULL, /* GCancellable */
1095 - if (result == NULL)
1097 - g_variant_get (result, "(&o)", &session_id);
1098 - session = polkit_unix_session_new (session_id);
1099 - g_variant_unref (result);
1104 @@ -420,8 +537,57 @@
1105 POLKIT_ERROR_NOT_SUPPORTED,
1106 "Cannot get user for subject of type %s",
1107 g_type_name (G_TYPE_FROM_INSTANCE (subject)));
1111 + /* Now do pid -> same session */
1112 +#ifdef HAVE_LIBSYSTEMD
1113 + if (monitor->sd_source != NULL)
1115 + gchar *session_id;
1116 + if (sd_pid_get_session (pid, &session_id) >= 0)
1118 + session = polkit_unix_session_new (session_id);
1119 + free (session_id);
1122 +#if HAVE_SD_UID_GET_DISPLAY
1124 + /* Now do pid -> uid -> graphical session (systemd version 213)*/
1125 + if (sd_pid_get_owner_uid (pid, &uid) < 0)
1128 + if (sd_uid_get_display (uid, &session_id) >= 0)
1130 + session = polkit_unix_session_new (session_id);
1131 + free (session_id);
1137 +#endif /* HAVE_LIBSYSTEMD */
1139 + gchar *session_id;
1141 + result = g_dbus_connection_call_sync (monitor->system_bus,
1142 + "org.freedesktop.ConsoleKit",
1143 + "/org/freedesktop/ConsoleKit/Manager",
1144 + "org.freedesktop.ConsoleKit.Manager",
1145 + "GetSessionForUnixProcess",
1146 + g_variant_new ("(u)", pid),
1147 + G_VARIANT_TYPE ("(o)"),
1148 + G_DBUS_CALL_FLAGS_NONE,
1149 + -1, /* timeout_msec */
1150 + NULL, /* GCancellable */
1152 + if (result == NULL)
1154 + g_variant_get (result, "(&o)", &session_id);
1155 + session = polkit_unix_session_new (session_id);
1156 + g_variant_unref (result);
1162 @@ -472,7 +638,22 @@
1163 polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
1164 PolkitSubject *session)
1166 - return get_boolean (monitor, session, "is_local");
1167 +#ifdef HAVE_LIBSYSTEMD
1168 + if (monitor->sd_source != NULL)
1172 + if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
1181 +#endif /* HAVE_LIBSYSTEMD */
1182 + return get_boolean (monitor, session, "is_local");
1186 @@ -480,6 +661,44 @@
1187 polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
1188 PolkitSubject *session)
1190 - return get_boolean (monitor, session, "is_active");
1191 +#ifdef HAVE_LIBSYSTEMD
1192 + if (monitor->sd_source != NULL)
1194 + const char *session_id;
1197 + gboolean is_active = FALSE;
1199 + session_id = polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session));
1201 + g_debug ("Checking whether session %s is active.", session_id);
1203 + /* Check whether *any* of the user's current sessions are active. */
1204 + if (sd_session_get_uid (session_id, &uid) < 0)
1207 + g_debug ("Session %s has UID %u.", session_id, uid);
1209 + if (sd_uid_get_state (uid, &state) < 0)
1212 + g_debug ("UID %u has state %s.", uid, state);
1214 + is_active = (g_strcmp0 (state, "active") == 0);
1220 + /* Fall back to checking the session. This is not ideal, since the user
1221 + * might have multiple sessions, and we cannot guarantee to have chosen
1224 + * See: https://bugs.freedesktop.org/show_bug.cgi?id=76358. */
1225 + return sd_session_is_active (session_id);
1228 +#endif /* HAVE_LIBSYSTEMD */
1229 + return get_boolean (monitor, session, "is_active");
1232 diff -ruN polkit-0.113.orig/src/polkitbackend/polkitbackendsessionmonitor-systemd.c polkit-0.113/src/polkitbackend/polkitbackendsessionmonitor-systemd.c
1233 --- polkit-0.113.orig/src/polkitbackend/polkitbackendsessionmonitor-systemd.c 2015-06-19 22:31:02.000000000 +0200
1234 +++ polkit-0.113/src/polkitbackend/polkitbackendsessionmonitor-systemd.c 1970-01-01 01:00:00.000000000 +0100
1237 - * Copyright (C) 2011 Red Hat, Inc.
1239 - * This library is free software; you can redistribute it and/or
1240 - * modify it under the terms of the GNU Lesser General Public
1241 - * License as published by the Free Software Foundation; either
1242 - * version 2 of the License, or (at your option) any later version.
1244 - * This library is distributed in the hope that it will be useful,
1245 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1246 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1247 - * Lesser General Public License for more details.
1249 - * You should have received a copy of the GNU Lesser General
1250 - * Public License along with this library; if not, write to the
1251 - * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1252 - * Boston, MA 02111-1307, USA.
1254 - * Author: Matthias Clasen
1257 -#include "config.h"
1261 -#include <string.h>
1262 -#include <glib/gstdio.h>
1263 -#include <systemd/sd-login.h>
1264 -#include <stdlib.h>
1266 -#include <polkit/polkit.h>
1267 -#include "polkitbackendsessionmonitor.h"
1270 - * SECTION:polkitbackendsessionmonitor
1271 - * @title: PolkitBackendSessionMonitor
1272 - * @short_description: Monitor sessions
1274 - * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
1281 - sd_login_monitor *monitor;
1285 -sd_source_prepare (GSource *source,
1293 -sd_source_check (GSource *source)
1295 - SdSource *sd_source = (SdSource *)source;
1297 - return sd_source->pollfd.revents != 0;
1301 -sd_source_dispatch (GSource *source,
1302 - GSourceFunc callback,
1303 - gpointer user_data)
1306 - SdSource *sd_source = (SdSource *)source;
1309 - g_warn_if_fail (callback != NULL);
1311 - ret = (*callback) (user_data);
1313 - sd_login_monitor_flush (sd_source->monitor);
1319 -sd_source_finalize (GSource *source)
1321 - SdSource *sd_source = (SdSource*)source;
1323 - sd_login_monitor_unref (sd_source->monitor);
1326 -static GSourceFuncs sd_source_funcs = {
1327 - sd_source_prepare,
1329 - sd_source_dispatch,
1330 - sd_source_finalize
1334 -sd_source_new (void)
1337 - SdSource *sd_source;
1340 - source = g_source_new (&sd_source_funcs, sizeof (SdSource));
1341 - sd_source = (SdSource *)source;
1343 - if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
1345 - g_printerr ("Error getting login monitor: %d", ret);
1349 - sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
1350 - sd_source->pollfd.events = G_IO_IN;
1351 - g_source_add_poll (source, &sd_source->pollfd);
1357 -struct _PolkitBackendSessionMonitor
1359 - GObject parent_instance;
1361 - GDBusConnection *system_bus;
1363 - GSource *sd_source;
1366 -struct _PolkitBackendSessionMonitorClass
1368 - GObjectClass parent_class;
1370 - void (*changed) (PolkitBackendSessionMonitor *monitor);
1380 -static guint signals[LAST_SIGNAL] = {0};
1382 -G_DEFINE_TYPE (PolkitBackendSessionMonitor, polkit_backend_session_monitor, G_TYPE_OBJECT);
1384 -/* ---------------------------------------------------------------------------------------------------- */
1387 -sessions_changed (gpointer user_data)
1389 - PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
1391 - g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
1398 -polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor)
1403 - monitor->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
1404 - if (monitor->system_bus == NULL)
1406 - g_printerr ("Error getting system bus: %s", error->message);
1407 - g_error_free (error);
1410 - monitor->sd_source = sd_source_new ();
1411 - g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
1412 - g_source_attach (monitor->sd_source, NULL);
1416 -polkit_backend_session_monitor_finalize (GObject *object)
1418 - PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (object);
1420 - if (monitor->system_bus != NULL)
1421 - g_object_unref (monitor->system_bus);
1423 - if (monitor->sd_source != NULL)
1425 - g_source_destroy (monitor->sd_source);
1426 - g_source_unref (monitor->sd_source);
1429 - if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL)
1430 - G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object);
1434 -polkit_backend_session_monitor_class_init (PolkitBackendSessionMonitorClass *klass)
1436 - GObjectClass *gobject_class;
1438 - gobject_class = G_OBJECT_CLASS (klass);
1440 - gobject_class->finalize = polkit_backend_session_monitor_finalize;
1443 - * PolkitBackendSessionMonitor::changed:
1444 - * @monitor: A #PolkitBackendSessionMonitor
1446 - * Emitted when something changes.
1448 - signals[CHANGED_SIGNAL] = g_signal_new ("changed",
1449 - POLKIT_BACKEND_TYPE_SESSION_MONITOR,
1450 - G_SIGNAL_RUN_LAST,
1451 - G_STRUCT_OFFSET (PolkitBackendSessionMonitorClass, changed),
1452 - NULL, /* accumulator */
1453 - NULL, /* accumulator data */
1454 - g_cclosure_marshal_VOID__VOID,
1459 -PolkitBackendSessionMonitor *
1460 -polkit_backend_session_monitor_new (void)
1462 - PolkitBackendSessionMonitor *monitor;
1464 - monitor = POLKIT_BACKEND_SESSION_MONITOR (g_object_new (POLKIT_BACKEND_TYPE_SESSION_MONITOR, NULL));
1469 -/* ---------------------------------------------------------------------------------------------------- */
1472 -polkit_backend_session_monitor_get_sessions (PolkitBackendSessionMonitor *monitor)
1478 -/* ---------------------------------------------------------------------------------------------------- */
1481 - * polkit_backend_session_monitor_get_user:
1482 - * @monitor: A #PolkitBackendSessionMonitor.
1483 - * @subject: A #PolkitSubject.
1484 - * @error: Return location for error.
1486 - * Gets the user corresponding to @subject or %NULL if no user exists.
1488 - * Returns: %NULL if @error is set otherwise a #PolkitUnixUser that should be freed with g_object_unref().
1491 -polkit_backend_session_monitor_get_user_for_subject (PolkitBackendSessionMonitor *monitor,
1492 - PolkitSubject *subject,
1495 - PolkitIdentity *ret;
1500 - if (POLKIT_IS_UNIX_PROCESS (subject))
1502 - uid = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject));
1503 - if ((gint) uid == -1)
1505 - g_set_error (error,
1507 - POLKIT_ERROR_FAILED,
1508 - "Unix process subject does not have uid set");
1511 - ret = polkit_unix_user_new (uid);
1513 - else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1515 - ret = (PolkitIdentity*)polkit_system_bus_name_get_user_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error);
1517 - else if (POLKIT_IS_UNIX_SESSION (subject))
1520 - if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
1522 - g_set_error (error,
1524 - POLKIT_ERROR_FAILED,
1525 - "Error getting uid for session");
1529 - ret = polkit_unix_user_new (uid);
1537 - * polkit_backend_session_monitor_get_session_for_subject:
1538 - * @monitor: A #PolkitBackendSessionMonitor.
1539 - * @subject: A #PolkitSubject.
1540 - * @error: Return location for error.
1542 - * Gets the session corresponding to @subject or %NULL if no session exists.
1544 - * Returns: %NULL if @error is set otherwise a #PolkitUnixSession that should be freed with g_object_unref().
1547 -polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMonitor *monitor,
1548 - PolkitSubject *subject,
1551 - PolkitUnixProcess *tmp_process = NULL;
1552 - PolkitUnixProcess *process = NULL;
1553 - PolkitSubject *session = NULL;
1554 - char *session_id = NULL;
1556 -#if HAVE_SD_UID_GET_DISPLAY
1560 - if (POLKIT_IS_UNIX_PROCESS (subject))
1561 - process = POLKIT_UNIX_PROCESS (subject); /* We already have a process */
1562 - else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1564 - /* Convert bus name to process */
1565 - tmp_process = (PolkitUnixProcess*)polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error);
1568 - process = tmp_process;
1572 - g_set_error (error,
1574 - POLKIT_ERROR_NOT_SUPPORTED,
1575 - "Cannot get session for subject of type %s",
1576 - g_type_name (G_TYPE_FROM_INSTANCE (subject)));
1579 - /* Now do process -> pid -> same session */
1580 - g_assert (process != NULL);
1581 - pid = polkit_unix_process_get_pid (process);
1583 - if (sd_pid_get_session (pid, &session_id) >= 0)
1585 - session = polkit_unix_session_new (session_id);
1589 -#if HAVE_SD_UID_GET_DISPLAY
1590 - /* Now do process -> uid -> graphical session (systemd version 213)*/
1591 - if (sd_pid_get_owner_uid (pid, &uid) < 0)
1594 - if (sd_uid_get_display (uid, &session_id) >= 0)
1596 - session = polkit_unix_session_new (session_id);
1602 - free (session_id);
1603 - if (tmp_process) g_object_unref (tmp_process);
1608 -polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
1609 - PolkitSubject *session)
1613 - if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
1624 -polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
1625 - PolkitSubject *session)
1627 - const char *session_id;
1630 - gboolean is_active = FALSE;
1632 - session_id = polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session));
1634 - g_debug ("Checking whether session %s is active.", session_id);
1636 - /* Check whether *any* of the user's current sessions are active. */
1637 - if (sd_session_get_uid (session_id, &uid) < 0)
1640 - g_debug ("Session %s has UID %u.", session_id, uid);
1642 - if (sd_uid_get_state (uid, &state) < 0)
1645 - g_debug ("UID %u has state %s.", uid, state);
1647 - is_active = (g_strcmp0 (state, "active") == 0);
1653 - /* Fall back to checking the session. This is not ideal, since the user
1654 - * might have multiple sessions, and we cannot guarantee to have chosen
1657 - * See: https://bugs.freedesktop.org/show_bug.cgi?id=76358. */
1658 - return sd_session_is_active (session_id);