Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 468938
Collapse All | Expand All

(-)a/configure.ac (-1 / +1 lines)
Lines 166-172 if test "$enable_libsystemd_login" != "no"; then Link Here
166
                    have_libsystemd_login=yes,
166
                    have_libsystemd_login=yes,
167
                    have_libsystemd_login=no)
167
                    have_libsystemd_login=no)
168
  if test "$have_libsystemd_login" = "yes"; then
168
  if test "$have_libsystemd_login" = "yes"; then
169
    SESSION_TRACKING=libsystemd-login
169
    SESSION_TRACKING="$SESSION_TRACKING libsystemd-login"
170
    AC_DEFINE([HAVE_LIBSYSTEMD_LOGIN], 1, [Define to 1 if libsystemd-login is available])
170
    AC_DEFINE([HAVE_LIBSYSTEMD_LOGIN], 1, [Define to 1 if libsystemd-login is available])
171
  else
171
  else
172
    if test "$enable_libsystemd_login" = "yes"; then
172
    if test "$enable_libsystemd_login" = "yes"; then
(-)a/src/polkit/Makefile.am (-6 / +1 lines)
Lines 81-93 libpolkit_gobject_1_la_SOURCES = \ Link Here
81
	polkitpermission.c			polkitpermission.h			\
81
	polkitpermission.c			polkitpermission.h			\
82
        $(NULL)
82
        $(NULL)
83
83
84
if HAVE_LIBSYSTEMD_LOGIN
85
libpolkit_gobject_1_la_SOURCES += \
84
libpolkit_gobject_1_la_SOURCES += \
86
	polkitunixsession-systemd.c		polkitunixsession.h
85
	polkitunixsession.c				polkitunixsession.h
87
else
88
libpolkit_gobject_1_la_SOURCES += \
89
	polkitunixsession.c			polkitunixsession.h
90
endif
91
86
92
libpolkit_gobject_1_la_CFLAGS =                                        	\
87
libpolkit_gobject_1_la_CFLAGS =                                        	\
93
        -D_POLKIT_COMPILATION                                  		\
88
        -D_POLKIT_COMPILATION                                  		\
(-)a/src/polkit/polkitunixsession-systemd.c (-490 lines)
Lines 1-490 Link Here
1
/*
2
 * Copyright (C) 2011 Red Hat, Inc.
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General
15
 * Public License along with this library; if not, write to the
16
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17
 * Boston, MA 02111-1307, USA.
18
 *
19
 * Author: Matthias Clasen
20
 */
21
22
#ifdef HAVE_CONFIG_H
23
#  include "config.h"
24
#endif
25
26
#include <stdlib.h>
27
#include <string.h>
28
#include "polkitunixsession.h"
29
#include "polkitsubject.h"
30
#include "polkiterror.h"
31
#include "polkitprivate.h"
32
33
#include <systemd/sd-login.h>
34
35
/**
36
 * SECTION:polkitunixsession
37
 * @title: PolkitUnixSession
38
 * @short_description: Unix sessions
39
 *
40
 * An object that represents an user session.
41
 *
42
 * The session id is an opaque string obtained from ConsoleKit.
43
 */
44
45
/**
46
 * PolkitUnixSession:
47
 *
48
 * The #PolkitUnixSession struct should not be accessed directly.
49
 */
50
struct _PolkitUnixSession
51
{
52
  GObject parent_instance;
53
54
  gchar *session_id;
55
56
  gint pid;
57
};
58
59
struct _PolkitUnixSessionClass
60
{
61
  GObjectClass parent_class;
62
};
63
64
enum
65
{
66
  PROP_0,
67
  PROP_SESSION_ID,
68
  PROP_PID,
69
};
70
71
static void subject_iface_init        (PolkitSubjectIface *subject_iface);
72
static void initable_iface_init       (GInitableIface *initable_iface);
73
static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
74
75
G_DEFINE_TYPE_WITH_CODE (PolkitUnixSession, polkit_unix_session, G_TYPE_OBJECT,
76
                         G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init)
77
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
78
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
79
                         );
80
81
static void
82
polkit_unix_session_init (PolkitUnixSession *session)
83
{
84
}
85
86
static void
87
polkit_unix_session_finalize (GObject *object)
88
{
89
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
90
91
  g_free (session->session_id);
92
93
  if (G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize != NULL)
94
    G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize (object);
95
}
96
97
static void
98
polkit_unix_session_get_property (GObject    *object,
99
                                  guint       prop_id,
100
                                  GValue     *value,
101
                                  GParamSpec *pspec)
102
{
103
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
104
105
  switch (prop_id)
106
    {
107
    case PROP_SESSION_ID:
108
      g_value_set_string (value, session->session_id);
109
      break;
110
111
    default:
112
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113
      break;
114
    }
115
}
116
117
static void
118
polkit_unix_session_set_property (GObject      *object,
119
                                  guint         prop_id,
120
                                  const GValue *value,
121
                                  GParamSpec   *pspec)
122
{
123
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
124
125
  switch (prop_id)
126
    {
127
    case PROP_SESSION_ID:
128
      polkit_unix_session_set_session_id (session, g_value_get_string (value));
129
      break;
130
131
    case PROP_PID:
132
      session->pid = g_value_get_int (value);
133
      break;
134
135
    default:
136
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137
      break;
138
    }
139
}
140
141
static void
142
polkit_unix_session_class_init (PolkitUnixSessionClass *klass)
143
{
144
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
145
146
  gobject_class->finalize     = polkit_unix_session_finalize;
147
  gobject_class->get_property = polkit_unix_session_get_property;
148
  gobject_class->set_property = polkit_unix_session_set_property;
149
150
  /**
151
   * PolkitUnixSession:session-id:
152
   *
153
   * The UNIX session id.
154
   */
155
  g_object_class_install_property (gobject_class,
156
                                   PROP_SESSION_ID,
157
                                   g_param_spec_string ("session-id",
158
                                                        "Session ID",
159
                                                        "The UNIX session ID",
160
                                                        NULL,
161
                                                        G_PARAM_CONSTRUCT |
162
                                                        G_PARAM_READWRITE |
163
                                                        G_PARAM_STATIC_NAME |
164
                                                        G_PARAM_STATIC_BLURB |
165
                                                        G_PARAM_STATIC_NICK));
166
167
168
  /**
169
   * PolkitUnixSession:pid:
170
   *
171
   * The UNIX process id to look up the session.
172
   */
173
  g_object_class_install_property (gobject_class,
174
                                   PROP_PID,
175
                                   g_param_spec_int ("pid",
176
                                                     "Process ID",
177
                                                     "Process ID to use for looking up the session",
178
                                                     0,
179
                                                     G_MAXINT,
180
                                                     0,
181
                                                     G_PARAM_CONSTRUCT_ONLY |
182
                                                     G_PARAM_WRITABLE |
183
                                                     G_PARAM_STATIC_NAME |
184
                                                     G_PARAM_STATIC_BLURB |
185
                                                     G_PARAM_STATIC_NICK));
186
187
}
188
189
/**
190
 * polkit_unix_session_get_session_id:
191
 * @session: A #PolkitUnixSession.
192
 *
193
 * Gets the session id for @session.
194
 *
195
 * Returns: The session id for @session. Do not free this string, it
196
 * is owned by @session.
197
 **/
