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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser 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"
30 #include "dbus-common.h"
31 #include "syscall-list.h"
34 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_input, exec_input, ExecInput);
35 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_output, exec_output, ExecOutput);
37 static int bus_execute_append_env_files(DBusMessageIter *i, const char *property, void *data) {
38 char **env_files = data, **j;
39 DBusMessageIter sub, sub2;
44 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "(sb)", &sub))
47 STRV_FOREACH(j, env_files) {
48 dbus_bool_t b = false;
56 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
57 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &fn) ||
58 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_BOOLEAN, &b) ||
59 !dbus_message_iter_close_container(&sub, &sub2))
63 if (!dbus_message_iter_close_container(i, &sub))
69 static int bus_execute_append_oom_score_adjust(DBusMessageIter *i, const char *property, void *data) {
70 ExecContext *c = data;
77 if (c->oom_score_adjust_set)
78 n = c->oom_score_adjust;
80 _cleanup_free_ char *t = NULL;
83 if (read_one_line_file("/proc/self/oom_score_adj", &t) >= 0) {
88 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
94 static int bus_execute_append_nice(DBusMessageIter *i, const char *property, void *data) {
95 ExecContext *c = data;
105 n = getpriority(PRIO_PROCESS, 0);
107 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
113 static int bus_execute_append_ioprio(DBusMessageIter *i, const char *property, void *data) {
114 ExecContext *c = data;
124 n = ioprio_get(IOPRIO_WHO_PROCESS, 0);
126 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
132 static int bus_execute_append_cpu_sched_policy(DBusMessageIter *i, const char *property, void *data) {
133 ExecContext *c = data;
140 if (c->cpu_sched_set)
141 n = c->cpu_sched_policy;
143 n = sched_getscheduler(0);
145 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
151 static int bus_execute_append_cpu_sched_priority(DBusMessageIter *i, const char *property, void *data) {
152 ExecContext *c = data;
159 if (c->cpu_sched_set)
160 n = c->cpu_sched_priority;
162 struct sched_param p = {};
164 if (sched_getparam(0, &p) >= 0)
165 n = p.sched_priority;
170 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))
176 static int bus_execute_append_affinity(DBusMessageIter *i, const char *property, void *data) {
177 ExecContext *c = data;
185 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "y", &sub))
189 b = dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &c->cpuset, CPU_ALLOC_SIZE(c->cpuset_ncpus));
191 b = dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &c->cpuset, 0);
196 if (!dbus_message_iter_close_container(i, &sub))
202 static int bus_execute_append_timer_slack_nsec(DBusMessageIter *i, const char *property, void *data) {
203 ExecContext *c = data;
210 if (c->timer_slack_nsec != (nsec_t) -1)
211 u = (uint64_t) c->timer_slack_nsec;
213 u = (uint64_t) prctl(PR_GET_TIMERSLACK);
215 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
221 static int bus_execute_append_capability_bs(DBusMessageIter *i, const char *property, void *data) {
222 ExecContext *c = data;
223 uint64_t normal, inverted;
229 /* We store this negated internally, to match the kernel, but
230 * we expose it normalized. */
232 normal = *(uint64_t*) data;
235 return bus_property_append_uint64(i, property, &inverted);
238 static int bus_execute_append_capabilities(DBusMessageIter *i, const char *property, void *data) {
239 ExecContext *c = data;
249 s = t = cap_to_text(c->capabilities, NULL);
256 b = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &s);
267 static int bus_execute_append_rlimits(DBusMessageIter *i, const char *property, void *data) {
268 ExecContext *c = data;
276 assert_se((r = rlimit_from_string(property)) >= 0);
279 u = (uint64_t) c->rlimit[r]->rlim_max;
281 struct rlimit rl = {};
285 u = (uint64_t) rl.rlim_max;
288 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
294 int bus_execute_append_command(DBusMessageIter *i, const char *property, void *data) {
295 ExecCommand *c = data;
296 DBusMessageIter sub, sub2, sub3;
301 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "(sasbttttuii)", &sub))
304 LIST_FOREACH(command, c, c) {
307 int32_t code, status;
313 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
314 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &c->path) ||
315 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_ARRAY, "s", &sub3))
318 STRV_FOREACH(l, c->argv)
319 if (!dbus_message_iter_append_basic(&sub3, DBUS_TYPE_STRING, l))
322 pid = (uint32_t) c->exec_status.pid;
323 code = (int32_t) c->exec_status.code;
324 status = (int32_t) c->exec_status.status;
328 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
329 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_BOOLEAN, &b) ||
330 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT64, &c->exec_status.start_timestamp.realtime) ||
331 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT64, &c->exec_status.start_timestamp.monotonic) ||
332 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT64, &c->exec_status.exit_timestamp.realtime) ||
333 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT64, &c->exec_status.exit_timestamp.monotonic) ||
334 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &pid) ||
335 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_INT32, &code) ||
336 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_INT32, &status))
339 if (!dbus_message_iter_close_container(&sub, &sub2))
343 if (!dbus_message_iter_close_container(i, &sub))
349 static int bus_execute_append_syscall_filter(DBusMessageIter *i, const char *property, void *data) {
350 ExecContext *c = data;
358 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "u", &sub))
361 if (c->syscall_filter)
362 b = dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_UINT32, &c->syscall_filter, (syscall_max() + 31) >> 4);
364 b = dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_UINT32, &c->syscall_filter, 0);
369 if (!dbus_message_iter_close_container(i, &sub))
375 const BusProperty bus_exec_context_properties[] = {
376 { "Environment", bus_property_append_strv, "as", offsetof(ExecContext, environment), true },
377 { "EnvironmentFiles", bus_execute_append_env_files, "a(sb)", offsetof(ExecContext, environment_files), true },
378 { "UMask", bus_property_append_mode, "u", offsetof(ExecContext, umask) },
379 { "LimitCPU", bus_execute_append_rlimits, "t", 0 },
380 { "LimitFSIZE", bus_execute_append_rlimits, "t", 0 },
381 { "LimitDATA", bus_execute_append_rlimits, "t", 0 },
382 { "LimitSTACK", bus_execute_append_rlimits, "t", 0 },
383 { "LimitCORE", bus_execute_append_rlimits, "t", 0 },
384 { "LimitRSS", bus_execute_append_rlimits, "t", 0 },
385 { "LimitNOFILE", bus_execute_append_rlimits, "t", 0 },
386 { "LimitAS", bus_execute_append_rlimits, "t", 0 },
387 { "LimitNPROC", bus_execute_append_rlimits, "t", 0 },
388 { "LimitMEMLOCK", bus_execute_append_rlimits, "t", 0 },
389 { "LimitLOCKS", bus_execute_append_rlimits, "t", 0 },
390 { "LimitSIGPENDING", bus_execute_append_rlimits, "t", 0 },
391 { "LimitMSGQUEUE", bus_execute_append_rlimits, "t", 0 },
392 { "LimitNICE", bus_execute_append_rlimits, "t", 0 },
393 { "LimitRTPRIO", bus_execute_append_rlimits, "t", 0 },
394 { "LimitRTTIME", bus_execute_append_rlimits, "t", 0 },
395 { "WorkingDirectory", bus_property_append_string, "s", offsetof(ExecContext, working_directory), true },
396 { "RootDirectory", bus_property_append_string, "s", offsetof(ExecContext, root_directory), true },
397 { "OOMScoreAdjust", bus_execute_append_oom_score_adjust, "i", 0 },
398 { "Nice", bus_execute_append_nice, "i", 0 },
399 { "IOScheduling", bus_execute_append_ioprio, "i", 0 },
400 { "CPUSchedulingPolicy", bus_execute_append_cpu_sched_policy, "i", 0 },
401 { "CPUSchedulingPriority", bus_execute_append_cpu_sched_priority, "i", 0 },
402 { "CPUAffinity", bus_execute_append_affinity, "ay", 0 },
403 { "TimerSlackNSec", bus_execute_append_timer_slack_nsec, "t", 0 },
404 { "CPUSchedulingResetOnFork", bus_property_append_bool, "b", offsetof(ExecContext, cpu_sched_reset_on_fork) },
405 { "NonBlocking", bus_property_append_bool, "b", offsetof(ExecContext, non_blocking) },
406 { "StandardInput", bus_execute_append_input, "s", offsetof(ExecContext, std_input) },
407 { "StandardOutput", bus_execute_append_output, "s", offsetof(ExecContext, std_output) },
408 { "StandardError", bus_execute_append_output, "s", offsetof(ExecContext, std_error) },
409 { "TTYPath", bus_property_append_string, "s", offsetof(ExecContext, tty_path), true },
410 { "TTYReset", bus_property_append_bool, "b", offsetof(ExecContext, tty_reset) },
411 { "TTYVHangup", bus_property_append_bool, "b", offsetof(ExecContext, tty_vhangup) },
412 { "TTYVTDisallocate", bus_property_append_bool, "b", offsetof(ExecContext, tty_vt_disallocate) },
413 { "SyslogPriority", bus_property_append_int, "i", offsetof(ExecContext, syslog_priority) },
414 { "SyslogIdentifier", bus_property_append_string, "s", offsetof(ExecContext, syslog_identifier), true },
415 { "SyslogLevelPrefix", bus_property_append_bool, "b", offsetof(ExecContext, syslog_level_prefix) },
416 { "Capabilities", bus_execute_append_capabilities, "s", 0 },
417 { "SecureBits", bus_property_append_int, "i", offsetof(ExecContext, secure_bits) },
418 { "CapabilityBoundingSet", bus_execute_append_capability_bs, "t", offsetof(ExecContext, capability_bounding_set_drop) },
419 { "User", bus_property_append_string, "s", offsetof(ExecContext, user), true },
420 { "Group", bus_property_append_string, "s", offsetof(ExecContext, group), true },
421 { "SupplementaryGroups", bus_property_append_strv, "as", offsetof(ExecContext, supplementary_groups), true },
422 { "TCPWrapName", bus_property_append_string, "s", offsetof(ExecContext, tcpwrap_name), true },
423 { "PAMName", bus_property_append_string, "s", offsetof(ExecContext, pam_name), true },
424 { "ReadWriteDirectories", bus_property_append_strv, "as", offsetof(ExecContext, read_write_dirs), true },
425 { "ReadOnlyDirectories", bus_property_append_strv, "as", offsetof(ExecContext, read_only_dirs), true },
426 { "InaccessibleDirectories", bus_property_append_strv, "as", offsetof(ExecContext, inaccessible_dirs), true },
427 { "MountFlags", bus_property_append_ul, "t", offsetof(ExecContext, mount_flags) },
428 { "PrivateTmp", bus_property_append_bool, "b", offsetof(ExecContext, private_tmp) },
429 { "PrivateNetwork", bus_property_append_bool, "b", offsetof(ExecContext, private_network) },
430 { "SameProcessGroup", bus_property_append_bool, "b", offsetof(ExecContext, same_pgrp) },
431 { "UtmpIdentifier", bus_property_append_string, "s", offsetof(ExecContext, utmp_id), true },
432 { "IgnoreSIGPIPE", bus_property_append_bool, "b", offsetof(ExecContext, ignore_sigpipe) },
433 { "NoNewPrivileges", bus_property_append_bool, "b", offsetof(ExecContext, no_new_privileges) },
434 { "SystemCallFilter", bus_execute_append_syscall_filter, "au", 0 },