1 diff -urN polkit-0.107.old/configure.ac polkit-0.107/configure.ac
2 --- polkit-0.107.old/configure.ac 2012-10-07 21:31:48.402911710 +0200
3 +++ polkit-0.107/configure.ac 2012-10-07 22:27:11.041586743 +0200
5 [enable_libsystemd_login=auto])
6 if test "$enable_libsystemd_login" != "no"; then
7 PKG_CHECK_MODULES(LIBSYSTEMD_LOGIN,
9 + [libsystemd-login libsystemd-daemon],
10 have_libsystemd_login=yes,
11 have_libsystemd_login=no)
12 if test "$have_libsystemd_login" = "yes"; then
13 diff -urN polkit-0.107.old/src/polkit/Makefile.am polkit-0.107/src/polkit/Makefile.am
14 --- polkit-0.107.old/src/polkit/Makefile.am 2012-10-07 21:31:48.379578504 +0200
15 +++ polkit-0.107/src/polkit/Makefile.am 2012-10-07 21:51:14.746593099 +0200
17 polkitimplicitauthorization.c polkitimplicitauthorization.h \
18 polkittemporaryauthorization.c polkittemporaryauthorization.h \
19 polkitpermission.c polkitpermission.h \
22 -if HAVE_LIBSYSTEMD_LOGIN
23 -libpolkit_gobject_1_la_SOURCES += \
24 - polkitunixsession-systemd.c polkitunixsession.h
26 -libpolkit_gobject_1_la_SOURCES += \
27 - polkitunixsession.c polkitunixsession.h
29 + polkitunixsession.c polkitunixsession.h \
32 libpolkit_gobject_1_la_CFLAGS = \
33 -D_POLKIT_COMPILATION \
34 diff -urN polkit-0.107.old/src/polkit/polkitunixsession.c polkit-0.107/src/polkit/polkitunixsession.c
35 --- polkit-0.107.old/src/polkit/polkitunixsession.c 2012-10-07 21:31:48.376245189 +0200
36 +++ polkit-0.107/src/polkit/polkitunixsession.c 2012-10-07 22:44:55.042489220 +0200
38 #include "polkiterror.h"
39 #include "polkitprivate.h"
41 +#ifdef HAVE_LIBSYSTEMD_LOGIN
43 +# include <systemd/sd-daemon.h>
44 +# include <systemd/sd-login.h>
48 * SECTION:polkitunixsession
49 * @title: PolkitUnixSession
52 PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
53 GDBusConnection *connection;
55 + GVariant *result = NULL;
61 - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
62 - if (connection == NULL)
64 +#ifdef HAVE_LIBSYSTEMD_LOGIN
67 - result = g_dbus_connection_call_sync (connection,
68 - "org.freedesktop.ConsoleKit", /* name */
69 - session->session_id, /* object path */
70 - "org.freedesktop.ConsoleKit.Session", /* interface name */
71 - "GetUser", /* method */
72 - NULL, /* parameters */
73 - G_VARIANT_TYPE ("(u)"),
74 - G_DBUS_CALL_FLAGS_NONE,
80 + if (sd_booted () > 0)
82 + if (sd_session_get_uid (session->session_id, &uid) == 0)
86 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
88 + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
89 + if (connection == NULL)
92 + result = g_dbus_connection_call_sync (connection,
93 + "org.freedesktop.ConsoleKit", /* name */
94 + session->session_id, /* object path */
95 + "org.freedesktop.ConsoleKit.Session", /* interface name */
96 + "GetUser", /* method */
97 + NULL, /* parameters */
98 + G_VARIANT_TYPE ("(u)"),
99 + G_DBUS_CALL_FLAGS_NONE,
103 + if (result == NULL)
107 - g_variant_unref (result);
109 + g_variant_unref (result);
113 if (connection != NULL)
114 g_object_unref (connection);
127 @@ -484,28 +505,51 @@
131 - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
132 - if (connection == NULL)
135 - result = g_dbus_connection_call_sync (connection,
136 - "org.freedesktop.ConsoleKit", /* name */
137 - "/org/freedesktop/ConsoleKit/Manager", /* object path */
138 - "org.freedesktop.ConsoleKit.Manager", /* interface name */
139 - "GetSessionForUnixProcess", /* method */
140 - g_variant_new ("(u)", session->pid), /* parameters */
141 - G_VARIANT_TYPE ("(o)"),
142 - G_DBUS_CALL_FLAGS_NONE,
146 - if (result == NULL)
148 +#ifdef HAVE_LIBSYSTEMD_LOGIN
151 + if (sd_booted () > 0)
153 + if (sd_pid_get_session (session->pid, &s) == 0)
155 + session->session_id = g_strdup (s);
161 + g_set_error (error,
163 + POLKIT_ERROR_FAILED,
164 + "No session for pid %d",
165 + (gint) session->pid);
168 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
170 + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
171 + if (connection == NULL)
174 + result = g_dbus_connection_call_sync (connection,
175 + "org.freedesktop.ConsoleKit", /* name */
176 + "/org/freedesktop/ConsoleKit/Manager", /* object path */
177 + "org.freedesktop.ConsoleKit.Manager", /* interface name */
178 + "GetSessionForUnixProcess", /* method */
179 + g_variant_new ("(u)", session->pid), /* parameters */
180 + G_VARIANT_TYPE ("(o)"),
181 + G_DBUS_CALL_FLAGS_NONE,
185 + if (result == NULL)
188 - g_variant_get (result, "(o)", &session->session_id);
189 - g_variant_unref (result);
190 + g_variant_get (result, "(o)", &session->session_id);
191 + g_variant_unref (result);
198 if (connection != NULL)
199 diff -urN polkit-0.107.old/src/polkit/polkitunixsession-systemd.c polkit-0.107/src/polkit/polkitunixsession-systemd.c
200 --- polkit-0.107.old/src/polkit/polkitunixsession-systemd.c 2012-10-07 21:31:48.379578504 +0200
201 +++ polkit-0.107/src/polkit/polkitunixsession-systemd.c 1970-01-01 01:00:00.000000000 +0100
204 - * Copyright (C) 2011 Red Hat, Inc.
206 - * This library is free software; you can redistribute it and/or
207 - * modify it under the terms of the GNU Lesser General Public
208 - * License as published by the Free Software Foundation; either
209 - * version 2 of the License, or (at your option) any later version.
211 - * This library is distributed in the hope that it will be useful,
212 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
213 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
214 - * Lesser General Public License for more details.
216 - * You should have received a copy of the GNU Lesser General
217 - * Public License along with this library; if not, write to the
218 - * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
219 - * Boston, MA 02111-1307, USA.
221 - * Author: Matthias Clasen
224 -#ifdef HAVE_CONFIG_H
225 -# include "config.h"
230 -#include "polkitunixsession.h"
231 -#include "polkitsubject.h"
232 -#include "polkiterror.h"
233 -#include "polkitprivate.h"
235 -#include <systemd/sd-login.h>
238 - * SECTION:polkitunixsession
239 - * @title: PolkitUnixSession
240 - * @short_description: Unix sessions
242 - * An object that represents an user session.
244 - * The session id is an opaque string obtained from ConsoleKit.
248 - * PolkitUnixSession:
250 - * The #PolkitUnixSession struct should not be accessed directly.
252 -struct _PolkitUnixSession
254 - GObject parent_instance;
261 -struct _PolkitUnixSessionClass
263 - GObjectClass parent_class;
273 -static void subject_iface_init (PolkitSubjectIface *subject_iface);
274 -static void initable_iface_init (GInitableIface *initable_iface);
275 -static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
277 -G_DEFINE_TYPE_WITH_CODE (PolkitUnixSession, polkit_unix_session, G_TYPE_OBJECT,
278 - G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init)
279 - G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
280 - G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
284 -polkit_unix_session_init (PolkitUnixSession *session)
289 -polkit_unix_session_finalize (GObject *object)
291 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
293 - g_free (session->session_id);
295 - if (G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize != NULL)
296 - G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize (object);
300 -polkit_unix_session_get_property (GObject *object,
305 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
309 - case PROP_SESSION_ID:
310 - g_value_set_string (value, session->session_id);
314 - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
320 -polkit_unix_session_set_property (GObject *object,
322 - const GValue *value,
325 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
329 - case PROP_SESSION_ID:
330 - polkit_unix_session_set_session_id (session, g_value_get_string (value));
334 - session->pid = g_value_get_int (value);
338 - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 -polkit_unix_session_class_init (PolkitUnixSessionClass *klass)
346 - GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
348 - gobject_class->finalize = polkit_unix_session_finalize;
349 - gobject_class->get_property = polkit_unix_session_get_property;
350 - gobject_class->set_property = polkit_unix_session_set_property;
353 - * PolkitUnixSession:session-id:
355 - * The UNIX session id.
357 - g_object_class_install_property (gobject_class,
359 - g_param_spec_string ("session-id",
361 - "The UNIX session ID",
363 - G_PARAM_CONSTRUCT |
364 - G_PARAM_READWRITE |
365 - G_PARAM_STATIC_NAME |
366 - G_PARAM_STATIC_BLURB |
367 - G_PARAM_STATIC_NICK));
371 - * PolkitUnixSession:pid:
373 - * The UNIX process id to look up the session.
375 - g_object_class_install_property (gobject_class,
377 - g_param_spec_int ("pid",
379 - "Process ID to use for looking up the session",
383 - G_PARAM_CONSTRUCT_ONLY |
385 - G_PARAM_STATIC_NAME |
386 - G_PARAM_STATIC_BLURB |
387 - G_PARAM_STATIC_NICK));
392 - * polkit_unix_session_get_session_id:
393 - * @session: A #PolkitUnixSession.
395 - * Gets the session id for @session.
397 - * Returns: The session id for @session. Do not free this string, it
398 - * is owned by @session.
401 -polkit_unix_session_get_session_id (PolkitUnixSession *session)
403 - g_return_val_if_fail (POLKIT_IS_UNIX_SESSION (session), NULL);
404 - return session->session_id;
408 - * polkit_unix_session_set_session_id:
409 - * @session: A #PolkitUnixSession.
410 - * @session_id: The session id.
412 - * Sets the session id for @session to @session_id.
415 -polkit_unix_session_set_session_id (PolkitUnixSession *session,
416 - const gchar *session_id)
418 - g_return_if_fail (POLKIT_IS_UNIX_SESSION (session));
419 - /*g_return_if_fail (session_id != NULL);*/
420 - g_free (session->session_id);
421 - session->session_id = g_strdup (session_id);
425 - * polkit_unix_session_new:
426 - * @session_id: The session id.
428 - * Creates a new #PolkitUnixSession for @session_id.
430 - * Returns: (transfer full): A #PolkitUnixSession. Free with g_object_unref().
433 -polkit_unix_session_new (const gchar *session_id)
435 - return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_UNIX_SESSION,
436 - "session-id", session_id,
441 - * polkit_unix_session_new_for_process:
442 - * @pid: The process id of the process to get the session for.
443 - * @cancellable: (allow-none): A #GCancellable or %NULL.
444 - * @callback: A #GAsyncReadyCallback to call when the request is satisfied
445 - * @user_data: The data to pass to @callback.
447 - * Asynchronously creates a new #PolkitUnixSession object for the
448 - * process with process id @pid.
450 - * When the operation is finished, @callback will be invoked in the
451 - * <link linkend="g-main-context-push-thread-default">thread-default
452 - * main loop</link> of the thread you are calling this method
453 - * from. You can then call
454 - * polkit_unix_session_new_for_process_finish() to get the result of
457 - * This method constructs the object asynchronously, for the synchronous and blocking version
458 - * use polkit_unix_session_new_for_process_sync().
461 -polkit_unix_session_new_for_process (gint pid,
462 - GCancellable *cancellable,
463 - GAsyncReadyCallback callback,
464 - gpointer user_data)
466 - g_async_initable_new_async (POLKIT_TYPE_UNIX_SESSION,
467 - G_PRIORITY_DEFAULT,
476 - * polkit_unix_session_new_for_process_finish:
477 - * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to polkit_unix_session_new_for_process().
478 - * @error: (allow-none): Return location for error.
480 - * Finishes constructing a #PolkitSubject for a process id.
482 - * Returns: (transfer full) (allow-none): A #PolkitUnixSession for the @pid passed to
483 - * polkit_unix_session_new_for_process() or %NULL if @error is
484 - * set. Free with g_object_unref().
487 -polkit_unix_session_new_for_process_finish (GAsyncResult *res,
491 - GObject *source_object;
493 - source_object = g_async_result_get_source_object (res);
494 - g_assert (source_object != NULL);
496 - object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
499 - g_object_unref (source_object);
501 - if (object != NULL)
502 - return POLKIT_SUBJECT (object);
509 - * polkit_unix_session_new_for_process_sync:
510 - * @pid: The process id of the process to get the session for.
511 - * @cancellable: (allow-none): A #GCancellable or %NULL.
512 - * @error: (allow-none): Return location for error.
514 - * Creates a new #PolkitUnixSession for the process with process id @pid.
516 - * This is a synchronous call - the calling thread is blocked until a
517 - * reply is received. For the asynchronous version, see
518 - * polkit_unix_session_new_for_process().
520 - * Returns: (allow-none) (transfer full): A #PolkitUnixSession for
521 - * @pid or %NULL if @error is set. Free with g_object_unref().
524 -polkit_unix_session_new_for_process_sync (gint pid,
525 - GCancellable *cancellable,
528 - return POLKIT_SUBJECT (g_initable_new (POLKIT_TYPE_UNIX_SESSION,
536 -polkit_unix_session_hash (PolkitSubject *subject)
538 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
540 - return g_str_hash (session->session_id);
544 -polkit_unix_session_equal (PolkitSubject *a,
547 - PolkitUnixSession *session_a;
548 - PolkitUnixSession *session_b;
550 - session_a = POLKIT_UNIX_SESSION (a);
551 - session_b = POLKIT_UNIX_SESSION (b);
553 - return g_strcmp0 (session_a->session_id, session_b->session_id) == 0;
557 -polkit_unix_session_to_string (PolkitSubject *subject)
559 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
561 - return g_strdup_printf ("unix-session:%s", session->session_id);
565 -polkit_unix_session_exists_sync (PolkitSubject *subject,
566 - GCancellable *cancellable,
569 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
570 - gboolean ret = FALSE;
573 - if (sd_session_get_uid (session->session_id, &uid) == 0)
580 -exists_in_thread_func (GSimpleAsyncResult *res,
582 - GCancellable *cancellable)
586 - if (!polkit_unix_session_exists_sync (POLKIT_SUBJECT (object),
590 - g_simple_async_result_set_from_error (res, error);
591 - g_error_free (error);
596 -polkit_unix_session_exists (PolkitSubject *subject,
597 - GCancellable *cancellable,
598 - GAsyncReadyCallback callback,
599 - gpointer user_data)
601 - GSimpleAsyncResult *simple;
603 - g_return_if_fail (POLKIT_IS_UNIX_SESSION (subject));
605 - simple = g_simple_async_result_new (G_OBJECT (subject),
608 - polkit_unix_session_exists);
609 - g_simple_async_result_run_in_thread (simple,
610 - exists_in_thread_func,
611 - G_PRIORITY_DEFAULT,
613 - g_object_unref (simple);
617 -polkit_unix_session_exists_finish (PolkitSubject *subject,
621 - GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
624 - g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == polkit_unix_session_exists);
628 - if (g_simple_async_result_propagate_error (simple, error))
631 - ret = g_simple_async_result_get_op_res_gboolean (simple);
638 -subject_iface_init (PolkitSubjectIface *subject_iface)
640 - subject_iface->hash = polkit_unix_session_hash;
641 - subject_iface->equal = polkit_unix_session_equal;
642 - subject_iface->to_string = polkit_unix_session_to_string;
643 - subject_iface->exists = polkit_unix_session_exists;
644 - subject_iface->exists_finish = polkit_unix_session_exists_finish;
645 - subject_iface->exists_sync = polkit_unix_session_exists_sync;
649 -polkit_unix_session_initable_init (GInitable *initable,
650 - GCancellable *cancellable,
653 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (initable);
654 - gboolean ret = FALSE;
657 - if (session->session_id != NULL)
659 - /* already set, nothing to do */
664 - if (sd_pid_get_session (session->pid, &s) == 0)
666 - session->session_id = g_strdup (s);
672 - g_set_error (error,
674 - POLKIT_ERROR_FAILED,
675 - "No session for pid %d",
676 - (gint) session->pid);
683 -initable_iface_init (GInitableIface *initable_iface)
685 - initable_iface->init = polkit_unix_session_initable_init;
689 -async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
691 - /* use default implementation to run GInitable code in a thread */
693 diff -urN polkit-0.107.old/src/polkitbackend/Makefile.am polkit-0.107/src/polkitbackend/Makefile.am
694 --- polkit-0.107.old/src/polkitbackend/Makefile.am 2012-10-07 21:31:48.382911819 +0200
695 +++ polkit-0.107/src/polkitbackend/Makefile.am 2012-10-07 21:53:38.455814545 +0200
697 polkitbackendactionpool.h polkitbackendactionpool.c \
698 polkitbackendconfigsource.h polkitbackendconfigsource.c \
699 polkitbackendactionlookup.h polkitbackendactionlookup.c \
702 -if HAVE_LIBSYSTEMD_LOGIN
703 -libpolkit_backend_1_la_SOURCES += \
704 - polkitbackendsessionmonitor.h polkitbackendsessionmonitor-systemd.c
706 -libpolkit_backend_1_la_SOURCES += \
707 - polkitbackendsessionmonitor.h polkitbackendsessionmonitor.c
709 + polkitbackendsessionmonitor.h polkitbackendsessionmonitor.c \
712 libpolkit_backend_1_la_CFLAGS = \
713 -D_POLKIT_COMPILATION \
714 diff -urN polkit-0.107.old/src/polkitbackend/polkitbackendjsauthority.c polkit-0.107/src/polkitbackend/polkitbackendjsauthority.c
715 --- polkit-0.107.old/src/polkitbackend/polkitbackendjsauthority.c 2012-10-07 21:31:48.382911819 +0200
716 +++ polkit-0.107/src/polkitbackend/polkitbackendjsauthority.c 2012-10-07 21:52:58.786029455 +0200
718 #include <polkit/polkitprivate.h>
720 #ifdef HAVE_LIBSYSTEMD_LOGIN
721 +#include <systemd/sd-daemon.h>
722 #include <systemd/sd-login.h>
723 #endif /* HAVE_LIBSYSTEMD_LOGIN */
725 @@ -731,11 +732,14 @@
728 #ifdef HAVE_LIBSYSTEMD_LOGIN
729 - if (sd_pid_get_session (pid, &session_str) == 0)
730 + if (sd_booted () > 0)
732 - if (sd_session_get_seat (session_str, &seat_str) == 0)
733 + if (sd_pid_get_session (pid, &session_str) == 0)
736 + if (sd_session_get_seat (session_str, &seat_str) == 0)
742 #endif /* HAVE_LIBSYSTEMD_LOGIN */
743 diff -urN polkit-0.107.old/src/polkitbackend/polkitbackendsessionmonitor.c polkit-0.107/src/polkitbackend/polkitbackendsessionmonitor.c
744 --- polkit-0.107.old/src/polkitbackend/polkitbackendsessionmonitor.c 2012-10-07 21:31:48.379578504 +0200
745 +++ polkit-0.107/src/polkitbackend/polkitbackendsessionmonitor.c 2012-10-07 22:25:54.305335789 +0200
748 #include <glib/gstdio.h>
750 +#ifdef HAVE_LIBSYSTEMD_LOGIN
751 +# include <stdlib.h>
752 +# include <systemd/sd-daemon.h>
753 +# include <systemd/sd-login.h>
754 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
756 #include <polkit/polkit.h>
757 #include "polkitbackendsessionmonitor.h"
760 * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
763 +#ifdef HAVE_LIBSYSTEMD_LOGIN
768 + sd_login_monitor *monitor;
772 +sd_source_prepare (GSource *source,
780 +sd_source_check (GSource *source)
782 + SdSource *sd_source = (SdSource *)source;
784 + return sd_source->pollfd.revents != 0;
788 +sd_source_dispatch (GSource *source,
789 + GSourceFunc callback,
790 + gpointer user_data)
793 + SdSource *sd_source = (SdSource *)source;
796 + g_warn_if_fail (callback != NULL);
798 + ret = (*callback) (user_data);
800 + sd_login_monitor_flush (sd_source->monitor);
806 +sd_source_finalize (GSource *source)
808 + SdSource *sd_source = (SdSource*)source;
810 + sd_login_monitor_unref (sd_source->monitor);
813 +static GSourceFuncs sd_source_funcs = {
816 + sd_source_dispatch,
821 +sd_source_new (void)
824 + SdSource *sd_source;
827 + source = g_source_new (&sd_source_funcs, sizeof (SdSource));
828 + sd_source = (SdSource *)source;
830 + if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
832 + g_printerr ("Error getting login monitor: %d", ret);
836 + sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
837 + sd_source->pollfd.events = G_IO_IN;
838 + g_source_add_poll (source, &sd_source->pollfd);
843 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
845 struct _PolkitBackendSessionMonitor
847 GObject parent_instance;
850 GFileMonitor *database_monitor;
851 time_t database_mtime;
853 +#ifdef HAVE_LIBSYSTEMD_LOGIN
854 + GSource *sd_source;
855 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
858 struct _PolkitBackendSessionMonitorClass
861 /* ---------------------------------------------------------------------------------------------------- */
863 +#ifdef HAVE_LIBSYSTEMD_LOGIN
865 +sessions_changed (gpointer user_data)
867 + PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
869 + g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
873 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
876 reload_database (PolkitBackendSessionMonitor *monitor,
878 @@ -176,31 +280,47 @@
879 g_error_free (error);
883 - if (!ensure_database (monitor, &error))
884 +#ifdef HAVE_LIBSYSTEMD_LOGIN
885 + monitor->sd_source = NULL;
887 + if (sd_booted () > 0)
889 - g_printerr ("Error loading " CKDB_PATH ": %s", error->message);
890 - g_error_free (error);
891 + monitor->sd_source = sd_source_new ();
892 + g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
893 + g_source_attach (monitor->sd_source, NULL);
895 + monitor->database_monitor = NULL;
896 + monitor->database = NULL;
899 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
902 + if (!ensure_database (monitor, &error))
904 + g_printerr ("Error loading " CKDB_PATH ": %s", error->message);
905 + g_error_free (error);
909 - file = g_file_new_for_path (CKDB_PATH);
910 - monitor->database_monitor = g_file_monitor_file (file,
912 + file = g_file_new_for_path (CKDB_PATH);
913 + monitor->database_monitor = g_file_monitor_file (file,
917 - g_object_unref (file);
918 - if (monitor->database_monitor == NULL)
920 - g_printerr ("Error monitoring " CKDB_PATH ": %s", error->message);
921 - g_error_free (error);
925 - g_signal_connect (monitor->database_monitor,
927 - G_CALLBACK (on_file_monitor_changed),
929 + g_object_unref (file);
930 + if (monitor->database_monitor == NULL)
932 + g_printerr ("Error monitoring " CKDB_PATH ": %s", error->message);
933 + g_error_free (error);
937 + g_signal_connect (monitor->database_monitor,
939 + G_CALLBACK (on_file_monitor_changed),
946 if (monitor->database != NULL)
947 g_key_file_free (monitor->database);
949 + if (monitor->sd_source != NULL)
951 + g_source_destroy (monitor->sd_source);
952 + g_source_unref (monitor->sd_source);
955 if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL)
956 G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object);
958 @@ -328,22 +454,38 @@
960 else if (POLKIT_IS_UNIX_SESSION (subject))
962 - if (!ensure_database (monitor, error))
963 +#ifdef HAVE_LIBSYSTEMD_LOGIN
964 + if (monitor->sd_source != NULL)
966 - g_prefix_error (error, "Error getting user for session: Error ensuring CK database at " CKDB_PATH ": ");
968 + if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
970 + g_set_error (error,
972 + POLKIT_ERROR_FAILED,
973 + "Error getting uid for session");
978 - group = g_strdup_printf ("Session %s", polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)));
979 - local_error = NULL;
980 - uid = g_key_file_get_integer (monitor->database, group, "uid", &local_error);
981 - if (local_error != NULL)
983 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
985 - g_propagate_prefixed_error (error, local_error, "Error getting uid using " CKDB_PATH ": ");
986 + if (!ensure_database (monitor, error))
988 + g_prefix_error (error, "Error getting user for session: Error ensuring CK database at " CKDB_PATH ": ");
992 + group = g_strdup_printf ("Session %s", polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)));
993 + local_error = NULL;
994 + uid = g_key_file_get_integer (monitor->database, group, "uid", &local_error);
995 + if (local_error != NULL)
997 + g_propagate_prefixed_error (error, local_error, "Error getting uid using " CKDB_PATH ": ");
1006 ret = polkit_unix_user_new (uid);
1008 @@ -373,29 +515,46 @@
1010 if (POLKIT_IS_UNIX_PROCESS (subject))
1012 - const gchar *session_id;
1014 - result = g_dbus_connection_call_sync (monitor->system_bus,
1015 - "org.freedesktop.ConsoleKit",
1016 - "/org/freedesktop/ConsoleKit/Manager",
1017 - "org.freedesktop.ConsoleKit.Manager",
1018 - "GetSessionForUnixProcess",
1019 - g_variant_new ("(u)", polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject))),
1020 - G_VARIANT_TYPE ("(o)"),
1021 - G_DBUS_CALL_FLAGS_NONE,
1022 - -1, /* timeout_msec */
1023 - NULL, /* GCancellable */
1025 - if (result == NULL)
1027 - g_variant_get (result, "(&o)", &session_id);
1028 - session = polkit_unix_session_new (session_id);
1029 - g_variant_unref (result);
1030 +#ifdef HAVE_LIBSYSTEMD_LOGIN
1031 + if (monitor->sd_source != NULL)
1033 + gchar *session_id;
1036 + pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject));
1037 + if (sd_pid_get_session (pid, &session_id) < 0)
1040 + session = polkit_unix_session_new (session_id);
1041 + free (session_id);
1044 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
1046 + const gchar *session_id;
1048 + result = g_dbus_connection_call_sync (monitor->system_bus,
1049 + "org.freedesktop.ConsoleKit",
1050 + "/org/freedesktop/ConsoleKit/Manager",
1051 + "org.freedesktop.ConsoleKit.Manager",
1052 + "GetSessionForUnixProcess",
1053 + g_variant_new ("(u)", polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject))),
1054 + G_VARIANT_TYPE ("(o)"),
1055 + G_DBUS_CALL_FLAGS_NONE,
1056 + -1, /* timeout_msec */
1057 + NULL, /* GCancellable */
1059 + if (result == NULL)
1061 + g_variant_get (result, "(&o)", &session_id);
1062 + session = polkit_unix_session_new (session_id);
1063 + g_variant_unref (result);
1066 else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1069 - const gchar *session_id;
1070 + gchar *session_id;
1073 result = g_dbus_connection_call_sync (monitor->system_bus,
1074 @@ -414,22 +573,35 @@
1075 g_variant_get (result, "(u)", &pid);
1076 g_variant_unref (result);
1078 - result = g_dbus_connection_call_sync (monitor->system_bus,
1079 - "org.freedesktop.ConsoleKit",
1080 - "/org/freedesktop/ConsoleKit/Manager",
1081 - "org.freedesktop.ConsoleKit.Manager",
1082 - "GetSessionForUnixProcess",
1083 - g_variant_new ("(u)", pid),
1084 - G_VARIANT_TYPE ("(o)"),
1085 - G_DBUS_CALL_FLAGS_NONE,
1086 - -1, /* timeout_msec */
1087 - NULL, /* GCancellable */
1089 - if (result == NULL)
1091 - g_variant_get (result, "(&o)", &session_id);
1092 - session = polkit_unix_session_new (session_id);
1093 - g_variant_unref (result);
1094 +#ifdef HAVE_LIBSYSTEMD_LOGIN
1095 + if (monitor->sd_source != NULL)
1097 + if (sd_pid_get_session (pid, &session_id) < 0)
1100 + session = polkit_unix_session_new (session_id);
1101 + free (session_id);
1104 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
1106 + result = g_dbus_connection_call_sync (monitor->system_bus,
1107 + "org.freedesktop.ConsoleKit",
1108 + "/org/freedesktop/ConsoleKit/Manager",
1109 + "org.freedesktop.ConsoleKit.Manager",
1110 + "GetSessionForUnixProcess",
1111 + g_variant_new ("(u)", pid),
1112 + G_VARIANT_TYPE ("(o)"),
1113 + G_DBUS_CALL_FLAGS_NONE,
1114 + -1, /* timeout_msec */
1115 + NULL, /* GCancellable */
1117 + if (result == NULL)
1119 + g_variant_get (result, "(&o)", &session_id);
1120 + session = polkit_unix_session_new (session_id);
1121 + g_variant_unref (result);
1126 @@ -490,7 +662,22 @@
1127 polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
1128 PolkitSubject *session)
1130 - return get_boolean (monitor, session, "is_local");
1131 +#ifdef HAVE_LIBSYSTEMD_LOGIN
1132 + if (monitor->sd_source != NULL)
1136 + if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
1145 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
1146 + return get_boolean (monitor, session, "is_local");
1150 @@ -498,6 +685,11 @@
1151 polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
1152 PolkitSubject *session)
1154 - return get_boolean (monitor, session, "is_active");
1155 +#ifdef HAVE_LIBSYSTEMD_LOGIN
1156 + if (monitor->sd_source != NULL)
1157 + return sd_session_is_active (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)));
1159 +#endif /* HAVE_LIBSYSTEMD_LOGIN */
1160 + return get_boolean (monitor, session, "is_active");
1163 diff -urN polkit-0.107.old/src/polkitbackend/polkitbackendsessionmonitor-systemd.c polkit-0.107/src/polkitbackend/polkitbackendsessionmonitor-systemd.c
1164 --- polkit-0.107.old/src/polkitbackend/polkitbackendsessionmonitor-systemd.c 2012-10-07 21:31:48.382911819 +0200
1165 +++ polkit-0.107/src/polkitbackend/polkitbackendsessionmonitor-systemd.c 1970-01-01 01:00:00.000000000 +0100
1168 - * Copyright (C) 2011 Red Hat, Inc.
1170 - * This library is free software; you can redistribute it and/or
1171 - * modify it under the terms of the GNU Lesser General Public
1172 - * License as published by the Free Software Foundation; either
1173 - * version 2 of the License, or (at your option) any later version.
1175 - * This library is distributed in the hope that it will be useful,
1176 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1177 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1178 - * Lesser General Public License for more details.
1180 - * You should have received a copy of the GNU Lesser General
1181 - * Public License along with this library; if not, write to the
1182 - * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1183 - * Boston, MA 02111-1307, USA.
1185 - * Author: Matthias Clasen
1188 -#include "config.h"
1192 -#include <string.h>
1193 -#include <glib/gstdio.h>
1194 -#include <systemd/sd-login.h>
1195 -#include <stdlib.h>
1197 -#include <polkit/polkit.h>
1198 -#include "polkitbackendsessionmonitor.h"
1201 - * SECTION:polkitbackendsessionmonitor
1202 - * @title: PolkitBackendSessionMonitor
1203 - * @short_description: Monitor sessions
1205 - * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
1212 - sd_login_monitor *monitor;
1216 -sd_source_prepare (GSource *source,
1224 -sd_source_check (GSource *source)
1226 - SdSource *sd_source = (SdSource *)source;
1228 - return sd_source->pollfd.revents != 0;
1232 -sd_source_dispatch (GSource *source,
1233 - GSourceFunc callback,
1234 - gpointer user_data)
1237 - SdSource *sd_source = (SdSource *)source;
1240 - g_warn_if_fail (callback != NULL);
1242 - ret = (*callback) (user_data);
1244 - sd_login_monitor_flush (sd_source->monitor);
1250 -sd_source_finalize (GSource *source)
1252 - SdSource *sd_source = (SdSource*)source;
1254 - sd_login_monitor_unref (sd_source->monitor);
1257 -static GSourceFuncs sd_source_funcs = {
1258 - sd_source_prepare,
1260 - sd_source_dispatch,
1261 - sd_source_finalize
1265 -sd_source_new (void)
1268 - SdSource *sd_source;
1271 - source = g_source_new (&sd_source_funcs, sizeof (SdSource));
1272 - sd_source = (SdSource *)source;
1274 - if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
1276 - g_printerr ("Error getting login monitor: %d", ret);
1280 - sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
1281 - sd_source->pollfd.events = G_IO_IN;
1282 - g_source_add_poll (source, &sd_source->pollfd);
1288 -struct _PolkitBackendSessionMonitor
1290 - GObject parent_instance;
1292 - GDBusConnection *system_bus;
1294 - GSource *sd_source;
1297 -struct _PolkitBackendSessionMonitorClass
1299 - GObjectClass parent_class;
1301 - void (*changed) (PolkitBackendSessionMonitor *monitor);
1311 -static guint signals[LAST_SIGNAL] = {0};
1313 -G_DEFINE_TYPE (PolkitBackendSessionMonitor, polkit_backend_session_monitor, G_TYPE_OBJECT);
1315 -/* ---------------------------------------------------------------------------------------------------- */
1318 -sessions_changed (gpointer user_data)
1320 - PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
1322 - g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
1329 -polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor)
1334 - monitor->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
1335 - if (monitor->system_bus == NULL)
1337 - g_printerr ("Error getting system bus: %s", error->message);
1338 - g_error_free (error);
1341 - monitor->sd_source = sd_source_new ();
1342 - g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
1343 - g_source_attach (monitor->sd_source, NULL);
1347 -polkit_backend_session_monitor_finalize (GObject *object)
1349 - PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (object);
1351 - if (monitor->system_bus != NULL)
1352 - g_object_unref (monitor->system_bus);
1354 - if (monitor->sd_source != NULL)
1356 - g_source_destroy (monitor->sd_source);
1357 - g_source_unref (monitor->sd_source);
1360 - if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL)
1361 - G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object);
1365 -polkit_backend_session_monitor_class_init (PolkitBackendSessionMonitorClass *klass)
1367 - GObjectClass *gobject_class;
1369 - gobject_class = G_OBJECT_CLASS (klass);
1371 - gobject_class->finalize = polkit_backend_session_monitor_finalize;
1374 - * PolkitBackendSessionMonitor::changed:
1375 - * @monitor: A #PolkitBackendSessionMonitor
1377 - * Emitted when something changes.
1379 - signals[CHANGED_SIGNAL] = g_signal_new ("changed",
1380 - POLKIT_BACKEND_TYPE_SESSION_MONITOR,
1381 - G_SIGNAL_RUN_LAST,
1382 - G_STRUCT_OFFSET (PolkitBackendSessionMonitorClass, changed),
1383 - NULL, /* accumulator */
1384 - NULL, /* accumulator data */
1385 - g_cclosure_marshal_VOID__VOID,
1390 -PolkitBackendSessionMonitor *
1391 -polkit_backend_session_monitor_new (void)
1393 - PolkitBackendSessionMonitor *monitor;
1395 - monitor = POLKIT_BACKEND_SESSION_MONITOR (g_object_new (POLKIT_BACKEND_TYPE_SESSION_MONITOR, NULL));
1400 -/* ---------------------------------------------------------------------------------------------------- */
1403 -polkit_backend_session_monitor_get_sessions (PolkitBackendSessionMonitor *monitor)
1409 -/* ---------------------------------------------------------------------------------------------------- */
1412 - * polkit_backend_session_monitor_get_user:
1413 - * @monitor: A #PolkitBackendSessionMonitor.
1414 - * @subject: A #PolkitSubject.
1415 - * @error: Return location for error.
1417 - * Gets the user corresponding to @subject or %NULL if no user exists.
1419 - * Returns: %NULL if @error is set otherwise a #PolkitUnixUser that should be freed with g_object_unref().
1422 -polkit_backend_session_monitor_get_user_for_subject (PolkitBackendSessionMonitor *monitor,
1423 - PolkitSubject *subject,
1426 - PolkitIdentity *ret;
1431 - if (POLKIT_IS_UNIX_PROCESS (subject))
1433 - uid = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject));
1434 - if ((gint) uid == -1)
1436 - g_set_error (error,
1438 - POLKIT_ERROR_FAILED,
1439 - "Unix process subject does not have uid set");
1442 - ret = polkit_unix_user_new (uid);
1444 - else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1448 - result = g_dbus_connection_call_sync (monitor->system_bus,
1449 - "org.freedesktop.DBus",
1450 - "/org/freedesktop/DBus",
1451 - "org.freedesktop.DBus",
1452 - "GetConnectionUnixUser",
1453 - g_variant_new ("(s)", polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))),
1454 - G_VARIANT_TYPE ("(u)"),
1455 - G_DBUS_CALL_FLAGS_NONE,
1456 - -1, /* timeout_msec */
1457 - NULL, /* GCancellable */
1459 - if (result == NULL)
1461 - g_variant_get (result, "(u)", &uid);
1462 - g_variant_unref (result);
1464 - ret = polkit_unix_user_new (uid);
1466 - else if (POLKIT_IS_UNIX_SESSION (subject))
1469 - if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
1471 - g_set_error (error,
1473 - POLKIT_ERROR_FAILED,
1474 - "Error getting uid for session");
1478 - ret = polkit_unix_user_new (uid);
1486 - * polkit_backend_session_monitor_get_session_for_subject:
1487 - * @monitor: A #PolkitBackendSessionMonitor.
1488 - * @subject: A #PolkitSubject.
1489 - * @error: Return location for error.
1491 - * Gets the session corresponding to @subject or %NULL if no session exists.
1493 - * Returns: %NULL if @error is set otherwise a #PolkitUnixSession that should be freed with g_object_unref().
1496 -polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMonitor *monitor,
1497 - PolkitSubject *subject,
1500 - PolkitSubject *session;
1504 - if (POLKIT_IS_UNIX_PROCESS (subject))
1506 - gchar *session_id;
1509 - pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject));
1510 - if (sd_pid_get_session (pid, &session_id) < 0)
1513 - session = polkit_unix_session_new (session_id);
1514 - free (session_id);
1516 - else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1519 - gchar *session_id;
1522 - result = g_dbus_connection_call_sync (monitor->system_bus,
1523 - "org.freedesktop.DBus",
1524 - "/org/freedesktop/DBus",
1525 - "org.freedesktop.DBus",
1526 - "GetConnectionUnixProcessID",
1527 - g_variant_new ("(s)", polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))),
1528 - G_VARIANT_TYPE ("(u)"),
1529 - G_DBUS_CALL_FLAGS_NONE,
1530 - -1, /* timeout_msec */
1531 - NULL, /* GCancellable */
1533 - if (result == NULL)
1535 - g_variant_get (result, "(u)", &pid);
1536 - g_variant_unref (result);
1538 - if (sd_pid_get_session (pid, &session_id) < 0)
1541 - session = polkit_unix_session_new (session_id);
1542 - free (session_id);
1546 - g_set_error (error,
1548 - POLKIT_ERROR_NOT_SUPPORTED,
1549 - "Cannot get user for subject of type %s",
1550 - g_type_name (G_TYPE_FROM_INSTANCE (subject)));
1559 -polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
1560 - PolkitSubject *session)
1564 - if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
1575 -polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
1576 - PolkitSubject *session)
1578 - return sd_session_is_active (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)));