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/>.
22 #include <sys/prctl.h>
28 #include "syscall-list.h"
31 #include "dbus-execute.h"
32 #include "capability.h"
34 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_exec_output, exec_output, ExecOutput);
36 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exec_input, exec_input, ExecInput);
38 static int property_get_environment_files(
41 const char *interface,
43 sd_bus_message *reply,
45 sd_bus_error *error) {
47 ExecContext *c = userdata;
55 r = sd_bus_message_open_container(reply, 'a', "(sb)");
59 STRV_FOREACH(j, c->environment_files) {
62 r = sd_bus_message_append(reply, "(sb)", fn[0] == '-' ? fn + 1 : fn, fn[0] == '-');
67 return sd_bus_message_close_container(reply);
70 static int property_get_rlimit(
73 const char *interface,
75 sd_bus_message *reply,
77 sd_bus_error *error) {
86 rl = *(struct rlimit**) userdata;
88 u = (uint64_t) rl->rlim_max;
90 struct rlimit buf = {};
93 z = rlimit_from_string(property);
98 u = (uint64_t) buf.rlim_max;
101 return sd_bus_message_append(reply, "t", u);
104 static int property_get_oom_score_adjust(
107 const char *interface,
108 const char *property,
109 sd_bus_message *reply,
111 sd_bus_error *error) {
114 ExecContext *c = userdata;
121 if (c->oom_score_adjust_set)
122 n = c->oom_score_adjust;
124 _cleanup_free_ char *t = NULL;
127 if (read_one_line_file("/proc/self/oom_score_adj", &t) >= 0)
131 return sd_bus_message_append(reply, "i", n);
134 static int property_get_nice(
137 const char *interface,
138 const char *property,
139 sd_bus_message *reply,
141 sd_bus_error *error) {
144 ExecContext *c = userdata;
155 n = getpriority(PRIO_PROCESS, 0);
160 return sd_bus_message_append(reply, "i", n);
163 static int property_get_ioprio(
166 const char *interface,
167 const char *property,
168 sd_bus_message *reply,
170 sd_bus_error *error) {
173 ExecContext *c = userdata;
183 n = ioprio_get(IOPRIO_WHO_PROCESS, 0);
185 n = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 4);
188 return sd_bus_message_append(reply, "i", n);
191 static int property_get_cpu_sched_policy(
194 const char *interface,
195 const char *property,
196 sd_bus_message *reply,
198 sd_bus_error *error) {
200 ExecContext *c = userdata;
207 if (c->cpu_sched_set)
208 n = c->cpu_sched_policy;
210 n = sched_getscheduler(0);
215 return sd_bus_message_append(reply, "i", n);
218 static int property_get_cpu_sched_priority(
221 const char *interface,
222 const char *property,
223 sd_bus_message *reply,
225 sd_bus_error *error) {
227 ExecContext *c = userdata;
234 if (c->cpu_sched_set)
235 n = c->cpu_sched_priority;
237 struct sched_param p = {};
239 if (sched_getparam(0, &p) >= 0)
240 n = p.sched_priority;
245 return sd_bus_message_append(reply, "i", n);
248 static int property_get_cpu_affinity(
251 const char *interface,
252 const char *property,
253 sd_bus_message *reply,
255 sd_bus_error *error) {
257 ExecContext *c = userdata;
264 return sd_bus_message_append_array(reply, 'y', c->cpuset, CPU_ALLOC_SIZE(c->cpuset_ncpus));
266 return sd_bus_message_append_array(reply, 'y', NULL, 0);
269 static int property_get_timer_slack_nsec(
272 const char *interface,
273 const char *property,
274 sd_bus_message *reply,
276 sd_bus_error *error) {
278 ExecContext *c = userdata;
285 if (c->timer_slack_nsec != (nsec_t) -1)
286 u = (uint64_t) c->timer_slack_nsec;
288 u = (uint64_t) prctl(PR_GET_TIMERSLACK);
290 return sd_bus_message_append(reply, "t", u);
293 static int property_get_capability_bounding_set(
296 const char *interface,
297 const char *property,
298 sd_bus_message *reply,
300 sd_bus_error *error) {
302 ExecContext *c = userdata;
308 /* We store this negated internally, to match the kernel, but
309 * we expose it normalized. */
310 return sd_bus_message_append(reply, "t", ~c->capability_bounding_set_drop);
313 static int property_get_capabilities(
316 const char *interface,
317 const char *property,
318 sd_bus_message *reply,
320 sd_bus_error *error) {
322 ExecContext *c = userdata;
323 _cleanup_cap_free_charp_ char *t = NULL;
331 s = t = cap_to_text(c->capabilities, NULL);
338 return sd_bus_message_append(reply, "s", s);
341 static int property_get_syscall_filter(
344 const char *interface,
345 const char *property,
346 sd_bus_message *reply,
348 sd_bus_error *error) {
350 ExecContext *c = userdata;
356 if (c->syscall_filter)
357 return sd_bus_message_append_array(reply, 'u', c->syscall_filter, (syscall_max() + 31) >> 4);
359 return sd_bus_message_append_array(reply, 'u', NULL, 0);
362 const sd_bus_vtable bus_exec_vtable[] = {
363 SD_BUS_VTABLE_START(0),
364 SD_BUS_PROPERTY("Environment", "as", NULL, offsetof(ExecContext, environment), SD_BUS_VTABLE_PROPERTY_CONST),
365 SD_BUS_PROPERTY("EnvironmentFiles", "a(sb)", property_get_environment_files, 0, SD_BUS_VTABLE_PROPERTY_CONST),
366 SD_BUS_PROPERTY("UMask", "u", bus_property_get_mode, offsetof(ExecContext, umask), SD_BUS_VTABLE_PROPERTY_CONST),
367 SD_BUS_PROPERTY("LimitCPU", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST),
368 SD_BUS_PROPERTY("LimitFSIZE", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST),
369 SD_BUS_PROPERTY("LimitDATA", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_DATA]), SD_BUS_VTABLE_PROPERTY_CONST),
370 SD_BUS_PROPERTY("LimitSTACK", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_STACK]), SD_BUS_VTABLE_PROPERTY_CONST),
371 SD_BUS_PROPERTY("LimitCORE", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CORE]), SD_BUS_VTABLE_PROPERTY_CONST),
372 SD_BUS_PROPERTY("LimitRSS", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RSS]), SD_BUS_VTABLE_PROPERTY_CONST),
373 SD_BUS_PROPERTY("LimitNOFILE", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NOFILE]), SD_BUS_VTABLE_PROPERTY_CONST),
374 SD_BUS_PROPERTY("LimitAS", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_AS]), SD_BUS_VTABLE_PROPERTY_CONST),
375 SD_BUS_PROPERTY("LimitNPROC", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NPROC]), SD_BUS_VTABLE_PROPERTY_CONST),
376 SD_BUS_PROPERTY("LimitMEMLOCK", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MEMLOCK]), SD_BUS_VTABLE_PROPERTY_CONST),
377 SD_BUS_PROPERTY("LimitLOCKS", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_LOCKS]), SD_BUS_VTABLE_PROPERTY_CONST),
378 SD_BUS_PROPERTY("LimitSIGPENDING", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_SIGPENDING]), SD_BUS_VTABLE_PROPERTY_CONST),
379 SD_BUS_PROPERTY("LimitMSGQUEUE", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_MSGQUEUE]), SD_BUS_VTABLE_PROPERTY_CONST),
380 SD_BUS_PROPERTY("LimitNICE", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_NICE]), SD_BUS_VTABLE_PROPERTY_CONST),
381 SD_BUS_PROPERTY("LimitRTPRIO", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTPRIO]), SD_BUS_VTABLE_PROPERTY_CONST),
382 SD_BUS_PROPERTY("LimitRTTIME", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_RTTIME]), SD_BUS_VTABLE_PROPERTY_CONST),
383 SD_BUS_PROPERTY("WorkingDirectory", "s", NULL, offsetof(ExecContext, working_directory), SD_BUS_VTABLE_PROPERTY_CONST),
384 SD_BUS_PROPERTY("RootDirectory", "s", NULL, offsetof(ExecContext, root_directory), SD_BUS_VTABLE_PROPERTY_CONST),
385 SD_BUS_PROPERTY("OOMScoreAdjust", "i", property_get_oom_score_adjust, 0, SD_BUS_VTABLE_PROPERTY_CONST),
386 SD_BUS_PROPERTY("Nice", "i", property_get_nice, 0, SD_BUS_VTABLE_PROPERTY_CONST),
387 SD_BUS_PROPERTY("IOScheduling", "i", property_get_ioprio, 0, SD_BUS_VTABLE_PROPERTY_CONST),
388 SD_BUS_PROPERTY("CPUSchedulingPolicy", "i", property_get_cpu_sched_policy, 0, SD_BUS_VTABLE_PROPERTY_CONST),
389 SD_BUS_PROPERTY("CPUSchedulingPriority", "i", property_get_cpu_sched_priority, 0, SD_BUS_VTABLE_PROPERTY_CONST),
390 SD_BUS_PROPERTY("CPUAffinity", "ay", property_get_cpu_affinity, 0, SD_BUS_VTABLE_PROPERTY_CONST),
391 SD_BUS_PROPERTY("TimerSlackNSec", "t", property_get_timer_slack_nsec, 0, SD_BUS_VTABLE_PROPERTY_CONST),
392 SD_BUS_PROPERTY("CPUSchedulingResetOnFork", "b", bus_property_get_bool, offsetof(ExecContext, cpu_sched_reset_on_fork), SD_BUS_VTABLE_PROPERTY_CONST),
393 SD_BUS_PROPERTY("NonBlocking", "b", bus_property_get_bool, offsetof(ExecContext, non_blocking), SD_BUS_VTABLE_PROPERTY_CONST),
394 SD_BUS_PROPERTY("StandardInput", "s", property_get_exec_input, offsetof(ExecContext, std_input), SD_BUS_VTABLE_PROPERTY_CONST),
395 SD_BUS_PROPERTY("StandardOutput", "s", bus_property_get_exec_output, offsetof(ExecContext, std_output), SD_BUS_VTABLE_PROPERTY_CONST),
396 SD_BUS_PROPERTY("StandardError", "s", bus_property_get_exec_output, offsetof(ExecContext, std_error), SD_BUS_VTABLE_PROPERTY_CONST),
397 SD_BUS_PROPERTY("TTYPath", "s", NULL, offsetof(ExecContext, tty_path), SD_BUS_VTABLE_PROPERTY_CONST),
398 SD_BUS_PROPERTY("TTYReset", "b", bus_property_get_bool, offsetof(ExecContext, tty_reset), SD_BUS_VTABLE_PROPERTY_CONST),
399 SD_BUS_PROPERTY("TTYVHangup", "b", bus_property_get_bool, offsetof(ExecContext, tty_vhangup), SD_BUS_VTABLE_PROPERTY_CONST),
400 SD_BUS_PROPERTY("TTYVTDisallocate", "b", bus_property_get_bool, offsetof(ExecContext, tty_vt_disallocate), SD_BUS_VTABLE_PROPERTY_CONST),
401 SD_BUS_PROPERTY("SyslogPriority", "i", bus_property_get_int, offsetof(ExecContext, syslog_priority), SD_BUS_VTABLE_PROPERTY_CONST),
402 SD_BUS_PROPERTY("SyslogIdentifier", "s", NULL, offsetof(ExecContext, syslog_identifier), SD_BUS_VTABLE_PROPERTY_CONST),
403 SD_BUS_PROPERTY("SyslogLevelPrefix", "b", bus_property_get_bool, offsetof(ExecContext, syslog_level_prefix), SD_BUS_VTABLE_PROPERTY_CONST),
404 SD_BUS_PROPERTY("Capabilities", "s", property_get_capabilities, 0, SD_BUS_VTABLE_PROPERTY_CONST),
405 SD_BUS_PROPERTY("SecureBits", "i", bus_property_get_int, offsetof(ExecContext, secure_bits), SD_BUS_VTABLE_PROPERTY_CONST),
406 SD_BUS_PROPERTY("CapabilityBoundingSet", "t", property_get_capability_bounding_set, 0, SD_BUS_VTABLE_PROPERTY_CONST),
407 SD_BUS_PROPERTY("User", "s", NULL, offsetof(ExecContext, user), SD_BUS_VTABLE_PROPERTY_CONST),
408 SD_BUS_PROPERTY("Group", "s", NULL, offsetof(ExecContext, group), SD_BUS_VTABLE_PROPERTY_CONST),
409 SD_BUS_PROPERTY("SupplementaryGroups", "as", NULL, offsetof(ExecContext, supplementary_groups), SD_BUS_VTABLE_PROPERTY_CONST),
410 SD_BUS_PROPERTY("TCPWrapName", "s", NULL, offsetof(ExecContext, tcpwrap_name), SD_BUS_VTABLE_PROPERTY_CONST),
411 SD_BUS_PROPERTY("PAMName", "s", NULL, offsetof(ExecContext, pam_name), SD_BUS_VTABLE_PROPERTY_CONST),
412 SD_BUS_PROPERTY("ReadWriteDirectories", "as", NULL, offsetof(ExecContext, read_write_dirs), SD_BUS_VTABLE_PROPERTY_CONST),
413 SD_BUS_PROPERTY("ReadOnlyDirectories", "as", NULL, offsetof(ExecContext, read_only_dirs), SD_BUS_VTABLE_PROPERTY_CONST),
414 SD_BUS_PROPERTY("InaccessibleDirectories", "as", NULL, offsetof(ExecContext, inaccessible_dirs), SD_BUS_VTABLE_PROPERTY_CONST),
415 SD_BUS_PROPERTY("MountFlags", "t", bus_property_get_ulong, offsetof(ExecContext, mount_flags), SD_BUS_VTABLE_PROPERTY_CONST),
416 SD_BUS_PROPERTY("PrivateTmp", "b", bus_property_get_bool, offsetof(ExecContext, private_tmp), SD_BUS_VTABLE_PROPERTY_CONST),
417 SD_BUS_PROPERTY("PrivateNetwork", "b", bus_property_get_bool, offsetof(ExecContext, private_network), SD_BUS_VTABLE_PROPERTY_CONST),
418 SD_BUS_PROPERTY("PrivateDevices", "b", bus_property_get_bool, offsetof(ExecContext, private_devices), SD_BUS_VTABLE_PROPERTY_CONST),
419 SD_BUS_PROPERTY("SameProcessGroup", "b", bus_property_get_bool, offsetof(ExecContext, same_pgrp), SD_BUS_VTABLE_PROPERTY_CONST),
420 SD_BUS_PROPERTY("UtmpIdentifier", "s", NULL, offsetof(ExecContext, utmp_id), SD_BUS_VTABLE_PROPERTY_CONST),
421 SD_BUS_PROPERTY("IgnoreSIGPIPE", "b", bus_property_get_bool, offsetof(ExecContext, ignore_sigpipe), SD_BUS_VTABLE_PROPERTY_CONST),
422 SD_BUS_PROPERTY("NoNewPrivileges", "b", bus_property_get_bool, offsetof(ExecContext, no_new_privileges), SD_BUS_VTABLE_PROPERTY_CONST),
423 SD_BUS_PROPERTY("SystemCallFilter", "au", property_get_syscall_filter, 0, SD_BUS_VTABLE_PROPERTY_CONST),
427 static int append_exec_command(sd_bus_message *reply, ExecCommand *c) {
436 r = sd_bus_message_open_container(reply, 'r', "sasbttttuii");
440 r = sd_bus_message_append(reply, "s", c->path);
444 r = sd_bus_message_append_strv(reply, c->argv);
448 r = sd_bus_message_append(reply, "bttttuii",
450 c->exec_status.start_timestamp.realtime,
451 c->exec_status.start_timestamp.monotonic,
452 c->exec_status.exit_timestamp.realtime,
453 c->exec_status.exit_timestamp.monotonic,
454 (uint32_t) c->exec_status.pid,
455 (int32_t) c->exec_status.code,
456 (int32_t) c->exec_status.status);
460 return sd_bus_message_close_container(reply);
463 int bus_property_get_exec_command(
466 const char *interface,
467 const char *property,
468 sd_bus_message *reply,
470 sd_bus_error *ret_error) {
472 ExecCommand *c = (ExecCommand*) userdata;
478 r = sd_bus_message_open_container(reply, 'a', "(sasbttttuii)");
482 r = append_exec_command(reply, c);
486 return sd_bus_message_close_container(reply);
489 int bus_property_get_exec_command_list(
492 const char *interface,
493 const char *property,
494 sd_bus_message *reply,
496 sd_bus_error *ret_error) {
498 ExecCommand *c = *(ExecCommand**) userdata;
504 r = sd_bus_message_open_container(reply, 'a', "(sasbttttuii)");
508 LIST_FOREACH(command, c, c) {
509 r = append_exec_command(reply, c);
514 return sd_bus_message_close_container(reply);