#include "mkdir.h"
#include "path-util.h"
#include "logind-inhibit.h"
+#include "fileio.h"
Inhibitor* inhibitor_new(Manager *m, const char* id) {
Inhibitor *i;
assert(i);
- r = safe_mkdir("/run/systemd/inhibit", 0755, 0, 0);
+ r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0);
if (r < 0)
goto finish;
i->started = true;
- manager_send_changed(i->manager, "Inhibited\0");
+ manager_send_changed(i->manager, i->mode == INHIBIT_BLOCK ? "BlockInhibited\0" : "DelayInhibited\0");
return 0;
}
i->started = false;
- manager_send_changed(i->manager, "Inhibited\0");
+ manager_send_changed(i->manager, i->mode == INHIBIT_BLOCK ? "BlockInhibited\0" : "DelayInhibited\0");
return 0;
}
if (mm >= 0)
i->mode = mm;
- if (uid)
- parse_uid(uid, &i->uid);
+ if (uid) {
+ r = parse_uid(uid, &i->uid);
+ if (r < 0)
+ goto finish;
+ }
- if (pid)
- parse_pid(pid, &i->pid);
+ if (pid) {
+ r = parse_pid(pid, &i->pid);
+ if (r < 0)
+ goto finish;
+ }
if (who) {
cc = cunescape(who);
/* Create FIFO */
if (!i->fifo_path) {
- r = safe_mkdir("/run/systemd/inhibit", 0755, 0, 0);
+ r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0);
if (r < 0)
return r;
zero(ev);
ev.events = 0;
- ev.data.u32 = FD_FIFO_BASE + i->fifo_fd;
+ ev.data.u32 = FD_OTHER_BASE + i->fifo_fd;
if (epoll_ctl(i->manager->epoll_fd, EPOLL_CTL_ADD, i->fifo_fd, &ev) < 0)
return -errno;
return what;
}
-bool manager_is_inhibited(Manager *m, InhibitWhat w, InhibitMode mm, dual_timestamp *since) {
+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;
+
+ 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 *i;
Iterator j;
struct dual_timestamp ts = { 0, 0 };
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;
}
const char *inhibit_what_to_string(InhibitWhat w) {
-
- static const char* const table[_INHIBIT_WHAT_MAX] = {
- [0] = "",
- [INHIBIT_SHUTDOWN] = "shutdown",
- [INHIBIT_SUSPEND] = "suspend",
- [INHIBIT_IDLE] = "idle",
- [INHIBIT_SHUTDOWN|INHIBIT_SUSPEND] = "shutdown:suspend",
- [INHIBIT_SHUTDOWN|INHIBIT_IDLE] = "shutdown:idle",
- [INHIBIT_SHUTDOWN|INHIBIT_SUSPEND|INHIBIT_IDLE] = "shutdown:suspend:idle",
- [INHIBIT_SUSPEND|INHIBIT_IDLE] = "suspend:idle"
- };
+ static __thread char buffer[97];
+ char *p;
if (w < 0 || w >= _INHIBIT_WHAT_MAX)
return NULL;
- return table[w];
+ 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) {
size_t l;
FOREACH_WORD_SEPARATOR(w, l, s, ":", state) {
- if (l == 8 && strncmp(w, "shutdown", l) == 0)
+ if (l == 8 && strneq(w, "shutdown", l))
what |= INHIBIT_SHUTDOWN;
- else if (l == 7 && strncmp(w, "suspend", l) == 0)
- what |= INHIBIT_SUSPEND;
- else if (l == 4 && strncmp(w, "idle", l) == 0)
+ else if (l == 5 && strneq(w, "sleep", l))
+ what |= INHIBIT_SLEEP;
+ else if (l == 4 && strneq(w, "idle", l))
what |= INHIBIT_IDLE;
+ else if (l == 16 && strneq(w, "handle-power-key", l))
+ what |= INHIBIT_HANDLE_POWER_KEY;
+ else if (l == 18 && strneq(w, "handle-suspend-key", l))
+ what |= INHIBIT_HANDLE_SUSPEND_KEY;
+ else if (l == 20 && strneq(w, "handle-hibernate-key", l))
+ what |= INHIBIT_HANDLE_HIBERNATE_KEY;
+ else if (l == 17 && strneq(w, "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] = {