1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
15 #include "format-util.h"
16 #include "logind-dbus.h"
17 #include "logind-inhibit.h"
19 #include "parse-util.h"
20 #include "string-table.h"
21 #include "string-util.h"
22 #include "tmpfile-util.h"
23 #include "user-util.h"
26 Inhibitor
* inhibitor_new(Manager
*m
, const char* id
) {
31 i
= new0(Inhibitor
, 1);
35 i
->state_file
= strappend("/run/systemd/inhibit/", id
);
39 i
->id
= basename(i
->state_file
);
41 if (hashmap_put(m
->inhibitors
, i
->id
, i
) < 0) {
52 void inhibitor_free(Inhibitor
*i
) {
55 hashmap_remove(i
->manager
->inhibitors
, i
->id
);
57 inhibitor_remove_fifo(i
);
63 (void) unlink(i
->state_file
);
70 int inhibitor_save(Inhibitor
*i
) {
71 _cleanup_free_
char *temp_path
= NULL
;
72 _cleanup_fclose_
FILE *f
= NULL
;
77 r
= mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE
);
81 r
= fopen_temporary(i
->state_file
, &f
, &temp_path
);
85 fchmod(fileno(f
), 0644);
88 "# This is private data. Do not parse.\n"
93 inhibit_what_to_string(i
->what
),
94 inhibit_mode_to_string(i
->mode
),
99 _cleanup_free_
char *cc
= NULL
;
101 cc
= cescape(i
->who
);
107 fprintf(f
, "WHO=%s\n", cc
);
111 _cleanup_free_
char *cc
= NULL
;
113 cc
= cescape(i
->why
);
119 fprintf(f
, "WHY=%s\n", cc
);
123 fprintf(f
, "FIFO=%s\n", i
->fifo_path
);
125 r
= fflush_and_check(f
);
129 if (rename(temp_path
, i
->state_file
) < 0) {
137 (void) unlink(i
->state_file
);
140 (void) unlink(temp_path
);
142 return log_error_errno(r
, "Failed to save inhibit data %s: %m", i
->state_file
);
145 int inhibitor_start(Inhibitor
*i
) {
151 dual_timestamp_get(&i
->since
);
153 log_debug("Inhibitor %s (%s) pid="PID_FMT
" uid="UID_FMT
" mode=%s started.",
154 strna(i
->who
), strna(i
->why
),
156 inhibit_mode_to_string(i
->mode
));
162 manager_send_changed(i
->manager
, i
->mode
== INHIBIT_BLOCK
? "BlockInhibited" : "DelayInhibited", NULL
);
167 int inhibitor_stop(Inhibitor
*i
) {
171 log_debug("Inhibitor %s (%s) pid="PID_FMT
" uid="UID_FMT
" mode=%s stopped.",
172 strna(i
->who
), strna(i
->why
),
174 inhibit_mode_to_string(i
->mode
));
177 (void) unlink(i
->state_file
);
181 manager_send_changed(i
->manager
, i
->mode
== INHIBIT_BLOCK
? "BlockInhibited" : "DelayInhibited", NULL
);
186 int inhibitor_load(Inhibitor
*i
) {
201 r
= parse_env_file(NULL
, i
->state_file
,
208 "FIFO", &i
->fifo_path
);
212 w
= what
? inhibit_what_from_string(what
) : 0;
216 mm
= mode
? inhibit_mode_from_string(mode
) : INHIBIT_BLOCK
;
221 r
= parse_uid(uid
, &i
->uid
);
227 r
= parse_pid(pid
, &i
->pid
);
233 r
= cunescape(who
, 0, &cc
);
242 r
= cunescape(why
, 0, &cc
);
253 fd
= inhibitor_create_fifo(i
);
260 static int inhibitor_dispatch_fifo(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
261 Inhibitor
*i
= userdata
;
264 assert(fd
== i
->fifo_fd
);
273 int inhibitor_create_fifo(Inhibitor
*i
) {
280 r
= mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE
);
284 i
->fifo_path
= strjoin("/run/systemd/inhibit/", i
->id
, ".ref");
288 if (mkfifo(i
->fifo_path
, 0600) < 0 && errno
!= EEXIST
)
292 /* Open reading side */
293 if (i
->fifo_fd
< 0) {
294 i
->fifo_fd
= open(i
->fifo_path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
299 if (!i
->event_source
) {
300 r
= sd_event_add_io(i
->manager
->event
, &i
->event_source
, i
->fifo_fd
, 0, inhibitor_dispatch_fifo
, i
);
304 r
= sd_event_source_set_priority(i
->event_source
, SD_EVENT_PRIORITY_IDLE
-10);
309 /* Open writing side */
310 r
= open(i
->fifo_path
, O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
);
317 void inhibitor_remove_fifo(Inhibitor
*i
) {
320 i
->event_source
= sd_event_source_unref(i
->event_source
);
321 i
->fifo_fd
= safe_close(i
->fifo_fd
);
324 (void) unlink(i
->fifo_path
);
325 i
->fifo_path
= mfree(i
->fifo_path
);
329 InhibitWhat
manager_inhibit_what(Manager
*m
, InhibitMode mm
) {
332 InhibitWhat what
= 0;
336 HASHMAP_FOREACH(i
, m
->inhibitors
, j
)
337 if (i
->mode
== mm
&& i
->started
)
343 static int pid_is_active(Manager
*m
, pid_t pid
) {
347 /* Get client session. This is not what you are looking for these days.
349 r
= manager_get_session_by_pid(m
, pid
, &s
);
353 /* If there's no session assigned to it, then it's globally
354 * active on all ttys */
358 return session_is_active(s
);
361 bool manager_is_inhibited(
365 dual_timestamp
*since
,
366 bool ignore_inactive
,
369 Inhibitor
**offending
) {
373 struct dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
374 bool inhibited
= false;
377 assert(w
> 0 && w
< _INHIBIT_WHAT_MAX
);
379 HASHMAP_FOREACH(i
, m
->inhibitors
, j
) {
389 if (ignore_inactive
&& pid_is_active(m
, i
->pid
) <= 0)
392 if (ignore_uid
&& i
->uid
== uid
)
396 i
->since
.monotonic
< ts
.monotonic
)
411 const char *inhibit_what_to_string(InhibitWhat w
) {
412 static thread_local
char buffer
[97];
415 if (w
< 0 || w
>= _INHIBIT_WHAT_MAX
)
419 if (w
& INHIBIT_SHUTDOWN
)
420 p
= stpcpy(p
, "shutdown:");
421 if (w
& INHIBIT_SLEEP
)
422 p
= stpcpy(p
, "sleep:");
423 if (w
& INHIBIT_IDLE
)
424 p
= stpcpy(p
, "idle:");
425 if (w
& INHIBIT_HANDLE_POWER_KEY
)
426 p
= stpcpy(p
, "handle-power-key:");
427 if (w
& INHIBIT_HANDLE_SUSPEND_KEY
)
428 p
= stpcpy(p
, "handle-suspend-key:");
429 if (w
& INHIBIT_HANDLE_HIBERNATE_KEY
)
430 p
= stpcpy(p
, "handle-hibernate-key:");
431 if (w
& INHIBIT_HANDLE_LID_SWITCH
)
432 p
= stpcpy(p
, "handle-lid-switch:");
442 InhibitWhat
inhibit_what_from_string(const char *s
) {
443 InhibitWhat what
= 0;
444 const char *word
, *state
;
447 FOREACH_WORD_SEPARATOR(word
, l
, s
, ":", state
) {
448 if (l
== 8 && strneq(word
, "shutdown", l
))
449 what
|= INHIBIT_SHUTDOWN
;
450 else if (l
== 5 && strneq(word
, "sleep", l
))
451 what
|= INHIBIT_SLEEP
;
452 else if (l
== 4 && strneq(word
, "idle", l
))
453 what
|= INHIBIT_IDLE
;
454 else if (l
== 16 && strneq(word
, "handle-power-key", l
))
455 what
|= INHIBIT_HANDLE_POWER_KEY
;
456 else if (l
== 18 && strneq(word
, "handle-suspend-key", l
))
457 what
|= INHIBIT_HANDLE_SUSPEND_KEY
;
458 else if (l
== 20 && strneq(word
, "handle-hibernate-key", l
))
459 what
|= INHIBIT_HANDLE_HIBERNATE_KEY
;
460 else if (l
== 17 && strneq(word
, "handle-lid-switch", l
))
461 what
|= INHIBIT_HANDLE_LID_SWITCH
;
463 return _INHIBIT_WHAT_INVALID
;
469 static const char* const inhibit_mode_table
[_INHIBIT_MODE_MAX
] = {
470 [INHIBIT_BLOCK
] = "block",
471 [INHIBIT_DELAY
] = "delay"
474 DEFINE_STRING_TABLE_LOOKUP(inhibit_mode
, InhibitMode
);