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"
29 static void inhibitor_remove_fifo(Inhibitor
*i
);
31 int inhibitor_new(Inhibitor
**ret
, Manager
*m
, const char* id
) {
32 _cleanup_(inhibitor_freep
) Inhibitor
*i
= NULL
;
39 i
= new(Inhibitor
, 1);
45 .what
= _INHIBIT_WHAT_INVALID
,
46 .mode
= _INHIBIT_MODE_INVALID
,
51 i
->state_file
= path_join("/run/systemd/inhibit", id
);
55 i
->id
= basename(i
->state_file
);
57 r
= hashmap_put(m
->inhibitors
, i
->id
, i
);
65 Inhibitor
* inhibitor_free(Inhibitor
*i
) {
73 sd_event_source_unref(i
->event_source
);
74 safe_close(i
->fifo_fd
);
76 hashmap_remove(i
->manager
->inhibitors
, i
->id
);
78 /* Note that we don't remove neither the state file nor the fifo path here, since we want both to
79 * survive daemon restarts */
86 static int inhibitor_save(Inhibitor
*i
) {
87 _cleanup_free_
char *temp_path
= NULL
;
88 _cleanup_fclose_
FILE *f
= NULL
;
93 r
= mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE
);
97 r
= fopen_temporary(i
->state_file
, &f
, &temp_path
);
101 (void) fchmod(fileno(f
), 0644);
104 "# This is private data. Do not parse.\n"
109 inhibit_what_to_string(i
->what
),
110 inhibit_mode_to_string(i
->mode
),
115 _cleanup_free_
char *cc
= NULL
;
117 cc
= cescape(i
->who
);
123 fprintf(f
, "WHO=%s\n", cc
);
127 _cleanup_free_
char *cc
= NULL
;
129 cc
= cescape(i
->why
);
135 fprintf(f
, "WHY=%s\n", cc
);
139 fprintf(f
, "FIFO=%s\n", i
->fifo_path
);
141 r
= fflush_and_check(f
);
145 if (rename(temp_path
, i
->state_file
) < 0) {
153 (void) unlink(i
->state_file
);
156 (void) unlink(temp_path
);
158 return log_error_errno(r
, "Failed to save inhibit data %s: %m", i
->state_file
);
161 static int bus_manager_send_inhibited_change(Inhibitor
*i
) {
162 const char *property
;
166 property
= i
->mode
== INHIBIT_BLOCK
? "BlockInhibited" : "DelayInhibited";
168 return manager_send_changed(i
->manager
, property
, NULL
);
171 int inhibitor_start(Inhibitor
*i
) {
177 dual_timestamp_get(&i
->since
);
179 log_debug("Inhibitor %s (%s) pid="PID_FMT
" uid="UID_FMT
" mode=%s started.",
180 strna(i
->who
), strna(i
->why
),
182 inhibit_mode_to_string(i
->mode
));
188 bus_manager_send_inhibited_change(i
);
193 void inhibitor_stop(Inhibitor
*i
) {
197 log_debug("Inhibitor %s (%s) pid="PID_FMT
" uid="UID_FMT
" mode=%s stopped.",
198 strna(i
->who
), strna(i
->why
),
200 inhibit_mode_to_string(i
->mode
));
202 inhibitor_remove_fifo(i
);
205 (void) unlink(i
->state_file
);
209 bus_manager_send_inhibited_change(i
);
212 int inhibitor_load(Inhibitor
*i
) {
213 _cleanup_free_
char *what
= NULL
, *uid
= NULL
, *pid
= NULL
, *who
= NULL
, *why
= NULL
, *mode
= NULL
;
220 r
= parse_env_file(NULL
, i
->state_file
,
227 "FIFO", &i
->fifo_path
);
229 return log_error_errno(r
, "Failed to read %s: %m", i
->state_file
);
231 w
= what
? inhibit_what_from_string(what
) : 0;
235 mm
= mode
? inhibit_mode_from_string(mode
) : INHIBIT_BLOCK
;
240 r
= parse_uid(uid
, &i
->uid
);
242 log_debug_errno(r
, "Failed to parse UID of inhibitor: %s", uid
);
246 r
= parse_pid(pid
, &i
->pid
);
248 log_debug_errno(r
, "Failed to parse PID of inhibitor: %s", pid
);
252 l
= cunescape(who
, 0, &cc
);
254 return log_debug_errno(l
, "Failed to unescape \"who\" of inhibitor: %m");
256 free_and_replace(i
->who
, cc
);
260 l
= cunescape(why
, 0, &cc
);
262 return log_debug_errno(l
, "Failed to unescape \"why\" of inhibitor: %m");
264 free_and_replace(i
->why
, cc
);
268 _cleanup_close_
int fd
= -1;
270 /* Let's re-open the FIFO on both sides, and close the writing side right away */
271 fd
= inhibitor_create_fifo(i
);
273 return log_error_errno(fd
, "Failed to reopen FIFO: %m");
279 static int inhibitor_dispatch_fifo(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
280 Inhibitor
*i
= userdata
;
283 assert(fd
== i
->fifo_fd
);
292 int inhibitor_create_fifo(Inhibitor
*i
) {
299 r
= mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE
);
303 i
->fifo_path
= strjoin("/run/systemd/inhibit/", i
->id
, ".ref");
307 if (mkfifo(i
->fifo_path
, 0600) < 0 && errno
!= EEXIST
)
311 /* Open reading side */
312 if (i
->fifo_fd
< 0) {
313 i
->fifo_fd
= open(i
->fifo_path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
318 if (!i
->event_source
) {
319 r
= sd_event_add_io(i
->manager
->event
, &i
->event_source
, i
->fifo_fd
, 0, inhibitor_dispatch_fifo
, i
);
323 r
= sd_event_source_set_priority(i
->event_source
, SD_EVENT_PRIORITY_IDLE
-10);
327 (void) sd_event_source_set_description(i
->event_source
, "inhibitor-ref");
330 /* Open writing side */
331 return RET_NERRNO(open(i
->fifo_path
, O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
));
334 static void inhibitor_remove_fifo(Inhibitor
*i
) {
337 i
->event_source
= sd_event_source_unref(i
->event_source
);
338 i
->fifo_fd
= safe_close(i
->fifo_fd
);
341 (void) unlink(i
->fifo_path
);
342 i
->fifo_path
= mfree(i
->fifo_path
);
346 bool inhibitor_is_orphan(Inhibitor
*i
) {
358 if (pipe_eof(i
->fifo_fd
) != 0)
364 InhibitWhat
manager_inhibit_what(Manager
*m
, InhibitMode mm
) {
366 InhibitWhat what
= 0;
370 HASHMAP_FOREACH(i
, m
->inhibitors
)
371 if (i
->mode
== mm
&& i
->started
)
377 static int pid_is_active(Manager
*m
, pid_t pid
) {
381 /* Get client session. This is not what you are looking for these days.
383 r
= manager_get_session_by_pid(m
, pid
, &s
);
387 /* If there's no session assigned to it, then it's globally
388 * active on all ttys */
392 return session_is_active(s
);
395 bool manager_is_inhibited(
399 dual_timestamp
*since
,
400 bool ignore_inactive
,
403 Inhibitor
**offending
) {
406 struct dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
407 bool inhibited
= false;
410 assert(w
> 0 && w
< _INHIBIT_WHAT_MAX
);
412 HASHMAP_FOREACH(i
, m
->inhibitors
) {
422 if (ignore_inactive
&& pid_is_active(m
, i
->pid
) <= 0)
425 if (ignore_uid
&& i
->uid
== uid
)
429 i
->since
.monotonic
< ts
.monotonic
)
444 const char *inhibit_what_to_string(InhibitWhat w
) {
445 static thread_local
char buffer
[STRLEN(
450 "handle-suspend-key:"
451 "handle-hibernate-key:"
453 "handle-reboot-key")+1];
456 if (w
< 0 || w
>= _INHIBIT_WHAT_MAX
)
460 if (w
& INHIBIT_SHUTDOWN
)
461 p
= stpcpy(p
, "shutdown:");
462 if (w
& INHIBIT_SLEEP
)
463 p
= stpcpy(p
, "sleep:");
464 if (w
& INHIBIT_IDLE
)
465 p
= stpcpy(p
, "idle:");
466 if (w
& INHIBIT_HANDLE_POWER_KEY
)
467 p
= stpcpy(p
, "handle-power-key:");
468 if (w
& INHIBIT_HANDLE_SUSPEND_KEY
)
469 p
= stpcpy(p
, "handle-suspend-key:");
470 if (w
& INHIBIT_HANDLE_HIBERNATE_KEY
)
471 p
= stpcpy(p
, "handle-hibernate-key:");
472 if (w
& INHIBIT_HANDLE_LID_SWITCH
)
473 p
= stpcpy(p
, "handle-lid-switch:");
474 if (w
& INHIBIT_HANDLE_REBOOT_KEY
)
475 p
= stpcpy(p
, "handle-reboot-key:");
485 int inhibit_what_from_string(const char *s
) {
486 InhibitWhat what
= 0;
488 for (const char *p
= s
;;) {
489 _cleanup_free_
char *word
= NULL
;
492 /* A sanity check that our return values fit in an int */
493 assert_cc((int) _INHIBIT_WHAT_MAX
== _INHIBIT_WHAT_MAX
);
495 r
= extract_first_word(&p
, &word
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
501 if (streq(word
, "shutdown"))
502 what
|= INHIBIT_SHUTDOWN
;
503 else if (streq(word
, "sleep"))
504 what
|= INHIBIT_SLEEP
;
505 else if (streq(word
, "idle"))
506 what
|= INHIBIT_IDLE
;
507 else if (streq(word
, "handle-power-key"))
508 what
|= INHIBIT_HANDLE_POWER_KEY
;
509 else if (streq(word
, "handle-suspend-key"))
510 what
|= INHIBIT_HANDLE_SUSPEND_KEY
;
511 else if (streq(word
, "handle-hibernate-key"))
512 what
|= INHIBIT_HANDLE_HIBERNATE_KEY
;
513 else if (streq(word
, "handle-lid-switch"))
514 what
|= INHIBIT_HANDLE_LID_SWITCH
;
515 else if (streq(word
, "handle-reboot-key"))
516 what
|= INHIBIT_HANDLE_REBOOT_KEY
;
518 return _INHIBIT_WHAT_INVALID
;
522 static const char* const inhibit_mode_table
[_INHIBIT_MODE_MAX
] = {
523 [INHIBIT_BLOCK
] = "block",
524 [INHIBIT_DELAY
] = "delay"
527 DEFINE_STRING_TABLE_LOOKUP(inhibit_mode
, InhibitMode
);