]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-inhibit.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / login / logind-inhibit.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "env-file.h"
11 #include "errno-list.h"
12 #include "errno-util.h"
13 #include "escape.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "format-util.h"
17 #include "io-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"
27
28 static void inhibitor_remove_fifo(Inhibitor *i);
29
30 int inhibitor_new(Inhibitor **ret, Manager *m, const char* id) {
31 _cleanup_(inhibitor_freep) Inhibitor *i = NULL;
32 int r;
33
34 assert(ret);
35 assert(m);
36 assert(id);
37
38 i = new(Inhibitor, 1);
39 if (!i)
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 = -EBADF,
48 };
49
50 i->state_file = path_join("/run/systemd/inhibit", id);
51 if (!i->state_file)
52 return -ENOMEM;
53
54 i->id = basename(i->state_file);
55
56 r = hashmap_put(m->inhibitors, i->id, i);
57 if (r < 0)
58 return r;
59
60 *ret = TAKE_PTR(i);
61 return 0;
62 }
63
64 Inhibitor* inhibitor_free(Inhibitor *i) {
65
66 if (!i)
67 return NULL;
68
69 free(i->who);
70 free(i->why);
71
72 sd_event_source_unref(i->event_source);
73 safe_close(i->fifo_fd);
74
75 hashmap_remove(i->manager->inhibitors, i->id);
76
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
82 return mfree(i);
83 }
84
85 static int inhibitor_save(Inhibitor *i) {
86 _cleanup_free_ char *temp_path = NULL;
87 _cleanup_fclose_ FILE *f = NULL;
88 int r;
89
90 assert(i);
91
92 r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE);
93 if (r < 0)
94 goto fail;
95
96 r = fopen_temporary(i->state_file, &f, &temp_path);
97 if (r < 0)
98 goto fail;
99
100 (void) fchmod(fileno(f), 0644);
101
102 fprintf(f,
103 "# This is private data. Do not parse.\n"
104 "WHAT=%s\n"
105 "MODE=%s\n"
106 "UID="UID_FMT"\n"
107 "PID="PID_FMT"\n",
108 inhibit_what_to_string(i->what),
109 inhibit_mode_to_string(i->mode),
110 i->uid,
111 i->pid);
112
113 if (i->who) {
114 _cleanup_free_ char *cc = NULL;
115
116 cc = cescape(i->who);
117 if (!cc) {
118 r = -ENOMEM;
119 goto fail;
120 }
121
122 fprintf(f, "WHO=%s\n", cc);
123 }
124
125 if (i->why) {
126 _cleanup_free_ char *cc = NULL;
127
128 cc = cescape(i->why);
129 if (!cc) {
130 r = -ENOMEM;
131 goto fail;
132 }
133
134 fprintf(f, "WHY=%s\n", cc);
135 }
136
137 if (i->fifo_path)
138 fprintf(f, "FIFO=%s\n", i->fifo_path);
139
140 r = fflush_and_check(f);
141 if (r < 0)
142 goto fail;
143
144 if (rename(temp_path, i->state_file) < 0) {
145 r = -errno;
146 goto fail;
147 }
148
149 return 0;
150
151 fail:
152 (void) unlink(i->state_file);
153
154 if (temp_path)
155 (void) unlink(temp_path);
156
157 return log_error_errno(r, "Failed to save inhibit data %s: %m", i->state_file);
158 }
159
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
170 int inhibitor_start(Inhibitor *i) {
171 assert(i);
172
173 if (i->started)
174 return 0;
175
176 dual_timestamp_get(&i->since);
177
178 log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s started.",
179 strna(i->who), strna(i->why),
180 i->pid, i->uid,
181 inhibit_mode_to_string(i->mode));
182
183 i->started = true;
184
185 inhibitor_save(i);
186
187 bus_manager_send_inhibited_change(i);
188
189 return 0;
190 }
191
192 void inhibitor_stop(Inhibitor *i) {
193 assert(i);
194
195 if (i->started)
196 log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s stopped.",
197 strna(i->who), strna(i->why),
198 i->pid, i->uid,
199 inhibit_mode_to_string(i->mode));
200
201 inhibitor_remove_fifo(i);
202
203 if (i->state_file)
204 (void) unlink(i->state_file);
205
206 i->started = false;
207
208 bus_manager_send_inhibited_change(i);
209 }
210
211 int inhibitor_load(Inhibitor *i) {
212 _cleanup_free_ char *what = NULL, *uid = NULL, *pid = NULL, *who = NULL, *why = NULL, *mode = NULL;
213 InhibitWhat w;
214 InhibitMode mm;
215 char *cc;
216 ssize_t l;
217 int r;
218
219 r = parse_env_file(NULL, i->state_file,
220 "WHAT", &what,
221 "UID", &uid,
222 "PID", &pid,
223 "WHO", &who,
224 "WHY", &why,
225 "MODE", &mode,
226 "FIFO", &i->fifo_path);
227 if (r < 0)
228 return log_error_errno(r, "Failed to read %s: %m", i->state_file);
229
230 w = what ? inhibit_what_from_string(what) : 0;
231 if (w >= 0)
232 i->what = w;
233
234 mm = mode ? inhibit_mode_from_string(mode) : INHIBIT_BLOCK;
235 if (mm >= 0)
236 i->mode = mm;
237
238 if (uid) {
239 r = parse_uid(uid, &i->uid);
240 if (r < 0)
241 log_debug_errno(r, "Failed to parse UID of inhibitor: %s", uid);
242 }
243
244 if (pid) {
245 r = parse_pid(pid, &i->pid);
246 if (r < 0)
247 log_debug_errno(r, "Failed to parse PID of inhibitor: %s", pid);
248 }
249
250 if (who) {
251 l = cunescape(who, 0, &cc);
252 if (l < 0)
253 return log_debug_errno(l, "Failed to unescape \"who\" of inhibitor: %m");
254
255 free_and_replace(i->who, cc);
256 }
257
258 if (why) {
259 l = cunescape(why, 0, &cc);
260 if (l < 0)
261 return log_debug_errno(l, "Failed to unescape \"why\" of inhibitor: %m");
262
263 free_and_replace(i->why, cc);
264 }
265
266 if (i->fifo_path) {
267 _cleanup_close_ int fd = -EBADF;
268
269 /* Let's re-open the FIFO on both sides, and close the writing side right away */
270 fd = inhibitor_create_fifo(i);
271 if (fd < 0)
272 return log_error_errno(fd, "Failed to reopen FIFO: %m");
273 }
274
275 return 0;
276 }
277
278 static int inhibitor_dispatch_fifo(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
279 Inhibitor *i = ASSERT_PTR(userdata);
280
281 assert(s);
282 assert(fd == i->fifo_fd);
283
284 inhibitor_stop(i);
285 inhibitor_free(i);
286
287 return 0;
288 }
289
290 int inhibitor_create_fifo(Inhibitor *i) {
291 int r;
292
293 assert(i);
294
295 /* Create FIFO */
296 if (!i->fifo_path) {
297 r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE);
298 if (r < 0)
299 return r;
300
301 i->fifo_path = strjoin("/run/systemd/inhibit/", i->id, ".ref");
302 if (!i->fifo_path)
303 return -ENOMEM;
304
305 if (mkfifo(i->fifo_path, 0600) < 0 && errno != EEXIST)
306 return -errno;
307 }
308
309 /* Open reading side */
310 if (i->fifo_fd < 0) {
311 i->fifo_fd = open(i->fifo_path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
312 if (i->fifo_fd < 0)
313 return -errno;
314 }
315
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);
318 if (r < 0)
319 return r;
320
321 r = sd_event_source_set_priority(i->event_source, SD_EVENT_PRIORITY_IDLE-10);
322 if (r < 0)
323 return r;
324
325 (void) sd_event_source_set_description(i->event_source, "inhibitor-ref");
326 }
327
328 /* Open writing side */
329 return RET_NERRNO(open(i->fifo_path, O_WRONLY|O_CLOEXEC|O_NONBLOCK));
330 }
331
332 static void inhibitor_remove_fifo(Inhibitor *i) {
333 assert(i);
334
335 i->event_source = sd_event_source_unref(i->event_source);
336 i->fifo_fd = safe_close(i->fifo_fd);
337
338 if (i->fifo_path) {
339 (void) unlink(i->fifo_path);
340 i->fifo_path = mfree(i->fifo_path);
341 }
342 }
343
344 bool inhibitor_is_orphan(Inhibitor *i) {
345 assert(i);
346
347 if (!i->started)
348 return true;
349
350 if (!i->fifo_path)
351 return true;
352
353 if (i->fifo_fd < 0)
354 return true;
355
356 if (pipe_eof(i->fifo_fd) != 0)
357 return true;
358
359 return false;
360 }
361
362 InhibitWhat manager_inhibit_what(Manager *m, InhibitMode mm) {
363 Inhibitor *i;
364 InhibitWhat what = 0;
365
366 assert(m);
367
368 HASHMAP_FOREACH(i, m->inhibitors)
369 if (i->mode == mm && i->started)
370 what |= i->what;
371
372 return what;
373 }
374
375 static int pid_is_active(Manager *m, pid_t pid) {
376 Session *s;
377 int r;
378
379 /* Get client session. This is not what you are looking for these days.
380 * FIXME #6852 */
381 r = manager_get_session_by_pid(m, pid, &s);
382 if (r < 0)
383 return r;
384
385 /* If there's no session assigned to it, then it's globally
386 * active on all ttys */
387 if (r == 0)
388 return 1;
389
390 return session_is_active(s);
391 }
392
393 bool manager_is_inhibited(
394 Manager *m,
395 InhibitWhat w,
396 InhibitMode mm,
397 dual_timestamp *since,
398 bool ignore_inactive,
399 bool ignore_uid,
400 uid_t uid,
401 Inhibitor **offending) {
402
403 Inhibitor *i;
404 struct dual_timestamp ts = DUAL_TIMESTAMP_NULL;
405 bool inhibited = false;
406
407 assert(m);
408 assert(w > 0 && w < _INHIBIT_WHAT_MAX);
409
410 HASHMAP_FOREACH(i, m->inhibitors) {
411 if (!i->started)
412 continue;
413
414 if (!(i->what & w))
415 continue;
416
417 if (i->mode != mm)
418 continue;
419
420 if (ignore_inactive && pid_is_active(m, i->pid) <= 0)
421 continue;
422
423 if (ignore_uid && i->uid == uid)
424 continue;
425
426 if (!inhibited ||
427 i->since.monotonic < ts.monotonic)
428 ts = i->since;
429
430 inhibited = true;
431
432 if (offending)
433 *offending = i;
434 }
435
436 if (since)
437 *since = ts;
438
439 return inhibited;
440 }
441
442 const char *inhibit_what_to_string(InhibitWhat w) {
443 static thread_local char buffer[STRLEN(
444 "shutdown:"
445 "sleep:"
446 "idle:"
447 "handle-power-key:"
448 "handle-suspend-key:"
449 "handle-hibernate-key:"
450 "handle-lid-switch:"
451 "handle-reboot-key")+1];
452 char *p;
453
454 if (w < 0 || w >= _INHIBIT_WHAT_MAX)
455 return NULL;
456
457 p = buffer;
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:");
474
475 if (p > buffer)
476 *(p-1) = 0;
477 else
478 *p = 0;
479
480 return buffer;
481 }
482
483 int inhibit_what_from_string(const char *s) {
484 InhibitWhat what = 0;
485
486 for (const char *p = s;;) {
487 _cleanup_free_ char *word = NULL;
488 int r;
489
490 /* A sanity check that our return values fit in an int */
491 assert_cc((int) _INHIBIT_WHAT_MAX == _INHIBIT_WHAT_MAX);
492
493 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
494 if (r < 0)
495 return r;
496 if (r == 0)
497 return what;
498
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;
515 else
516 return _INHIBIT_WHAT_INVALID;
517 }
518 }
519
520 static const char* const inhibit_mode_table[_INHIBIT_MODE_MAX] = {
521 [INHIBIT_BLOCK] = "block",
522 [INHIBIT_DELAY] = "delay"
523 };
524
525 DEFINE_STRING_TABLE_LOOKUP(inhibit_mode, InhibitMode);