2 This file is part of elogind.
4 Copyright 2017 Sven Eden
6 elogind is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 elogind is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with elogind; If not, see <http://www.gnu.org/licenses/>.
25 #include "mount-setup.h"
26 #include "socket-util.h"
27 #include "string-util.h"
29 #include "umask-util.h"
32 #define CGROUPS_AGENT_RCVBUF_SIZE (8*1024*1024)
35 static int signal_agent_released(sd_bus_message *message, void *userdata, sd_bus_error *error) {
36 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
37 Manager *m = userdata;
45 /* only accept org.freedesktop.login1.Agent from UID=0 */
46 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
50 r = sd_bus_creds_get_euid(creds, &sender_uid);
51 if (r < 0 || sender_uid != 0)
54 /* parse 'cgroup-empty' notification */
55 r = sd_bus_message_read(message, "s", &cgroup);
57 bus_log_parse_error(r);
61 manager_notify_cgroup_empty(m, cgroup);
66 /// Add-On for manager_connect_bus()
67 /// Original: src/core/dbus.c:bus_setup_system()
68 void elogind_bus_setup_system(Manager* m) {
74 /* if we are a user instance we get the Released message via the system bus */
75 if (MANAGER_IS_USER(m)) {
80 "interface='org.freedesktop.login1.Agent',"
82 "path='/org/freedesktop.login1/agent'",
83 signal_agent_released, m);
85 log_warning_errno(r, "Failed to register Released match on system bus: %m");
88 log_debug("Successfully connected to system bus.");
91 static int manager_dispatch_cgroups_agent_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
92 Manager *m = userdata;
96 n = recv(fd, buf, sizeof(buf), 0);
98 return log_error_errno(errno, "Failed to read cgroups agent message: %m");
100 log_error("Got zero-length cgroups agent message, ignoring.");
103 if ((size_t) n >= sizeof(buf)) {
104 log_error("Got overly long cgroups agent message, ignoring.");
108 if (memchr(buf, 0, n)) {
109 log_error("Got cgroups agent message with embedded NUL byte, ignoring.");
114 manager_notify_cgroup_empty(m, buf);
119 /// Add-On for manager_connect_bus()
120 /// Original: src/core/manager.c:manager_setup_cgroups_agent()
121 int elogind_setup_cgroups_agent(Manager *m) {
123 static const union sockaddr_union sa = {
124 .un.sun_family = AF_UNIX,
125 .un.sun_path = "/run/systemd/cgroups-agent",
129 /* This creates a listening socket we receive cgroups agent messages on. We do not use D-Bus for delivering
130 * these messages from the cgroups agent binary to PID 1, as the cgroups agent binary is very short-living, and
131 * each instance of it needs a new D-Bus connection. Since D-Bus connections are SOCK_STREAM/AF_UNIX, on
132 * overloaded systems the backlog of the D-Bus socket becomes relevant, as not more than the configured number
133 * of D-Bus connections may be queued until the kernel will start dropping further incoming connections,
134 * possibly resulting in lost cgroups agent messages. To avoid this, we'll use a private SOCK_DGRAM/AF_UNIX
135 * socket, where no backlog is relevant as communication may take place without an actual connect() cycle, and
136 * we thus won't lose messages.
138 * Note that PID 1 will forward the agent message to system bus, so that the user systemd instance may listen
139 * to it. The system instance hence listens on this special socket, but the user instances listen on the system
140 * bus for these messages. */
145 if (!MANAGER_IS_SYSTEM(m))
148 if (cg_unified(SYSTEMD_CGROUP_CONTROLLER) > 0) /* We don't need this anymore on the unified hierarchy */
151 if (m->cgroups_agent_fd < 0) {
152 _cleanup_close_ int fd = -1;
154 /* First free all secondary fields */
155 m->cgroups_agent_event_source = sd_event_source_unref(m->cgroups_agent_event_source);
157 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
159 return log_error_errno(errno, "Failed to allocate cgroups agent socket: %m");
161 fd_inc_rcvbuf(fd, CGROUPS_AGENT_RCVBUF_SIZE);
163 (void) unlink(sa.un.sun_path);
165 /* Only allow root to connect to this socket */
167 r = bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
169 return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path);
171 m->cgroups_agent_fd = fd;
175 if (!m->cgroups_agent_event_source) {
176 r = sd_event_add_io(m->event, &m->cgroups_agent_event_source, m->cgroups_agent_fd, EPOLLIN, manager_dispatch_cgroups_agent_fd, m);
178 return log_error_errno(r, "Failed to allocate cgroups agent event source: %m");
180 /* Process cgroups notifications early, but after having processed service notification messages or
181 * SIGCHLD signals, so that a cgroup running empty is always just the last safety net of notification,
182 * and we collected the metadata the notification and SIGCHLD stuff offers first. Also see handling of
183 * cgroup inotify for the unified cgroup stuff. */
184 r = sd_event_source_set_priority(m->cgroups_agent_event_source, SD_EVENT_PRIORITY_NORMAL-5);
186 return log_error_errno(r, "Failed to set priority of cgroups agent event source: %m");
188 (void) sd_event_source_set_description(m->cgroups_agent_event_source, "manager-cgroups-agent");
194 /// Add-On for manager_free()
195 void elogind_manager_free(Manager* m) {
197 manager_shutdown_cgroup(m, true);
199 sd_event_source_unref(m->cgroups_agent_event_source);
201 safe_close(m->cgroups_agent_fd);
203 strv_free(m->suspend_mode);
204 strv_free(m->suspend_state);
205 strv_free(m->hibernate_mode);
206 strv_free(m->hibernate_state);
207 strv_free(m->hybrid_sleep_mode);
208 strv_free(m->hybrid_sleep_state);
211 /// Add-On for manager_new()
212 int elogind_manager_new(Manager* m) {
215 m->cgroups_agent_fd = -1;
216 m->pin_cgroupfs_fd = -1;
219 /* Init sleep modes and states */
220 m->suspend_mode = NULL;
221 m->suspend_state = NULL;
222 m->hibernate_mode = NULL;
223 m->hibernate_state = NULL;
224 m->hybrid_sleep_mode = NULL;
225 m->hybrid_sleep_state = NULL;
227 /* If elogind should be its own controller, mount its cgroup */
228 if (streq(SYSTEMD_CGROUP_CONTROLLER, "name=elogind")) {
230 r = mount_setup(true);
232 m->is_system = false;
236 r = manager_setup_cgroup(m);
241 /// Add-On for manager_reset_config()
242 void elogind_manager_reset_config(Manager* m) {
244 #ifdef ENABLE_DEBUG_ELOGIND
246 #endif // ENABLE_DEBUG_ELOGIND
248 /* Set default Sleep config if not already set by logind.conf */
249 if (!m->suspend_state)
250 m->suspend_state = strv_new("mem", "standby", "freeze", NULL);
251 if (!m->hibernate_mode)
252 m->hibernate_mode = strv_new("platform", "shutdown", NULL);
253 if (!m->hibernate_state)
254 m->hibernate_state = strv_new("disk", NULL);
255 if (!m->hybrid_sleep_mode)
256 m->hybrid_sleep_mode = strv_new("suspend", "platform", "shutdown", NULL);
257 if (!m->hybrid_sleep_state)
258 m->hybrid_sleep_state = strv_new("disk", NULL);
260 #ifdef ENABLE_DEBUG_ELOGIND
262 while (m->suspend_mode && m->suspend_mode[++dbg_cnt])
263 log_debug_elogind("suspend_mode[%d] = %s",
264 dbg_cnt, m->suspend_mode[dbg_cnt]);
266 while (m->suspend_state[++dbg_cnt])
267 log_debug_elogind("suspend_state[%d] = %s",
268 dbg_cnt, m->suspend_state[dbg_cnt]);
270 while (m->hibernate_mode[++dbg_cnt])
271 log_debug_elogind("hibernate_mode[%d] = %s",
272 dbg_cnt, m->hibernate_mode[dbg_cnt]);
274 while (m->hibernate_state[++dbg_cnt])
275 log_debug_elogind("hibernate_state[%d] = %s",
276 dbg_cnt, m->hibernate_state[dbg_cnt]);
278 while (m->hybrid_sleep_mode[++dbg_cnt])
279 log_debug_elogind("hybrid_sleep_mode[%d] = %s",
280 dbg_cnt, m->hybrid_sleep_mode[dbg_cnt]);
282 while (m->hybrid_sleep_state[++dbg_cnt])
283 log_debug_elogind("hybrid_sleep_state[%d] = %s",
284 dbg_cnt, m->hybrid_sleep_state[dbg_cnt]);
285 #endif // ENABLE_DEBUG_ELOGIND