198
const gchar *
199
polkit_unix_session_get_session_id (PolkitUnixSession *session)
200
{
201
  g_return_val_if_fail (POLKIT_IS_UNIX_SESSION (session), NULL);
202
  return session->session_id;
203
}
204
205
/**
206
 * polkit_unix_session_set_session_id:
207
 * @session: A #PolkitUnixSession.
208
 * @session_id: The session id.
209
 *
210
 * Sets the session id for @session to @session_id.
211
 **/
212
void
213
polkit_unix_session_set_session_id (PolkitUnixSession *session,
214
                                    const gchar       *session_id)
215
{
216
  g_return_if_fail (POLKIT_IS_UNIX_SESSION (session));
217
  /*g_return_if_fail (session_id != NULL);*/
218
  g_free (session->session_id);
219
  session->session_id = g_strdup (session_id);
220
}
221
222
/**
223
 * polkit_unix_session_new:
224
 * @session_id: The session id.
225
 *
226
 * Creates a new #PolkitUnixSession for @session_id.
227
 *
228
 * Returns: (transfer full): A #PolkitUnixSession. Free with g_object_unref().
229
 **/
230
PolkitSubject *
231
polkit_unix_session_new (const gchar *session_id)
232
{
233
  return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_UNIX_SESSION,
234
                                       "session-id", session_id,
235
                                       NULL));
236
}
237
238
/**
239
 * polkit_unix_session_new_for_process:
240
 * @pid: The process id of the process to get the session for.
241
 * @cancellable: (allow-none): A #GCancellable or %NULL.
242
 * @callback: A #GAsyncReadyCallback to call when the request is satisfied
243
 * @user_data: The data to pass to @callback.
244
 *
245
 * Asynchronously creates a new #PolkitUnixSession object for the
246
 * process with process id @pid.
247
 *
248
 * When the operation is finished, @callback will be invoked in the
249
 * <link linkend="g-main-context-push-thread-default">thread-default
250
 * main loop</link> of the thread you are calling this method
251
 * from. You can then call
252
 * polkit_unix_session_new_for_process_finish() to get the result of
253
 * the operation.
254
 *
255
 * This method constructs the object asynchronously, for the synchronous and blocking version
256
 * use polkit_unix_session_new_for_process_sync().
257
 **/
258
void
259
polkit_unix_session_new_for_process (gint                pid,
260
                                     GCancellable       *cancellable,
261
                                     GAsyncReadyCallback callback,
262
                                     gpointer            user_data)
263
{
264
  g_async_initable_new_async (POLKIT_TYPE_UNIX_SESSION,
265
                              G_PRIORITY_DEFAULT,
266
                              cancellable,
267
                              callback,
268
                              user_data,
269
                              "pid", pid,
270
                              NULL);
271
}
272
273
/**
274
 * polkit_unix_session_new_for_process_finish:
275
 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to polkit_unix_session_new_for_process().
276
 * @error: (allow-none): Return location for error.
277
 *
278
 * Finishes constructing a #PolkitSubject for a process id.
279
 *
280
 * Returns: (transfer full) (allow-none): A #PolkitUnixSession for the @pid passed to
281
 *     polkit_unix_session_new_for_process() or %NULL if @error is
282
 *     set. Free with g_object_unref().
283
 **/
284
PolkitSubject *
285
polkit_unix_session_new_for_process_finish (GAsyncResult   *res,
286
                                            GError        **error)
287
{
288
  GObject *object;
289
  GObject *source_object;
290
291
  source_object = g_async_result_get_source_object (res);
292
  g_assert (source_object != NULL);
293
294
  object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
295
                                        res,
296
                                        error);
297
  g_object_unref (source_object);
298
299
  if (object != NULL)
300
    return POLKIT_SUBJECT (object);
301
  else
302
    return NULL;
303
}
304
305
306
/**
307
 * polkit_unix_session_new_for_process_sync:
308
 * @pid: The process id of the process to get the session for.
309
 * @cancellable: (allow-none): A #GCancellable or %NULL.
310
 * @error: (allow-none): Return location for error.
311
 *
312
 * Creates a new #PolkitUnixSession for the process with process id @pid.
313
 *
314
 * This is a synchronous call - the calling thread is blocked until a
315
 * reply is received. For the asynchronous version, see
316
 * polkit_unix_session_new_for_process().
317
 *
318
 * Returns: (allow-none) (transfer full): A #PolkitUnixSession for
319
 * @pid or %NULL if @error is set. Free with g_object_unref().
320
 **/
321
PolkitSubject *
322
polkit_unix_session_new_for_process_sync (gint           pid,
323
                                          GCancellable  *cancellable,
324
                                          GError       **error)
325
{
326
  return POLKIT_SUBJECT (g_initable_new (POLKIT_TYPE_UNIX_SESSION,
327
                                         cancellable,
328
                                         error,
329
                                         "pid", pid,
330
                                         NULL));
331
}
332
333
static guint
334
polkit_unix_session_hash (PolkitSubject *subject)
335
{
336
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
337
338
  return g_str_hash (session->session_id);
339
}
340
341
static gboolean
342
polkit_unix_session_equal (PolkitSubject *a,
343
                           PolkitSubject *b)
