1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
12 #include "format-util.h"
13 #include "logind-inhibit.h"
15 #include "parse-util.h"
16 #include "string-table.h"
17 #include "string-util.h"
18 #include "user-util.h"
21 Inhibitor* inhibitor_new(Manager *m, const char* id) {
26 i = new0(Inhibitor, 1);
30 i->state_file = strappend("/run/systemd/inhibit/", id);
34 i->id = basename(i->state_file);
36 if (hashmap_put(m->inhibitors, i->id, i) < 0) {
47 void inhibitor_free(Inhibitor *i) {
50 hashmap_remove(i->manager->inhibitors, i->id);
52 inhibitor_remove_fifo(i);
58 unlink(i->state_file);
65 int inhibitor_save(Inhibitor *i) {
66 _cleanup_free_ char *temp_path = NULL;
67 _cleanup_fclose_ FILE *f = NULL;
72 r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE);
76 r = fopen_temporary(i->state_file, &f, &temp_path);
80 fchmod(fileno(f), 0644);
83 "# This is private data. Do not parse.\n"
88 inhibit_what_to_string(i->what),
89 inhibit_mode_to_string(i->mode),
94 _cleanup_free_ char *cc = NULL;
102 fprintf(f, "WHO=%s\n", cc);
106 _cleanup_free_ char *cc = NULL;
108 cc = cescape(i->why);
114 fprintf(f, "WHY=%s\n", cc);
118 fprintf(f, "FIFO=%s\n", i->fifo_path);
120 r = fflush_and_check(f);
124 if (rename(temp_path, i->state_file) < 0) {
132 (void) unlink(i->state_file);
135 (void) unlink(temp_path);
137 return log_error_errno(r, "Failed to save inhibit data %s: %m", i->state_file);
140 int inhibitor_start(Inhibitor *i) {
146 dual_timestamp_get(&i->since);
148 log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s started.",
149 strna(i->who), strna(i->why),
151 inhibit_mode_to_string(i->mode));
157 manager_send_changed(i->manager, i->mode == INHIBIT_BLOCK ? "BlockInhibited" : "DelayInhibited", NULL);
162 int inhibitor_stop(Inhibitor *i) {
166 log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s stopped.",
167 strna(i->who), strna(i->why),
169 inhibit_mode_to_string(i->mode));
172 unlink(i->state_file);
176 manager_send_changed(i->manager, i->mode == INHIBIT_BLOCK ? "BlockInhibited" : "DelayInhibited", NULL);
181 int inhibitor_load(Inhibitor *i) {
196 r = parse_env_file(NULL, i->state_file, NEWLINE,
203 "FIFO", &i->fifo_path,
208 w = what ? inhibit_what_from_string(what) : 0;
212 mm = mode ? inhibit_mode_from_string(mode) : INHIBIT_BLOCK;
217 r = parse_uid(uid, &i->uid);
223 r = parse_pid(pid, &i->pid);
229 r = cunescape(who, 0, &cc);
238 r = cunescape(why, 0, &cc);
249 fd = inhibitor_create_fifo(i);
256 static int inhibitor_dispatch_fifo(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
257 Inhibitor *i = userdata;
260 assert(fd == i->fifo_fd);
269 int inhibitor_create_fifo(Inhibitor *i) {
276 r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE);
280 i->fifo_path = strjoin("/run/systemd/inhibit/", i->id, ".ref");
284 if (mkfifo(i->fifo_path, 0600) < 0 && errno != EEXIST)
288 /* Open reading side */
289 if (i->fifo_fd < 0) {
290 i->fifo_fd = open(i->fifo_path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
295 if (!i->event_source) {
296 r = sd_event_add_io(i->manager->event, &i->event_source, i->fifo_fd, 0, inhibitor_dispatch_fifo, i);
300 r = sd_event_source_set_priority(i->event_source, SD_EVENT_PRIORITY_IDLE-10);
305 /* Open writing side */
306 r = open(i->fifo_path, O_WRONLY|O_CLOEXEC|O_NONBLOCK);
313 void inhibitor_remove_fifo(Inhibitor *i) {
316 i->event_source = sd_event_source_unref(i->event_source);
317 i->fifo_fd = safe_close(i->fifo_fd);
320 unlink(i->fifo_path);
321 i->fifo_path = mfree(i->fifo_path);
325 InhibitWhat manager_inhibit_what(Manager *m, InhibitMode mm) {
328 InhibitWhat what = 0;
332 HASHMAP_FOREACH(i, m->inhibitors, j)
333 if (i->mode == mm && i->started)
339 static int pid_is_active(Manager *m, pid_t pid) {
343 /* Get client session. This is not what you are looking for these days.
345 r = manager_get_session_by_pid(m, pid, &s);
349 /* If there's no session assigned to it, then it's globally
350 * active on all ttys */
354 return session_is_active(s);
357 bool manager_is_inhibited(
361 dual_timestamp *since,
362 bool ignore_inactive,
365 Inhibitor **offending) {
369 struct dual_timestamp ts = DUAL_TIMESTAMP_NULL;
370 bool inhibited = false;
373 assert(w > 0 && w < _INHIBIT_WHAT_MAX);
375 HASHMAP_FOREACH(i, m->inhibitors, j) {
385 if (ignore_inactive && pid_is_active(m, i->pid) <= 0)
388 if (ignore_uid && i->uid == uid)
392 i->since.monotonic < ts.monotonic)
407 const char *inhibit_what_to_string(InhibitWhat w) {
408 static thread_local char buffer[97];
411 if (w < 0 || w >= _INHIBIT_WHAT_MAX)
415 if (w & INHIBIT_SHUTDOWN)
416 p = stpcpy(p, "shutdown:");
417 if (w & INHIBIT_SLEEP)
418 p = stpcpy(p, "sleep:");
419 if (w & INHIBIT_IDLE)
420 p = stpcpy(p, "idle:");
421 if (w & INHIBIT_HANDLE_POWER_KEY)
422 p = stpcpy(p, "handle-power-key:");
423 if (w & INHIBIT_HANDLE_SUSPEND_KEY)
424 p = stpcpy(p, "handle-suspend-key:");
425 if (w & INHIBIT_HANDLE_HIBERNATE_KEY)
426 p = stpcpy(p, "handle-hibernate-key:");
427 if (w & INHIBIT_HANDLE_LID_SWITCH)
428 p = stpcpy(p, "handle-lid-switch:");
438 InhibitWhat inhibit_what_from_string(const char *s) {
439 InhibitWhat what = 0;
440 const char *word, *state;
443 FOREACH_WORD_SEPARATOR(word, l, s, ":", state) {
444 if (l == 8 && strneq(word, "shutdown", l))
445 what |= INHIBIT_SHUTDOWN;
446 else if (l == 5 && strneq(word, "sleep", l))
447 what |= INHIBIT_SLEEP;
448 else if (l == 4 && strneq(word, "idle", l))
449 what |= INHIBIT_IDLE;
450 else if (l == 16 && strneq(word, "handle-power-key", l))
451 what |= INHIBIT_HANDLE_POWER_KEY;
452 else if (l == 18 && strneq(word, "handle-suspend-key", l))
453 what |= INHIBIT_HANDLE_SUSPEND_KEY;
454 else if (l == 20 && strneq(word, "handle-hibernate-key", l))
455 what |= INHIBIT_HANDLE_HIBERNATE_KEY;
456 else if (l == 17 && strneq(word, "handle-lid-switch", l))
457 what |= INHIBIT_HANDLE_LID_SWITCH;
459 return _INHIBIT_WHAT_INVALID;
465 static const char* const inhibit_mode_table[_INHIBIT_MODE_MAX] = {
466 [INHIBIT_BLOCK] = "block",
467 [INHIBIT_DELAY] = "delay"
470 DEFINE_STRING_TABLE_LOOKUP(inhibit_mode, InhibitMode);