1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "formats-util.h"
#include "logind-inhibit.h"
#include "mkdir.h"
#include "parse-util.h"
#include "string-util.h"
#include "user-util.h"
#include "util.h"
Inhibitor* inhibitor_new(Manager *m, const char* id) {
Inhibitor *i;
assert(m);
i = new0(Inhibitor, 1);
if (!i)
return NULL;
i->state_file = strappend("/run/systemd/inhibit/", id);
if (!i->state_file) {
free(i);
return NULL;
}
i->id = basename(i->state_file);
if (hashmap_put(m->inhibitors, i->id, i) < 0) {
free(i->state_file);
free(i);
return NULL;
}
i->manager = m;
i->fifo_fd = -1;
return i;
}
void inhibitor_free(Inhibitor *i) {
assert(i);
hashmap_remove(i->manager->inhibitors, i->id);
inhibitor_remove_fifo(i);
free(i->who);
free(i->why);
if (i->state_file) {
unlink(i->state_file);
free(i->state_file);
}
free(i);
}
int inhibitor_save(Inhibitor *i) {
_cleanup_free_ char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(i);
r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0);
if (r < 0)
goto fail;
r = fopen_temporary(i->state_file, &f, &temp_path);
if (r < 0)
goto fail;
fchmod(fileno(f), 0644);
fprintf(f,
"# This is private data. Do not parse.\n"
"WHAT=%s\n"
"MODE=%s\n"
"UID="UID_FMT"\n"
"PID="PID_FMT"\n",
inhibit_what_to_string(i->what),
inhibit_mode_to_string(i->mode),
i->uid,
i->pid);
if (i->who) {
_cleanup_free_ char *cc = NULL;
cc = cescape(i->who);
if (!cc) {
r = -ENOMEM;
goto fail;
}
fprintf(f, "WHO=%s\n", cc);
}
if (i->why) {
_cleanup_free_ char *cc = NULL;
cc = cescape(i->why);
if (!cc) {
r = -ENOMEM;
goto fail;
}
fprintf(f, "WHY=%s\n", cc);
}
if (i->fifo_path)
fprintf(f, "FIFO=%s\n", i->fifo_path);
r = fflush_and_check(f);
if (r < 0)
goto fail;
if (rename(temp_path, i->state_file) < 0) {
r = -errno;
goto fail;
}
return 0;
fail:
(void) unlink(i->state_file);
if (temp_path)
(void) unlink(temp_path);
return log_error_errno(r, "Failed to save inhibit data %s: %m", i->state_file);
}
int inhibitor_start(Inhibitor *i) {
assert(i);
if (i->started)
return 0;
dual_timestamp_get(&i->since);
log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s started.",
strna(i->who), strna(i->why),
i->pid, i->uid,
inhibit_mode_to_string(i->mode));
inhibitor_save(i);
i->started = true;
manager_send_changed(i->manager, i->mode == INHIBIT_BLOCK ? "BlockInhibited" : "DelayInhibited", NULL);
return 0;
}
int inhibitor_stop(Inhibitor *i) {
assert(i);
if (i->started)
log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s stopped.",
strna(i->who), strna(i->why),
i->pid, i->uid,
inhibit_mode_to_string(i->mode));
if (i->state_file)
unlink(i->state_file);
i->started = false;
manager_send_changed(i->manager, i->mode == INHIBIT_BLOCK ? "BlockInhibited" : "DelayInhibited", NULL);
return 0;
}
int inhibitor_load(Inhibitor *i) {
_cleanup_free_ char
*what = NULL,
*uid = NULL,
*pid = NULL,
*who = NULL,
*why = NULL,
*mode = NULL;
InhibitWhat w;
InhibitMode mm;
char *cc;
int r;
r = parse_env_file(i->state_file, NEWLINE,
"WHAT", &what,
"UID", &uid,
"PID", &pid,
"WHO", &who,
"WHY", &why,
"MODE", &mode,
"FIFO", &i->fifo_path,
NULL);
if (r < 0)
return r;
w = what ? inhibit_what_from_string(what) : 0;
if (w >= 0)
i->what = w;
mm = mode ? inhibit_mode_from_string(mode) : INHIBIT_BLOCK;
if (mm >= 0)
i->mode = mm;
if (uid) {
r = parse_uid(uid, &i->uid);
if (r < 0)
return r;
}
if (pid) {
r = parse_pid(pid, &i->pid);
if (r < 0)
return r;
}
if (who) {
r = cunescape(who, 0, &cc);
if (r < 0)
return r;
free(i->who);
i->who = cc;
}
if (why) {
r = cunescape(why, 0, &cc);
if (r < 0)
return r;
free(i->why);
i->why = cc;
}
if (i->fifo_path) {
int fd;
fd = inhibitor_create_fifo(i);
safe_close(fd);
}
return 0;
}
static int inhibitor_dispatch_fifo(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
Inhibitor *i = userdata;
assert(s);
assert(fd == i->fifo_fd);
assert(i);
inhibitor_stop(i);
inhibitor_free(i);
return 0;
}
int inhibitor_create_fifo(Inhibitor *i) {
int r;
assert(i);
/* Create FIFO */
if (!i->fifo_path) {
r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0);
if (r < 0)
return r;
i->fifo_path = strjoin("/run/systemd/inhibit/", i->id, ".ref", NULL);
if (!i->fifo_path)
return -ENOMEM;
if (mkfifo(i->fifo_path, 0600) < 0 && errno != EEXIST)
return -errno;
}
/* Open reading side */
if (i->fifo_fd < 0) {
i->fifo_fd = open(i->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY);
if (i->fifo_fd < 0)
return -errno;
}
if (!i->event_source) {
r = sd_event_add_io(i->manager->event, &i->event_source, i->fifo_fd, 0, inhibitor_dispatch_fifo, i);
if (r < 0)
return r;
r = sd_event_source_set_priority(i->event_source, SD_EVENT_PRIORITY_IDLE);
if (r < 0)
return r;
}
/* Open writing side */
r = open(i->fifo_path, O_WRONLY|O_CLOEXEC|O_NDELAY);
if (r < 0)
return -errno;
return r;
}
void inhibitor_remove_fifo(Inhibitor *i) {
assert(i);
i->event_source = sd_event_source_unref(i->event_source);
i->fifo_fd = safe_close(i->fifo_fd);
if (i->fifo_path) {
unlink(i->fifo_path);
i->fifo_path = mfree(i->fifo_path);
}
}
InhibitWhat manager_inhibit_what(Manager *m, InhibitMode mm) {
Inhibitor *i;
Iterator j;
InhibitWhat what = 0;
assert(m);
HASHMAP_FOREACH(i, m->inhibitors, j)
if (i->mode == mm)
what |= i->what;
return what;
}
static int pid_is_active(Manager *m, pid_t pid) {
Session *s;
int r;
r = manager_get_session_by_pid(m, pid, &s);
if (r < 0)
return r;
/* If there's no session assigned to it, then it's globally
* active on all ttys */
if (r == 0)
return 1;
return session_is_active(s);
}
bool manager_is_inhibited(
Manager *m,
InhibitWhat w,
InhibitMode mm,
dual_timestamp *since,
bool ignore_inactive,
bool ignore_uid,
uid_t uid,
Inhibitor **offending) {
Inhibitor *i;
Iterator j;
struct dual_timestamp ts = DUAL_TIMESTAMP_NULL;
bool inhibited = false;
assert(m);
assert(w > 0 && w < _INHIBIT_WHAT_MAX);
HASHMAP_FOREACH(i, m->inhibitors, j) {
if (!(i->what & w))
continue;
if (i->mode != mm)
continue;
if (ignore_inactive && pid_is_active(m, i->pid) <= 0)
continue;
if (ignore_uid && i->uid == uid)
continue;
if (!inhibited ||
i->since.monotonic < ts.monotonic)
ts = i->since;
inhibited = true;
if (offending)
*offending = i;
}
if (since)
*since = ts;
return inhibited;
}
const char *inhibit_what_to_string(InhibitWhat w) {
static thread_local char buffer[97];
char *p;
if (w < 0 || w >= _INHIBIT_WHAT_MAX)
return NULL;
p = buffer;
if (w & INHIBIT_SHUTDOWN)
p = stpcpy(p, "shutdown:");
if (w & INHIBIT_SLEEP)
p = stpcpy(p, "sleep:");
if (w & INHIBIT_IDLE)
p = stpcpy(p, "idle:");
if (w & INHIBIT_HANDLE_POWER_KEY)
p = stpcpy(p, "handle-power-key:");
if (w & INHIBIT_HANDLE_SUSPEND_KEY)
p = stpcpy(p, "handle-suspend-key:");
if (w & INHIBIT_HANDLE_HIBERNATE_KEY)
p = stpcpy(p, "handle-hibernate-key:");
if (w & INHIBIT_HANDLE_LID_SWITCH)
p = stpcpy(p, "handle-lid-switch:");
if (p > buffer)
*(p-1) = 0;
else
*p = 0;
return buffer;
}
InhibitWhat inhibit_what_from_string(const char *s) {
InhibitWhat what = 0;
const char *word, *state;
size_t l;
FOREACH_WORD_SEPARATOR(word, l, s, ":", state) {
if (l == 8 && strneq(word, "shutdown", l))
what |= INHIBIT_SHUTDOWN;
else if (l == 5 && strneq(word, "sleep", l))
what |= INHIBIT_SLEEP;
else if (l == 4 && strneq(word, "idle", l))
what |= INHIBIT_IDLE;
else if (l == 16 && strneq(word, "handle-power-key", l))
what |= INHIBIT_HANDLE_POWER_KEY;
else if (l == 18 && strneq(word, "handle-suspend-key", l))
what |= INHIBIT_HANDLE_SUSPEND_KEY;
else if (l == 20 && strneq(word, "handle-hibernate-key", l))
what |= INHIBIT_HANDLE_HIBERNATE_KEY;
else if (l == 17 && strneq(word, "handle-lid-switch", l))
what |= INHIBIT_HANDLE_LID_SWITCH;
else
return _INHIBIT_WHAT_INVALID;
}
return what;
}
static const char* const inhibit_mode_table[_INHIBIT_MODE_MAX] = {
[INHIBIT_BLOCK] = "block",
[INHIBIT_DELAY] = "delay"
};
DEFINE_STRING_TABLE_LOOKUP(inhibit_mode, InhibitMode);
|