344
{
345
  PolkitUnixSession *session_a;
346
  PolkitUnixSession *session_b;
347
348
  session_a = POLKIT_UNIX_SESSION (a);
349
  session_b = POLKIT_UNIX_SESSION (b);
350
351
  return g_strcmp0 (session_a->session_id, session_b->session_id) == 0;
352
}
353
354
static gchar *
355
polkit_unix_session_to_string (PolkitSubject *subject)
356
{
357
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
358
359
  return g_strdup_printf ("unix-session:%s", session->session_id);
360
}
361
362
static gboolean
363
polkit_unix_session_exists_sync (PolkitSubject   *subject,
364
                                 GCancellable    *cancellable,
365
                                 GError         **error)
366
{
367
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
368
  gboolean ret = FALSE;
369
  uid_t uid;
370
371
  if (sd_session_get_uid (session->session_id, &uid) == 0)
372
    ret = TRUE;
373
374
  return ret;
375
}
376
377
static void
378
exists_in_thread_func (GSimpleAsyncResult *res,
379
                       GObject            *object,
380
                       GCancellable       *cancellable)
381
{
382
  GError *error;
383
  error = NULL;
384
  if (!polkit_unix_session_exists_sync (POLKIT_SUBJECT (object),
385
                                        cancellable,
386
                                        &error))
387
    {
388
      g_simple_async_result_set_from_error (res, error);
389
      g_error_free (error);
390
    }
391
}
392
393
static void
394
polkit_unix_session_exists (PolkitSubject       *subject,
395
                            GCancellable        *cancellable,
396
                            GAsyncReadyCallback  callback,
397
                            gpointer             user_data)
398
{
399
  GSimpleAsyncResult *simple;
400
401
  g_return_if_fail (POLKIT_IS_UNIX_SESSION (subject));
402
403
  simple = g_simple_async_result_new (G_OBJECT (subject),
404
                                      callback,
405
                                      user_data,
406
                                      polkit_unix_session_exists);
407
  g_simple_async_result_run_in_thread (simple,
408
                                       exists_in_thread_func,
409
                                       G_PRIORITY_DEFAULT,
410
                                       cancellable);
411
  g_object_unref (simple);
412
}
413
414
static gboolean
415
polkit_unix_session_exists_finish (PolkitSubject  *subject,
416
                                      GAsyncResult   *res,
417
                                      GError        **error)
418
{
419
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
420
  gboolean ret;
421
422
  g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == polkit_unix_session_exists);
423
424
  ret = FALSE;
425
426
  if (g_simple_async_result_propagate_error (simple, error))
427
    goto out;
428
429
  ret = g_simple_async_result_get_op_res_gboolean (simple);
430
431
 out:
432
  return ret;
433
}
434
435
static void
436
subject_iface_init (PolkitSubjectIface *subject_iface)
437
{
438
  subject_iface->hash          = polkit_unix_session_hash;
439
  subject_iface->equal         = polkit_unix_session_equal;
440
  subject_iface->to_string     = polkit_unix_session_to_string;
441
  subject_iface->exists        = polkit_unix_session_exists;
442
  subject_iface->exists_finish = polkit_unix_session_exists_finish;
443
  subject_iface->exists_sync   = polkit_unix_session_exists_sync;
444
}
445
446
static gboolean
447
polkit_unix_session_initable_init (GInitable     *initable,
448
                                   GCancellable  *cancellable,
449
                                   GError       **error)
450
{
451
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (initable);
452
  gboolean ret = FALSE;
453
  char *s;
454
455
  if (session->session_id != NULL)
456
    {
457
      /* already set, nothing to do */
458
      ret = TRUE;
459
      goto out;
460
    }
461
462
  if (sd_pid_get_session (session->pid, &s) == 0)
463
    {
464
      session->session_id = g_strdup (s);
465
      free (s);
466
      ret = TRUE;
467
      goto out;
468
    }
469
470
  g_set_error (error,
471
               POLKIT_ERROR,
472
               POLKIT_ERROR_FAILED,
473
               "No session for pid %d",
474
               (gint) session->pid);
475
476
out:
477
  return ret;
478
}
479
480
static void
481
initable_iface_init (GInitableIface *initable_iface)
482
{
483
  initable_iface->init = polkit_unix_session_initable_init;
484
}
485
486
static void
487
async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
488
{
489
  /* use default implementation to run GInitable code in a thread */
490
}
(-)a/src/polkit/polkitunixsession.c (-5 / +50 lines)
Lines 1-5 Link Here
1
/*
1
/*
2
 * Copyright (C) 2008 Red Hat, Inc.
2
 * Copyright (C) 2008 Red Hat, Inc.
3
 * Copyright (C) 2013 Fabio Erculiani
3
 *
4
 *
4
 * This library is free software; you can redistribute it and/or
5
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * modify it under the terms of the GNU Lesser General Public
Lines 17-34 Link Here
17
 * Boston, MA 02111-1307, USA.
18
 * Boston, MA 02111-1307, USA.
18
 *
19
 *
19
 * Author: David Zeuthen <davidz@redhat.com>
20
 * Author: David Zeuthen <davidz@redhat.com>
21
 * Author: Fabio Erculiani <lxnay@sabayon.org>
20
 */
22
 */
21
23
22
#ifdef HAVE_CONFIG_H
24
#ifdef HAVE_CONFIG_H
23
#  include "config.h"
25
#  include "config.h"
24
#endif
26
#endif
25
27
28
#include <stdlib.h>
26
#include <string.h>
29
#include <string.h>
30
27
#include "polkitunixsession.h"
31
#include "polkitunixsession.h"
28
#include "polkitsubject.h"
32
#include "polkitsubject.h"
29
#include "polkiterror.h"
33
#include "polkiterror.h"
30
#include "polkitprivate.h"
34
#include "polkitprivate.h"
31
35
36
#ifdef HAVE_LIBSYSTEMD_LOGIN
37
#include <systemd/sd-login.h>
38
#endif /* HAVE_LIBSYSTEMD_LOGIN */
39
40
32
/**
41
/**
33
 * SECTION:polkitunixsession
42
 * SECTION:polkitunixsession
34
 * @title: PolkitUnixSession
43
 * @title: PolkitUnixSession
Lines 36-42 Link Here
36
 *
45
 *
37
 * An object that represents an user session.
46
 * An object that represents an user session.
38
 *
47
 *
39
 * The session id is an opaque string obtained from ConsoleKit.
48
 * The session id is an opaque string obtained from either ConsoleKit or
49
 * systemd-logind.
40
 */
50
 */
