Debian Codegraph @

modules/module-logind.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2021 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include <wp/wp.h>
10 #include <glib-unix.h>
11
12 #include <systemd/sd-login.h>
13
14 #include <spa/utils/result.h>
15 #include <spa/utils/string.h>
16
17 WP_DEFINE_LOCAL_LOG_TOPIC ("m-logind")
18
19 #define NAME "logind"
20
21 struct _WpLogind
22 {
23 WpPlugin parent;
24 sd_login_monitor *monitor;
25 GSource *source;
26 char *state;
27 };
28
29 enum {
30 ACTION_GET_STATE,
31 SIGNAL_STATE_CHANGED,
32 N_SIGNALS
33 };
34
35 static guint signals[N_SIGNALS] = {0};
36
37 G_DECLARE_FINAL_TYPE (WpLogind, wp_logind, WP, LOGIND, WpPlugin)
38 G_DEFINE_TYPE (WpLogind, wp_logind, WP_TYPE_PLUGIN)
39
40 static void
41 wp_logind_init (WpLogind * self)
42 {
43 }
44
45 static gchar *
46 wp_logind_get_state (WpLogind *self)
47 {
48 return g_strdup (self->state);
49 }
50
51 static int
52 wp_logind_get_user_state (uid_t uid, char **state)
53 {
54 int res;
55 if ((res = sd_uid_get_state (uid, state)) >= 0) {
56 if (g_strcmp0 (*state, "active") == 0 &&
57 sd_uid_get_seats (uid, 1, NULL) == 0) {
58 free (*state);
59 *state = strdup ("online");
60 }
61 }
62 return res;
63 }
64
65 static gboolean
66 wp_logind_source_ready (gint fd, GIOCondition condition, gpointer user_data)
67 {
68 WpLogind *self = WP_LOGIND (user_data);
69 sd_login_monitor_flush (self->monitor);
70 {
71 char *state = NULL;
72 if (wp_logind_get_user_state (getuid(), &state) >= 0) {
73 if (g_strcmp0 (state, self->state) != 0) {
74 char *tmp = state;
75 state = self->state;
76 self->state = tmp;
77 g_signal_emit (self, signals[SIGNAL_STATE_CHANGED], 0, self->state);
78 }
79 free (state);
80 }
81 }
82 return G_SOURCE_CONTINUE;
83 }
84
85 static void
86 wp_logind_enable (WpPlugin * plugin, WpTransition * transition)
87 {
88 WpLogind *self = WP_LOGIND (plugin);
89 int res = 0;
90
91 if ((res = sd_login_monitor_new ("uid", &self->monitor)) < 0) {
92 wp_transition_return_error (transition, g_error_new (G_IO_ERROR,
93 g_io_error_from_errno (-res),
94 "failed to start systemd logind monitor: %d (%s)",
95 res, spa_strerror(res)));
96 return;
97 }
98
99 if ((res = wp_logind_get_user_state (getuid(), &self->state)) < 0) {
100 wp_transition_return_error (transition, g_error_new (G_IO_ERROR,
101 g_io_error_from_errno (-res),
102 "failed to get systemd login state: %d (%s)",
103 res, spa_strerror(res)));
104 g_clear_pointer (&self->monitor, sd_login_monitor_unref);
105 return;
106 }
107
108 self->source = g_unix_fd_source_new (
109 sd_login_monitor_get_fd (self->monitor),
110 sd_login_monitor_get_events (self->monitor));
111 g_source_set_callback (self->source, G_SOURCE_FUNC (wp_logind_source_ready),
112 self, NULL);
113
114 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (plugin));
115 GMainContext *context = wp_core_get_g_main_context (core);
116 g_source_attach (self->source, context);
117
118 wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0);
119 }
120
121 static void
122 wp_logind_disable (WpPlugin * plugin)
123 {
124 WpLogind *self = WP_LOGIND (plugin);
125
126 g_clear_pointer (&self->state, free);
127 g_source_destroy (self->source);
128 g_clear_pointer (&self->source, g_source_unref);
129 g_clear_pointer (&self->monitor, sd_login_monitor_unref);
130 }
131
132 static void
133 wp_logind_class_init (WpLogindClass * klass)
134 {
135 WpPluginClass *plugin_class = (WpPluginClass *) klass;
136
137 plugin_class->enable = wp_logind_enable;
138 plugin_class->disable = wp_logind_disable;
139
140 signals[ACTION_GET_STATE] = g_signal_new_class_handler (
141 "get-state", G_TYPE_FROM_CLASS (klass),
142 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
143 (GCallback) wp_logind_get_state,
144 NULL, NULL, NULL, G_TYPE_STRING, 0);
145
146 signals[SIGNAL_STATE_CHANGED] = g_signal_new (
147 "state-changed", G_TYPE_FROM_CLASS (klass),
148 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_STRING);
149 }
150
151 WP_PLUGIN_EXPORT GObject *
152 wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error)
153 {
154 return G_OBJECT (g_object_new (wp_logind_get_type (),
155 "name", NAME,
156 "core", core,
157 NULL));
158 }