]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-inhibit.c
logind: also port session leader tracking over to PidRef
[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(Inhibitor **ret, Manager *m, const char* id) {
33 _cleanup_(inhibitor_freep) Inhibitor *i = NULL;
34 int r;
35
36 assert(ret);
37 assert(m);
38 assert(id);
39
40 i = new(Inhibitor, 1);
41 if (!i)
42 return -ENOMEM;
43
44 *i = (Inhibitor) {
45 .manager = m,
46 .what = _INHIBIT_WHAT_INVALID,
47 .mode = _INHIBIT_MODE_INVALID,
48 .uid = UID_INVALID,
49 .fifo_fd = -EBADF,
50 .pid = PIDREF_NULL,
51 };
52
53 i->state_file = path_join("/run/systemd/inhibit", id);
54 if (!i->state_file)
55 return -ENOMEM;
56
57 i->id = basename(i->state_file);
58
59 r = hashmap_put(m->inhibitors, i->id, i);
60 if (r < 0)
61 return r;
62
63 *ret = TAKE_PTR(i);
64 return 0;
65 }
66
67 Inhibitor* inhibitor_free(Inhibitor *i) {
68
69 if (!i)
70 return NULL;
71
72 free(i->who);
73 free(i->why);
74
75 sd_event_source_unref(i->event_source);
76 safe_close(i->fifo_fd);
77
78 hashmap_remove(i->manager->inhibitors, i->id);
79
80 /* Note that we don't remove neither the state file nor the fifo path here, since we want both to
81 * survive daemon restarts */
82 free(i->state_file);
83 free(i->fifo_path);
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_get(&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 re-open 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 pid_is_active(Manager *m, pid_t pid) {
380 Session *s;
381 int r;
382
383 /* Get client session. This is not what you are looking for these days.
384 * FIXME #6852 */
385 r = manager_get_session_by_pid(m, pid, &s);
386 if (r < 0)
387 return r;
388
389 /* If there's no session assigned to it, then it's globally
390 * active on all ttys */
391 if (r == 0)
392 return 1;
393
394 return session_is_active(s);
395 }
396
397 bool manager_is_inhibited(
398 Manager *m,
399 InhibitWhat w,
400 InhibitMode mm,
401 dual_timestamp *since,
402 bool ignore_inactive,
403 bool ignore_uid,
404 uid_t uid,
405 Inhibitor **offending) {
406
407 Inhibitor *i;
408 struct dual_timestamp ts = DUAL_TIMESTAMP_NULL;
409 bool inhibited = false;
410
411 assert(m);
412 assert(w > 0 && w < _INHIBIT_WHAT_MAX);
413
414 HASHMAP_FOREACH(i, m->inhibitors) {
415 if (!i->started)
416 continue;
417
418 if (!(i->what & w))
419 continue;
420
421 if (i->mode != mm)
422 continue;
423
424 if (ignore_inactive && pid_is_active(m, i->pid.pid) <= 0)
425 continue;
426
427 if (ignore_uid && i->uid == uid)
428 continue;
429
430 if (!inhibited ||
431 i->since.monotonic < ts.monotonic)
432 ts = i->since;
433
434 inhibited = true;
435
436 if (offending)
437 *offending = i;
438 }
439
440 if (since)
441 *since = ts;
442
443 return inhibited;
444 }
445
446 const char *inhibit_what_to_string(InhibitWhat w) {
447 static thread_local char buffer[STRLEN(
448 "shutdown:"
449 "sleep:"
450 "idle:"
451 "handle-power-key:"
452 "handle-suspend-key:"
453 "handle-hibernate-key:"
454 "handle-lid-switch:"
455 "handle-reboot-key")+1];
456 char *p;
457
458 if (w < 0 || w >= _INHIBIT_WHAT_MAX)
459 return NULL;
460
461 p = buffer;
462 if (w & INHIBIT_SHUTDOWN)
463 p = stpcpy(p, "shutdown:");
464 if (w & INHIBIT_SLEEP)
465 p = stpcpy(p, "sleep:");
466 if (w & INHIBIT_IDLE)
467 p = stpcpy(p, "idle:");
468 if (w & INHIBIT_HANDLE_POWER_KEY)
469 p = stpcpy(p, "handle-power-key:");
470 if (w & INHIBIT_HANDLE_SUSPEND_KEY)
471 p = stpcpy(p, "handle-suspend-key:");
472 if (w & INHIBIT_HANDLE_HIBERNATE_KEY)
473 p = stpcpy(p, "handle-hibernate-key:");
474 if (w & INHIBIT_HANDLE_LID_SWITCH)
475 p = stpcpy(p, "handle-lid-switch:");
476 if (w & INHIBIT_HANDLE_REBOOT_KEY)
477 p = stpcpy(p, "handle-reboot-key:");
478
479 if (p > buffer)
480 *(p-1) = 0;
481 else
482 *p = 0;
483
484 return buffer;
485 }
486
487 int inhibit_what_from_string(const char *s) {
488 InhibitWhat what = 0;
489
490 for (const char *p = s;;) {
491 _cleanup_free_ char *word = NULL;
492 int r;
493
494 /* A sanity check that our return values fit in an int */
495 assert_cc((int) _INHIBIT_WHAT_MAX == _INHIBIT_WHAT_MAX);
496
497 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
498 if (r < 0)
499 return r;
500 if (r == 0)
501 return what;
502
503 if (streq(word, "shutdown"))
504 what |= INHIBIT_SHUTDOWN;
505 else if (streq(word, "sleep"))
506 what |= INHIBIT_SLEEP;
507 else if (streq(word, "idle"))
508 what |= INHIBIT_IDLE;
509 else if (streq(word, "handle-power-key"))
510 what |= INHIBIT_HANDLE_POWER_KEY;
511 else if (streq(word, "handle-suspend-key"))
512 what |= INHIBIT_HANDLE_SUSPEND_KEY;
513 else if (streq(word, "handle-hibernate-key"))
514 what |= INHIBIT_HANDLE_HIBERNATE_KEY;
515 else if (streq(word, "handle-lid-switch"))
516 what |= INHIBIT_HANDLE_LID_SWITCH;
517 else if (streq(word, "handle-reboot-key"))
518 what |= INHIBIT_HANDLE_REBOOT_KEY;
519 else
520 return _INHIBIT_WHAT_INVALID;
521 }
522 }
523
524 static const char* const inhibit_mode_table[_INHIBIT_MODE_MAX] = {
525 [INHIBIT_BLOCK] = "block",
526 [INHIBIT_DELAY] = "delay"
527 };
528
529 DEFINE_STRING_TABLE_LOOKUP(inhibit_mode, InhibitMode);