41
51
42
/**
52
/**
Lines 358-373 polkit_unix_session_to_string (PolkitSubject *subject) Link Here
358
368
359
static gboolean
369
static gboolean
360
polkit_unix_session_exists_sync (PolkitSubject   *subject,
370
polkit_unix_session_exists_sync (PolkitSubject   *subject,
361
                                    GCancellable    *cancellable,
371
                                     GCancellable    *cancellable,
362
                                    GError         **error)
372
                                     GError         **error)
363
{
373
{
364
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
374
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
365
  GDBusConnection *connection;
375
  GDBusConnection *connection;
366
  GVariant *result;
376
  GVariant *result;
367
  gboolean ret;
377
  gboolean ret;
368
378
379
  connection = NULL;
369
  ret = FALSE;
380
  ret = FALSE;
370
381
382
#ifdef HAVE_LIBSYSTEMD_LOGIN
383
  if (LOGIND_RUNNING())
384
  {
385
	  uid_t uid;
386
	  if (sd_session_get_uid (session->session_id, &uid) == 0)
387
	  {
388
		ret = TRUE;
389
		goto out;
390
	  }
391
	  goto out;
392
  }
393
#endif
394
371
  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
395
  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
372
  if (connection == NULL)
396
  if (connection == NULL)
373
    goto out;
397
    goto out;
Lines 434-441 polkit_unix_session_exists (PolkitSubject *subject, Link Here
434
458
435
static gboolean
459
static gboolean
436
polkit_unix_session_exists_finish (PolkitSubject  *subject,
460
polkit_unix_session_exists_finish (PolkitSubject  *subject,
437
                                      GAsyncResult   *res,
461
                                       GAsyncResult   *res,
438
                                      GError        **error)
462
                                       GError        **error)
439
{
463
{
440
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
464
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
441
  gboolean ret;
465
  gboolean ret;
Lines 473-478 polkit_unix_session_initable_init (GInitable *initable, Link Here
473
  GDBusConnection *connection;
497
  GDBusConnection *connection;
474
  GVariant *result;
498
  GVariant *result;
475
  gboolean ret;
499
  gboolean ret;
500
  char *s;
476
501
477
  connection = NULL;
502
  connection = NULL;
478
  ret = FALSE;
503
  ret = FALSE;
Lines 484-489 polkit_unix_session_initable_init (GInitable *initable, Link Here
484
      goto out;
509
      goto out;
485
    }
510
    }
486
511
512
#ifdef HAVE_LIBSYSTEMD_LOGIN
513
  if (LOGIND_RUNNING())
514
  {
515
	  if (sd_pid_get_session (session->pid, &s) == 0)
516
	  {
517
		  session->session_id = g_strdup (s);
518
		  free (s);
519
		  ret = TRUE;
520
		  goto out;
521
	  }
522
523
	  g_set_error (error,
524
	               POLKIT_ERROR,
525
	               POLKIT_ERROR_FAILED,
526
	               "No session for pid %d",
527
	               (gint) session->pid);
528
	  goto out;
529
  }
530
#endif
531
487
  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
532
  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
488
  if (connection == NULL)
533
  if (connection == NULL)
489
    goto out;
534
    goto out;
(-)a/src/polkit/polkitunixsession.h (+5 lines)
Lines 30-35 Link Here
30
#include <gio/gio.h>
30
#include <gio/gio.h>
31
#include <polkit/polkittypes.h>
31
#include <polkit/polkittypes.h>
32
32
33
/* check if logind is running
34
 * thanks to: https://bugzilla.gnome.org/show_bug.cgi?id=696266
35
 */
36
#define LOGIND_RUNNING() (access("/run/systemd/seats/", F_OK) >= 0)
37
33
G_BEGIN_DECLS
38
G_BEGIN_DECLS
34
39
35
#define POLKIT_TYPE_UNIX_SESSION          (polkit_unix_session_get_type())
40
#define POLKIT_TYPE_UNIX_SESSION          (polkit_unix_session_get_type())
(-)a/src/polkitbackend/Makefile.am (-5 lines)
Lines 38-50 libpolkit_backend_1_la_SOURCES = \ Link Here
38
	polkitbackendactionlookup.h		polkitbackendactionlookup.c		\
38
	polkitbackendactionlookup.h		polkitbackendactionlookup.c		\
39
        $(NULL)
39
        $(NULL)
40
40
41
if HAVE_LIBSYSTEMD_LOGIN
42
libpolkit_backend_1_la_SOURCES += \
43
	polkitbackendsessionmonitor.h		polkitbackendsessionmonitor-systemd.c
44
else
45
libpolkit_backend_1_la_SOURCES += \
41
libpolkit_backend_1_la_SOURCES += \
46
	polkitbackendsessionmonitor.h		polkitbackendsessionmonitor.c
42
	polkitbackendsessionmonitor.h		polkitbackendsessionmonitor.c
47
endif
48
43
49
libpolkit_backend_1_la_CFLAGS =                                        	\
44
libpolkit_backend_1_la_CFLAGS =                                        	\
50
        -D_POLKIT_COMPILATION                                  		\
45
        -D_POLKIT_COMPILATION                                  		\
(-)a/src/polkitbackend/polkitbackendsessionmonitor-systemd.c (-414 lines)
Lines 1-414 Link Here
1
/*
2
 * Copyright (C) 2011 Red Hat, Inc.
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General
15
 * Public License along with this library; if not, write to the
16
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17
 * Boston, MA 02111-1307, USA.
18
 *
19
 * Author: Matthias Clasen
20
 */
21
22
#include "config.h"
23
#include <errno.h>
24
#include <pwd.h>
25
#include <grp.h>
26
#include <string.h>
27
#include <glib/gstdio.h>
28
#include <systemd/sd-login.h>
29
#include <stdlib.h>
30
31
#include <polkit/polkit.h>
32
#include "polkitbackendsessionmonitor.h"
33
34
/* <internal>
35
 * SECTION:polkitbackendsessionmonitor
36
 * @title: PolkitBackendSessionMonitor
37
 * @short_description: Monitor sessions
38
 *
39
 * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
40
 */
41
42
typedef struct
43
{
44
  GSource source;
45
  GPollFD pollfd;
46
  sd_login_monitor *monitor;
47
} SdSource;
48
49
static gboolean
50
sd_source_prepare (GSource *source,
51
                   gint    *timeout)
