1 diff -uNr polkit-0.104/configure.ac polkit-0.104.systemd/configure.ac
2 --- polkit-0.104/configure.ac 2012-01-03 17:25:49.000000000 +0100
3 +++ polkit-0.104.systemd/configure.ac 2012-04-02 20:56:04.906628803 +0200
6 if test "$enable_systemd" != "no"; then
7 PKG_CHECK_MODULES(SYSTEMD,
9 + [libsystemd-login libsystemd-daemon],
12 if test "$have_systemd" = "yes"; then
13 + AC_DEFINE(HAVE_SYSTEMD, 1, [Define if systemd is used for session tracking])
14 SESSION_TRACKING=systemd
16 if test "$enable_systemd" = "yes"; then
17 - AC_MSG_ERROR([systemd support requested but libsystemd-login1 library not found])
18 + AC_MSG_ERROR([systemd support requested but systemd libraries not found])
22 diff -uNr polkit-0.104/src/polkit/Makefile.am polkit-0.104.systemd/src/polkit/Makefile.am
23 --- polkit-0.104/src/polkit/Makefile.am 2012-01-03 16:03:47.000000000 +0100
24 +++ polkit-0.104.systemd/src/polkit/Makefile.am 2012-04-02 20:55:14.940232827 +0200
26 polkitimplicitauthorization.c polkitimplicitauthorization.h \
27 polkittemporaryauthorization.c polkittemporaryauthorization.h \
28 polkitpermission.c polkitpermission.h \
32 -libpolkit_gobject_1_la_SOURCES += \
33 - polkitunixsession-systemd.c polkitunixsession.h
35 -libpolkit_gobject_1_la_SOURCES += \
36 polkitunixsession.c polkitunixsession.h
40 libpolkit_gobject_1_la_CFLAGS = \
41 -D_POLKIT_COMPILATION \
42 diff -uNr polkit-0.104/src/polkit/polkitunixsession.c polkit-0.104.systemd/src/polkit/polkitunixsession.c
43 --- polkit-0.104/src/polkit/polkitunixsession.c 2011-10-18 19:02:27.000000000 +0200
44 +++ polkit-0.104.systemd/src/polkit/polkitunixsession.c 2012-04-02 20:55:14.940232827 +0200
51 #include "polkitunixsession.h"
52 #include "polkitsubject.h"
53 #include "polkiterror.h"
54 #include "polkitprivate.h"
57 +# include <systemd/sd-daemon.h>
58 +# include <systemd/sd-login.h>
62 * SECTION:polkitunixsession
63 * @title: PolkitUnixSession
65 PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
66 GDBusConnection *connection;
71 + gboolean ret = FALSE;
73 - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
74 - if (connection == NULL)
77 - result = g_dbus_connection_call_sync (connection,
78 - "org.freedesktop.ConsoleKit", /* name */
79 - session->session_id, /* object path */
80 - "org.freedesktop.ConsoleKit.Session", /* interface name */
81 - "GetUser", /* method */
82 - NULL, /* parameters */
83 - G_VARIANT_TYPE ("(u)"),
84 - G_DBUS_CALL_FLAGS_NONE,
93 + if (sd_booted () > 0)
95 + if (sd_session_get_uid (session->session_id, &uid) == 0)
101 + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
102 + if (connection == NULL)
105 + result = g_dbus_connection_call_sync (connection,
106 + "org.freedesktop.ConsoleKit", /* name */
107 + session->session_id, /* object path */
108 + "org.freedesktop.ConsoleKit.Session", /* interface name */
109 + "GetUser", /* method */
110 + NULL, /* parameters */
111 + G_VARIANT_TYPE ("(u)"),
112 + G_DBUS_CALL_FLAGS_NONE,
116 + if (result == NULL)
120 - g_variant_unref (result);
122 + g_variant_unref (result);
125 - if (connection != NULL)
126 - g_object_unref (connection);
128 + if (connection != NULL)
129 + g_object_unref (connection);
137 PolkitUnixSession *session = POLKIT_UNIX_SESSION (initable);
138 - GDBusConnection *connection;
139 + GDBusConnection *connection = NULL;
145 + gboolean ret = FALSE;
147 if (session->session_id != NULL)
149 @@ -484,33 +497,56 @@
153 - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
154 - if (connection == NULL)
159 + if (sd_booted () > 0)
161 + if (sd_pid_get_session (session->pid, &s) == 0)
163 + session->session_id = g_strdup (s);
169 + g_set_error (error,
171 + POLKIT_ERROR_FAILED,
172 + "No session for pid %d",
173 + (gint) session->pid);
178 + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
179 + if (connection == NULL)
182 + result = g_dbus_connection_call_sync (connection,
183 + "org.freedesktop.ConsoleKit", /* name */
184 + "/org/freedesktop/ConsoleKit/Manager", /* object path */
185 + "org.freedesktop.ConsoleKit.Manager", /* interface name */
186 + "GetSessionForUnixProcess", /* method */
187 + g_variant_new ("(u)", session->pid), /* parameters */
188 + G_VARIANT_TYPE ("(o)"),
189 + G_DBUS_CALL_FLAGS_NONE,
193 + if (result == NULL)
196 - result = g_dbus_connection_call_sync (connection,
197 - "org.freedesktop.ConsoleKit", /* name */
198 - "/org/freedesktop/ConsoleKit/Manager", /* object path */
199 - "org.freedesktop.ConsoleKit.Manager", /* interface name */
200 - "GetSessionForUnixProcess", /* method */
201 - g_variant_new ("(u)", session->pid), /* parameters */
202 - G_VARIANT_TYPE ("(o)"),
203 - G_DBUS_CALL_FLAGS_NONE,
207 - if (result == NULL)
209 + g_variant_get (result, "(o)", &session->session_id);
210 + g_variant_unref (result);
212 - g_variant_get (result, "(o)", &session->session_id);
213 - g_variant_unref (result);
220 if (connection != NULL)
221 g_object_unref (connection);
226 diff -uNr polkit-0.104/src/polkit/polkitunixsession-systemd.c polkit-0.104.systemd/src/polkit/polkitunixsession-systemd.c
227 --- polkit-0.104/src/polkit/polkitunixsession-systemd.c 2012-01-03 16:03:47.000000000 +0100
228 +++ polkit-0.104.systemd/src/polkit/polkitunixsession-systemd.c 1970-01-01 01:00:00.000000000 +0100
231 - * Copyright (C) 2011 Red Hat, Inc.
233 - * This library is free software; you can redistribute it and/or
234 - * modify it under the terms of the GNU Lesser General Public
235 - * License as published by the Free Software Foundation; either
236 - * version 2 of the License, or (at your option) any later version.
238 - * This library is distributed in the hope that it will be useful,
239 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
240 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
241 - * Lesser General Public License for more details.
243 - * You should have received a copy of the GNU Lesser General
244 - * Public License along with this library; if not, write to the
245 - * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
246 - * Boston, MA 02111-1307, USA.
248 - * Author: Matthias Clasen
251 -#ifdef HAVE_CONFIG_H
252 -# include "config.h"
256 -#include "polkitunixsession.h"
257 -#include "polkitsubject.h"
258 -#include "polkiterror.h"
259 -#include "polkitprivate.h"
261 -#include <systemd/sd-login.h>
264 - * SECTION:polkitunixsession
265 - * @title: PolkitUnixSession
266 - * @short_description: Unix sessions
268 - * An object that represents an user session.
270 - * The session id is an opaque string obtained from ConsoleKit.
274 - * PolkitUnixSession:
276 - * The #PolkitUnixSession struct should not be accessed directly.
278 -struct _PolkitUnixSession
280 - GObject parent_instance;
287 -struct _PolkitUnixSessionClass
289 - GObjectClass parent_class;
299 -static void subject_iface_init (PolkitSubjectIface *subject_iface);
300 -static void initable_iface_init (GInitableIface *initable_iface);
301 -static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
303 -G_DEFINE_TYPE_WITH_CODE (PolkitUnixSession, polkit_unix_session, G_TYPE_OBJECT,
304 - G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init)
305 - G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
306 - G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
310 -polkit_unix_session_init (PolkitUnixSession *session)
315 -polkit_unix_session_finalize (GObject *object)
317 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
319 - g_free (session->session_id);
321 - if (G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize != NULL)
322 - G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize (object);
326 -polkit_unix_session_get_property (GObject *object,
331 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
335 - case PROP_SESSION_ID:
336 - g_value_set_string (value, session->session_id);
340 - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 -polkit_unix_session_set_property (GObject *object,
348 - const GValue *value,
351 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
355 - case PROP_SESSION_ID:
356 - polkit_unix_session_set_session_id (session, g_value_get_string (value));
360 - session->pid = g_value_get_int (value);
364 - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
370 -polkit_unix_session_class_init (PolkitUnixSessionClass *klass)
372 - GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
374 - gobject_class->finalize = polkit_unix_session_finalize;
375 - gobject_class->get_property = polkit_unix_session_get_property;
376 - gobject_class->set_property = polkit_unix_session_set_property;
379 - * PolkitUnixSession:session-id:
381 - * The UNIX session id.
383 - g_object_class_install_property (gobject_class,
385 - g_param_spec_string ("session-id",
387 - "The UNIX session ID",
389 - G_PARAM_CONSTRUCT |
390 - G_PARAM_READWRITE |
391 - G_PARAM_STATIC_NAME |
392 - G_PARAM_STATIC_BLURB |
393 - G_PARAM_STATIC_NICK));
397 - * PolkitUnixSession:pid:
399 - * The UNIX process id to look up the session.
401 - g_object_class_install_property (gobject_class,
403 - g_param_spec_int ("pid",
405 - "Process ID to use for looking up the session",
409 - G_PARAM_CONSTRUCT_ONLY |
411 - G_PARAM_STATIC_NAME |
412 - G_PARAM_STATIC_BLURB |
413 - G_PARAM_STATIC_NICK));
418 - * polkit_unix_session_get_session_id:
419 - * @session: A #PolkitUnixSession.
421 - * Gets the session id for @session.
423 - * Returns: The session id for @session. Do not free this string, it
424 - * is owned by @session.
427 -polkit_unix_session_get_session_id (PolkitUnixSession *session)
429 - g_return_val_if_fail (POLKIT_IS_UNIX_SESSION (session), NULL);
430 - return session->session_id;
434 - * polkit_unix_session_set_session_id:
435 - * @session: A #PolkitUnixSession.
436 - * @session_id: The session id.
438 - * Sets the session id for @session to @session_id.
441 -polkit_unix_session_set_session_id (PolkitUnixSession *session,
442 - const gchar *session_id)
444 - g_return_if_fail (POLKIT_IS_UNIX_SESSION (session));
445 - /*g_return_if_fail (session_id != NULL);*/
446 - g_free (session->session_id);
447 - session->session_id = g_strdup (session_id);
451 - * polkit_unix_session_new:
452 - * @session_id: The session id.
454 - * Creates a new #PolkitUnixSession for @session_id.
456 - * Returns: (transfer full): A #PolkitUnixSession. Free with g_object_unref().
459 -polkit_unix_session_new (const gchar *session_id)
461 - return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_UNIX_SESSION,
462 - "session-id", session_id,
467 - * polkit_unix_session_new_for_process:
468 - * @pid: The process id of the process to get the session for.
469 - * @cancellable: (allow-none): A #GCancellable or %NULL.
470 - * @callback: A #GAsyncReadyCallback to call when the request is satisfied
471 - * @user_data: The data to pass to @callback.
473 - * Asynchronously creates a new #PolkitUnixSession object for the
474 - * process with process id @pid.
476 - * When the operation is finished, @callback will be invoked in the
477 - * <link linkend="g-main-context-push-thread-default">thread-default
478 - * main loop</link> of the thread you are calling this method
479 - * from. You can then call
480 - * polkit_unix_session_new_for_process_finish() to get the result of
483 - * This method constructs the object asynchronously, for the synchronous and blocking version
484 - * use polkit_unix_session_new_for_process_sync().
487 -polkit_unix_session_new_for_process (gint pid,
488 - GCancellable *cancellable,
489 - GAsyncReadyCallback callback,
490 - gpointer user_data)
492 - g_async_initable_new_async (POLKIT_TYPE_UNIX_SESSION,
493 - G_PRIORITY_DEFAULT,
502 - * polkit_unix_session_new_for_process_finish:
503 - * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to polkit_unix_session_new_for_process().
504 - * @error: (allow-none): Return location for error.
506 - * Finishes constructing a #PolkitSubject for a process id.
508 - * Returns: (transfer full) (allow-none): A #PolkitUnixSession for the @pid passed to
509 - * polkit_unix_session_new_for_process() or %NULL if @error is
510 - * set. Free with g_object_unref().
513 -polkit_unix_session_new_for_process_finish (GAsyncResult *res,
517 - GObject *source_object;
519 - source_object = g_async_result_get_source_object (res);
520 - g_assert (source_object != NULL);
522 - object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
525 - g_object_unref (source_object);
527 - if (object != NULL)
528 - return POLKIT_SUBJECT (object);
535 - * polkit_unix_session_new_for_process_sync:
536 - * @pid: The process id of the process to get the session for.
537 - * @cancellable: (allow-none): A #GCancellable or %NULL.
538 - * @error: (allow-none): Return location for error.
540 - * Creates a new #PolkitUnixSession for the process with process id @pid.
542 - * This is a synchronous call - the calling thread is blocked until a
543 - * reply is received. For the asynchronous version, see
544 - * polkit_unix_session_new_for_process().
546 - * Returns: (allow-none) (transfer full): A #PolkitUnixSession for
547 - * @pid or %NULL if @error is set. Free with g_object_unref().
550 -polkit_unix_session_new_for_process_sync (gint pid,
551 - GCancellable *cancellable,
554 - return POLKIT_SUBJECT (g_initable_new (POLKIT_TYPE_UNIX_SESSION,
562 -polkit_unix_session_hash (PolkitSubject *subject)
564 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
566 - return g_str_hash (session->session_id);
570 -polkit_unix_session_equal (PolkitSubject *a,
573 - PolkitUnixSession *session_a;
574 - PolkitUnixSession *session_b;
576 - session_a = POLKIT_UNIX_SESSION (a);
577 - session_b = POLKIT_UNIX_SESSION (b);
579 - return g_strcmp0 (session_a->session_id, session_b->session_id) == 0;
583 -polkit_unix_session_to_string (PolkitSubject *subject)
585 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
587 - return g_strdup_printf ("unix-session:%s", session->session_id);
591 -polkit_unix_session_exists_sync (PolkitSubject *subject,
592 - GCancellable *cancellable,
595 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
601 - if (!sd_session_get_uid (session->session_id, &uid))
608 -exists_in_thread_func (GSimpleAsyncResult *res,
610 - GCancellable *cancellable)
614 - if (!polkit_unix_session_exists_sync (POLKIT_SUBJECT (object),
618 - g_simple_async_result_set_from_error (res, error);
619 - g_error_free (error);
624 -polkit_unix_session_exists (PolkitSubject *subject,
625 - GCancellable *cancellable,
626 - GAsyncReadyCallback callback,
627 - gpointer user_data)
629 - GSimpleAsyncResult *simple;
631 - g_return_if_fail (POLKIT_IS_UNIX_SESSION (subject));
633 - simple = g_simple_async_result_new (G_OBJECT (subject),
636 - polkit_unix_session_exists);
637 - g_simple_async_result_run_in_thread (simple,
638 - exists_in_thread_func,
639 - G_PRIORITY_DEFAULT,
641 - g_object_unref (simple);
645 -polkit_unix_session_exists_finish (PolkitSubject *subject,
649 - GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
652 - g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == polkit_unix_session_exists);
656 - if (g_simple_async_result_propagate_error (simple, error))
659 - ret = g_simple_async_result_get_op_res_gboolean (simple);
666 -subject_iface_init (PolkitSubjectIface *subject_iface)
668 - subject_iface->hash = polkit_unix_session_hash;
669 - subject_iface->equal = polkit_unix_session_equal;
670 - subject_iface->to_string = polkit_unix_session_to_string;
671 - subject_iface->exists = polkit_unix_session_exists;
672 - subject_iface->exists_finish = polkit_unix_session_exists_finish;
673 - subject_iface->exists_sync = polkit_unix_session_exists_sync;
677 -polkit_unix_session_initable_init (GInitable *initable,
678 - GCancellable *cancellable,
681 - PolkitUnixSession *session = POLKIT_UNIX_SESSION (initable);
686 - if (session->session_id != NULL)
688 - /* already set, nothing to do */
693 - if (!sd_pid_get_session (session->pid, &session->session_id))
701 -initable_iface_init (GInitableIface *initable_iface)
703 - initable_iface->init = polkit_unix_session_initable_init;
707 -async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
709 - /* use default implementation to run GInitable code in a thread */
711 diff -uNr polkit-0.104/src/polkitbackend/Makefile.am polkit-0.104.systemd/src/polkitbackend/Makefile.am
712 --- polkit-0.104/src/polkitbackend/Makefile.am 2012-01-03 16:03:47.000000000 +0100
713 +++ polkit-0.104.systemd/src/polkitbackend/Makefile.am 2012-04-02 20:55:14.943566142 +0200
715 polkitbackendconfigsource.h polkitbackendconfigsource.c \
716 polkitbackendactionlookup.h polkitbackendactionlookup.c \
717 polkitbackendlocalauthorizationstore.h polkitbackendlocalauthorizationstore.c \
721 -libpolkit_backend_1_la_SOURCES += \
722 - polkitbackendsessionmonitor.h polkitbackendsessionmonitor-systemd.c
724 -libpolkit_backend_1_la_SOURCES += \
725 polkitbackendsessionmonitor.h polkitbackendsessionmonitor.c
729 libpolkit_backend_1_la_CFLAGS = \
730 -D_POLKIT_COMPILATION \
731 diff -uNr polkit-0.104/src/polkitbackend/polkitbackendsessionmonitor.c polkit-0.104.systemd/src/polkitbackend/polkitbackendsessionmonitor.c
732 --- polkit-0.104/src/polkitbackend/polkitbackendsessionmonitor.c 2011-10-18 19:02:27.000000000 +0200
733 +++ polkit-0.104.systemd/src/polkitbackend/polkitbackendsessionmonitor.c 2012-04-02 20:55:14.943566142 +0200
736 #include <glib/gstdio.h>
739 +# include <systemd/sd-daemon.h>
740 +# include <systemd/sd-login.h>
741 +# include <stdlib.h>
744 #include <polkit/polkit.h>
745 #include "polkitbackendsessionmonitor.h"
748 * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
756 + sd_login_monitor *monitor;
760 +sd_source_prepare (GSource *source,
768 +sd_source_check (GSource *source)
770 + SdSource *sd_source = (SdSource *)source;
772 + return sd_source->pollfd.revents != 0;
776 +sd_source_dispatch (GSource *source,
777 + GSourceFunc callback,
778 + gpointer user_data)
781 + SdSource *sd_source = (SdSource *)source;
784 + g_warn_if_fail (callback != NULL);
786 + ret = (*callback) (user_data);
788 + sd_login_monitor_flush (sd_source->monitor);
794 +sd_source_finalize (GSource *source)
796 + SdSource *sd_source = (SdSource*)source;
798 + sd_login_monitor_unref (sd_source->monitor);
801 +static GSourceFuncs sd_source_funcs = {
804 + sd_source_dispatch,
809 +sd_source_new (void)
812 + SdSource *sd_source;
815 + source = g_source_new (&sd_source_funcs, sizeof (SdSource));
816 + sd_source = (SdSource *)source;
818 + if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
820 + g_printerr ("Error getting login monitor: %d", ret);
824 + sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
825 + sd_source->pollfd.events = G_IO_IN;
826 + g_source_add_poll (source, &sd_source->pollfd);
831 +#endif /* HAVE_SYSTEMD */
833 struct _PolkitBackendSessionMonitor
835 GObject parent_instance;
838 GFileMonitor *database_monitor;
839 time_t database_mtime;
842 + GSource *sd_source;
846 struct _PolkitBackendSessionMonitorClass
848 g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
853 +sessions_changed (gpointer user_data)
855 + PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
857 + g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
864 polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor)
866 @@ -176,31 +280,47 @@
867 g_error_free (error);
871 - if (!ensure_database (monitor, &error))
873 - g_printerr ("Error loading " CKDB_PATH ": %s", error->message);
874 - g_error_free (error);
877 + monitor->sd_source = NULL;
879 + if (sd_booted () > 0)
881 + monitor->sd_source = sd_source_new ();
882 + g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
883 + g_source_attach (monitor->sd_source, NULL);
886 - file = g_file_new_for_path (CKDB_PATH);
887 - monitor->database_monitor = g_file_monitor_file (file,
888 - G_FILE_MONITOR_NONE,
891 - g_object_unref (file);
892 - if (monitor->database_monitor == NULL)
894 - g_printerr ("Error monitoring " CKDB_PATH ": %s", error->message);
895 - g_error_free (error);
896 + monitor->database = NULL;
897 + monitor->database_monitor = NULL;
902 - g_signal_connect (monitor->database_monitor,
904 - G_CALLBACK (on_file_monitor_changed),
907 + if (!ensure_database (monitor, &error))
909 + g_printerr ("Error loading " CKDB_PATH ": %s", error->message);
910 + g_error_free (error);
914 + file = g_file_new_for_path (CKDB_PATH);
915 + monitor->database_monitor = g_file_monitor_file (file,
916 + G_FILE_MONITOR_NONE,
919 + g_object_unref (file);
920 + if (monitor->database_monitor == NULL)
922 + g_printerr ("Error monitoring " CKDB_PATH ": %s", error->message);
923 + g_error_free (error);
927 + g_signal_connect (monitor->database_monitor,
929 + G_CALLBACK (on_file_monitor_changed),
936 if (monitor->system_bus != NULL)
937 g_object_unref (monitor->system_bus);
940 + if (monitor->sd_source != NULL)
942 + g_source_destroy (monitor->sd_source);
943 + g_source_unref (monitor->sd_source);
947 if (monitor->database_monitor != NULL)
948 g_object_unref (monitor->database_monitor);
950 @@ -328,22 +456,38 @@
952 else if (POLKIT_IS_UNIX_SESSION (subject))
954 - if (!ensure_database (monitor, error))
956 + if (monitor->sd_source != NULL)
958 - g_prefix_error (error, "Error getting user for session: Error ensuring CK database at " CKDB_PATH ": ");
960 + if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
962 + g_set_error (error,
964 + POLKIT_ERROR_FAILED,
965 + "Error getting uid for session");
970 - group = g_strdup_printf ("Session %s", polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)));
971 - local_error = NULL;
972 - uid = g_key_file_get_integer (monitor->database, group, "uid", &local_error);
973 - if (local_error != NULL)
977 - g_propagate_prefixed_error (error, local_error, "Error getting uid using " CKDB_PATH ": ");
978 + if (!ensure_database (monitor, error))
980 + g_prefix_error (error, "Error getting user for session: Error ensuring CK database at " CKDB_PATH ": ");
984 + group = g_strdup_printf ("Session %s", polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)));
985 + local_error = NULL;
986 + uid = g_key_file_get_integer (monitor->database, group, "uid", &local_error);
987 + if (local_error != NULL)
989 + g_propagate_prefixed_error (error, local_error, "Error getting uid using " CKDB_PATH ": ");
998 ret = polkit_unix_user_new (uid);
1000 @@ -373,29 +517,46 @@
1002 if (POLKIT_IS_UNIX_PROCESS (subject))
1004 - const gchar *session_id;
1006 - result = g_dbus_connection_call_sync (monitor->system_bus,
1007 - "org.freedesktop.ConsoleKit",
1008 - "/org/freedesktop/ConsoleKit/Manager",
1009 - "org.freedesktop.ConsoleKit.Manager",
1010 - "GetSessionForUnixProcess",
1011 - g_variant_new ("(u)", polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject))),
1012 - G_VARIANT_TYPE ("(o)"),
1013 - G_DBUS_CALL_FLAGS_NONE,
1014 - -1, /* timeout_msec */
1015 - NULL, /* GCancellable */
1017 - if (result == NULL)
1019 - g_variant_get (result, "(&o)", &session_id);
1020 - session = polkit_unix_session_new (session_id);
1021 - g_variant_unref (result);
1022 +#ifdef HAVE_SYSTEMD
1023 + if (monitor->sd_source != NULL)
1025 + gchar *session_id;
1028 + pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject));
1029 + if (sd_pid_get_session (pid, &session_id) < 0)
1032 + session = polkit_unix_session_new (session_id);
1033 + free (session_id);
1038 + const gchar *session_id;
1040 + result = g_dbus_connection_call_sync (monitor->system_bus,
1041 + "org.freedesktop.ConsoleKit",
1042 + "/org/freedesktop/ConsoleKit/Manager",
1043 + "org.freedesktop.ConsoleKit.Manager",
1044 + "GetSessionForUnixProcess",
1045 + g_variant_new ("(u)", polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject))),
1046 + G_VARIANT_TYPE ("(o)"),
1047 + G_DBUS_CALL_FLAGS_NONE,
1048 + -1, /* timeout_msec */
1049 + NULL, /* GCancellable */
1051 + if (result == NULL)
1053 + g_variant_get (result, "(&o)", &session_id);
1054 + session = polkit_unix_session_new (session_id);
1055 + g_variant_unref (result);
1058 else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1061 - const gchar *session_id;
1062 + gchar *session_id;
1065 result = g_dbus_connection_call_sync (monitor->system_bus,
1066 @@ -414,22 +575,35 @@
1067 g_variant_get (result, "(u)", &pid);
1068 g_variant_unref (result);
1070 - result = g_dbus_connection_call_sync (monitor->system_bus,
1071 - "org.freedesktop.ConsoleKit",
1072 - "/org/freedesktop/ConsoleKit/Manager",
1073 - "org.freedesktop.ConsoleKit.Manager",
1074 - "GetSessionForUnixProcess",
1075 - g_variant_new ("(u)", pid),
1076 - G_VARIANT_TYPE ("(o)"),
1077 - G_DBUS_CALL_FLAGS_NONE,
1078 - -1, /* timeout_msec */
1079 - NULL, /* GCancellable */
1081 - if (result == NULL)
1083 - g_variant_get (result, "(&o)", &session_id);
1084 - session = polkit_unix_session_new (session_id);
1085 - g_variant_unref (result);
1086 +#ifdef HAVE_SYSTEMD
1087 + if (monitor->sd_source != NULL)
1089 + if (sd_pid_get_session (pid, &session_id) < 0)
1092 + session = polkit_unix_session_new (session_id);
1093 + free (session_id);
1098 + result = g_dbus_connection_call_sync (monitor->system_bus,
1099 + "org.freedesktop.ConsoleKit",
1100 + "/org/freedesktop/ConsoleKit/Manager",
1101 + "org.freedesktop.ConsoleKit.Manager",
1102 + "GetSessionForUnixProcess",
1103 + g_variant_new ("(u)", pid),
1104 + G_VARIANT_TYPE ("(o)"),
1105 + G_DBUS_CALL_FLAGS_NONE,
1106 + -1, /* timeout_msec */
1107 + NULL, /* GCancellable */
1109 + if (result == NULL)
1111 + g_variant_get (result, "(&o)", &session_id);
1112 + session = polkit_unix_session_new (session_id);
1113 + g_variant_unref (result);
1118 @@ -490,7 +664,22 @@
1119 polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
1120 PolkitSubject *session)
1122 - return get_boolean (monitor, session, "is_local");
1123 +#ifdef HAVE_SYSTEMD
1124 + if (monitor->sd_source != NULL)
1128 + if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
1138 + return get_boolean (monitor, session, "is_local");
1142 @@ -498,6 +687,11 @@
1143 polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
1144 PolkitSubject *session)
1146 - return get_boolean (monitor, session, "is_active");
1147 +#ifdef HAVE_SYSTEMD
1148 + if (monitor->sd_source != NULL)
1149 + return sd_session_is_active (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)));
1152 + return get_boolean (monitor, session, "is_active");
1155 diff -uNr polkit-0.104/src/polkitbackend/polkitbackendsessionmonitor-systemd.c polkit-0.104.systemd/src/polkitbackend/polkitbackendsessionmonitor-systemd.c
1156 --- polkit-0.104/src/polkitbackend/polkitbackendsessionmonitor-systemd.c 2012-01-03 16:03:47.000000000 +0100
1157 +++ polkit-0.104.systemd/src/polkitbackend/polkitbackendsessionmonitor-systemd.c 1970-01-01 01:00:00.000000000 +0100
1160 - * Copyright (C) 2011 Red Hat, Inc.
1162 - * This library is free software; you can redistribute it and/or
1163 - * modify it under the terms of the GNU Lesser General Public
1164 - * License as published by the Free Software Foundation; either
1165 - * version 2 of the License, or (at your option) any later version.
1167 - * This library is distributed in the hope that it will be useful,
1168 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1169 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1170 - * Lesser General Public License for more details.
1172 - * You should have received a copy of the GNU Lesser General
1173 - * Public License along with this library; if not, write to the
1174 - * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1175 - * Boston, MA 02111-1307, USA.
1177 - * Author: Matthias Clasen
1180 -#include "config.h"
1184 -#include <string.h>
1185 -#include <glib/gstdio.h>
1186 -#include <systemd/sd-login.h>
1187 -#include <stdlib.h>
1189 -#include <polkit/polkit.h>
1190 -#include "polkitbackendsessionmonitor.h"
1193 - * SECTION:polkitbackendsessionmonitor
1194 - * @title: PolkitBackendSessionMonitor
1195 - * @short_description: Monitor sessions
1197 - * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
1204 - sd_login_monitor *monitor;
1208 -sd_source_prepare (GSource *source,
1216 -sd_source_check (GSource *source)
1218 - SdSource *sd_source = (SdSource *)source;
1220 - return sd_source->pollfd.revents != 0;
1224 -sd_source_dispatch (GSource *source,
1225 - GSourceFunc callback,
1226 - gpointer user_data)
1229 - SdSource *sd_source = (SdSource *)source;
1232 - g_warn_if_fail (callback != NULL);
1234 - ret = (*callback) (user_data);
1236 - sd_login_monitor_flush (sd_source->monitor);
1242 -sd_source_finalize (GSource *source)
1244 - SdSource *sd_source = (SdSource*)source;
1246 - sd_login_monitor_unref (sd_source->monitor);
1249 -static GSourceFuncs sd_source_funcs = {
1250 - sd_source_prepare,
1252 - sd_source_dispatch,
1253 - sd_source_finalize
1257 -sd_source_new (void)
1260 - SdSource *sd_source;
1263 - source = g_source_new (&sd_source_funcs, sizeof (SdSource));
1264 - sd_source = (SdSource *)source;
1266 - if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
1268 - g_printerr ("Error getting login monitor: %d", ret);
1272 - sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
1273 - sd_source->pollfd.events = G_IO_IN;
1274 - g_source_add_poll (source, &sd_source->pollfd);
1280 -struct _PolkitBackendSessionMonitor
1282 - GObject parent_instance;
1284 - GDBusConnection *system_bus;
1286 - GSource *sd_source;
1289 -struct _PolkitBackendSessionMonitorClass
1291 - GObjectClass parent_class;
1293 - void (*changed) (PolkitBackendSessionMonitor *monitor);
1303 -static guint signals[LAST_SIGNAL] = {0};
1305 -G_DEFINE_TYPE (PolkitBackendSessionMonitor, polkit_backend_session_monitor, G_TYPE_OBJECT);
1307 -/* ---------------------------------------------------------------------------------------------------- */
1310 -sessions_changed (gpointer user_data)
1312 - PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
1314 - g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
1321 -polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor)
1326 - monitor->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
1327 - if (monitor->system_bus == NULL)
1329 - g_printerr ("Error getting system bus: %s", error->message);
1330 - g_error_free (error);
1333 - monitor->sd_source = sd_source_new ();
1334 - g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
1335 - g_source_attach (monitor->sd_source, NULL);
1339 -polkit_backend_session_monitor_finalize (GObject *object)
1341 - PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (object);
1343 - if (monitor->system_bus != NULL)
1344 - g_object_unref (monitor->system_bus);
1346 - if (monitor->sd_source != NULL)
1348 - g_source_destroy (monitor->sd_source);
1349 - g_source_unref (monitor->sd_source);
1352 - if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL)
1353 - G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object);
1357 -polkit_backend_session_monitor_class_init (PolkitBackendSessionMonitorClass *klass)
1359 - GObjectClass *gobject_class;
1361 - gobject_class = G_OBJECT_CLASS (klass);
1363 - gobject_class->finalize = polkit_backend_session_monitor_finalize;
1366 - * PolkitBackendSessionMonitor::changed:
1367 - * @monitor: A #PolkitBackendSessionMonitor
1369 - * Emitted when something changes.
1371 - signals[CHANGED_SIGNAL] = g_signal_new ("changed",
1372 - POLKIT_BACKEND_TYPE_SESSION_MONITOR,
1373 - G_SIGNAL_RUN_LAST,
1374 - G_STRUCT_OFFSET (PolkitBackendSessionMonitorClass, changed),
1375 - NULL, /* accumulator */
1376 - NULL, /* accumulator data */
1377 - g_cclosure_marshal_VOID__VOID,
1382 -PolkitBackendSessionMonitor *
1383 -polkit_backend_session_monitor_new (void)
1385 - PolkitBackendSessionMonitor *monitor;
1387 - monitor = POLKIT_BACKEND_SESSION_MONITOR (g_object_new (POLKIT_BACKEND_TYPE_SESSION_MONITOR, NULL));
1392 -/* ---------------------------------------------------------------------------------------------------- */
1395 -polkit_backend_session_monitor_get_sessions (PolkitBackendSessionMonitor *monitor)
1401 -/* ---------------------------------------------------------------------------------------------------- */
1404 - * polkit_backend_session_monitor_get_user:
1405 - * @monitor: A #PolkitBackendSessionMonitor.
1406 - * @subject: A #PolkitSubject.
1407 - * @error: Return location for error.
1409 - * Gets the user corresponding to @subject or %NULL if no user exists.
1411 - * Returns: %NULL if @error is set otherwise a #PolkitUnixUser that should be freed with g_object_unref().
1414 -polkit_backend_session_monitor_get_user_for_subject (PolkitBackendSessionMonitor *monitor,
1415 - PolkitSubject *subject,
1418 - PolkitIdentity *ret;
1423 - if (POLKIT_IS_UNIX_PROCESS (subject))
1425 - uid = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject));
1426 - if ((gint) uid == -1)
1428 - g_set_error (error,
1430 - POLKIT_ERROR_FAILED,
1431 - "Unix process subject does not have uid set");
1434 - ret = polkit_unix_user_new (uid);
1436 - else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1440 - result = g_dbus_connection_call_sync (monitor->system_bus,
1441 - "org.freedesktop.DBus",
1442 - "/org/freedesktop/DBus",
1443 - "org.freedesktop.DBus",
1444 - "GetConnectionUnixUser",
1445 - g_variant_new ("(s)", polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))),
1446 - G_VARIANT_TYPE ("(u)"),
1447 - G_DBUS_CALL_FLAGS_NONE,
1448 - -1, /* timeout_msec */
1449 - NULL, /* GCancellable */
1451 - if (result == NULL)
1453 - g_variant_get (result, "(u)", &uid);
1454 - g_variant_unref (result);
1456 - ret = polkit_unix_user_new (uid);
1458 - else if (POLKIT_IS_UNIX_SESSION (subject))
1461 - if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
1463 - g_set_error (error,
1465 - POLKIT_ERROR_FAILED,
1466 - "Error getting uid for session");
1470 - ret = polkit_unix_user_new (uid);
1478 - * polkit_backend_session_monitor_get_session_for_subject:
1479 - * @monitor: A #PolkitBackendSessionMonitor.
1480 - * @subject: A #PolkitSubject.
1481 - * @error: Return location for error.
1483 - * Gets the session corresponding to @subject or %NULL if no session exists.
1485 - * Returns: %NULL if @error is set otherwise a #PolkitUnixSession that should be freed with g_object_unref().
1488 -polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMonitor *monitor,
1489 - PolkitSubject *subject,
1492 - PolkitSubject *session;
1496 - if (POLKIT_IS_UNIX_PROCESS (subject))
1498 - gchar *session_id;
1501 - pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject));
1502 - if (sd_pid_get_session (pid, &session_id) < 0)
1505 - session = polkit_unix_session_new (session_id);
1506 - free (session_id);
1508 - else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
1511 - gchar *session_id;
1514 - result = g_dbus_connection_call_sync (monitor->system_bus,
1515 - "org.freedesktop.DBus",
1516 - "/org/freedesktop/DBus",
1517 - "org.freedesktop.DBus",
1518 - "GetConnectionUnixProcessID",
1519 - g_variant_new ("(s)", polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))),
1520 - G_VARIANT_TYPE ("(u)"),
1521 - G_DBUS_CALL_FLAGS_NONE,
1522 - -1, /* timeout_msec */
1523 - NULL, /* GCancellable */
1525 - if (result == NULL)
1527 - g_variant_get (result, "(u)", &pid);
1528 - g_variant_unref (result);
1530 - if (sd_pid_get_session (pid, &session_id) < 0)
1533 - session = polkit_unix_session_new (session_id);
1534 - free (session_id);
1538 - g_set_error (error,
1540 - POLKIT_ERROR_NOT_SUPPORTED,
1541 - "Cannot get user for subject of type %s",
1542 - g_type_name (G_TYPE_FROM_INSTANCE (subject)));
1551 -polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
1552 - PolkitSubject *session)
1556 - if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
1567 -polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
1568 - PolkitSubject *session)
1570 - return sd_session_is_active (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)));