1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "alloc-util.h"
11 #include "errno-list.h"
12 #include "errno-util.h"
16 #include "format-util.h"
18 #include "logind-dbus.h"
19 #include "logind-inhibit.h"
20 #include "mkdir-label.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "string-table.h"
24 #include "string-util.h"
25 #include "tmpfile-util.h"
26 #include "user-util.h"
28 static void inhibitor_remove_fifo(Inhibitor
*i
);
30 int inhibitor_new(Inhibitor
**ret
, Manager
*m
, const char* id
) {
31 _cleanup_(inhibitor_freep
) Inhibitor
*i
= NULL
;
38 i
= new(Inhibitor
, 1);
44 .what
= _INHIBIT_WHAT_INVALID
,
45 .mode
= _INHIBIT_MODE_INVALID
,
50 i
->state_file
= path_join("/run/systemd/inhibit", id
);
54 i
->id
= basename(i
->state_file
);
56 r
= hashmap_put(m
->inhibitors
, i
->id
, i
);
64 Inhibitor
* inhibitor_free(Inhibitor
*i
) {
72 sd_event_source_unref(i
->event_source
);
73 safe_close(i
->fifo_fd
);
75 hashmap_remove(i
->manager
->inhibitors
, i
->id
);
77 /* Note that we don't remove neither the state file nor the fifo path here, since we want both to
78 * survive daemon restarts */
85 static int inhibitor_save(Inhibitor
*i
) {
86 _cleanup_free_
char *temp_path
= NULL
;
87 _cleanup_fclose_
FILE *f
= NULL
;
92 r
= mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE
);
96 r
= fopen_temporary(i
->state_file
, &f
, &temp_path
);
100 (void) fchmod(fileno(f
), 0644);
103 "# This is private data. Do not parse.\n"
108 inhibit_what_to_string(i
->what
),
109 inhibit_mode_to_string(i
->mode
),
114 _cleanup_free_
char *cc
= NULL
;
116 cc
= cescape(i
->who
);
122 fprintf(f
, "WHO=%s\n", cc
);
126 _cleanup_free_
char *cc
= NULL
;
128 cc
= cescape(i
->why
);
134 fprintf(f
, "WHY=%s\n", cc
);
138 fprintf(f
, "FIFO=%s\n", i
->fifo_path
);
140 r
= fflush_and_check(f
);
144 if (rename(temp_path
, i
->state_file
) < 0) {
152 (void) unlink(i
->state_file
);
155 (void) unlink(temp_path
);
157 return log_error_errno(r
, "Failed to save inhibit data %s: %m", i
->state_file
);
160 static int bus_manager_send_inhibited_change(Inhibitor
*i
) {
161 const char *property
;
165 property
= i
->mode
== INHIBIT_BLOCK
? "BlockInhibited" : "DelayInhibited";
167 return manager_send_changed(i
->manager
, property
, NULL
);
170 int inhibitor_start(Inhibitor
*i
) {
176 dual_timestamp_get(&i
->since
);
178 log_debug("Inhibitor %s (%s) pid="PID_FMT
" uid="UID_FMT
" mode=%s started.",
179 strna(i
->who
), strna(i
->why
),
181 inhibit_mode_to_string(i
->mode
));
187 bus_manager_send_inhibited_change(i
);
192 void inhibitor_stop(Inhibitor
*i
) {
196 log_debug("Inhibitor %s (%s) pid="PID_FMT
" uid="UID_FMT
" mode=%s stopped.",
197 strna(i
->who
), strna(i
->why
),
199 inhibit_mode_to_string(i
->mode
));
201 inhibitor_remove_fifo(i
);
204 (void) unlink(i
->state_file
);
208 bus_manager_send_inhibited_change(i
);
211 int inhibitor_load(Inhibitor
*i
) {
212 _cleanup_free_
char *what
= NULL
, *uid
= NULL
, *pid
= NULL
, *who
= NULL
, *why
= NULL
, *mode
= NULL
;
219 r
= parse_env_file(NULL
, i
->state_file
,
226 "FIFO", &i
->fifo_path
);
228 return log_error_errno(r
, "Failed to read %s: %m", i
->state_file
);
230 w
= what
? inhibit_what_from_string(what
) : 0;
234 mm
= mode
? inhibit_mode_from_string(mode
) : INHIBIT_BLOCK
;
239 r
= parse_uid(uid
, &i
->uid
);
241 log_debug_errno(r
, "Failed to parse UID of inhibitor: %s", uid
);
245 r
= parse_pid(pid
, &i
->pid
);
247 log_debug_errno(r
, "Failed to parse PID of inhibitor: %s", pid
);
251 l
= cunescape(who
, 0, &cc
);
253 return log_debug_errno(l
, "Failed to unescape \"who\" of inhibitor: %m");
255 free_and_replace(i
->who
, cc
);
259 l
= cunescape(why
, 0, &cc
);
261 return log_debug_errno(l
, "Failed to unescape \"why\" of inhibitor: %m");
263 free_and_replace(i
->why
, cc
);
267 _cleanup_close_
int fd
= -EBADF
;
269 /* Let's re-open the FIFO on both sides, and close the writing side right away */
270 fd
= inhibitor_create_fifo(i
);
272 return log_error_errno(fd
, "Failed to reopen FIFO: %m");
278 static int inhibitor_dispatch_fifo(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
279 Inhibitor
*i
= ASSERT_PTR(userdata
);
282 assert(fd
== i
->fifo_fd
);
290 int inhibitor_create_fifo(Inhibitor
*i
) {
297 r
= mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE
);
301 i
->fifo_path
= strjoin("/run/systemd/inhibit/", i
->id
, ".ref");
305 if (mkfifo(i
->fifo_path
, 0600) < 0 && errno
!= EEXIST
)
309 /* Open reading side */
310 if (i
->fifo_fd
< 0) {
311 i
->fifo_fd
= open(i
->fifo_path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
316 if (!i
->event_source
) {
317 r
= sd_event_add_io(i
->manager
->event
, &i
->event_source
, i
->fifo_fd
, 0, inhibitor_dispatch_fifo
, i
);
321 r
= sd_event_source_set_priority(i
->event_source
, SD_EVENT_PRIORITY_IDLE
-10);
325 (void) sd_event_source_set_description(i
->event_source
, "inhibitor-ref");
328 /* Open writing side */
329 return RET_NERRNO(open(i
->fifo_path
, O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
));
332 static void inhibitor_remove_fifo(Inhibitor
*i
) {
335 i
->event_source
= sd_event_source_unref(i
->event_source
);
336 i
->fifo_fd
= safe_close(i
->fifo_fd
);
339 (void) unlink(i
->fifo_path
);
340 i
->fifo_path
= mfree(i
->fifo_path
);
344 bool inhibitor_is_orphan(Inhibitor
*i
) {
356 if (pipe_eof(i
->fifo_fd
) != 0)
362 InhibitWhat
manager_inhibit_what(Manager
*m
, InhibitMode mm
) {
364 InhibitWhat what
= 0;
368 HASHMAP_FOREACH(i
, m
->inhibitors
)
369 if (i
->mode
== mm
&& i
->started
)
375 static int pid_is_active(Manager
*m
, pid_t pid
) {
379 /* Get client session. This is not what you are looking for these days.
381 r
= manager_get_session_by_pid(m
, pid
, &s
);
385 /* If there's no session assigned to it, then it's globally
386 * active on all ttys */
390 return session_is_active(s
);
393 bool manager_is_inhibited(
397 dual_timestamp
*since
,
398 bool ignore_inactive
,
401 Inhibitor
**offending
) {
404 struct dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
405 bool inhibited
= false;
408 assert(w
> 0 && w
< _INHIBIT_WHAT_MAX
);
410 HASHMAP_FOREACH(i
, m
->inhibitors
) {
420 if (ignore_inactive
&& pid_is_active(m
, i
->pid
) <= 0)
423 if (ignore_uid
&& i
->uid
== uid
)
427 i
->since
.monotonic
< ts
.monotonic
)
442 const char *inhibit_what_to_string(InhibitWhat w
) {
443 static thread_local
char buffer
[STRLEN(
448 "handle-suspend-key:"
449 "handle-hibernate-key:"
451 "handle-reboot-key")+1];
454 if (w
< 0 || w
>= _INHIBIT_WHAT_MAX
)
458 if (w
& INHIBIT_SHUTDOWN
)
459 p
= stpcpy(p
, "shutdown:");
460 if (w
& INHIBIT_SLEEP
)
461 p
= stpcpy(p
, "sleep:");
462 if (w
& INHIBIT_IDLE
)
463 p
= stpcpy(p
, "idle:");
464 if (w
& INHIBIT_HANDLE_POWER_KEY
)
465 p
= stpcpy(p
, "handle-power-key:");
466 if (w
& INHIBIT_HANDLE_SUSPEND_KEY
)
467 p
= stpcpy(p
, "handle-suspend-key:");
468 if (w
& INHIBIT_HANDLE_HIBERNATE_KEY
)
469 p
= stpcpy(p
, "handle-hibernate-key:");
470 if (w
& INHIBIT_HANDLE_LID_SWITCH
)
471 p
= stpcpy(p
, "handle-lid-switch:");
472 if (w
& INHIBIT_HANDLE_REBOOT_KEY
)
473 p
= stpcpy(p
, "handle-reboot-key:");
483 int inhibit_what_from_string(const char *s
) {
484 InhibitWhat what
= 0;
486 for (const char *p
= s
;;) {
487 _cleanup_free_
char *word
= NULL
;
490 /* A sanity check that our return values fit in an int */
491 assert_cc((int) _INHIBIT_WHAT_MAX
== _INHIBIT_WHAT_MAX
);
493 r
= extract_first_word(&p
, &word
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
499 if (streq(word
, "shutdown"))
500 what
|= INHIBIT_SHUTDOWN
;
501 else if (streq(word
, "sleep"))
502 what
|= INHIBIT_SLEEP
;
503 else if (streq(word
, "idle"))
504 what
|= INHIBIT_IDLE
;
505 else if (streq(word
, "handle-power-key"))
506 what
|= INHIBIT_HANDLE_POWER_KEY
;
507 else if (streq(word
, "handle-suspend-key"))
508 what
|= INHIBIT_HANDLE_SUSPEND_KEY
;
509 else if (streq(word
, "handle-hibernate-key"))
510 what
|= INHIBIT_HANDLE_HIBERNATE_KEY
;
511 else if (streq(word
, "handle-lid-switch"))
512 what
|= INHIBIT_HANDLE_LID_SWITCH
;
513 else if (streq(word
, "handle-reboot-key"))
514 what
|= INHIBIT_HANDLE_REBOOT_KEY
;
516 return _INHIBIT_WHAT_INVALID
;
520 static const char* const inhibit_mode_table
[_INHIBIT_MODE_MAX
] = {
521 [INHIBIT_BLOCK
] = "block",
522 [INHIBIT_DELAY
] = "delay"
525 DEFINE_STRING_TABLE_LOOKUP(inhibit_mode
, InhibitMode
);