52
{
53
  *timeout = -1;
54
  return FALSE;
55
}
56
57
static gboolean
58
sd_source_check (GSource *source)
59
{
60
  SdSource *sd_source = (SdSource *)source;
61
62
  return sd_source->pollfd.revents != 0;
63
}
64
65
static gboolean
66
sd_source_dispatch (GSource     *source,
67
                    GSourceFunc  callback,
68
                    gpointer     user_data)
69
70
{
71
  SdSource *sd_source = (SdSource *)source;
72
  gboolean ret;
73
74
  g_warn_if_fail (callback != NULL);
75
76
  ret = (*callback) (user_data);
77
78
  sd_login_monitor_flush (sd_source->monitor);
79
80
  return ret;
81
}
82
83
static void
84
sd_source_finalize (GSource *source)
85
{
86
  SdSource *sd_source = (SdSource*)source;
87
88
  sd_login_monitor_unref (sd_source->monitor);
89
}
90
91
static GSourceFuncs sd_source_funcs = {
92
  sd_source_prepare,
93
  sd_source_check,
94
  sd_source_dispatch,
95
  sd_source_finalize
96
};
97
98
static GSource *
99
sd_source_new (void)
100
{
101
  GSource *source;
102
  SdSource *sd_source;
103
  int ret;
104
105
  source = g_source_new (&sd_source_funcs, sizeof (SdSource));
106
  sd_source = (SdSource *)source;
107
108
  if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
109
    {
110
      g_printerr ("Error getting login monitor: %d", ret);
111
    }
112
  else
113
    {
114
      sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
115
      sd_source->pollfd.events = G_IO_IN;
116
      g_source_add_poll (source, &sd_source->pollfd);
117
    }
118
119
  return source;
120
}
121
122
struct _PolkitBackendSessionMonitor
123
{
124
  GObject parent_instance;
125
126
  GDBusConnection *system_bus;
127
128
  GSource *sd_source;
129
};
130
131
struct _PolkitBackendSessionMonitorClass
132
{
133
  GObjectClass parent_class;
134
135
  void (*changed) (PolkitBackendSessionMonitor *monitor);
136
};
137
138
139
enum
140
{
141
  CHANGED_SIGNAL,
142
  LAST_SIGNAL,
143
};
144
145
static guint signals[LAST_SIGNAL] = {0};
146
147
G_DEFINE_TYPE (PolkitBackendSessionMonitor, polkit_backend_session_monitor, G_TYPE_OBJECT);
148
149
/* ---------------------------------------------------------------------------------------------------- */
150
151
static gboolean
152
sessions_changed (gpointer user_data)
153
{
154
  PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
155
156
  g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
157
158
  return TRUE;
159
}
160
161
162
static void
163
polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor)
164
{
165
  GError *error;
166
167
  error = NULL;
168
  monitor->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
169
  if (monitor->system_bus == NULL)
170
    {
171
      g_printerr ("Error getting system bus: %s", error->message);
172
      g_error_free (error);
173
    }
174
175
  monitor->sd_source = sd_source_new ();
176
  g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
177
  g_source_attach (monitor->sd_source, NULL);
178
}
179
180
static void
181
polkit_backend_session_monitor_finalize (GObject *object)
182
{
183
  PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (object);
184
185
  if (monitor->system_bus != NULL)
186
    g_object_unref (monitor->system_bus);
187
188
  if (monitor->sd_source != NULL)
189
    {
190
      g_source_destroy (monitor->sd_source);
191
      g_source_unref (monitor->sd_source);
192
    }
193
194
  if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL)
195
    G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object);
196
}
197
198
static void
199
polkit_backend_session_monitor_class_init (PolkitBackendSessionMonitorClass *klass)
200
{
201
  GObjectClass *gobject_class;
202
203
  gobject_class = G_OBJECT_CLASS (klass);
204
205
  gobject_class->finalize = polkit_backend_session_monitor_finalize;
206
207
  /**
208
   * PolkitBackendSessionMonitor::changed:
209
   * @monitor: A #PolkitBackendSessionMonitor
210
   *
211
   * Emitted when something changes.
212
   */
213
  signals[CHANGED_SIGNAL] = g_signal_new ("changed",
214
                                          POLKIT_BACKEND_TYPE_SESSION_MONITOR,
215
                                          G_SIGNAL_RUN_LAST,
216
                                          G_STRUCT_OFFSET (PolkitBackendSessionMonitorClass, changed),
217
                                          NULL,                   /* accumulator      */
218
                                          NULL,                   /* accumulator data */
219
                                          g_cclosure_marshal_VOID__VOID,
220
                                          G_TYPE_NONE,
221
                                          0);
222
}
223
224
PolkitBackendSessionMonitor *
225
polkit_backend_session_monitor_new (void)
226
{
227
  PolkitBackendSessionMonitor *monitor;
228
229
  monitor = POLKIT_BACKEND_SESSION_MONITOR (g_object_new (POLKIT_BACKEND_TYPE_SESSION_MONITOR, NULL));
230
231
  return monitor;
232
}
233
234
/* ---------------------------------------------------------------------------------------------------- */
235
236
GList *
237
polkit_backend_session_monitor_get_sessions (PolkitBackendSessionMonitor *monitor)
238
{
239
  /* TODO */
240
  return NULL;
241
}
242
243
/* ---------------------------------------------------------------------------------------------------- */
244
245
/**
246
 * polkit_backend_session_monitor_get_user:
247
 * @monitor: A #PolkitBackendSessionMonitor.
248
 * @subject: A #PolkitSubject.
249
 * @error: Return location for error.
250
 *
251
 * Gets the user corresponding to @subject or %NULL if no user exists.
252
 *
253
 * Returns: %NULL if @error is set otherwise a #PolkitUnixUser that should be freed with g_object_unref().
254
 */
255
PolkitIdentity *
256
polkit_backend_session_monitor_get_user_for_subject (PolkitBackendSessionMonitor  *monitor,
257
                                                     PolkitSubject                *subject,
258
                                                     GError                      **error)
259
{
260
  PolkitIdentity *ret;
261
  guint32 uid;
262
263
  ret = NULL;
264
265
  if (POLKIT_IS_UNIX_PROCESS (subject))
266
    {
267
      uid = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject));
268
      if ((gint) uid == -1)
269
        {
270
          g_set_error (error,
271
                       POLKIT_ERROR,
272
                       POLKIT_ERROR_FAILED,
273
                       "Unix process subject does not have uid set");
274
          goto out;
275
        }
