]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-inhibit.c
Merge pull request #32439 from CodethinkLabs/simple-mkosi-integration-tests
[thirdparty/systemd.git] / src / login / logind-inhibit.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "env-file.h"
11 #include "errno-list.h"
12 #include "errno-util.h"
13 #include "escape.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "format-util.h"
17 #include "fs-util.h"
18 #include "io-util.h"
19 #include "logind-dbus.h"
20 #include "logind-inhibit.h"
21 #include "missing_threads.h"
22 #include "mkdir-label.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "string-table.h"
26 #include "string-util.h"
27 #include "tmpfile-util.h"
28 #include "user-util.h"
29
30 static void inhibitor_remove_fifo(Inhibitor *i);
31
32 int inhibitor_new(Manager *m, const char* id, Inhibitor **ret) {
33 _cleanup_(inhibitor_freep) Inhibitor *i = NULL;
34 int r;
35
36 assert(m);
37 assert(id);
38 assert(ret);
39
40 i = new(Inhibitor, 1);
41 if (!i)
42 return -ENOMEM;
43
44 *i = (Inhibitor) {
45 .manager = m,
46 .id = strdup(id),
47 .state_file = path_join("/run/systemd/inhibit/", id),
48 .what = _INHIBIT_WHAT_INVALID,
49 .mode = _INHIBIT_MODE_INVALID,
50 .uid = UID_INVALID,
51 .fifo_fd = -EBADF,
52 .pid = PIDREF_NULL,
53 };
54
55 if (!i->id || !i->state_file)
56 return -ENOMEM;
57
58 r = hashmap_put(m->inhibitors, i->id, i);
59 if (r < 0)
60 return r;
61
62 *ret = TAKE_PTR(i);
63 return 0;
64 }
65
66 Inhibitor* inhibitor_free(Inhibitor *i) {
67
68 if (!i)
69 return NULL;
70
71 free(i->who);
72 free(i->why);
73
74 sd_event_source_unref(i->event_source);
75 safe_close(i->fifo_fd);
76
77 hashmap_remove(i->manager->inhibitors, i->id);
78
79 /* Note that we don't remove neither the state file nor the fifo path here, since we want both to
80 * survive daemon restarts */
81 free(i->fifo_path);
82 free(i->state_file);
83 free(i->id);
84
85 pidref_done(&i->pid);
86
87 return mfree(i);
88 }
89
90 static int inhibitor_save(Inhibitor *i) {
91 _cleanup_(unlink_and_freep) char *temp_path = NULL;
92 _cleanup_fclose_ FILE *f = NULL;
93 int r;
94
95 assert(i);
96
97 r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE);
98 if (r < 0)
99 goto fail;
100
101 r = fopen_temporary(i->state_file, &f, &temp_path);
102 if (r < 0)
103 goto fail;
104
105 (void) fchmod(fileno(f), 0644);
106
107 fprintf(f,
108 "# This is private data. Do not parse.\n"
109 "WHAT=%s\n"
110 "MODE=%s\n"
111 "UID="UID_FMT"\n"
112 "PID="PID_FMT"\n",
113 inhibit_what_to_string(i->what),
114 inhibit_mode_to_string(i->mode),
115 i->uid,
116 i->pid.pid);
117
118 if (i->who) {
119 _cleanup_free_ char *cc = NULL;
120
121 cc = cescape(i->who);
122 if (!cc) {
123 r = -ENOMEM;
124 goto fail;
125 }
126
127 fprintf(f, "WHO=%s\n", cc);
128 }
129
130 if (i->why) {
131 _cleanup_free_ char *cc = NULL;
132
133 cc = cescape(i->why);
134 if (!cc) {
135 r = -ENOMEM;
136 goto fail;
137 }
138
139 fprintf(f, "WHY=%s\n", cc);
140 }
141
142 if (i->fifo_path)
143 fprintf(f, "FIFO=%s\n", i->fifo_path);
144
145 r = fflush_and_check(f);
146 if (r < 0)
147 goto fail;
148
149 if (rename(temp_path, i->state_file) < 0) {
150 r = -errno;
151 goto fail;
152 }
153
154 temp_path = mfree(temp_path);
155 return 0;
156
157 fail:
158 (void) unlink(i->state_file);
159
160 return log_error_errno(r, "Failed to save inhibit data %s: %m", i->state_file);
161 }
162
163 static int bus_manager_send_inhibited_change(Inhibitor *i) {
164 const char *property;
165
166 assert(i);
167
168 property = i->mode == INHIBIT_BLOCK ? "BlockInhibited" : "DelayInhibited";
169
170 return manager_send_changed(i->manager, property, NULL);
171 }
172
173 int inhibitor_start(Inhibitor *i) {
174 assert(i);
175
176 if (i->started)
177 return 0;
178
179 dual_timestamp_now(&i->since);
180
181 log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s started.",
182 strna(i->who), strna(i->why),
183 i->pid.pid, i->uid,
184 inhibit_mode_to_string(i->mode));
185
186 i->started = true;
187
188 inhibitor_save(i);
189
190 bus_manager_send_inhibited_change(i);
191
192 return 0;
193 }
194
195 void inhibitor_stop(Inhibitor *i) {
196 assert(i);
197
198 if (i->started)
199 log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s stopped.",
200 strna(i->who), strna(i->why),
201 i->pid.pid, i->uid,
202 inhibit_mode_to_string(i->mode));
203
204 inhibitor_remove_fifo(i);
205
206 if (i->state_file)
207 (void) unlink(i->state_file);
208
209 i->started = false;
210
211 bus_manager_send_inhibited_change(i);
212 }
213
214 int inhibitor_load(Inhibitor *i) {
215 _cleanup_free_ char *what = NULL, *uid = NULL, *pid = NULL, *who = NULL, *why = NULL, *mode = NULL;
216 InhibitWhat w;
217 InhibitMode mm;
218 char *cc;
219 ssize_t l;
220 int r;
221
222 r = parse_env_file(NULL, i->state_file,
223 "WHAT", &what,
224 "UID", &uid,
225 "PID", &pid,
226 "WHO", &who,
227 "WHY", &why,
228 "MODE", &mode,
229 "FIFO", &i->fifo_path);
230 if (r < 0)
231 return log_error_errno(r, "Failed to read %s: %m", i->state_file);
232
233 w = what ? inhibit_what_from_string(what) : 0;
234 if (w >= 0)
235 i->what = w;
236
237 mm = mode ? inhibit_mode_from_string(mode) : INHIBIT_BLOCK;
238 if (mm >= 0)
239 i->mode = mm;
240
241 if (uid) {
242 r = parse_uid(uid, &i->uid);
243 if (r < 0)
244 log_debug_errno(r, "Failed to parse UID of inhibitor: %s", uid);
245 }
246
247 if (pid) {
248 pidref_done(&i->pid);
249 r = pidref_set_pidstr(&i->pid, pid);
250 if (r < 0)
251 log_debug_errno(r, "Failed to parse PID of inhibitor: %s", pid);
252 }
253
254 if (who) {
255 l = cunescape(who, 0, &cc);
256 if (l < 0)
257 return log_debug_errno(l, "Failed to unescape \"who\" of inhibitor: %m");
258
259 free_and_replace(i->who, cc);
260 }
261
262 if (why) {
263 l = cunescape(why, 0, &cc);
264 if (l < 0)
265 return log_debug_errno(l, "Failed to unescape \"why\" of inhibitor: %m");
266
267 free_and_replace(i->why, cc);
268 }
269
270 if (i->fifo_path) {
271 _cleanup_close_ int fd = -EBADF;
272
273 /* Let's reopen the FIFO on both sides, and close the writing side right away */
274 fd = inhibitor_create_fifo(i);
275 if (fd < 0)
276 return log_error_errno(fd, "Failed to reopen FIFO: %m");
277 }
278
279 return 0;
280 }
281
282 static int inhibitor_dispatch_fifo(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
283 Inhibitor *i = ASSERT_PTR(userdata);
284
285 assert(s);
286 assert(fd == i->fifo_fd);
287
288 inhibitor_stop(i);
289 inhibitor_free(i);
290
291 return 0;
292 }
293
294 int inhibitor_create_fifo(Inhibitor *i) {
295 int r;
296
297 assert(i);
298
299 /* Create FIFO */
300 if (!i->fifo_path) {
301 r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE);
302 if (r < 0)
303 return r;
304
305 i->fifo_path = strjoin("/run/systemd/inhibit/", i->id, ".ref");
306 if (!i->fifo_path)
307 return -ENOMEM;
308
309 if (mkfifo(i->fifo_path, 0600) < 0 && errno != EEXIST)
310 return -errno;
311 }
312
313 /* Open reading side */
314 if (i->fifo_fd < 0) {
315 i->fifo_fd = open(i->fifo_path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
316 if (i->fifo_fd < 0)
317 return -errno;
318 }
319
320 if (!i->event_source) {
321 r = sd_event_add_io(i->manager->event, &i->event_source, i->fifo_fd, 0, inhibitor_dispatch_fifo, i);
322 if (r < 0)
323 return r;
324
325 r = sd_event_source_set_priority(i->event_source, SD_EVENT_PRIORITY_IDLE-10);
326 if (r < 0)
327 return r;
328
329 (void) sd_event_source_set_description(i->event_source, "inhibitor-ref");
330 }
331
332 /* Open writing side */
333 return RET_NERRNO(open(i->fifo_path, O_WRONLY|O_CLOEXEC|O_NONBLOCK));
334 }
335
336 static void inhibitor_remove_fifo(Inhibitor *i) {
337 assert(i);
338
339 i->event_source = sd_event_source_unref(i->event_source);
340 i->fifo_fd = safe_close(i->fifo_fd);
341
342 if (i->fifo_path) {
343 (void) unlink(i->fifo_path);
344 i->fifo_path = mfree(i->fifo_path);
345 }
346 }
347
348 bool inhibitor_is_orphan(Inhibitor *i) {
349 assert(i);
350
351 if (!i->started)
352 return true;
353
354 if (!i->fifo_path)
355 return true;
356
357 if (i->fifo_fd < 0)
358 return true;
359
360 if (pipe_eof(i->fifo_fd) != 0)
361 return true;
362
363 return false;
364 }
365
366 InhibitWhat manager_inhibit_what(Manager *m, InhibitMode mm) {
367 Inhibitor *i;
368 InhibitWhat what = 0;
369
370 assert(m);
371
372 HASHMAP_FOREACH(i, m->inhibitors)
373 if (i->mode == mm && i->started)
374 what |= i->what;
375
376 return what;
377 }
378
379 static int pidref_is_active_session(Manager *m, const PidRef *pid) {
380 Session *s;
381 int r;
382
383 assert(m);
384 assert(pid);
385
386 /* Get client session. This is not what you are looking for these days.
387 * FIXME #6852 */
388 r = manager_get_session_by_pidref(m, pid, &s);
389 if (r < 0)
390 return r;
391
392 /* If there's no session assigned to it, then it's globally active on all ttys */
393 if (r == 0)
394 return 1;
395
396 return session_is_active(s);
397 }
398
399 bool manager_is_inhibited(
400 Manager *m,
401 InhibitWhat w,
402 InhibitMode mm,
403 dual_timestamp *since,
404 bool ignore_inactive,
405 bool ignore_uid,
406 uid_t uid,
407 Inhibitor **offending) {
408
409 Inhibitor *i;
410 struct dual_timestamp ts = DUAL_TIMESTAMP_NULL;
411 bool inhibited = false;
412
413 assert(m);
414 assert(w > 0);
415 assert(w < _INHIBIT_WHAT_MAX);
416
417 HASHMAP_FOREACH(i, m->inhibitors) {
418 if (!i->started)
419 continue;
420
421 if (!(i->what & w))
422 continue;
423
424 if (i->mode != mm)
425 continue;
426
427 if (ignore_inactive && pidref_is_active_session(m, &i->pid) <= 0)
428 continue;
429
430 if (ignore_uid && i->uid == uid)
431 continue;
432
433 if (!inhibited ||
434 i->since.monotonic < ts.monotonic)
435 ts = i->since;
436
437 inhibited = true;
438
439 if (offending)
440 *offending = i;
441 }
442
443 if (since)
444 *since = ts;
445
446 return inhibited;
447 }
448
449 const char *inhibit_what_to_string(InhibitWhat w) {
450 static thread_local char buffer[STRLEN(
451 "shutdown:"
452 "sleep:"
453 "idle:"
454 "handle-power-key:"
455 "handle-suspend-key:"
456 "handle-hibernate-key:"
457 "handle-lid-switch:"
458 "handle-reboot-key")+1];
459 char *p;
460
461 if (!inhibit_what_is_valid(w))
462 return NULL;
463
464 p = buffer;
465 if (w & INHIBIT_SHUTDOWN)
466 p = stpcpy(p, "shutdown:");
467 if (w & INHIBIT_SLEEP)
468 p = stpcpy(p, "sleep:");
469 if (w & INHIBIT_IDLE)
470 p = stpcpy(p, "idle:");
471 if (w & INHIBIT_HANDLE_POWER_KEY)
472 p = stpcpy(p, "handle-power-key:");
473 if (w & INHIBIT_HANDLE_SUSPEND_KEY)
474 p = stpcpy(p, "handle-suspend-key:");
475 if (w & INHIBIT_HANDLE_HIBERNATE_KEY)
476 p = stpcpy(p, "handle-hibernate-key:");
477 if (w & INHIBIT_HANDLE_LID_SWITCH)
478 p = stpcpy(p, "handle-lid-switch:");
479 if (w & INHIBIT_HANDLE_REBOOT_KEY)
480 p = stpcpy(p, "handle-reboot-key:");
481
482 if (p > buffer)
483 *(p-1) = 0;
484 else
485 *p = 0;
486
487 return buffer;
488 }
489
490 int inhibit_what_from_string(const char *s) {
491 InhibitWhat what = 0;
492
493 for (const char *p = s;;) {
494 _cleanup_free_ char *word = NULL;
495 int r;
496
497 /* A sanity check that our return values fit in an int */
498 assert_cc((int) _INHIBIT_WHAT_MAX == _INHIBIT_WHAT_MAX);
499
500 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
501 if (r < 0)
502 return r;
503 if (r == 0)
504 return what;
505
506 if (streq(word, "shutdown"))
507 what |= INHIBIT_SHUTDOWN;
508 else if (streq(word, "sleep"))
509 what |= INHIBIT_SLEEP;
510 else if (streq(word, "idle"))
511 what |= INHIBIT_IDLE;
512 else if (streq(word, "handle-power-key"))
513 what |= INHIBIT_HANDLE_POWER_KEY;
514 else if (streq(word, "handle-suspend-key"))
515 what |= INHIBIT_HANDLE_SUSPEND_KEY;
516 else if (streq(word, "handle-hibernate-key"))
517 what |= INHIBIT_HANDLE_HIBERNATE_KEY;
518 else if (streq(word, "handle-lid-switch"))
519 what |= INHIBIT_HANDLE_LID_SWITCH;
520 else if (streq(word, "handle-reboot-key"))
521 what |= INHIBIT_HANDLE_REBOOT_KEY;
522 else
523 return _INHIBIT_WHAT_INVALID;
524 }
525 }
526
527 static const char* const inhibit_mode_table[_INHIBIT_MODE_MAX] = {
528 [INHIBIT_BLOCK] = "block",
529 [INHIBIT_DELAY] = "delay"
530 };
531
532 DEFINE_STRING_TABLE_LOOKUP(inhibit_mode, InhibitMode);