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 diff -ruN polkit-0.113.orig/src/polkit/polkitunixsession.c polkit-0.113/src/polkit/polkitunixsession.c
33 --- polkit-0.113.orig/src/polkit/polkitunixsession.c 2014-01-14 23:42:25.000000000 +0100
34 +++ polkit-0.113/src/polkit/polkitunixsession.c 2015-09-26 23:40:21.671982135 +0200
36 #include "polkiterror.h"
37 #include "polkitprivate.h"
39 +#ifdef HAVE_LIBSYSTEMD
41 +# include <systemd/sd-daemon.h>
42 +# include <systemd/sd-login.h>
46 * SECTION:polkitunixsession
47 * @title: PolkitUnixSession
50 PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
51 GDBusConnection *connection;
53 + GVariant *result = NULL;
59 - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
60 - if (connection == NULL)
62 +#ifdef HAVE_LIBSYSTEMD
65 - result = g_dbus_connection_call_sync (connection,
66 - "org.freedesktop.ConsoleKit", /* name */
67 - session->session_id, /* object path */
68 - "org.freedesktop.ConsoleKit.Session", /* interface name */
69 - "GetUser", /* method */
70 - NULL, /* parameters */
71 - G_VARIANT_TYPE ("(u)"),
72 - G_DBUS_CALL_FLAGS_NONE,
78 + if (sd_booted () > 0)
80 + if (sd_session_get_uid (session->session_id, &uid) == 0)
84 +#endif /* HAVE_LIBSYSTEMD */
86 + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
87 + if (connection == NULL)
90 + result = g_dbus_connection_call_sync (connection,
91 + "org.freedesktop.ConsoleKit", /* name */
92 + session->session_id, /* object path */
93 + "org.freedesktop.ConsoleKit.Session", /* interface name */
94 + "GetUser", /* method */
95 + NULL, /* parameters */
96 + G_VARIANT_TYPE ("(u)"),
97 + G_DBUS_CALL_FLAGS_NONE,
101 + if (result == NULL)
105 - g_variant_unref (result);
107 + g_variant_unref (result);
111 if (connection != NULL)
112 g_object_unref (connection);
125 @@ -484,28 +505,51 @@
129 - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
130 - if (connection == NULL)
133 - result = g_dbus_connection_call_sync (connection,
134 - "org.freedesktop.ConsoleKit", /* name */
135 - "/org/freedesktop/ConsoleKit/Manager", /* object path */
136 - "org.freedesktop.ConsoleKit.Manager", /* interface name */
137 - "GetSessionForUnixProcess", /* method */
138 - g_variant_new ("(u)", session->pid), /* parameters */
139 - G_VARIANT_TYPE ("(o)"),
140 - G_DBUS_CALL_FLAGS_NONE,
144 - if (result == NULL)
146 +#ifdef HAVE_LIBSYSTEMD
149 + if (sd_booted () > 0)
151 + if (sd_pid_get_session (session->pid, &s) == 0)
153 + session->session_id = g_strdup (s);
159 + g_set_error (error,
161 + POLKIT_ERROR_FAILED,
162 + "No session for pid %d",
163 + (gint) session->pid);
166 +#endif /* HAVE_LIBSYSTEMD */
168 + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
169 + if (connection == NULL)
172 + result = g_dbus_connection_call_sync (connection,
173 + "org.freedesktop.ConsoleKit", /* name */
174 + "/org/freedesktop/ConsoleKit/Manager", /* object path */
175 + "org.freedesktop.ConsoleKit.Manager", /* interface name */
176 + "GetSessionForUnixProcess", /* method */
177 + g_variant_new ("(u)", session->pid), /* parameters */
178 + G_VARIANT_TYPE ("(o)"),
179 + G_DBUS_CALL_FLAGS_NONE,
183 + if (result == NULL)
186 - g_variant_get (result, "(o)", &session->session_id);
187 - g_variant_unref (result);
188 + g_variant_get (result, "(o)", &session->session_id);
189 + g_variant_unref (result);
196 if (connection != NULL)
197 diff -ruN polkit-0.113.orig/src/polkit/polkitunixsession-systemd.c polkit-0.113/src/polkit/polkitunixsession-systemd.c
198 --- polkit-0.113.orig/src/polkit/polkitunixsession-systemd.c 2014-01-14 23:42:25.000000000 +0100
199 +++ polkit-0.113/src/polkit/polkitunixsession-systemd.c 1970-01-01 01:00:00.000000000 +0100
202 - * Copyright (C) 2011 Red Hat, Inc.
204 - * This library is free software; you can redistribute it and/or
205 - * modify it under the terms of the GNU Lesser General Public
206 - * License as published by the Free Software Foundation; either
207 - * version 2 of the License, or (at your option) any later version.
209 - * This library is distributed in the hope that it will be useful,
210 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
211 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
212 - * Lesser General Public License for more details.
214 - * You should have received a copy of the GNU Lesser General
215 - * Public License along with this library; if not, write to the
216 - * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
217 - * Boston, MA 02111-1307, USA.
219 - * Author: Matthias Clasen
222 -#ifdef HAVE_CONFIG_H
223 -# include "config.h"
228 -#include "polkitunixsession.h"
229 -#include "polkitsubject.h"
230 -#include "polkiterror.h"
231 -#include "polkitprivate.h"
233 -#include <systemd/sd-login.h>
236 - * SECTION:polkitunixsession
237 - * @title: PolkitUnixSession
238 - * @short_description: Unix sessions
240 - * An object that represents an user session.
242 - * The session id is an opaque string obtained from ConsoleKit.
246 - * PolkitUnixSession:
248 - * The #PolkitUnixSession struct should not be accessed directly.
250 -struct _PolkitUnixSession
252 - GObject parent_instance;
259 -struct _PolkitUnixSessionClass
261 - GObjectClass parent_class;
271 -static void subject_iface_init (PolkitSubjectIface *subject_iface);
272 -static void initable_iface_init (GInitableIface *initable_iface);
273 -static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
275 -G_DEFINE_TYPE_WITH_CODE (PolkitUnixSession, polkit_unix_session, G_TYPE_OBJECT,
276 - G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init)
277 - G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
278 - G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
282 -polkit_unix_session_init (PolkitUnixSession *session)
287 -polkit_unix_session_finalize (GObject *object)
289 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
291 - g_free (session->session_id);
293 - if (G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize != NULL)
294 - G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize (object);
298 -polkit_unix_session_get_property (GObject *object,
303 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
307 - case PROP_SESSION_ID:
308 - g_value_set_string (value, session->session_id);
312 - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318 -polkit_unix_session_set_property (GObject *object,
320 - const GValue *value,
323 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
327 - case PROP_SESSION_ID:
328 - polkit_unix_session_set_session_id (session, g_value_get_string (value));
332 - session->pid = g_value_get_int (value);
336 - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 -polkit_unix_session_class_init (PolkitUnixSessionClass *klass)
344 - GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
346 - gobject_class->finalize = polkit_unix_session_finalize;
347 - gobject_class->get_property = polkit_unix_session_get_property;
348 - gobject_class->set_property = polkit_unix_session_set_property;
351 - * PolkitUnixSession:session-id:
353 - * The UNIX session id.
355 - g_object_class_install_property (gobject_class,
357 - g_param_spec_string ("session-id",
359 - "The UNIX session ID",
361 - G_PARAM_CONSTRUCT |
362 - G_PARAM_READWRITE |
363 - G_PARAM_STATIC_NAME |
364 - G_PARAM_STATIC_BLURB |
365 - G_PARAM_STATIC_NICK));
369 - * PolkitUnixSession:pid:
371 - * The UNIX process id to look up the session.
373 - g_object_class_install_property (gobject_class,
375 - g_param_spec_int ("pid",
377 - "Process ID to use for looking up the session",
381 - G_PARAM_CONSTRUCT_ONLY |
383 - G_PARAM_STATIC_NAME |
384 - G_PARAM_STATIC_BLURB |
385 - G_PARAM_STATIC_NICK));
390 - * polkit_unix_session_get_session_id:
391 - * @session: A #PolkitUnixSession.
393 - * Gets the session id for @session.
395 - * Returns: The session id for @session. Do not free this string, it
396 - * is owned by @session.
399 -polkit_unix_session_get_session_id (PolkitUnixSession *session)
401 - g_return_val_if_fail (POLKIT_IS_UNIX_SESSION (session), NULL);
402 - return session->session_id;
406 - * polkit_unix_session_set_session_id:
407 - * @session: A #PolkitUnixSession.
408 - * @session_id: The session id.
410 - * Sets the session id for @session to @session_id.
413 -polkit_unix_session_set_session_id (PolkitUnixSession *session,
414 - const gchar *session_id)
416 - g_return_if_fail (POLKIT_IS_UNIX_SESSION (session));
417 - /*g_return_if_fail (session_id != NULL);*/
418 - g_free (session->session_id);
419 - session->session_id = g_strdup (session_id);
423 - * polkit_unix_session_new:
424 - * @session_id: The session id.
426 - * Creates a new #PolkitUnixSession for @session_id.
428 - * Returns: (transfer full): A #PolkitUnixSession. Free with g_object_unref().
431 -polkit_unix_session_new (const gchar *session_id)
433 - return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_UNIX_SESSION,
434 - "session-id", session_id,
439 - * polkit_unix_session_new_for_process:
440 - * @pid: The process id of the process to get the session for.
441 - * @cancellable: (allow-none): A #GCancellable or %NULL.
442 - * @callback: A #GAsyncReadyCallback to call when the request is satisfied
443 - * @user_data: The data to pass to @callback.
445 - * Asynchronously creates a new #PolkitUnixSession object for the
446 - * process with process id @pid.
448 - * When the operation is finished, @callback will be invoked in the
449 - * <link linkend="g-main-context-push-thread-default">thread-default
450 - * main loop</link> of the thread you are calling this method
451 - * from. You can then call
452 - * polkit_unix_session_new_for_process_finish() to get the result of
455 - * This method constructs the object asynchronously, for the synchronous and blocking version
456 - * use polkit_unix_session_new_for_process_sync().
459 -polkit_unix_session_new_for_process (gint pid,
460 - GCancellable *cancellable,
461 - GAsyncReadyCallback callback,
462 - gpointer user_data)
464 - g_async_initable_new_async (POLKIT_TYPE_UNIX_SESSION,
465 - G_PRIORITY_DEFAULT,
474 - * polkit_unix_session_new_for_process_finish:
475 - * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to polkit_unix_session_new_for_process().
476 - * @error: (allow-none): Return location for error.
478 - * Finishes constructing a #PolkitSubject for a process id.
480 - * Returns: (transfer full) (allow-none): A #PolkitUnixSession for the @pid passed to
481 - * polkit_unix_session_new_for_process() or %NULL if @error is
482 - * set. Free with g_object_unref().
485 -polkit_unix_session_new_for_process_finish (GAsyncResult *res,
489 - GObject *source_object;
491 - source_object = g_async_result_get_source_object (res);
492 - g_assert (source_object != NULL);
494 - object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
497 - g_object_unref (source_object);
499 - if (object != NULL)
500 - return POLKIT_SUBJECT (object);
507 - * polkit_unix_session_new_for_process_sync:
508 - * @pid: The process id of the process to get the session for.
509 - * @cancellable: (allow-none): A #GCancellable or %NULL.
510 - * @error: (allow-none): Return location for error.
512 - * Creates a new #PolkitUnixSession for the process with process id @pid.
514 - * This is a synchronous call - the calling thread is blocked until a
515 - * reply is received. For the asynchronous version, see
516 - * polkit_unix_session_new_for_process().
518 - * Returns: (allow-none) (transfer full): A #PolkitUnixSession for
519 - * @pid or %NULL if @error is set. Free with g_object_unref().
522 -polkit_unix_session_new_for_process_sync (gint pid,
523 - GCancellable *cancellable,
526 - return POLKIT_SUBJECT (g_initable_new (POLKIT_TYPE_UNIX_SESSION,
534 -polkit_unix_session_hash (PolkitSubject *subject)
536 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
538 - return g_str_hash (session->session_id);
542 -polkit_unix_session_equal (PolkitSubject *a,
545 - PolkitUnixSession *session_a;
546 - PolkitUnixSession *session_b;
548 - session_a = POLKIT_UNIX_SESSION (a);
549 - session_b = POLKIT_UNIX_SESSION (b);
551 - return g_strcmp0 (session_a->session_id, session_b->session_id) == 0;
555 -polkit_unix_session_to_string (PolkitSubject *subject)
557 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
559 - return g_strdup_printf ("unix-session:%s", session->session_id);
563 -polkit_unix_session_exists_sync (PolkitSubject *subject,
564 - GCancellable *cancellable,
567 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
568 - gboolean ret = FALSE;
571 - if (sd_session_get_uid (session->session_id, &uid) == 0)
578 -exists_in_thread_func (GSimpleAsyncResult *res,
580 - GCancellable *cancellable)
584 - if (!polkit_unix_session_exists_sync (POLKIT_SUBJECT (object),
588 - g_simple_async_result_set_from_error (res, error);
589 - g_error_free (error);
594 -polkit_unix_session_exists (PolkitSubject *subject,
595 - GCancellable *cancellable,
596 - GAsyncReadyCallback callback,
597 - gpointer user_data)
599 - GSimpleAsyncResult *simple;
601 - g_return_if_fail (POLKIT_IS_UNIX_SESSION (subject));
603 - simple = g_simple_async_result_new (G_OBJECT (subject),
606 - polkit_unix_session_exists);
607 - g_simple_async_result_run_in_thread (simple,
608 - exists_in_thread_func,
609 - G_PRIORITY_DEFAULT,
611 - g_object_unref (simple);
615 -polkit_unix_session_exists_finish (PolkitSubject *subject,
619 - GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
622 - g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == polkit_unix_session_exists);
626 - if (g_simple_async_result_propagate_error (simple, error))
629 - ret = g_simple_async_result_get_op_res_gboolean (simple);
636 -subject_iface_init (PolkitSubjectIface *subject_iface)
638 - subject_iface->hash = polkit_unix_session_hash;
639 - subject_iface->equal = polkit_unix_session_equal;
640 - subject_iface->to_string = polkit_unix_session_to_string;
641 - subject_iface->exists = polkit_unix_session_exists;
642 - subject_iface->exists_finish = polkit_unix_session_exists_finish;
643 - subject_iface->exists_sync = polkit_unix_session_exists_sync;
647 -polkit_unix_session_initable_init (GInitable *initable,
648 - GCancellable *cancellable,
651 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (initable);
652 - gboolean ret = FALSE;
655 - if (session->session_id != NULL)
657 - /* already set, nothing to do */
662 - if (sd_pid_get_session (session->pid, &s) == 0)
664 - session->session_id = g_strdup (s);
670 - g_set_error (error,
672 - POLKIT_ERROR_FAILED,
673 - "No session for pid %d",
674 - (gint) session->pid);
681 -initable_iface_init (GInitableIface *initable_iface)
683 - initable_iface->init = polkit_unix_session_initable_init;
687 -async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
689 - /* use default implementation to run GInitable code in a thread */
691 diff -ruN polkit-0.113.orig/src/polkitbackend/Makefile.am polkit-0.113/src/polkitbackend/Makefile.am
692 --- polkit-0.113.orig/src/polkitbackend/Makefile.am 2015-06-19 22:31:02.000000000 +0200
693 +++ polkit-0.113/src/polkitbackend/Makefile.am 2015-09-26 23:40:21.674982125 +0200
695 polkitbackendactionpool.h polkitbackendactionpool.c \
696 polkitbackendconfigsource.h polkitbackendconfigsource.c \
697 polkitbackendactionlookup.h polkitbackendactionlookup.c \
701 -libpolkit_backend_1_la_SOURCES += \
702 - polkitbackendsessionmonitor.h polkitbackendsessionmonitor-systemd.c
704 -libpolkit_backend_1_la_SOURCES += \
705 polkitbackendsessionmonitor.h polkitbackendsessionmonitor.c
708 libpolkit_backend_1_la_CFLAGS = \
709 -D_POLKIT_COMPILATION \
710 diff -ruN polkit-0.113.orig/src/polkitbackend/polkitbackendjsauthority.c polkit-0.113/src/polkitbackend/polkitbackendjsauthority.c
711 --- polkit-0.113.orig/src/polkitbackend/polkitbackendjsauthority.c 2015-06-19 22:39:58.000000000 +0200
712 +++ polkit-0.113/src/polkitbackend/polkitbackendjsauthority.c 2015-09-26 23:40:21.674982125 +0200
714 #include <polkit/polkitprivate.h>
716 #ifdef HAVE_LIBSYSTEMD
717 +#include <systemd/sd-daemon.h>
718 #include <systemd/sd-login.h>
719 #endif /* HAVE_LIBSYSTEMD */
724 #ifdef HAVE_LIBSYSTEMD
725 + if (sd_booted () > 0)
727 if (sd_pid_get_session (pid, &session_str) == 0)
729 if (sd_session_get_seat (session_str, &seat_str) == 0)
735 #endif /* HAVE_LIBSYSTEMD */
737 g_assert (POLKIT_IS_UNIX_USER (user_for_subject));
738 diff -ruN polkit-0.113.orig/src/polkitbackend/polkitbackendsessionmonitor.c polkit-0.113/src/polkitbackend/polkitbackendsessionmonitor.c
739 --- polkit-0.113.orig/src/polkitbackend/polkitbackendsessionmonitor.c 2015-06-06 01:24:06.000000000 +0200
740 +++ polkit-0.113/src/polkitbackend/polkitbackendsessionmonitor.c 2015-09-26 23:40:39.451918791 +0200
743 #include <glib/gstdio.h>
745 +#ifdef HAVE_LIBSYSTEMD
746 +# include <stdlib.h>
747 +# include <systemd/sd-daemon.h>
748 +# include <systemd/sd-login.h>
749 +#endif /* HAVE_LIBSYSTEMD */
751 #include <polkit/polkit.h>
752 #include "polkitbackendsessionmonitor.h"
755 * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
758 +#ifdef HAVE_LIBSYSTEMD
763 + sd_login_monitor *monitor;
767 +sd_source_prepare (GSource *source,
775 +sd_source_check (GSource *source)
777 + SdSource *sd_source = (SdSource *)source;
779 + return sd_source->pollfd.revents != 0;
783 +sd_source_dispatch (GSource *source,
784 + GSourceFunc callback,
785 + gpointer user_data)
788 + SdSource *sd_source = (SdSource *)source;
791 + g_warn_if_fail (callback != NULL);
793 + ret = (*callback) (user_data);
795 + sd_login_monitor_flush (sd_source->monitor);
801 +sd_source_finalize (GSource *source)
803 + SdSource *sd_source = (SdSource*)source;
805 + sd_login_monitor_unref (sd_source->monitor);
808 +static GSourceFuncs sd_source_funcs = {
811 + sd_source_dispatch,
816 +sd_source_new (void)
819 + SdSource *sd_source;
822 + source = g_source_new (&sd_source_funcs, sizeof (SdSource));
823 + sd_source = (SdSource *)source;
825 + if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
827 + g_printerr ("Error getting login monitor: %d", ret);
831 + sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
832 + sd_source->pollfd.events = G_IO_IN;
833 + g_source_add_poll (source, &sd_source->pollfd);
838 +#endif /* HAVE_LIBSYSTEMD */
840 struct _PolkitBackendSessionMonitor
842 GObject parent_instance;
845 GFileMonitor *database_monitor;
846 time_t database_mtime;
848 +#ifdef HAVE_LIBSYSTEMD
849 + GSource *sd_source;
850 +#endif /* HAVE_LIBSYSTEMD */
853 struct _PolkitBackendSessionMonitorClass
856 /* ---------------------------------------------------------------------------------------------------- */
858 +#ifdef HAVE_LIBSYSTEMD
860 +sessions_changed (gpointer user_data)
862 + PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
864 + g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
868 +#endif /* HAVE_LIBSYSTEMD */
871 reload_database (PolkitBackendSessionMonitor *monitor,
873 @@ -176,31 +280,47 @@
874 g_error_free (error);
878 - if (!ensure_database (monitor, &error))
879 +#ifdef HAVE_LIBSYSTEMD
880 + monitor->sd_source = NULL;
882 + if (sd_booted () > 0)
884 - g_printerr ("Error loading " CKDB_PATH ": %s", error->message);
885 - g_error_free (error);
886 + monitor->sd_source = sd_source_new ();
887 + g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
888 + g_source_attach (monitor->sd_source, NULL);
890 + monitor->database_monitor = NULL;
891 + monitor->database = NULL;
894 +#endif /* HAVE_LIBSYSTEMD */
897 + if (!ensure_database (monitor, &error))
899 + g_printerr ("Error loading " CKDB_PATH ": %s", error->message);
900 + g_error_free (error);
904 - file = g_file_new_for_path (CKDB_PATH);
905 - monitor->database_monitor = g_file_monitor_file (file,
907 + file = g_file_new_for_path (CKDB_PATH);
908 + monitor->database_monitor = g_file_monitor_file (file,
912 - g_object_unref (file);
913 - if (monitor->database_monitor == NULL)
915 - g_printerr ("Error monitoring " CKDB_PATH ": %s", error->message);
916 - g_error_free (error);
920 - g_signal_connect (monitor->database_monitor,
922 - G_CALLBACK (on_file_monitor_changed),
924 + g_object_unref (file);
925 + if (monitor->database_monitor == NULL)
927 + g_printerr ("Error monitoring " CKDB_PATH ": %s", error->message);
928 + g_error_free (error);
932 + g_signal_connect (monitor->database_monitor,
934 + G_CALLBACK (on_file_monitor_changed),
941 if (monitor->database != NULL)
942 g_key_file_free (monitor->database);
944 + if (monitor->sd_source != NULL)
946 + g_source_destroy (monitor->sd_source);
947 + g_source_unref (monitor->sd_source);
950 if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL)
951 G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object);
953 @@ -310,22 +436,38 @@
955 else if (POLKIT_IS_UNIX_SESSION (subject))
957 - if (!ensure_database (monitor, error))
958 +#ifdef HAVE_LIBSYSTEMD
959 + if (monitor->sd_source != NULL)
961 - g_prefix_error (error, "Error getting user for session: Error ensuring CK database at " CKDB_PATH ": ");
963 + if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
965 + g_set_error (error,
967 + POLKIT_ERROR_FAILED,
968 + "Error getting uid for session");
973 - group = g_strdup_printf ("Session %s", polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)));
974 - local_error = NULL;
975 - uid = g_key_file_get_integer (monitor->database, group, "uid", &local_error);
976 - if (local_error != NULL)
978 +#endif /* HAVE_LIBSYSTEMD */
980 - g_propagate_prefixed_error (error, local_error, "Error getting uid using " CKDB_PATH ": ");
981 + if (!ensure_database (monitor, error))
983 + g_prefix_error (error, "Error getting user for session: Error ensuring CK database at " CKDB_PATH ": ");
987 + group = g_strdup_printf ("Session %s", polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)));
988 + local_error = NULL;
989 + uid = g_key_file_get_integer (monitor->database, group, "uid", &local_error);
990 + if (local_error != NULL)
992 + g_propagate_prefixed_error (error, local_error, "Error getting uid using " CKDB_PATH ": ");
1001 ret = polkit_unix_user_new (uid);
1003 @@ -349,35 +491,26 @@
1004 PolkitSubject *subject,
1007 - PolkitSubject *session;
1010 + PolkitSubject *session = NULL;
1013 if (POLKIT_IS_UNIX_PROCESS (subject))
1015 - const gchar *session_id;
1017 - result = g_dbus_connection_call_sync (monitor->system_bus,
1018 - "org.freedesktop.ConsoleKit",
1019 - "/org/freedesktop/ConsoleKit/Manager",
1020 - "org.freedesktop.ConsoleKit.Manager",
1021 - "GetSessionForUnixProcess",
1022 - g_variant_new ("(u)", polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject))),
1023 - G_VARIANT_TYPE ("(o)"),
1024 - G_DBUS_CALL_FLAGS_NONE,
1025 - -1, /* timeout_msec */
1026 - NULL, /* GCancellable */
1028 - if (result == NULL)
1030 - g_variant_get (result, "(&o)", &session_id);
1031 - session = polkit_unix_session_new (session_id);
1032 - g_variant_unref (result);
1033 + { /* We already have a process; now do process -> pid */
1034 + pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject));
1036 else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1039 - const gchar *session_id;
1040 + { /* Convert bus name to process / pid */
1041 +#ifdef HAVE_LIBSYSTEMD
1042 + if (monitor->sd_source != NULL)
1044 + PolkitUnixProcess *process = (PolkitUnixProcess*)polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME(subject), NULL, error);
1047 + pid = polkit_unix_process_get_pid (process);
1048 + g_object_unref (process);
1054 result = g_dbus_connection_call_sync (monitor->system_bus,
1055 @@ -395,23 +528,7 @@
1057 g_variant_get (result, "(u)", &pid);
1058 g_variant_unref (result);
1060 - result = g_dbus_connection_call_sync (monitor->system_bus,
1061 - "org.freedesktop.ConsoleKit",
1062 - "/org/freedesktop/ConsoleKit/Manager",
1063 - "org.freedesktop.ConsoleKit.Manager",
1064 - "GetSessionForUnixProcess",
1065 - g_variant_new ("(u)", pid),
1066 - G_VARIANT_TYPE ("(o)"),
1067 - G_DBUS_CALL_FLAGS_NONE,
1068 - -1, /* timeout_msec */
1069 - NULL, /* GCancellable */
1071 - if (result == NULL)
1073 - g_variant_get (result, "(&o)", &session_id);
1074 - session = polkit_unix_session_new (session_id);
1075 - g_variant_unref (result);
1080 @@ -420,8 +537,57 @@
1081 POLKIT_ERROR_NOT_SUPPORTED,
1082 "Cannot get user for subject of type %s",
1083 g_type_name (G_TYPE_FROM_INSTANCE (subject)));
1087 + /* Now do pid -> same session */
1088 +#ifdef HAVE_LIBSYSTEMD
1089 + if (monitor->sd_source != NULL)
1091 + gchar *session_id;
1092 + if (sd_pid_get_session (pid, &session_id) >= 0)
1094 + session = polkit_unix_session_new (session_id);
1095 + free (session_id);
1098 +#if HAVE_SD_UID_GET_DISPLAY
1100 + /* Now do pid -> uid -> graphical session (systemd version 213)*/
1101 + if (sd_pid_get_owner_uid (pid, &uid) < 0)
1104 + if (sd_uid_get_display (uid, &session_id) >= 0)
1106 + session = polkit_unix_session_new (session_id);
1107 + free (session_id);
1113 +#endif /* HAVE_LIBSYSTEMD */
1115 + gchar *session_id;
1117 + result = g_dbus_connection_call_sync (monitor->system_bus,
1118 + "org.freedesktop.ConsoleKit",
1119 + "/org/freedesktop/ConsoleKit/Manager",
1120 + "org.freedesktop.ConsoleKit.Manager",
1121 + "GetSessionForUnixProcess",
1122 + g_variant_new ("(u)", pid),
1123 + G_VARIANT_TYPE ("(o)"),
1124 + G_DBUS_CALL_FLAGS_NONE,
1125 + -1, /* timeout_msec */
1126 + NULL, /* GCancellable */
1128 + if (result == NULL)
1130 + g_variant_get (result, "(&o)", &session_id);
1131 + session = polkit_unix_session_new (session_id);
1132 + g_variant_unref (result);
1138 @@ -472,7 +638,22 @@
1139 polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
1140 PolkitSubject *session)
1142 - return get_boolean (monitor, session, "is_local");
1143 +#ifdef HAVE_LIBSYSTEMD
1144 + if (monitor->sd_source != NULL)
1148 + if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
1157 +#endif /* HAVE_LIBSYSTEMD */
1158 + return get_boolean (monitor, session, "is_local");
1162 @@ -480,6 +661,44 @@
1163 polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
1164 PolkitSubject *session)
1166 - return get_boolean (monitor, session, "is_active");
1167 +#ifdef HAVE_LIBSYSTEMD
1168 + if (monitor->sd_source != NULL)
1170 + const char *session_id;
1173 + gboolean is_active = FALSE;
1175 + session_id = polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session));
1177 + g_debug ("Checking whether session %s is active.", session_id);
1179 + /* Check whether *any* of the user's current sessions are active. */
1180 + if (sd_session_get_uid (session_id, &uid) < 0)
1183 + g_debug ("Session %s has UID %u.", session_id, uid);
1185 + if (sd_uid_get_state (uid, &state) < 0)
1188 + g_debug ("UID %u has state %s.", uid, state);
1190 + is_active = (g_strcmp0 (state, "active") == 0);
1196 + /* Fall back to checking the session. This is not ideal, since the user
1197 + * might have multiple sessions, and we cannot guarantee to have chosen
1200 + * See: https://bugs.freedesktop.org/show_bug.cgi?id=76358. */
1201 + return sd_session_is_active (session_id);
1204 +#endif /* HAVE_LIBSYSTEMD */
1205 + return get_boolean (monitor, session, "is_active");
1208 diff -ruN polkit-0.113.orig/src/polkitbackend/polkitbackendsessionmonitor-systemd.c polkit-0.113/src/polkitbackend/polkitbackendsessionmonitor-systemd.c
1209 --- polkit-0.113.orig/src/polkitbackend/polkitbackendsessionmonitor-systemd.c 2015-06-19 22:31:02.000000000 +0200
1210 +++ polkit-0.113/src/polkitbackend/polkitbackendsessionmonitor-systemd.c 1970-01-01 01:00:00.000000000 +0100
1213 - * Copyright (C) 2011 Red Hat, Inc.
1215 - * This library is free software; you can redistribute it and/or
1216 - * modify it under the terms of the GNU Lesser General Public
1217 - * License as published by the Free Software Foundation; either
1218 - * version 2 of the License, or (at your option) any later version.
1220 - * This library is distributed in the hope that it will be useful,
1221 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1222 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1223 - * Lesser General Public License for more details.
1225 - * You should have received a copy of the GNU Lesser General
1226 - * Public License along with this library; if not, write to the
1227 - * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1228 - * Boston, MA 02111-1307, USA.
1230 - * Author: Matthias Clasen
1233 -#include "config.h"
1237 -#include <string.h>
1238 -#include <glib/gstdio.h>
1239 -#include <systemd/sd-login.h>
1240 -#include <stdlib.h>
1242 -#include <polkit/polkit.h>
1243 -#include "polkitbackendsessionmonitor.h"
1246 - * SECTION:polkitbackendsessionmonitor
1247 - * @title: PolkitBackendSessionMonitor
1248 - * @short_description: Monitor sessions
1250 - * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
1257 - sd_login_monitor *monitor;
1261 -sd_source_prepare (GSource *source,
1269 -sd_source_check (GSource *source)
1271 - SdSource *sd_source = (SdSource *)source;
1273 - return sd_source->pollfd.revents != 0;
1277 -sd_source_dispatch (GSource *source,
1278 - GSourceFunc callback,
1279 - gpointer user_data)
1282 - SdSource *sd_source = (SdSource *)source;
1285 - g_warn_if_fail (callback != NULL);
1287 - ret = (*callback) (user_data);
1289 - sd_login_monitor_flush (sd_source->monitor);
1295 -sd_source_finalize (GSource *source)
1297 - SdSource *sd_source = (SdSource*)source;
1299 - sd_login_monitor_unref (sd_source->monitor);
1302 -static GSourceFuncs sd_source_funcs = {
1303 - sd_source_prepare,
1305 - sd_source_dispatch,
1306 - sd_source_finalize
1310 -sd_source_new (void)
1313 - SdSource *sd_source;
1316 - source = g_source_new (&sd_source_funcs, sizeof (SdSource));
1317 - sd_source = (SdSource *)source;
1319 - if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
1321 - g_printerr ("Error getting login monitor: %d", ret);
1325 - sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
1326 - sd_source->pollfd.events = G_IO_IN;
1327 - g_source_add_poll (source, &sd_source->pollfd);
1333 -struct _PolkitBackendSessionMonitor
1335 - GObject parent_instance;
1337 - GDBusConnection *system_bus;
1339 - GSource *sd_source;
1342 -struct _PolkitBackendSessionMonitorClass
1344 - GObjectClass parent_class;
1346 - void (*changed) (PolkitBackendSessionMonitor *monitor);
1356 -static guint signals[LAST_SIGNAL] = {0};
1358 -G_DEFINE_TYPE (PolkitBackendSessionMonitor, polkit_backend_session_monitor, G_TYPE_OBJECT);
1360 -/* ---------------------------------------------------------------------------------------------------- */
1363 -sessions_changed (gpointer user_data)
1365 - PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
1367 - g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
1374 -polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor)
1379 - monitor->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
1380 - if (monitor->system_bus == NULL)
1382 - g_printerr ("Error getting system bus: %s", error->message);
1383 - g_error_free (error);
1386 - monitor->sd_source = sd_source_new ();
1387 - g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
1388 - g_source_attach (monitor->sd_source, NULL);
1392 -polkit_backend_session_monitor_finalize (GObject *object)
1394 - PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (object);
1396 - if (monitor->system_bus != NULL)
1397 - g_object_unref (monitor->system_bus);
1399 - if (monitor->sd_source != NULL)
1401 - g_source_destroy (monitor->sd_source);
1402 - g_source_unref (monitor->sd_source);
1405 - if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL)
1406 - G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object);
1410 -polkit_backend_session_monitor_class_init (PolkitBackendSessionMonitorClass *klass)
1412 - GObjectClass *gobject_class;
1414 - gobject_class = G_OBJECT_CLASS (klass);
1416 - gobject_class->finalize = polkit_backend_session_monitor_finalize;
1419 - * PolkitBackendSessionMonitor::changed:
1420 - * @monitor: A #PolkitBackendSessionMonitor
1422 - * Emitted when something changes.
1424 - signals[CHANGED_SIGNAL] = g_signal_new ("changed",
1425 - POLKIT_BACKEND_TYPE_SESSION_MONITOR,
1426 - G_SIGNAL_RUN_LAST,
1427 - G_STRUCT_OFFSET (PolkitBackendSessionMonitorClass, changed),
1428 - NULL, /* accumulator */
1429 - NULL, /* accumulator data */
1430 - g_cclosure_marshal_VOID__VOID,
1435 -PolkitBackendSessionMonitor *
1436 -polkit_backend_session_monitor_new (void)
1438 - PolkitBackendSessionMonitor *monitor;
1440 - monitor = POLKIT_BACKEND_SESSION_MONITOR (g_object_new (POLKIT_BACKEND_TYPE_SESSION_MONITOR, NULL));
1445 -/* ---------------------------------------------------------------------------------------------------- */
1448 -polkit_backend_session_monitor_get_sessions (PolkitBackendSessionMonitor *monitor)
1454 -/* ---------------------------------------------------------------------------------------------------- */
1457 - * polkit_backend_session_monitor_get_user:
1458 - * @monitor: A #PolkitBackendSessionMonitor.
1459 - * @subject: A #PolkitSubject.
1460 - * @error: Return location for error.
1462 - * Gets the user corresponding to @subject or %NULL if no user exists.
1464 - * Returns: %NULL if @error is set otherwise a #PolkitUnixUser that should be freed with g_object_unref().
1467 -polkit_backend_session_monitor_get_user_for_subject (PolkitBackendSessionMonitor *monitor,
1468 - PolkitSubject *subject,
1471 - PolkitIdentity *ret;
1476 - if (POLKIT_IS_UNIX_PROCESS (subject))
1478 - uid = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject));
1479 - if ((gint) uid == -1)
1481 - g_set_error (error,
1483 - POLKIT_ERROR_FAILED,
1484 - "Unix process subject does not have uid set");
1487 - ret = polkit_unix_user_new (uid);
1489 - else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1491 - ret = (PolkitIdentity*)polkit_system_bus_name_get_user_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error);
1493 - else if (POLKIT_IS_UNIX_SESSION (subject))
1496 - if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
1498 - g_set_error (error,
1500 - POLKIT_ERROR_FAILED,
1501 - "Error getting uid for session");
1505 - ret = polkit_unix_user_new (uid);
1513 - * polkit_backend_session_monitor_get_session_for_subject:
1514 - * @monitor: A #PolkitBackendSessionMonitor.
1515 - * @subject: A #PolkitSubject.
1516 - * @error: Return location for error.
1518 - * Gets the session corresponding to @subject or %NULL if no session exists.
1520 - * Returns: %NULL if @error is set otherwise a #PolkitUnixSession that should be freed with g_object_unref().
1523 -polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMonitor *monitor,
1524 - PolkitSubject *subject,
1527 - PolkitUnixProcess *tmp_process = NULL;
1528 - PolkitUnixProcess *process = NULL;
1529 - PolkitSubject *session = NULL;
1530 - char *session_id = NULL;
1532 -#if HAVE_SD_UID_GET_DISPLAY
1536 - if (POLKIT_IS_UNIX_PROCESS (subject))
1537 - process = POLKIT_UNIX_PROCESS (subject); /* We already have a process */
1538 - else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1540 - /* Convert bus name to process */
1541 - tmp_process = (PolkitUnixProcess*)polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error);
1544 - process = tmp_process;
1548 - g_set_error (error,
1550 - POLKIT_ERROR_NOT_SUPPORTED,
1551 - "Cannot get session for subject of type %s",
1552 - g_type_name (G_TYPE_FROM_INSTANCE (subject)));
1555 - /* Now do process -> pid -> same session */
1556 - g_assert (process != NULL);
1557 - pid = polkit_unix_process_get_pid (process);
1559 - if (sd_pid_get_session (pid, &session_id) >= 0)
1561 - session = polkit_unix_session_new (session_id);
1565 -#if HAVE_SD_UID_GET_DISPLAY
1566 - /* Now do process -> uid -> graphical session (systemd version 213)*/
1567 - if (sd_pid_get_owner_uid (pid, &uid) < 0)
1570 - if (sd_uid_get_display (uid, &session_id) >= 0)
1572 - session = polkit_unix_session_new (session_id);
1578 - free (session_id);
1579 - if (tmp_process) g_object_unref (tmp_process);
1584 -polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
1585 - PolkitSubject *session)
1589 - if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
1600 -polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
1601 - PolkitSubject *session)
1603 - const char *session_id;
1606 - gboolean is_active = FALSE;
1608 - session_id = polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session));
1610 - g_debug ("Checking whether session %s is active.", session_id);
1612 - /* Check whether *any* of the user's current sessions are active. */
1613 - if (sd_session_get_uid (session_id, &uid) < 0)
1616 - g_debug ("Session %s has UID %u.", session_id, uid);
1618 - if (sd_uid_get_state (uid, &state) < 0)
1621 - g_debug ("UID %u has state %s.", uid, state);
1623 - is_active = (g_strcmp0 (state, "active") == 0);
1629 - /* Fall back to checking the session. This is not ideal, since the user
1630 - * might have multiple sessions, and we cannot guarantee to have chosen
1633 - * See: https://bugs.freedesktop.org/show_bug.cgi?id=76358. */
1634 - return sd_session_is_active (session_id);