276
      ret = polkit_unix_user_new (uid);
277
    }
278
  else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
279
    {
280
      GVariant *result;
281
282
      result = g_dbus_connection_call_sync (monitor->system_bus,
283
                                            "org.freedesktop.DBus",
284
                                            "/org/freedesktop/DBus",
285
                                            "org.freedesktop.DBus",
286
                                            "GetConnectionUnixUser",
287
                                            g_variant_new ("(s)", polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))),
288
                                            G_VARIANT_TYPE ("(u)"),
289
                                            G_DBUS_CALL_FLAGS_NONE,
290
                                            -1, /* timeout_msec */
291
                                            NULL, /* GCancellable */
292
                                            error);
293
      if (result == NULL)
294
        goto out;
295
      g_variant_get (result, "(u)", &uid);
296
      g_variant_unref (result);
297
298
      ret = polkit_unix_user_new (uid);
299
    }
300
  else if (POLKIT_IS_UNIX_SESSION (subject))
301
    {
302
303
      if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
304
        {
305
          g_set_error (error,
306
                       POLKIT_ERROR,
307
                       POLKIT_ERROR_FAILED,
308
                       "Error getting uid for session");
309
          goto out;
310
        }
311
312
      ret = polkit_unix_user_new (uid);
313
    }
314
315
 out:
316
  return ret;
317
}
318
319
/**
320
 * polkit_backend_session_monitor_get_session_for_subject:
321
 * @monitor: A #PolkitBackendSessionMonitor.
322
 * @subject: A #PolkitSubject.
323
 * @error: Return location for error.
324
 *
325
 * Gets the session corresponding to @subject or %NULL if no session exists.
326
 *
327
 * Returns: %NULL if @error is set otherwise a #PolkitUnixSession that should be freed with g_object_unref().
328
 */
329
PolkitSubject *
330
polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMonitor *monitor,
331
                                                        PolkitSubject               *subject,
332
                                                        GError                     **error)
333
{
334
  PolkitSubject *session;
335
336
  session = NULL;
337
338
  if (POLKIT_IS_UNIX_PROCESS (subject))
339
    {
340
      gchar *session_id;
341
      pid_t pid;
342
343
      pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject));
344
      if (sd_pid_get_session (pid, &session_id) < 0)
345
        goto out;
346
347
      session = polkit_unix_session_new (session_id);
348
      free (session_id);
349
    }
350
  else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
351
    {
352
      guint32 pid;
353
      gchar *session_id;
354
      GVariant *result;
355
356
      result = g_dbus_connection_call_sync (monitor->system_bus,
357
                                            "org.freedesktop.DBus",
358
                                            "/org/freedesktop/DBus",
359
                                            "org.freedesktop.DBus",
360
                                            "GetConnectionUnixProcessID",
361
                                            g_variant_new ("(s)", polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))),
362
                                            G_VARIANT_TYPE ("(u)"),
363
                                            G_DBUS_CALL_FLAGS_NONE,
364
                                            -1, /* timeout_msec */
365
                                            NULL, /* GCancellable */
366
                                            error);
367
      if (result == NULL)
368
        goto out;
369
      g_variant_get (result, "(u)", &pid);
370
      g_variant_unref (result);
371
372
      if (sd_pid_get_session (pid, &session_id) < 0)
373
        goto out;
374
375
      session = polkit_unix_session_new (session_id);
376
      free (session_id);
377
    }
378
  else
379
    {
380
      g_set_error (error,
381
                   POLKIT_ERROR,
382
                   POLKIT_ERROR_NOT_SUPPORTED,
383
                   "Cannot get user for subject of type %s",
384
                   g_type_name (G_TYPE_FROM_INSTANCE (subject)));
385
    }
386
387
 out:
388
389
  return session;
390
}
391
392
gboolean
393
polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor,
394
                                                 PolkitSubject               *session)
395
{
396
  char *seat;
397
398
  if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
399
    {
400
      free (seat);
401
      return TRUE;
402
    }
403
404
  return FALSE;
405
}
406
407
408
gboolean
409
polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
410
                                                  PolkitSubject               *session)
411
{
412
  return sd_session_is_active (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)));
413
}
414
(-)a/src/polkitbackend/polkitbackendsessionmonitor.c (-8 / +196 lines)
Lines 1-5 Link Here
1
/*
1
/*
2
 * Copyright (C) 2008 Red Hat, Inc.
2
 * Copyright (C) 2008 Red Hat, Inc.
3
 * Copyright (C) 2013 Fabio Erculiani
3
 *
4
 *
4
 * This library is free software; you can redistribute it and/or
5
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * modify it under the terms of the GNU Lesser General Public
Lines 17-32 Link Here
17
 * Boston, MA 02111-1307, USA.
18
 * Boston, MA 02111-1307, USA.
18
 *
19
 *
19
 * Author: David Zeuthen <davidz@redhat.com>
20
 * Author: David Zeuthen <davidz@redhat.com>
21
 * Author: Fabio Erculiani <lxnay@sabayon.org>
20
 */
22
 */
21
23
22
#include "config.h"
24
#include "config.h"
23
#include <errno.h>
25
#include <errno.h>
24
#include <pwd.h>
26
#include <pwd.h>
25
#include <grp.h>
27
#include <grp.h>
28
#include <stdlib.h>
26
#include <string.h>
29
#include <string.h>
27
#include <glib/gstdio.h>
30
#include <glib/gstdio.h>
28
31
29
#include <polkit/polkit.h>
32
#include <polkit/polkit.h>
33
#ifdef HAVE_LIBSYSTEMD_LOGIN
34
#include <systemd/sd-login.h>
35
#endif
36
30
#include "polkitbackendsessionmonitor.h"
37
#include "polkitbackendsessionmonitor.h"
31
38
32
#define CKDB_PATH "/var/run/ConsoleKit/database"
39
#define CKDB_PATH "/var/run/ConsoleKit/database"
Lines 36-44 Link Here
36
 * @title: PolkitBackendSessionMonitor
43
 * @title: PolkitBackendSessionMonitor
37
 * @short_description: Monitor sessions
44
 * @short_description: Monitor sessions
38
 *
45
 *
39
 * The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions.
46
 * The #PolkitBackendSessionMonitor class is a utility class to track and monitor
47
 * ConsoleKit sessions.
