1 /* PipeWire
2 *
3 * Copyright © 2021 Pauli Virtanen
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 /*
26 * Monitor systemd-logind events for changes in session/seat status, and keep session
27 * manager up-to-date on whether the current session is active.
28 */
29
30 #include "config.h"
31
32 #include <sys/types.h>
33 #include <unistd.h>
34
35 #include <systemd/sd-login.h>
36
37 #include <spa/utils/result.h>
38 #include <spa/utils/string.h>
39 #include "pipewire/pipewire.h"
40
41 #include "media-session.h"
42
43 /** \page page_media_session_module_logind Media Session Module: Logind
44 *
45 * The logind module uses systemd logind to keep track of the user's session
46 * and updates the media session's seat state accordingly.
47 *
48 * The session state may be used by other modules, e.g. the \ref
49 * page_media_session_module_bluez_monitor module enables/disables
50 * Bluetooth whenever the session changes between active and inactive.
51 */
52
53 #define NAME "logind"
54
55 PW_LOG_TOPIC_STATIC(mod_topic, "ms.mod." NAME);
56 #define PW_LOG_TOPIC_DEFAULT mod_topic
57
58 struct impl {
59 struct sm_media_session *session;
60 struct spa_hook listener;
61 struct pw_context *context;
62
63 sd_login_monitor *monitor;
64 struct spa_source source;
65 };
66
update_seat_active(struct impl * impl)67 static void update_seat_active(struct impl *impl)
68 {
69 char *state;
70 bool active;
71
72 if (sd_uid_get_state(getuid(), &state) < 0)
73 return;
74
75 active = spa_streq(state, "active");
76 free(state);
77
78 sm_media_session_seat_active_changed(impl->session, active);
79 }
80
monitor_event(struct spa_source * source)81 static void monitor_event(struct spa_source *source)
82 {
83 struct impl *impl = source->data;
84 sd_login_monitor_flush(impl->monitor);
85 update_seat_active(impl);
86 }
87
session_destroy(void * data)88 static void session_destroy(void *data)
89 {
90 struct impl *impl = data;
91 spa_hook_remove(&impl->listener);
92 if (impl->monitor) {
93 struct pw_loop *main_loop = pw_context_get_main_loop(impl->context);
94 pw_loop_remove_source(main_loop, &impl->source);
95 sd_login_monitor_unref(impl->monitor);
96 impl->monitor = NULL;
97 }
98 free(impl);
99 }
100
101 static const struct sm_media_session_events session_events = {
102 SM_VERSION_MEDIA_SESSION_EVENTS,
103 .destroy = session_destroy,
104 };
105
sm_logind_start(struct sm_media_session * session)106 int sm_logind_start(struct sm_media_session *session)
107 {
108 struct impl *impl;
109 struct pw_loop *main_loop;
110 int res;
111
112 PW_LOG_TOPIC_INIT(mod_topic);
113
114 impl = calloc(1, sizeof(struct impl));
115 if (impl == NULL)
116 return -errno;
117
118 impl->session = session;
119 impl->context = session->context;
120
121 if ((res = sd_login_monitor_new(NULL, &impl->monitor)) < 0)
122 goto fail;
123
124 main_loop = pw_context_get_main_loop(impl->context);
125
126 impl->source.data = impl;
127 impl->source.fd = sd_login_monitor_get_fd(impl->monitor);
128 impl->source.func = monitor_event;
129 impl->source.mask = sd_login_monitor_get_events(impl->monitor);
130 impl->source.rmask = 0;
131 pw_loop_add_source(main_loop, &impl->source);
132
133 sm_media_session_add_listener(impl->session, &impl->listener, &session_events, impl);
134
135 update_seat_active(impl);
136
137 return 0;
138
139 fail:
140 pw_log_error(": failed to start systemd logind monitor: %d (%s)", res, spa_strerror(res));
141 free(impl);
142 return res;
143 }
144