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 --- polkit-0.115/src/polkitbackend/polkitbackendsessionmonitor.c.orig 2018-06-26 15:17:52.000000000 +0200
763 +++ polkit-0.115/src/polkitbackend/polkitbackendsessionmonitor.c 2018-09-29 10:42:52.104190929 +0200
766 #include <glib/gstdio.h>
768 +#ifdef HAVE_LIBSYSTEMD
769 +# include <stdlib.h>
770 +# include <systemd/sd-daemon.h>
771 +# include <systemd/sd-login.h>
772 +#endif /* HAVE_LIBSYSTEMD */
774 #include <polkit/polkit.h>
775 #include <polkit/polkitprivate.h>
776 #include "polkitbackendsessionmonitor.h"
778 * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
781 +#ifdef HAVE_LIBSYSTEMD
786 + sd_login_monitor *monitor;
790 +sd_source_prepare (GSource *source,
798 +sd_source_check (GSource *source)
800 + SdSource *sd_source = (SdSource *)source;
802 + return sd_source->pollfd.revents != 0;
806 +sd_source_dispatch (GSource *source,
807 + GSourceFunc callback,
808 + gpointer user_data)
811 + SdSource *sd_source = (SdSource *)source;
814 + g_warn_if_fail (callback != NULL);
816 + ret = (*callback) (user_data);
818 + sd_login_monitor_flush (sd_source->monitor);
824 +sd_source_finalize (GSource *source)
826 + SdSource *sd_source = (SdSource*)source;
828 + sd_login_monitor_unref (sd_source->monitor);
831 +static GSourceFuncs sd_source_funcs = {
834 + sd_source_dispatch,
839 +sd_source_new (void)
842 + SdSource *sd_source;
845 + source = g_source_new (&sd_source_funcs, sizeof (SdSource));
846 + sd_source = (SdSource *)source;
848 + if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
850 + g_printerr ("Error getting login monitor: %d", ret);
854 + sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
855 + sd_source->pollfd.events = G_IO_IN;
856 + g_source_add_poll (source, &sd_source->pollfd);
861 +#endif /* HAVE_LIBSYSTEMD */
863 struct _PolkitBackendSessionMonitor
865 GObject parent_instance;
868 GFileMonitor *database_monitor;
869 time_t database_mtime;
871 +#ifdef HAVE_LIBSYSTEMD
872 + GSource *sd_source;
873 +#endif /* HAVE_LIBSYSTEMD */
876 struct _PolkitBackendSessionMonitorClass
879 /* ---------------------------------------------------------------------------------------------------- */
881 +#ifdef HAVE_LIBSYSTEMD
883 +sessions_changed (gpointer user_data)
885 + PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
887 + g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
891 +#endif /* HAVE_LIBSYSTEMD */
894 reload_database (PolkitBackendSessionMonitor *monitor,
896 @@ -177,31 +281,47 @@
897 g_error_free (error);
901 - if (!ensure_database (monitor, &error))
902 +#ifdef HAVE_LIBSYSTEMD
903 + monitor->sd_source = NULL;
905 + if (sd_booted () > 0)
907 - g_printerr ("Error loading " CKDB_PATH ": %s", error->message);
908 - g_error_free (error);
909 + monitor->sd_source = sd_source_new ();
910 + g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
911 + g_source_attach (monitor->sd_source, NULL);
913 + monitor->database_monitor = NULL;
914 + monitor->database = NULL;
917 +#endif /* HAVE_LIBSYSTEMD */
920 + if (!ensure_database (monitor, &error))
922 + g_printerr ("Error loading " CKDB_PATH ": %s", error->message);
923 + g_error_free (error);
927 - file = g_file_new_for_path (CKDB_PATH);
928 - monitor->database_monitor = g_file_monitor_file (file,
930 + file = g_file_new_for_path (CKDB_PATH);
931 + monitor->database_monitor = g_file_monitor_file (file,
935 - g_object_unref (file);
936 - if (monitor->database_monitor == NULL)
938 - g_printerr ("Error monitoring " CKDB_PATH ": %s", error->message);
939 - g_error_free (error);
943 - g_signal_connect (monitor->database_monitor,
945 - G_CALLBACK (on_file_monitor_changed),
947 + g_object_unref (file);
948 + if (monitor->database_monitor == NULL)
950 + g_printerr ("Error monitoring " CKDB_PATH ": %s", error->message);
951 + g_error_free (error);
955 + g_signal_connect (monitor->database_monitor,
957 + G_CALLBACK (on_file_monitor_changed),
964 if (monitor->database != NULL)
965 g_key_file_free (monitor->database);
967 + if (monitor->sd_source != NULL)
969 + g_source_destroy (monitor->sd_source);
970 + g_source_unref (monitor->sd_source);
973 if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL)
974 G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object);
978 else if (POLKIT_IS_UNIX_SESSION (subject))
980 +#ifdef HAVE_LIBSYSTEMD
981 + if (monitor->sd_source != NULL)
985 + if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
987 + g_set_error (error,
989 + POLKIT_ERROR_FAILED,
990 + "Error getting uid for session");
994 + ret = polkit_unix_user_new (uid);
998 +#endif /* HAVE_LIBSYSTEMD */
1005 ret = polkit_unix_user_new (uid);
1011 @@ -379,35 +526,26 @@
1012 PolkitSubject *subject,
1015 - PolkitSubject *session;
1018 + PolkitSubject *session = NULL;
1021 if (POLKIT_IS_UNIX_PROCESS (subject))
1023 - const gchar *session_id;
1025 - result = g_dbus_connection_call_sync (monitor->system_bus,
1026 - "org.freedesktop.ConsoleKit",
1027 - "/org/freedesktop/ConsoleKit/Manager",
1028 - "org.freedesktop.ConsoleKit.Manager",
1029 - "GetSessionForUnixProcess",
1030 - g_variant_new ("(u)", polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject))),
1031 - G_VARIANT_TYPE ("(o)"),
1032 - G_DBUS_CALL_FLAGS_NONE,
1033 - -1, /* timeout_msec */
1034 - NULL, /* GCancellable */
1036 - if (result == NULL)
1038 - g_variant_get (result, "(&o)", &session_id);
1039 - session = polkit_unix_session_new (session_id);
1040 - g_variant_unref (result);
1041 + { /* We already have a process; now do process -> pid */
1042 + pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject));
1044 else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1047 - const gchar *session_id;
1048 + { /* Convert bus name to process / pid */
1049 +#ifdef HAVE_LIBSYSTEMD
1050 + if (monitor->sd_source != NULL)
1052 + PolkitUnixProcess *process = (PolkitUnixProcess*)polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME(subject), NULL, error);
1055 + pid = polkit_unix_process_get_pid (process);
1056 + g_object_unref (process);
1062 result = g_dbus_connection_call_sync (monitor->system_bus,
1063 @@ -425,23 +563,7 @@
1065 g_variant_get (result, "(u)", &pid);
1066 g_variant_unref (result);
1068 - result = g_dbus_connection_call_sync (monitor->system_bus,
1069 - "org.freedesktop.ConsoleKit",
1070 - "/org/freedesktop/ConsoleKit/Manager",
1071 - "org.freedesktop.ConsoleKit.Manager",
1072 - "GetSessionForUnixProcess",
1073 - g_variant_new ("(u)", pid),
1074 - G_VARIANT_TYPE ("(o)"),
1075 - G_DBUS_CALL_FLAGS_NONE,
1076 - -1, /* timeout_msec */
1077 - NULL, /* GCancellable */
1079 - if (result == NULL)
1081 - g_variant_get (result, "(&o)", &session_id);
1082 - session = polkit_unix_session_new (session_id);
1083 - g_variant_unref (result);
1088 @@ -450,8 +572,57 @@
1089 POLKIT_ERROR_NOT_SUPPORTED,
1090 "Cannot get user for subject of type %s",
1091 g_type_name (G_TYPE_FROM_INSTANCE (subject)));
1095 + /* Now do pid -> same session */
1096 +#ifdef HAVE_LIBSYSTEMD
1097 + if (monitor->sd_source != NULL)
1099 + gchar *session_id;
1100 + if (sd_pid_get_session (pid, &session_id) >= 0)
1102 + session = polkit_unix_session_new (session_id);
1103 + free (session_id);
1106 +#if HAVE_SD_UID_GET_DISPLAY
1108 + /* Now do pid -> uid -> graphical session (systemd version 213)*/
1109 + if (sd_pid_get_owner_uid (pid, &uid) < 0)
1112 + if (sd_uid_get_display (uid, &session_id) >= 0)
1114 + session = polkit_unix_session_new (session_id);
1115 + free (session_id);
1121 +#endif /* HAVE_LIBSYSTEMD */
1123 + gchar *session_id;
1125 + result = g_dbus_connection_call_sync (monitor->system_bus,
1126 + "org.freedesktop.ConsoleKit",
1127 + "/org/freedesktop/ConsoleKit/Manager",
1128 + "org.freedesktop.ConsoleKit.Manager",
1129 + "GetSessionForUnixProcess",
1130 + g_variant_new ("(u)", pid),
1131 + G_VARIANT_TYPE ("(o)"),
1132 + G_DBUS_CALL_FLAGS_NONE,
1133 + -1, /* timeout_msec */
1134 + NULL, /* GCancellable */
1136 + if (result == NULL)
1138 + g_variant_get (result, "(&o)", &session_id);
1139 + session = polkit_unix_session_new (session_id);
1140 + g_variant_unref (result);
1146 @@ -502,7 +673,22 @@
1147 polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
1148 PolkitSubject *session)
1150 - return get_boolean (monitor, session, "is_local");
1151 +#ifdef HAVE_LIBSYSTEMD
1152 + if (monitor->sd_source != NULL)
1156 + if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
1165 +#endif /* HAVE_LIBSYSTEMD */
1166 + return get_boolean (monitor, session, "is_local");
1170 @@ -510,6 +696,44 @@
1171 polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
1172 PolkitSubject *session)
1174 - return get_boolean (monitor, session, "is_active");
1175 +#ifdef HAVE_LIBSYSTEMD
1176 + if (monitor->sd_source != NULL)
1178 + const char *session_id;
1181 + gboolean is_active = FALSE;
1183 + session_id = polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session));
1185 + g_debug ("Checking whether session %s is active.", session_id);
1187 + /* Check whether *any* of the user's current sessions are active. */
1188 + if (sd_session_get_uid (session_id, &uid) < 0)
1191 + g_debug ("Session %s has UID %u.", session_id, uid);
1193 + if (sd_uid_get_state (uid, &state) < 0)
1196 + g_debug ("UID %u has state %s.", uid, state);
1198 + is_active = (g_strcmp0 (state, "active") == 0);
1204 + /* Fall back to checking the session. This is not ideal, since the user
1205 + * might have multiple sessions, and we cannot guarantee to have chosen
1208 + * See: https://bugs.freedesktop.org/show_bug.cgi?id=76358. */
1209 + return sd_session_is_active (session_id);
1212 +#endif /* HAVE_LIBSYSTEMD */
1213 + return get_boolean (monitor, session, "is_active");
1216 --- polkit-0.115/src/polkitbackend/polkitbackendsessionmonitor-systemd.c.orig 2018-09-29 09:48:19.240894967 +0200
1217 +++ polkit-0.115/src/polkitbackend/polkitbackendsessionmonitor-systemd.c 1970-01-01 01:00:00.000000000 +0100
1220 - * Copyright (C) 2011 Red Hat, Inc.
1222 - * This library is free software; you can redistribute it and/or
1223 - * modify it under the terms of the GNU Lesser General Public
1224 - * License as published by the Free Software Foundation; either
1225 - * version 2 of the License, or (at your option) any later version.
1227 - * This library is distributed in the hope that it will be useful,
1228 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1229 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1230 - * Lesser General Public License for more details.
1232 - * You should have received a copy of the GNU Lesser General
1233 - * Public License along with this library; if not, write to the
1234 - * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1235 - * Boston, MA 02111-1307, USA.
1237 - * Author: Matthias Clasen
1240 -#include "config.h"
1244 -#include <string.h>
1245 -#include <glib/gstdio.h>
1246 -#include <systemd/sd-login.h>
1247 -#include <stdlib.h>
1249 -#include <polkit/polkit.h>
1250 -#include <polkit/polkitprivate.h>
1251 -#include "polkitbackendsessionmonitor.h"
1254 - * SECTION:polkitbackendsessionmonitor
1255 - * @title: PolkitBackendSessionMonitor
1256 - * @short_description: Monitor sessions
1258 - * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
1265 - sd_login_monitor *monitor;
1269 -sd_source_prepare (GSource *source,
1277 -sd_source_check (GSource *source)
1279 - SdSource *sd_source = (SdSource *)source;
1281 - return sd_source->pollfd.revents != 0;
1285 -sd_source_dispatch (GSource *source,
1286 - GSourceFunc callback,
1287 - gpointer user_data)
1290 - SdSource *sd_source = (SdSource *)source;
1293 - g_warn_if_fail (callback != NULL);
1295 - ret = (*callback) (user_data);
1297 - sd_login_monitor_flush (sd_source->monitor);
1303 -sd_source_finalize (GSource *source)
1305 - SdSource *sd_source = (SdSource*)source;
1307 - sd_login_monitor_unref (sd_source->monitor);
1310 -static GSourceFuncs sd_source_funcs = {
1311 - sd_source_prepare,
1313 - sd_source_dispatch,
1314 - sd_source_finalize
1318 -sd_source_new (void)
1321 - SdSource *sd_source;
1324 - source = g_source_new (&sd_source_funcs, sizeof (SdSource));
1325 - sd_source = (SdSource *)source;
1327 - if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
1329 - g_printerr ("Error getting login monitor: %d", ret);
1333 - sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
1334 - sd_source->pollfd.events = G_IO_IN;
1335 - g_source_add_poll (source, &sd_source->pollfd);
1341 -struct _PolkitBackendSessionMonitor
1343 - GObject parent_instance;
1345 - GDBusConnection *system_bus;
1347 - GSource *sd_source;
1350 -struct _PolkitBackendSessionMonitorClass
1352 - GObjectClass parent_class;
1354 - void (*changed) (PolkitBackendSessionMonitor *monitor);
1364 -static guint signals[LAST_SIGNAL] = {0};
1366 -G_DEFINE_TYPE (PolkitBackendSessionMonitor, polkit_backend_session_monitor, G_TYPE_OBJECT);
1368 -/* ---------------------------------------------------------------------------------------------------- */
1371 -sessions_changed (gpointer user_data)
1373 - PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
1375 - g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
1382 -polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor)
1387 - monitor->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
1388 - if (monitor->system_bus == NULL)
1390 - g_printerr ("Error getting system bus: %s", error->message);
1391 - g_error_free (error);
1394 - monitor->sd_source = sd_source_new ();
1395 - g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
1396 - g_source_attach (monitor->sd_source, NULL);
1400 -polkit_backend_session_monitor_finalize (GObject *object)
1402 - PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (object);
1404 - if (monitor->system_bus != NULL)
1405 - g_object_unref (monitor->system_bus);
1407 - if (monitor->sd_source != NULL)
1409 - g_source_destroy (monitor->sd_source);
1410 - g_source_unref (monitor->sd_source);
1413 - if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL)
1414 - G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object);
1418 -polkit_backend_session_monitor_class_init (PolkitBackendSessionMonitorClass *klass)
1420 - GObjectClass *gobject_class;
1422 - gobject_class = G_OBJECT_CLASS (klass);
1424 - gobject_class->finalize = polkit_backend_session_monitor_finalize;
1427 - * PolkitBackendSessionMonitor::changed:
1428 - * @monitor: A #PolkitBackendSessionMonitor
1430 - * Emitted when something changes.
1432 - signals[CHANGED_SIGNAL] = g_signal_new ("changed",
1433 - POLKIT_BACKEND_TYPE_SESSION_MONITOR,
1434 - G_SIGNAL_RUN_LAST,
1435 - G_STRUCT_OFFSET (PolkitBackendSessionMonitorClass, changed),
1436 - NULL, /* accumulator */
1437 - NULL, /* accumulator data */
1438 - g_cclosure_marshal_VOID__VOID,
1443 -PolkitBackendSessionMonitor *
1444 -polkit_backend_session_monitor_new (void)
1446 - PolkitBackendSessionMonitor *monitor;
1448 - monitor = POLKIT_BACKEND_SESSION_MONITOR (g_object_new (POLKIT_BACKEND_TYPE_SESSION_MONITOR, NULL));
1453 -/* ---------------------------------------------------------------------------------------------------- */
1456 -polkit_backend_session_monitor_get_sessions (PolkitBackendSessionMonitor *monitor)
1462 -/* ---------------------------------------------------------------------------------------------------- */
1465 - * polkit_backend_session_monitor_get_user:
1466 - * @monitor: A #PolkitBackendSessionMonitor.
1467 - * @subject: A #PolkitSubject.
1468 - * @result_matches: If not %NULL, set to indicate whether the return value matches current (RACY) state.
1469 - * @error: Return location for error.
1471 - * Gets the user corresponding to @subject or %NULL if no user exists.
1473 - * NOTE: For a #PolkitUnixProcess, the UID is read from @subject (which may
1474 - * come from e.g. a D-Bus client), so it may not correspond to the actual UID
1475 - * of the referenced process (at any point in time). This is indicated by
1476 - * setting @result_matches to %FALSE; the caller may reject such subjects or
1477 - * require additional privileges. @result_matches == %TRUE only indicates that
1478 - * the UID matched the underlying process at ONE point in time, it may not match
1481 - * Returns: %NULL if @error is set otherwise a #PolkitUnixUser that should be freed with g_object_unref().
1484 -polkit_backend_session_monitor_get_user_for_subject (PolkitBackendSessionMonitor *monitor,
1485 - PolkitSubject *subject,
1486 - gboolean *result_matches,
1489 - PolkitIdentity *ret;
1495 - if (POLKIT_IS_UNIX_PROCESS (subject))
1497 - gint subject_uid, current_uid;
1498 - GError *local_error;
1500 - subject_uid = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject));
1501 - if (subject_uid == -1)
1503 - g_set_error (error,
1505 - POLKIT_ERROR_FAILED,
1506 - "Unix process subject does not have uid set");
1509 - local_error = NULL;
1510 - current_uid = polkit_unix_process_get_racy_uid__ (POLKIT_UNIX_PROCESS (subject), &local_error);
1511 - if (local_error != NULL)
1513 - g_propagate_error (error, local_error);
1516 - ret = polkit_unix_user_new (subject_uid);
1517 - matches = (subject_uid == current_uid);
1519 - else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1521 - ret = (PolkitIdentity*)polkit_system_bus_name_get_user_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error);
1524 - else if (POLKIT_IS_UNIX_SESSION (subject))
1528 - if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
1530 - g_set_error (error,
1532 - POLKIT_ERROR_FAILED,
1533 - "Error getting uid for session");
1537 - ret = polkit_unix_user_new (uid);
1542 - if (result_matches != NULL)
1544 - *result_matches = matches;
1550 - * polkit_backend_session_monitor_get_session_for_subject:
1551 - * @monitor: A #PolkitBackendSessionMonitor.
1552 - * @subject: A #PolkitSubject.
1553 - * @error: Return location for error.
1555 - * Gets the session corresponding to @subject or %NULL if no session exists.
1557 - * Returns: %NULL if @error is set otherwise a #PolkitUnixSession that should be freed with g_object_unref().
1560 -polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMonitor *monitor,
1561 - PolkitSubject *subject,
1564 - PolkitUnixProcess *tmp_process = NULL;
1565 - PolkitUnixProcess *process = NULL;
1566 - PolkitSubject *session = NULL;
1567 - char *session_id = NULL;
1569 -#if HAVE_SD_UID_GET_DISPLAY
1573 - if (POLKIT_IS_UNIX_PROCESS (subject))
1574 - process = POLKIT_UNIX_PROCESS (subject); /* We already have a process */
1575 - else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1577 - /* Convert bus name to process */
1578 - tmp_process = (PolkitUnixProcess*)polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error);
1581 - process = tmp_process;
1585 - g_set_error (error,
1587 - POLKIT_ERROR_NOT_SUPPORTED,
1588 - "Cannot get session for subject of type %s",
1589 - g_type_name (G_TYPE_FROM_INSTANCE (subject)));
1592 - /* Now do process -> pid -> same session */
1593 - g_assert (process != NULL);
1594 - pid = polkit_unix_process_get_pid (process);
1596 - if (sd_pid_get_session (pid, &session_id) >= 0)
1598 - session = polkit_unix_session_new (session_id);
1602 -#if HAVE_SD_UID_GET_DISPLAY
1603 - /* Now do process -> uid -> graphical session (systemd version 213)*/
1604 - if (sd_pid_get_owner_uid (pid, &uid) < 0)
1607 - if (sd_uid_get_display (uid, &session_id) >= 0)
1609 - session = polkit_unix_session_new (session_id);
1615 - free (session_id);
1616 - if (tmp_process) g_object_unref (tmp_process);
1621 -polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
1622 - PolkitSubject *session)
1626 - if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
1637 -polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
1638 - PolkitSubject *session)
1640 - const char *session_id;
1643 - gboolean is_active = FALSE;
1645 - session_id = polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session));
1647 - g_debug ("Checking whether session %s is active.", session_id);
1649 - /* Check whether *any* of the user's current sessions are active. */
1650 - if (sd_session_get_uid (session_id, &uid) < 0)
1653 - g_debug ("Session %s has UID %u.", session_id, uid);
1655 - if (sd_uid_get_state (uid, &state) < 0)
1658 - g_debug ("UID %u has state %s.", uid, state);
1660 - is_active = (g_strcmp0 (state, "active") == 0);
1666 - /* Fall back to checking the session. This is not ideal, since the user
1667 - * might have multiple sessions, and we cannot guarantee to have chosen
1670 - * See: https://bugs.freedesktop.org/show_bug.cgi?id=76358. */
1671 - return sd_session_is_active (session_id);