40
 */
48
 */
41
49
50
#ifdef HAVE_LIBSYSTEMD_LOGIN
51
typedef struct
52
{
53
  GSource source;
54
  GPollFD pollfd;
55
  sd_login_monitor *monitor;
56
} SdSource;
57
58
static gboolean
59
sd_source_prepare (GSource *source,
60
                   gint    *timeout)
61
{
62
  *timeout = -1;
63
  return FALSE;
64
}
65
66
static gboolean
67
sd_source_check (GSource *source)
68
{
69
  SdSource *sd_source = (SdSource *)source;
70
71
  return sd_source->pollfd.revents != 0;
72
}
73
74
static gboolean
75
sd_source_dispatch (GSource     *source,
76
                    GSourceFunc  callback,
77
                    gpointer     user_data)
78
79
{
80
  SdSource *sd_source = (SdSource *)source;
81
  gboolean ret;
82
83
  g_warn_if_fail (callback != NULL);
84
85
  ret = (*callback) (user_data);
86
87
  sd_login_monitor_flush (sd_source->monitor);
88
89
  return ret;
90
}
91
92
static void
93
sd_source_finalize (GSource *source)
94
{
95
  SdSource *sd_source = (SdSource*)source;
96
97
  sd_login_monitor_unref (sd_source->monitor);
98
}
99
100
static GSourceFuncs sd_source_funcs = {
101
  sd_source_prepare,
102
  sd_source_check,
103
  sd_source_dispatch,
104
  sd_source_finalize
105
};
106
107
static GSource *
108
sd_source_new (void)
109
{
110
  GSource *source;
111
  SdSource *sd_source;
112
  int ret;
113
114
  source = g_source_new (&sd_source_funcs, sizeof (SdSource));
115
  sd_source = (SdSource *)source;
116
117
  if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
118
    {
119
      g_printerr ("Error getting login monitor: %d", ret);
120
    }
121
  else
122
    {
123
      sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
124
      sd_source->pollfd.events = G_IO_IN;
125
      g_source_add_poll (source, &sd_source->pollfd);
126
    }
127
128
  return source;
129
}
130
#endif
131
42
struct _PolkitBackendSessionMonitor
132
struct _PolkitBackendSessionMonitor
43
{
133
{
44
  GObject parent_instance;
134
  GObject parent_instance;
Lines 48-53 struct _PolkitBackendSessionMonitor Link Here
48
  GKeyFile *database;
138
  GKeyFile *database;
49
  GFileMonitor *database_monitor;
139
  GFileMonitor *database_monitor;
50
  time_t database_mtime;
140
  time_t database_mtime;
141
142
  GSource *sd_source;
51
};
143
};
52
144
53
struct _PolkitBackendSessionMonitorClass
145
struct _PolkitBackendSessionMonitorClass
Lines 68-74 static guint signals[LAST_SIGNAL] = {0}; Link Here
68
160
69
G_DEFINE_TYPE (PolkitBackendSessionMonitor, polkit_backend_session_monitor, G_TYPE_OBJECT);
161
G_DEFINE_TYPE (PolkitBackendSessionMonitor, polkit_backend_session_monitor, G_TYPE_OBJECT);
70
162
71
/* ---------------------------------------------------------------------------------------------------- */
72
163
73
static gboolean
164
static gboolean
74
reload_database (PolkitBackendSessionMonitor  *monitor,
165
reload_database (PolkitBackendSessionMonitor  *monitor,
Lines 162-167 on_file_monitor_changed (GFileMonitor *file_monitor, Link Here
162
  g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
253
  g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
163
}
254
}
164
255
256
#ifdef HAVE_LIBSYSTEMD_LOGIN
257
static gboolean
258
systemd_sessions_changed (gpointer user_data)
259
{
260
  PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data);
261
262
  g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
263
264
  return TRUE;
265
}
266
#endif
267
165
static void
268
static void
166
polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor)
269
polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor)
167
{
270
{
Lines 169-174 polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor) Link Here
169
  GFile *file;
272
  GFile *file;
170
273
171
  error = NULL;
274
  error = NULL;
275
  monitor->sd_source = NULL;
276
  monitor->database_monitor = NULL;
277
172
  monitor->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
278
  monitor->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
173
  if (monitor->system_bus == NULL)
279
  if (monitor->system_bus == NULL)
174
    {
280
    {
Lines 176-181 polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor) Link Here
176
      g_error_free (error);
282
      g_error_free (error);
177
    }
283
    }
178
284
285
#ifdef HAVE_LIBSYSTEMD_LOGIN
286
  if (LOGIND_RUNNING()) {
287
    monitor->sd_source = sd_source_new ();
288
    g_source_set_callback (monitor->sd_source, systemd_sessions_changed, monitor, NULL);
289
    g_source_attach (monitor->sd_source, NULL);
290
    return;
291
  }
292
#endif
293
179
  error = NULL;
294
  error = NULL;
180
  if (!ensure_database (monitor, &error))
295
  if (!ensure_database (monitor, &error))
