1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <dbus/dbus.h>
24 #include <sys/prctl.h>
26 #include "dbus-execute.h"
31 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_kill_mode, kill_mode, KillMode);
33 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_input, exec_input, ExecInput);
34 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_output, exec_output, ExecOutput);
36 int bus_execute_append_oom_score_adjust(Manager *m, DBusMessageIter *i, const char *property, void *data) {
37 ExecContext *c = data;
45 if (c->oom_score_adjust_set)
46 n = c->oom_score_adjust;
51 if (read_one_line_file("/proc/self/oom_score_adj", &t) >= 0) {
54 } else if (read_one_line_file("/proc/self/oom_adj", &t) >= 0) {
58 if (n == OOM_ADJUST_MAX)
59 n = OOM_SCORE_ADJ_MAX;
61 n = (n * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
65 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
71 int bus_execute_append_nice(Manager *m, DBusMessageIter *i, const char *property, void *data) {
72 ExecContext *c = data;
83 n = getpriority(PRIO_PROCESS, 0);
85 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
91 int bus_execute_append_ioprio(Manager *m, DBusMessageIter *i, const char *property, void *data) {
92 ExecContext *c = data;
103 n = ioprio_get(IOPRIO_WHO_PROCESS, 0);
105 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
111 int bus_execute_append_cpu_sched_policy(Manager *m, DBusMessageIter *i, const char *property, void *data) {
112 ExecContext *c = data;
120 if (c->cpu_sched_set)
121 n = c->cpu_sched_policy;
123 n = sched_getscheduler(0);
125 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
131 int bus_execute_append_cpu_sched_priority(Manager *m, DBusMessageIter *i, const char *property, void *data) {
132 ExecContext *c = data;
140 if (c->cpu_sched_set)
141 n = c->cpu_sched_priority;
143 struct sched_param p;
147 if (sched_getparam(0, &p) >= 0)
148 n = p.sched_priority;
151 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
157 int bus_execute_append_affinity(Manager *m, DBusMessageIter *i, const char *property, void *data) {
158 ExecContext *c = data;
167 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "y", &sub))
171 b = dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &c->cpuset, CPU_ALLOC_SIZE(c->cpuset_ncpus));
173 b = dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &c->cpuset, 0);
178 if (!dbus_message_iter_close_container(i, &sub))
184 int bus_execute_append_timer_slack_nsec(Manager *m, DBusMessageIter *i, const char *property, void *data) {
185 ExecContext *c = data;
193 if (c->timer_slack_nsec_set)
194 u = (uint64_t) c->timer_slack_nsec;
196 u = (uint64_t) prctl(PR_GET_TIMERSLACK);
198 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
204 int bus_execute_append_capabilities(Manager *m, DBusMessageIter *i, const char *property, void *data) {
205 ExecContext *c = data;
216 s = t = cap_to_text(c->capabilities, NULL);
223 b = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &s);
234 int bus_execute_append_rlimits(Manager *m, DBusMessageIter *i, const char *property, void *data) {
235 ExecContext *c = data;
244 assert_se((r = rlimit_from_string(property)) >= 0);
247 u = (uint64_t) c->rlimit[r]->rlim_max;
254 u = (uint64_t) rl.rlim_max;
257 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
263 int bus_execute_append_command(Manager *m, DBusMessageIter *i, const char *property, void *data) {
264 ExecCommand *c = data;
265 DBusMessageIter sub, sub2, sub3;
271 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "(sasbttuii)", &sub))
274 LIST_FOREACH(command, c, c) {
277 int32_t code, status;
282 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
283 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &c->path) ||
284 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_ARRAY, "s", &sub3))
287 STRV_FOREACH(l, c->argv)
288 if (!dbus_message_iter_append_basic(&sub3, DBUS_TYPE_STRING, l))
291 pid = (uint32_t) c->exec_status.pid;
292 code = (int32_t) c->exec_status.code;
293 status = (int32_t) c->exec_status.status;
295 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
296 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_BOOLEAN, &c->ignore) ||
297 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT64, &c->exec_status.start_timestamp.realtime) ||
298 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT64, &c->exec_status.exit_timestamp.realtime) ||
299 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &pid) ||
300 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_INT32, &code) ||
301 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_INT32, &status))
304 if (!dbus_message_iter_close_container(&sub, &sub2))
308 if (!dbus_message_iter_close_container(i, &sub))