]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f8e2fb7b LP |
2 | |
3 | #include <errno.h> | |
4 | #include <fcntl.h> | |
f8e2fb7b | 5 | #include <string.h> |
ca78ad1d ZJS |
6 | #include <sys/stat.h> |
7 | #include <sys/types.h> | |
f8e2fb7b LP |
8 | #include <unistd.h> |
9 | ||
b5efdb8a | 10 | #include "alloc-util.h" |
686d13b9 | 11 | #include "env-file.h" |
4f5dd394 | 12 | #include "escape.h" |
3ffd4af2 | 13 | #include "fd-util.h" |
a5c32cff | 14 | #include "fileio.h" |
f97b34a6 | 15 | #include "format-util.h" |
11eae36d | 16 | #include "io-util.h" |
6ecda0fb | 17 | #include "logind-dbus.h" |
3ffd4af2 | 18 | #include "logind-inhibit.h" |
4f5dd394 | 19 | #include "mkdir.h" |
6bedfcbb | 20 | #include "parse-util.h" |
b910cc72 | 21 | #include "path-util.h" |
8b43440b | 22 | #include "string-table.h" |
07630cea | 23 | #include "string-util.h" |
e4de7287 | 24 | #include "tmpfile-util.h" |
b1d4f8e1 | 25 | #include "user-util.h" |
4f5dd394 | 26 | #include "util.h" |
f8e2fb7b | 27 | |
290320ef LP |
28 | static void inhibitor_remove_fifo(Inhibitor *i); |
29 | ||
81280b2a LP |
30 | int inhibitor_new(Inhibitor **ret, Manager *m, const char* id) { |
31 | _cleanup_(inhibitor_freep) Inhibitor *i = NULL; | |
32 | int r; | |
f8e2fb7b | 33 | |
81280b2a | 34 | assert(ret); |
f8e2fb7b | 35 | assert(m); |
81280b2a | 36 | assert(id); |
f8e2fb7b | 37 | |
81280b2a | 38 | i = new(Inhibitor, 1); |
f8e2fb7b | 39 | if (!i) |
81280b2a LP |
40 | return -ENOMEM; |
41 | ||
42 | *i = (Inhibitor) { | |
43 | .manager = m, | |
44 | .what = _INHIBIT_WHAT_INVALID, | |
45 | .mode = _INHIBIT_MODE_INVALID, | |
46 | .uid = UID_INVALID, | |
47 | .fifo_fd = -1, | |
48 | }; | |
f8e2fb7b | 49 | |
b910cc72 | 50 | i->state_file = path_join("/run/systemd/inhibit", id); |
6b430fdb | 51 | if (!i->state_file) |
81280b2a | 52 | return -ENOMEM; |
f8e2fb7b | 53 | |
2b6bf07d | 54 | i->id = basename(i->state_file); |
f8e2fb7b | 55 | |
81280b2a LP |
56 | r = hashmap_put(m->inhibitors, i->id, i); |
57 | if (r < 0) | |
58 | return r; | |
f8e2fb7b | 59 | |
81280b2a LP |
60 | *ret = TAKE_PTR(i); |
61 | return 0; | |
f8e2fb7b LP |
62 | } |
63 | ||
81280b2a | 64 | Inhibitor* inhibitor_free(Inhibitor *i) { |
cc377381 | 65 | |
81280b2a LP |
66 | if (!i) |
67 | return NULL; | |
f8e2fb7b | 68 | |
cc377381 LP |
69 | free(i->who); |
70 | free(i->why); | |
71 | ||
81280b2a LP |
72 | sd_event_source_unref(i->event_source); |
73 | safe_close(i->fifo_fd); | |
f8e2fb7b | 74 | |
09172930 YW |
75 | hashmap_remove(i->manager->inhibitors, i->id); |
76 | ||
81280b2a LP |
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 */ | |
79 | free(i->state_file); | |
80 | free(i->fifo_path); | |
81 | ||
81280b2a | 82 | return mfree(i); |
f8e2fb7b LP |
83 | } |
84 | ||
290320ef | 85 | static int inhibitor_save(Inhibitor *i) { |
cc377381 LP |
86 | _cleanup_free_ char *temp_path = NULL; |
87 | _cleanup_fclose_ FILE *f = NULL; | |
f8e2fb7b | 88 | int r; |
f8e2fb7b LP |
89 | |
90 | assert(i); | |
91 | ||
37c1d5e9 | 92 | r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE); |
f8e2fb7b | 93 | if (r < 0) |
dacd6cee | 94 | goto fail; |
f8e2fb7b LP |
95 | |
96 | r = fopen_temporary(i->state_file, &f, &temp_path); | |
97 | if (r < 0) | |
dacd6cee | 98 | goto fail; |
f8e2fb7b | 99 | |
07530d70 | 100 | (void) fchmod(fileno(f), 0644); |
f8e2fb7b LP |
101 | |
102 | fprintf(f, | |
103 | "# This is private data. Do not parse.\n" | |
104 | "WHAT=%s\n" | |
eecd1362 | 105 | "MODE=%s\n" |
90b2de37 ZJS |
106 | "UID="UID_FMT"\n" |
107 | "PID="PID_FMT"\n", | |
f8e2fb7b | 108 | inhibit_what_to_string(i->what), |
eecd1362 | 109 | inhibit_mode_to_string(i->mode), |
90b2de37 ZJS |
110 | i->uid, |
111 | i->pid); | |
f8e2fb7b LP |
112 | |
113 | if (i->who) { | |
cc377381 LP |
114 | _cleanup_free_ char *cc = NULL; |
115 | ||
f8e2fb7b | 116 | cc = cescape(i->who); |
88231eb6 | 117 | if (!cc) { |
f8e2fb7b | 118 | r = -ENOMEM; |
88231eb6 TA |
119 | goto fail; |
120 | } | |
121 | ||
122 | fprintf(f, "WHO=%s\n", cc); | |
f8e2fb7b LP |
123 | } |
124 | ||
125 | if (i->why) { | |
cc377381 LP |
126 | _cleanup_free_ char *cc = NULL; |
127 | ||
f8e2fb7b | 128 | cc = cescape(i->why); |
88231eb6 | 129 | if (!cc) { |
f8e2fb7b | 130 | r = -ENOMEM; |
88231eb6 TA |
131 | goto fail; |
132 | } | |
133 | ||
134 | fprintf(f, "WHY=%s\n", cc); | |
f8e2fb7b LP |
135 | } |
136 | ||
137 | if (i->fifo_path) | |
138 | fprintf(f, "FIFO=%s\n", i->fifo_path); | |
139 | ||
dacd6cee LP |
140 | r = fflush_and_check(f); |
141 | if (r < 0) | |
142 | goto fail; | |
f8e2fb7b | 143 | |
dacd6cee | 144 | if (rename(temp_path, i->state_file) < 0) { |
f8e2fb7b | 145 | r = -errno; |
dacd6cee | 146 | goto fail; |
f8e2fb7b LP |
147 | } |
148 | ||
dacd6cee LP |
149 | return 0; |
150 | ||
151 | fail: | |
152 | (void) unlink(i->state_file); | |
153 | ||
154 | if (temp_path) | |
155 | (void) unlink(temp_path); | |
f8e2fb7b | 156 | |
dacd6cee | 157 | return log_error_errno(r, "Failed to save inhibit data %s: %m", i->state_file); |
f8e2fb7b LP |
158 | } |
159 | ||
fa39c2de LP |
160 | static int bus_manager_send_inhibited_change(Inhibitor *i) { |
161 | const char *property; | |
162 | ||
163 | assert(i); | |
164 | ||
165 | property = i->mode == INHIBIT_BLOCK ? "BlockInhibited" : "DelayInhibited"; | |
166 | ||
167 | return manager_send_changed(i->manager, property, NULL); | |
168 | } | |
169 | ||
f8e2fb7b LP |
170 | int inhibitor_start(Inhibitor *i) { |
171 | assert(i); | |
172 | ||
173 | if (i->started) | |
174 | return 0; | |
175 | ||
c7b5eb98 LP |
176 | dual_timestamp_get(&i->since); |
177 | ||
de0671ee | 178 | log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s started.", |
f8e2fb7b | 179 | strna(i->who), strna(i->why), |
de0671ee | 180 | i->pid, i->uid, |
eecd1362 | 181 | inhibit_mode_to_string(i->mode)); |
f8e2fb7b | 182 | |
f8e2fb7b LP |
183 | i->started = true; |
184 | ||
fa39c2de LP |
185 | inhibitor_save(i); |
186 | ||
187 | bus_manager_send_inhibited_change(i); | |
f8e2fb7b LP |
188 | |
189 | return 0; | |
190 | } | |
191 | ||
290320ef | 192 | void inhibitor_stop(Inhibitor *i) { |
f8e2fb7b LP |
193 | assert(i); |
194 | ||
195 | if (i->started) | |
de0671ee | 196 | log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s stopped.", |
f8e2fb7b | 197 | strna(i->who), strna(i->why), |
de0671ee | 198 | i->pid, i->uid, |
eecd1362 | 199 | inhibit_mode_to_string(i->mode)); |
f8e2fb7b | 200 | |
81280b2a LP |
201 | inhibitor_remove_fifo(i); |
202 | ||
f8e2fb7b | 203 | if (i->state_file) |
6990fb6b | 204 | (void) unlink(i->state_file); |
f8e2fb7b LP |
205 | |
206 | i->started = false; | |
207 | ||
fa39c2de | 208 | bus_manager_send_inhibited_change(i); |
f8e2fb7b LP |
209 | } |
210 | ||
211 | int inhibitor_load(Inhibitor *i) { | |
cc377381 LP |
212 | |
213 | _cleanup_free_ char | |
f8e2fb7b LP |
214 | *what = NULL, |
215 | *uid = NULL, | |
216 | *pid = NULL, | |
217 | *who = NULL, | |
eecd1362 LP |
218 | *why = NULL, |
219 | *mode = NULL; | |
f8e2fb7b | 220 | |
cc377381 LP |
221 | InhibitWhat w; |
222 | InhibitMode mm; | |
223 | char *cc; | |
224 | int r; | |
225 | ||
aa8fbc74 | 226 | r = parse_env_file(NULL, i->state_file, |
f8e2fb7b LP |
227 | "WHAT", &what, |
228 | "UID", &uid, | |
229 | "PID", &pid, | |
230 | "WHO", &who, | |
231 | "WHY", &why, | |
eecd1362 | 232 | "MODE", &mode, |
13df9c39 | 233 | "FIFO", &i->fifo_path); |
f8e2fb7b | 234 | if (r < 0) |
11b0dd0e | 235 | return log_error_errno(r, "Failed to read %s: %m", i->state_file); |
f8e2fb7b | 236 | |
eecd1362 | 237 | w = what ? inhibit_what_from_string(what) : 0; |
f8e2fb7b LP |
238 | if (w >= 0) |
239 | i->what = w; | |
240 | ||
eecd1362 LP |
241 | mm = mode ? inhibit_mode_from_string(mode) : INHIBIT_BLOCK; |
242 | if (mm >= 0) | |
243 | i->mode = mm; | |
244 | ||
a34faf57 LN |
245 | if (uid) { |
246 | r = parse_uid(uid, &i->uid); | |
247 | if (r < 0) | |
11b0dd0e | 248 | log_debug_errno(r, "Failed to parse UID of inhibitor: %s", uid); |
a34faf57 | 249 | } |
eecd1362 | 250 | |
a34faf57 LN |
251 | if (pid) { |
252 | r = parse_pid(pid, &i->pid); | |
253 | if (r < 0) | |
11b0dd0e | 254 | log_debug_errno(r, "Failed to parse PID of inhibitor: %s", pid); |
a34faf57 | 255 | } |
f8e2fb7b LP |
256 | |
257 | if (who) { | |
527b7a42 LP |
258 | r = cunescape(who, 0, &cc); |
259 | if (r < 0) | |
11b0dd0e | 260 | return log_oom(); |
f8e2fb7b | 261 | |
09f300c4 | 262 | free_and_replace(i->who, cc); |
f8e2fb7b LP |
263 | } |
264 | ||
265 | if (why) { | |
527b7a42 LP |
266 | r = cunescape(why, 0, &cc); |
267 | if (r < 0) | |
11b0dd0e | 268 | return log_oom(); |
f8e2fb7b | 269 | |
09f300c4 | 270 | free_and_replace(i->why, cc); |
f8e2fb7b LP |
271 | } |
272 | ||
273 | if (i->fifo_path) { | |
11b0dd0e | 274 | _cleanup_close_ int fd = -1; |
f8e2fb7b | 275 | |
11b0dd0e | 276 | /* Let's re-open the FIFO on both sides, and close the writing side right away */ |
f8e2fb7b | 277 | fd = inhibitor_create_fifo(i); |
11b0dd0e LP |
278 | if (fd < 0) |
279 | return log_error_errno(fd, "Failed to reopen FIFO: %m"); | |
f8e2fb7b LP |
280 | } |
281 | ||
cc377381 LP |
282 | return 0; |
283 | } | |
f8e2fb7b | 284 | |
cc377381 LP |
285 | static int inhibitor_dispatch_fifo(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
286 | Inhibitor *i = userdata; | |
287 | ||
288 | assert(s); | |
289 | assert(fd == i->fifo_fd); | |
290 | assert(i); | |
291 | ||
292 | inhibitor_stop(i); | |
293 | inhibitor_free(i); | |
294 | ||
295 | return 0; | |
f8e2fb7b LP |
296 | } |
297 | ||
298 | int inhibitor_create_fifo(Inhibitor *i) { | |
299 | int r; | |
300 | ||
301 | assert(i); | |
302 | ||
303 | /* Create FIFO */ | |
304 | if (!i->fifo_path) { | |
37c1d5e9 | 305 | r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE); |
f8e2fb7b LP |
306 | if (r < 0) |
307 | return r; | |
308 | ||
605405c6 | 309 | i->fifo_path = strjoin("/run/systemd/inhibit/", i->id, ".ref"); |
cc377381 | 310 | if (!i->fifo_path) |
f8e2fb7b LP |
311 | return -ENOMEM; |
312 | ||
313 | if (mkfifo(i->fifo_path, 0600) < 0 && errno != EEXIST) | |
314 | return -errno; | |
315 | } | |
316 | ||
317 | /* Open reading side */ | |
318 | if (i->fifo_fd < 0) { | |
db4a47e9 | 319 | i->fifo_fd = open(i->fifo_path, O_RDONLY|O_CLOEXEC|O_NONBLOCK); |
f8e2fb7b LP |
320 | if (i->fifo_fd < 0) |
321 | return -errno; | |
cc377381 | 322 | } |
f8e2fb7b | 323 | |
cc377381 | 324 | if (!i->event_source) { |
151b9b96 | 325 | r = sd_event_add_io(i->manager->event, &i->event_source, i->fifo_fd, 0, inhibitor_dispatch_fifo, i); |
f8e2fb7b LP |
326 | if (r < 0) |
327 | return r; | |
328 | ||
e11544a8 | 329 | r = sd_event_source_set_priority(i->event_source, SD_EVENT_PRIORITY_IDLE-10); |
cc377381 LP |
330 | if (r < 0) |
331 | return r; | |
3884274b LP |
332 | |
333 | (void) sd_event_source_set_description(i->event_source, "inhibitor-ref"); | |
f8e2fb7b LP |
334 | } |
335 | ||
336 | /* Open writing side */ | |
db4a47e9 | 337 | r = open(i->fifo_path, O_WRONLY|O_CLOEXEC|O_NONBLOCK); |
f8e2fb7b LP |
338 | if (r < 0) |
339 | return -errno; | |
340 | ||
341 | return r; | |
342 | } | |
343 | ||
290320ef | 344 | static void inhibitor_remove_fifo(Inhibitor *i) { |
f8e2fb7b LP |
345 | assert(i); |
346 | ||
03e334a1 LP |
347 | i->event_source = sd_event_source_unref(i->event_source); |
348 | i->fifo_fd = safe_close(i->fifo_fd); | |
f8e2fb7b LP |
349 | |
350 | if (i->fifo_path) { | |
6990fb6b | 351 | (void) unlink(i->fifo_path); |
a1e58e8e | 352 | i->fifo_path = mfree(i->fifo_path); |
f8e2fb7b LP |
353 | } |
354 | } | |
355 | ||
11eae36d LP |
356 | bool inhibitor_is_orphan(Inhibitor *i) { |
357 | assert(i); | |
358 | ||
359 | if (!i->started) | |
360 | return true; | |
361 | ||
362 | if (!i->fifo_path) | |
363 | return true; | |
364 | ||
365 | if (i->fifo_fd < 0) | |
366 | return true; | |
367 | ||
368 | if (pipe_eof(i->fifo_fd) != 0) | |
369 | return true; | |
370 | ||
371 | return false; | |
372 | } | |
373 | ||
eecd1362 | 374 | InhibitWhat manager_inhibit_what(Manager *m, InhibitMode mm) { |
f8e2fb7b LP |
375 | Inhibitor *i; |
376 | Iterator j; | |
377 | InhibitWhat what = 0; | |
378 | ||
379 | assert(m); | |
380 | ||
cc377381 | 381 | HASHMAP_FOREACH(i, m->inhibitors, j) |
5e8273ac | 382 | if (i->mode == mm && i->started) |
eecd1362 | 383 | what |= i->what; |
f8e2fb7b LP |
384 | |
385 | return what; | |
386 | } | |
387 | ||
beaafb2e LP |
388 | static int pid_is_active(Manager *m, pid_t pid) { |
389 | Session *s; | |
390 | int r; | |
391 | ||
7b33c622 AJ |
392 | /* Get client session. This is not what you are looking for these days. |
393 | * FIXME #6852 */ | |
beaafb2e | 394 | r = manager_get_session_by_pid(m, pid, &s); |
2c4f86c1 | 395 | if (r < 0) |
beaafb2e LP |
396 | return r; |
397 | ||
2c4f86c1 LP |
398 | /* If there's no session assigned to it, then it's globally |
399 | * active on all ttys */ | |
400 | if (r == 0) | |
401 | return 1; | |
402 | ||
beaafb2e LP |
403 | return session_is_active(s); |
404 | } | |
405 | ||
406 | bool manager_is_inhibited( | |
407 | Manager *m, | |
408 | InhibitWhat w, | |
409 | InhibitMode mm, | |
410 | dual_timestamp *since, | |
409133be LP |
411 | bool ignore_inactive, |
412 | bool ignore_uid, | |
85a428c6 LP |
413 | uid_t uid, |
414 | Inhibitor **offending) { | |
beaafb2e | 415 | |
c7b5eb98 LP |
416 | Inhibitor *i; |
417 | Iterator j; | |
5cb14b37 | 418 | struct dual_timestamp ts = DUAL_TIMESTAMP_NULL; |
c7b5eb98 LP |
419 | bool inhibited = false; |
420 | ||
421 | assert(m); | |
422 | assert(w > 0 && w < _INHIBIT_WHAT_MAX); | |
423 | ||
cc377381 | 424 | HASHMAP_FOREACH(i, m->inhibitors, j) { |
5e8273ac AF |
425 | if (!i->started) |
426 | continue; | |
427 | ||
c7b5eb98 LP |
428 | if (!(i->what & w)) |
429 | continue; | |
430 | ||
eecd1362 LP |
431 | if (i->mode != mm) |
432 | continue; | |
433 | ||
409133be LP |
434 | if (ignore_inactive && pid_is_active(m, i->pid) <= 0) |
435 | continue; | |
436 | ||
437 | if (ignore_uid && i->uid == uid) | |
beaafb2e LP |
438 | continue; |
439 | ||
c7b5eb98 LP |
440 | if (!inhibited || |
441 | i->since.monotonic < ts.monotonic) | |
442 | ts = i->since; | |
443 | ||
444 | inhibited = true; | |
85a428c6 LP |
445 | |
446 | if (offending) | |
447 | *offending = i; | |
c7b5eb98 LP |
448 | } |
449 | ||
450 | if (since) | |
451 | *since = ts; | |
452 | ||
453 | return inhibited; | |
454 | } | |
455 | ||
f8e2fb7b | 456 | const char *inhibit_what_to_string(InhibitWhat w) { |
ec202eae | 457 | static thread_local char buffer[97]; |
beaafb2e | 458 | char *p; |
f8e2fb7b LP |
459 | |
460 | if (w < 0 || w >= _INHIBIT_WHAT_MAX) | |
461 | return NULL; | |
462 | ||
beaafb2e LP |
463 | p = buffer; |
464 | if (w & INHIBIT_SHUTDOWN) | |
465 | p = stpcpy(p, "shutdown:"); | |
466 | if (w & INHIBIT_SLEEP) | |
467 | p = stpcpy(p, "sleep:"); | |
468 | if (w & INHIBIT_IDLE) | |
469 | p = stpcpy(p, "idle:"); | |
470 | if (w & INHIBIT_HANDLE_POWER_KEY) | |
471 | p = stpcpy(p, "handle-power-key:"); | |
8e7fd6ad LP |
472 | if (w & INHIBIT_HANDLE_SUSPEND_KEY) |
473 | p = stpcpy(p, "handle-suspend-key:"); | |
474 | if (w & INHIBIT_HANDLE_HIBERNATE_KEY) | |
475 | p = stpcpy(p, "handle-hibernate-key:"); | |
beaafb2e LP |
476 | if (w & INHIBIT_HANDLE_LID_SWITCH) |
477 | p = stpcpy(p, "handle-lid-switch:"); | |
478 | ||
479 | if (p > buffer) | |
480 | *(p-1) = 0; | |
481 | else | |
482 | *p = 0; | |
483 | ||
484 | return buffer; | |
f8e2fb7b LP |
485 | } |
486 | ||
487 | InhibitWhat inhibit_what_from_string(const char *s) { | |
488 | InhibitWhat what = 0; | |
a2a5291b | 489 | const char *word, *state; |
f8e2fb7b LP |
490 | size_t l; |
491 | ||
a2a5291b ZJS |
492 | FOREACH_WORD_SEPARATOR(word, l, s, ":", state) { |
493 | if (l == 8 && strneq(word, "shutdown", l)) | |
f8e2fb7b | 494 | what |= INHIBIT_SHUTDOWN; |
a2a5291b | 495 | else if (l == 5 && strneq(word, "sleep", l)) |
4943c1c9 | 496 | what |= INHIBIT_SLEEP; |
a2a5291b | 497 | else if (l == 4 && strneq(word, "idle", l)) |
f8e2fb7b | 498 | what |= INHIBIT_IDLE; |
a2a5291b | 499 | else if (l == 16 && strneq(word, "handle-power-key", l)) |
beaafb2e | 500 | what |= INHIBIT_HANDLE_POWER_KEY; |
a2a5291b | 501 | else if (l == 18 && strneq(word, "handle-suspend-key", l)) |
8e7fd6ad | 502 | what |= INHIBIT_HANDLE_SUSPEND_KEY; |
a2a5291b | 503 | else if (l == 20 && strneq(word, "handle-hibernate-key", l)) |
8e7fd6ad | 504 | what |= INHIBIT_HANDLE_HIBERNATE_KEY; |
a2a5291b | 505 | else if (l == 17 && strneq(word, "handle-lid-switch", l)) |
beaafb2e | 506 | what |= INHIBIT_HANDLE_LID_SWITCH; |
f8e2fb7b LP |
507 | else |
508 | return _INHIBIT_WHAT_INVALID; | |
509 | } | |
510 | ||
511 | return what; | |
f8e2fb7b | 512 | } |
eecd1362 LP |
513 | |
514 | static const char* const inhibit_mode_table[_INHIBIT_MODE_MAX] = { | |
515 | [INHIBIT_BLOCK] = "block", | |
516 | [INHIBIT_DELAY] = "delay" | |
517 | }; | |
518 | ||
519 | DEFINE_STRING_TABLE_LOOKUP(inhibit_mode, InhibitMode); |