181
    {
296
    {
Lines 212-217 polkit_backend_session_monitor_finalize (GObject *object) Link Here
212
  if (monitor->system_bus != NULL)
327
  if (monitor->system_bus != NULL)
213
    g_object_unref (monitor->system_bus);
328
    g_object_unref (monitor->system_bus);
214
329
330
  if (monitor->sd_source != NULL)
331
	{
332
	  g_source_destroy (monitor->sd_source);
333
	  g_source_unref (monitor->sd_source);
334
	}
335
215
  if (monitor->database_monitor != NULL)
336
  if (monitor->database_monitor != NULL)
216
    g_object_unref (monitor->database_monitor);
337
    g_object_unref (monitor->database_monitor);
217
338
Lines 258-265 polkit_backend_session_monitor_new (void) Link Here
258
  return monitor;
379
  return monitor;
259
}
380
}
260
381
261
/* ---------------------------------------------------------------------------------------------------- */
262
263
GList *
382
GList *
264
polkit_backend_session_monitor_get_sessions (PolkitBackendSessionMonitor *monitor)
383
polkit_backend_session_monitor_get_sessions (PolkitBackendSessionMonitor *monitor)
265
{
384
{
Lines 267-274 polkit_backend_session_monitor_get_sessions (PolkitBackendSessionMonitor *monito Link Here
267
  return NULL;
386
  return NULL;
268
}
387
}
269
388
270
/* ---------------------------------------------------------------------------------------------------- */
271
272
/**
389
/**
273
 * polkit_backend_session_monitor_get_user:
390
 * polkit_backend_session_monitor_get_user:
274
 * @monitor: A #PolkitBackendSessionMonitor.
391
 * @monitor: A #PolkitBackendSessionMonitor.
Lines 328-333 polkit_backend_session_monitor_get_user_for_subject (PolkitBackendSessionMonitor Link Here
328
    }
445
    }
329
  else if (POLKIT_IS_UNIX_SESSION (subject))
446
  else if (POLKIT_IS_UNIX_SESSION (subject))
330
    {
447
    {
448
449
#ifdef HAVE_LIBSYSTEMD_LOGIN
450
      if (LOGIND_RUNNING())
451
        {
452
          if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0)
453
            {
454
              g_set_error (error,
455
                           POLKIT_ERROR,
456
                           POLKIT_ERROR_FAILED,
457
                           "Error getting uid for session");
458
              goto out;
459
            }
460
          ret = polkit_unix_user_new (uid);
461
          goto out;
462
        }
463
      /* fall through */
464
#endif
465
331
      if (!ensure_database (monitor, error))
466
      if (!ensure_database (monitor, error))
332
        {
467
        {
333
          g_prefix_error (error, "Error getting user for session: Error ensuring CK database at " CKDB_PATH ": ");
468
          g_prefix_error (error, "Error getting user for session: Error ensuring CK database at " CKDB_PATH ": ");
Lines 373-379 polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMoni Link Here
373
508
374
  if (POLKIT_IS_UNIX_PROCESS (subject))
509
  if (POLKIT_IS_UNIX_PROCESS (subject))
375
    {
510
    {
376
      const gchar *session_id;
511
      gchar *session_id;
512
513
#ifdef HAVE_LIBSYSTEMD_LOGIN
514
      if (LOGIND_RUNNING())
515
        {
516
          pid_t pid;
517
518
          pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject));
519
          if (sd_pid_get_session (pid, &session_id) < 0)
520
            goto out;
521
522
          session = polkit_unix_session_new (session_id);
523
          free (session_id);
524
          goto out;
525
        }
526
      /* fall through */
527
#endif
377
      GVariant *result;
528
      GVariant *result;
378
      result = g_dbus_connection_call_sync (monitor->system_bus,
529
      result = g_dbus_connection_call_sync (monitor->system_bus,
379
                                            "org.freedesktop.ConsoleKit",
530
                                            "org.freedesktop.ConsoleKit",
Lines 395-401 polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMoni Link Here
395
  else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
546
  else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
396
    {
547
    {
397
      guint32 pid;
548
      guint32 pid;
398
      const gchar *session_id;
399
      GVariant *result;
549
      GVariant *result;
400
550
401
      result = g_dbus_connection_call_sync (monitor->system_bus,
551
      result = g_dbus_connection_call_sync (monitor->system_bus,
Lines 414-419 polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMoni Link Here
414
      g_variant_get (result, "(u)", &pid);
564
      g_variant_get (result, "(u)", &pid);
415
      g_variant_unref (result);
565
      g_variant_unref (result);
416
566
567
#ifdef HAVE_LIBSYSTEMD_LOGIN
568
      gchar *sd_session_id;
569
570
      if (LOGIND_RUNNING())
571
        {
572
          if (sd_pid_get_session (pid, &sd_session_id) < 0)
573
            goto out;
574
575
          session = polkit_unix_session_new (sd_session_id);
576
          free (sd_session_id);
577
          goto out;
578
        }
579
      /* fall through */
580
#endif
581
582
      const gchar *session_id;
417
      result = g_dbus_connection_call_sync (monitor->system_bus,
583
      result = g_dbus_connection_call_sync (monitor->system_bus,
418
                                            "org.freedesktop.ConsoleKit",
584
                                            "org.freedesktop.ConsoleKit",
419
                                            "/org/freedesktop/ConsoleKit/Manager",
585
                                            "/org/freedesktop/ConsoleKit/Manager",
Lines 490-495 gboolean Link Here
490
polkit_backend_session_monitor_is_session_local  (PolkitBackendSessionMonitor *monitor,
656
polkit_backend_session_monitor_is_session_local  (PolkitBackendSessionMonitor *monitor,
491
                                                  PolkitSubject               *session)
657
                                                  PolkitSubject               *session)
492
{
658
{
659
#if HAVE_LIBSYSTEMD_LOGIN
660
  if (LOGIND_RUNNING())
661
    {
662
      char *seat;
663
664
      if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat))
665
        {
666
          free (seat);
667
          return TRUE;
668
        }
669
670
      return FALSE;
671
    }
672
  /* fall through */
673
#endif
493
  return get_boolean (monitor, session, "is_local");
674
  return get_boolean (monitor, session, "is_local");
494
}
675
}
495
676
Lines 498-503 gboolean Link Here
498
polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
679
polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor,
499
                                                  PolkitSubject               *session)
680
                                                  PolkitSubject               *session)
500
{
681
{
682
#if HAVE_LIBSYSTEMD_LOGIN
683
  if (LOGIND_RUNNING())
684
    {
685
      return sd_session_is_active (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)));
686
    }
687
  /* fall through */
688
#endif
501
  return get_boolean (monitor, session, "is_active");
689
  return get_boolean (monitor, session, "is_active");
502
}
690
}
503
691
(-)a/src/polkitbackend/polkitbackendsessionmonitor.h (-1 / +6 lines)
Lines 29-34 Link Here
29
#include <glib-object.h>
29
#include <glib-object.h>
30
#include <polkitbackend/polkitbackendtypes.h>
30
#include <polkitbackend/polkitbackendtypes.h>
31
31
32
/* check if logind is running
33
 * thanks to: https://bugzilla.gnome.org/show_bug.cgi?id=696266
34
 */
35
#define LOGIND_RUNNING() (access("/run/systemd/seats/", F_OK) >= 0)
36
37
32
G_BEGIN_DECLS
38
G_BEGIN_DECLS
33
39
34
#define POLKIT_BACKEND_TYPE_SESSION_MONITOR         (polkit_backend_session_monitor_get_type ())
40
#define POLKIT_BACKEND_TYPE_SESSION_MONITOR         (polkit_backend_session_monitor_get_type ())
35
- 

Return to bug 468938