Debian Codegraph @

src/systemd/gsm-systemd.c

raw symbols SCIP
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 */
4
5 #include "config.h"
6
7 #include <unistd.h>
8
9 #include <glib/gstdio.h>
10 #include <systemd/sd-login.h>
11
12 #include "gsm-systemd.h"
13
14
15 gboolean
16 gsm_systemd_is_running (void)
17 {
18 enum {
19 UNINIT = 0,
20 RUNNING,
21 NOT_RUNNING,
22 };
23 static size_t status = UNINIT;
24
25 if (g_once_init_enter (&status)) {
26 gboolean is_running = g_access ("/run/systemd/seats/", F_OK) >= 0;
27
28 g_once_init_leave (&status, is_running ? RUNNING : NOT_RUNNING);
29 }
30
31 return status == RUNNING;
32 }
33
34
35 gboolean
36 gsm_systemd_get_process_info (pid_t pid,
37 char **sd_unit,
38 char **sd_session,
39 char **sd_seat,
40 uid_t *sd_owner)
41 {
42 char *unit = NULL;
43 char *session = NULL;
44 char *seat = NULL;
45 uid_t uid;
46
47 if (!gsm_systemd_is_running ()) {
48 g_set_str (sd_unit, NULL);
49 g_set_str (sd_session, NULL);
50 g_set_str (sd_seat, NULL);
51
52 return FALSE;
53 }
54
55 sd_pid_get_unit (pid, &unit);
56 g_set_str (sd_unit, unit);
57
58 sd_pid_get_session (pid, &session);
59 g_set_str (sd_session, session);
60
61 if (session) {
62 sd_session_get_seat (session, &seat);
63 g_set_str (sd_seat, seat);
64 }
65
66 if (sd_pid_get_owner_uid (pid, &uid) >= 0) {
67 *sd_owner = uid;
68 }
69
70 /* These are raw libc values, not glib, so we must manually free them */
71 g_clear_pointer (&unit, free);
72 g_clear_pointer (&session, free);
73 g_clear_pointer (&seat, free);
74
75 return TRUE;
76 }
77