Debian Codegraph @

src/polkit/polkitunixsession-systemd.c

raw
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 <stdlib.h>
23 #include <string.h>
24 #include "polkitunixsession.h"
25 #include "polkitsubject.h"
26 #include "polkiterror.h"
27 #include "polkitprivate.h"
28
29 #include <systemd/sd-login.h>
30
31 /**
32 * SECTION:polkitunixsession
33 * @title: PolkitUnixSession
34 * @short_description: Unix sessions
35 *
36 * An object that represents an user session.
37 *
38 * The session id is an opaque string obtained from ConsoleKit.
39 */
40
41 /**
42 * PolkitUnixSession:
43 *
44 * The #PolkitUnixSession struct should not be accessed directly.
45 */
46 struct _PolkitUnixSession
47 {
48 GObject parent_instance;
49
50 gchar *session_id;
51
52 gint pid;
53 };
54
55 struct _PolkitUnixSessionClass
56 {
57 GObjectClass parent_class;
58 };
59
60 enum
61 {
62 PROP_0,
63 PROP_SESSION_ID,
64 PROP_PID,
65 };
66
67 static void subject_iface_init (PolkitSubjectIface *subject_iface);
68 static void initable_iface_init (GInitableIface *initable_iface);
69 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
70
71 G_DEFINE_TYPE_WITH_CODE (PolkitUnixSession, polkit_unix_session, G_TYPE_OBJECT,
72 G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init)
73 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
74 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
75 );
76
77 static void
78 polkit_unix_session_init (PolkitUnixSession *session)
79 {
80 }
81
82 static void
83 polkit_unix_session_finalize (GObject *object)
84 {
85 PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
86
87 g_free (session->session_id);
88
89 if (G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize != NULL)
90 G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize (object);
91 }
92
93 static void
94 polkit_unix_session_get_property (GObject *object,
95 guint prop_id,
96 GValue *value,
97 GParamSpec *pspec)
98 {
99 PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
100
101 switch (prop_id)
102 {
103 case PROP_SESSION_ID:
104 g_value_set_string (value, session->session_id);
105 break;
106
107 default:
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109 break;
110 }
111 }
112
113 static void
114 polkit_unix_session_set_property (GObject *object,
115 guint prop_id,
116 const GValue *value,
117 GParamSpec *pspec)
118 {
119 PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);
120
121 switch (prop_id)
122 {
123 case PROP_SESSION_ID:
124 polkit_unix_session_set_session_id (session, g_value_get_string (value));
125 break;
126
127 case PROP_PID:
128 session->pid = g_value_get_int (value);
129 break;
130
131 default:
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133 break;
134 }
135 }
136
137 static void
138 polkit_unix_session_class_init (PolkitUnixSessionClass *klass)
139 {
140 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
141
142 gobject_class->finalize = polkit_unix_session_finalize;
143 gobject_class->get_property = polkit_unix_session_get_property;
144 gobject_class->set_property = polkit_unix_session_set_property;
145
146 /**
147 * PolkitUnixSession:session-id:
148 *
149 * The UNIX session id.
150 */
151 g_object_class_install_property (gobject_class,
152 PROP_SESSION_ID,
153 g_param_spec_string ("session-id",
154 "Session ID",
155 "The UNIX session ID",
156 NULL,
157 G_PARAM_CONSTRUCT |
158 G_PARAM_READWRITE |
159 G_PARAM_STATIC_NAME |
160 G_PARAM_STATIC_BLURB |
161 G_PARAM_STATIC_NICK));
162
163
164 /**
165 * PolkitUnixSession:pid:
166 *
167 * The UNIX process id to look up the session.
168 */
169 g_object_class_install_property (gobject_class,
170 PROP_PID,
171 g_param_spec_int ("pid",
172 "Process ID",
173 "Process ID to use for looking up the session",
174 0,
175 G_MAXINT,
176 0,
177 G_PARAM_CONSTRUCT_ONLY |
178 G_PARAM_WRITABLE |
179 G_PARAM_STATIC_NAME |
180 G_PARAM_STATIC_BLURB |
181 G_PARAM_STATIC_NICK));
182
183 }
184
185 /**
186 * polkit_unix_session_get_session_id:
187 * @session: A #PolkitUnixSession.
188 *
189 * Gets the session id for @session.
190 *
191 * Returns: The session id for @session. Do not free this string, it
192 * is owned by @session.
193 **/
194 const gchar *
195 polkit_unix_session_get_session_id (PolkitUnixSession *session)
196 {
197 g_return_val_if_fail (POLKIT_IS_UNIX_SESSION (session), NULL);
198 return session->session_id;
199 }
200
201 /**
202 * polkit_unix_session_set_session_id:
203 * @session: A #PolkitUnixSession.
204 * @session_id: The session id.
205 *
206 * Sets the session id for @session to @session_id.
207 **/
208 void
209 polkit_unix_session_set_session_id (PolkitUnixSession *session,
210 const gchar *session_id)
211 {
212 g_return_if_fail (POLKIT_IS_UNIX_SESSION (session));
213 /*g_return_if_fail (session_id != NULL);*/
214 g_free (session->session_id);
215 session->session_id = g_strdup (session_id);
216 }
217
218 /**
219 * polkit_unix_session_new:
220 * @session_id: The session id.
221 *
222 * Creates a new #PolkitUnixSession for @session_id.
223 *
224 * Returns: (transfer full): A #PolkitUnixSession. Free with g_object_unref().
225 **/
226 PolkitSubject *
227 polkit_unix_session_new (const gchar *session_id)
228 {
229 return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_UNIX_SESSION,
230 "session-id", session_id,
231 NULL));
232 }
233
234 /**
235 * polkit_unix_session_new_for_process:
236 * @pid: The process id of the process to get the session for.
237 * @cancellable: (allow-none): A #GCancellable or %NULL.
238 * @callback: A #GAsyncReadyCallback to call when the request is satisfied
239 * @user_data: The data to pass to @callback.
240 *
241 * Asynchronously creates a new #PolkitUnixSession object for the
242 * process with process id @pid.
243 *
244 * When the operation is finished, @callback will be invoked in the
245 * <link linkend="g-main-context-push-thread-default">thread-default
246 * main loop</link> of the thread you are calling this method
247 * from. You can then call
248 * polkit_unix_session_new_for_process_finish() to get the result of
249 * the operation.
250 *
251 * This method constructs the object asynchronously, for the synchronous and blocking version
252 * use polkit_unix_session_new_for_process_sync().
253 **/
254 void
255 polkit_unix_session_new_for_process (gint pid,
256 GCancellable *cancellable,
257 GAsyncReadyCallback callback,
258 gpointer user_data)
259 {
260 g_async_initable_new_async (POLKIT_TYPE_UNIX_SESSION,
261 G_PRIORITY_DEFAULT,
262 cancellable,
263 callback,
264 user_data,
265 "pid", pid,
266 NULL);
267 }
268
269 /**
270 * polkit_unix_session_new_for_process_finish:
271 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to polkit_unix_session_new_for_process().
272 * @error: (allow-none): Return location for error.
273 *
274 * Finishes constructing a #PolkitSubject for a process id.
275 *
276 * Returns: (transfer full) (allow-none): A #PolkitUnixSession for the @pid passed to
277 * polkit_unix_session_new_for_process() or %NULL if @error is
278 * set. Free with g_object_unref().
279 **/
280 PolkitSubject *
281 polkit_unix_session_new_for_process_finish (GAsyncResult *res,
282 GError **error)
283 {
284 GObject *object;
285 GObject *source_object;
286
287 source_object = g_async_result_get_source_object (res);
288 g_assert (source_object != NULL);
289
290 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
291 res,
292 error);
293 g_object_unref (source_object);
294
295 if (object != NULL)
296 return POLKIT_SUBJECT (object);
297 else
298 return NULL;
299 }
300
301
302 /**
303 * polkit_unix_session_new_for_process_sync:
304 * @pid: The process id of the process to get the session for.
305 * @cancellable: (allow-none): A #GCancellable or %NULL.
306 * @error: (allow-none): Return location for error.
307 *
308 * Creates a new #PolkitUnixSession for the process with process id @pid.
309 *
310 * This is a synchronous call - the calling thread is blocked until a
311 * reply is received. For the asynchronous version, see
312 * polkit_unix_session_new_for_process().
313 *
314 * Returns: (allow-none) (transfer full): A #PolkitUnixSession for
315 * @pid or %NULL if @error is set. Free with g_object_unref().
316 **/
317 PolkitSubject *
318 polkit_unix_session_new_for_process_sync (gint pid,
319 GCancellable *cancellable,
320 GError **error)
321 {
322 return POLKIT_SUBJECT (g_initable_new (POLKIT_TYPE_UNIX_SESSION,
323 cancellable,
324 error,
325 "pid", pid,
326 NULL));
327 }
328
329 static guint
330 polkit_unix_session_hash (PolkitSubject *subject)
331 {
332 PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
333
334 return g_str_hash (session->session_id);
335 }
336
337 static gboolean
338 polkit_unix_session_equal (PolkitSubject *a,
339 PolkitSubject *b)
340 {
341 PolkitUnixSession *session_a;
342 PolkitUnixSession *session_b;
343
344 session_a = POLKIT_UNIX_SESSION (a);
345 session_b = POLKIT_UNIX_SESSION (b);
346
347 return g_strcmp0 (session_a->session_id, session_b->session_id) == 0;
348 }
349
350 static gchar *
351 polkit_unix_session_to_string (PolkitSubject *subject)
352 {
353 PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
354
355 return g_strdup_printf ("unix-session:%s", session->session_id);
356 }
357
358 static gboolean
359 polkit_unix_session_exists_sync (PolkitSubject *subject,
360 GCancellable *cancellable,
361 GError **error)
362 {
363 PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
364 gboolean ret = FALSE;
365 uid_t uid;
366
367 if (sd_session_get_uid (session->session_id, &uid) == 0)
368 ret = TRUE;
369
370 return ret;
371 }
372
373 static void
374 exists_in_thread_func (GSimpleAsyncResult *res,
375 GObject *object,
376 GCancellable *cancellable)
377 {
378 GError *error;
379 error = NULL;
380 if (!polkit_unix_session_exists_sync (POLKIT_SUBJECT (object),
381 cancellable,
382 &error))
383 {
384 g_simple_async_result_set_from_error (res, error);
385 g_error_free (error);
386 }
387 }
388
389 static void
390 polkit_unix_session_exists (PolkitSubject *subject,
391 GCancellable *cancellable,
392 GAsyncReadyCallback callback,
393 gpointer user_data)
394 {
395 GSimpleAsyncResult *simple;
396
397 g_return_if_fail (POLKIT_IS_UNIX_SESSION (subject));
398
399 simple = g_simple_async_result_new (G_OBJECT (subject),
400 callback,
401 user_data,
402 polkit_unix_session_exists);
403 g_simple_async_result_run_in_thread (simple,
404 exists_in_thread_func,
405 G_PRIORITY_DEFAULT,
406 cancellable);
407 g_object_unref (simple);
408 }
409
410 static gboolean
411 polkit_unix_session_exists_finish (PolkitSubject *subject,
412 GAsyncResult *res,
413 GError **error)
414 {
415 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
416 gboolean ret;
417
418 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == polkit_unix_session_exists);
419
420 ret = FALSE;
421
422 if (g_simple_async_result_propagate_error (simple, error))
423 goto out;
424
425 ret = g_simple_async_result_get_op_res_gboolean (simple);
426
427 out:
428 return ret;
429 }
430
431 static void
432 subject_iface_init (PolkitSubjectIface *subject_iface)
433 {
434 subject_iface->hash = polkit_unix_session_hash;
435 subject_iface->equal = polkit_unix_session_equal;
436 subject_iface->to_string = polkit_unix_session_to_string;
437 subject_iface->exists = polkit_unix_session_exists;
438 subject_iface->exists_finish = polkit_unix_session_exists_finish;
439 subject_iface->exists_sync = polkit_unix_session_exists_sync;
440 }
441
442 static gboolean
443 polkit_unix_session_initable_init (GInitable *initable,
444 GCancellable *cancellable,
445 GError **error)
446 {
447 PolkitUnixSession *session = POLKIT_UNIX_SESSION (initable);
448 gboolean ret = FALSE;
449 char *s;
450 uid_t uid;
451
452 if (session->session_id != NULL)
453 {
454 /* already set, nothing to do */
455 ret = TRUE;
456 goto out;
457 }
458
459 if (sd_pid_get_session (session->pid, &s) == 0)
460 {
461 session->session_id = g_strdup (s);
462 free (s);
463 ret = TRUE;
464 goto out;
465 }
466
467 /* Now do process -> uid -> graphical session (systemd version 213)*/
468 if (sd_pid_get_owner_uid (session->pid, &uid) < 0)
469 goto error;
470
471 if (sd_uid_get_display (uid, &s) >= 0)
472 {
473 session->session_id = g_strdup (s);
474 free (s);
475 ret = TRUE;
476 goto out;
477 }
478
479 error:
480 g_set_error (error,
481 POLKIT_ERROR,
482 POLKIT_ERROR_FAILED,
483 "No session for pid %d",
484 (gint) session->pid);
485
486 out:
487 return ret;
488 }
489
490 static void
491 initable_iface_init (GInitableIface *initable_iface)
492 {
493 initable_iface->init = polkit_unix_session_initable_init;
494 }
495
496 static void
497 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
498 {
499 /* use default implementation to run GInitable code in a thread */
500 }