From 82c121a4754a9d405b07c75796e329942af2ccc5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 4 Jul 2010 16:44:58 +0200 Subject: dbus: complete exec coverage --- src/dbus-execute.c | 220 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/dbus-execute.h | 84 +++++++++++++++++--- src/dbus.c | 16 ++++ src/dbus.h | 1 + src/execute.c | 13 ++-- src/execute.h | 6 +- src/load-fragment.c | 10 ++- src/missing.h | 1 + src/systemctl.c | 22 ++++++ src/util.c | 28 +++++++ src/util.h | 3 + 11 files changed, 381 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/dbus-execute.c b/src/dbus-execute.c index 8840396bc1..a41b63992b 100644 --- a/src/dbus-execute.c +++ b/src/dbus-execute.c @@ -21,8 +21,228 @@ #include #include +#include #include "dbus-execute.h" +#include "missing.h" +#include "ioprio.h" DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_input, exec_input, ExecInput); DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_output, exec_output, ExecOutput); + +int bus_execute_append_oom_adjust(Manager *m, DBusMessageIter *i, const char *property, void *data) { + ExecContext *c = data; + int32_t n; + + assert(m); + assert(i); + assert(property); + assert(c); + + if (c->oom_adjust_set) + n = c->oom_adjust; + else { + char *t; + + n = 0; + if (read_one_line_file("/proc/self/oom_adj", &t) >= 0) { + safe_atoi(t, &n); + free(t); + } + } + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n)) + return -ENOMEM; + + return 0; +} + +int bus_execute_append_nice(Manager *m, DBusMessageIter *i, const char *property, void *data) { + ExecContext *c = data; + int32_t n; + + assert(m); + assert(i); + assert(property); + assert(c); + + if (c->nice_set) + n = c->nice; + else + n = getpriority(PRIO_PROCESS, 0); + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n)) + return -ENOMEM; + + return 0; +} + +int bus_execute_append_ioprio(Manager *m, DBusMessageIter *i, const char *property, void *data) { + ExecContext *c = data; + int32_t n; + + assert(m); + assert(i); + assert(property); + assert(c); + + if (c->ioprio_set) + n = c->ioprio; + else + n = ioprio_get(IOPRIO_WHO_PROCESS, 0); + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n)) + return -ENOMEM; + + return 0; +} + +int bus_execute_append_cpu_sched_policy(Manager *m, DBusMessageIter *i, const char *property, void *data) { + ExecContext *c = data; + int32_t n; + + assert(m); + assert(i); + assert(property); + assert(c); + + if (c->cpu_sched_set) + n = c->cpu_sched_policy; + else + n = sched_getscheduler(0); + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n)) + return -ENOMEM; + + return 0; +} + +int bus_execute_append_cpu_sched_priority(Manager *m, DBusMessageIter *i, const char *property, void *data) { + ExecContext *c = data; + int32_t n; + + assert(m); + assert(i); + assert(property); + assert(c); + + if (c->cpu_sched_set) + n = c->cpu_sched_priority; + else { + struct sched_param p; + n = 0; + + zero(p); + if (sched_getparam(0, &p) >= 0) + n = p.sched_priority; + } + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n)) + return -ENOMEM; + + return 0; +} + +int bus_execute_append_affinity(Manager *m, DBusMessageIter *i, const char *property, void *data) { + ExecContext *c = data; + dbus_bool_t b; + DBusMessageIter sub; + + assert(m); + assert(i); + assert(property); + assert(c); + + if (!(dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "y", &sub))) + return -ENOMEM; + + if (c->cpuset) + b = dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &c->cpuset, CPU_ALLOC_SIZE(c->cpuset_ncpus)); + else + b = dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &c->cpuset, 0); + + if (!b) + return -ENOMEM; + + if (!dbus_message_iter_close_container(i, &sub)) + return -ENOMEM; + + return 0; +} + +int bus_execute_append_timer_slack_ns(Manager *m, DBusMessageIter *i, const char *property, void *data) { + ExecContext *c = data; + uint64_t u; + + assert(m); + assert(i); + assert(property); + assert(c); + + if (c->timer_slack_ns_set) + u = (uint64_t) c->timer_slack_ns_set; + else + u = (uint64_t) prctl(PR_GET_TIMERSLACK); + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u)) + return -ENOMEM; + + return 0; +} + +int bus_execute_append_capabilities(Manager *m, DBusMessageIter *i, const char *property, void *data) { + ExecContext *c = data; + char *t = NULL; + const char *s; + dbus_bool_t b; + + assert(m); + assert(i); + assert(property); + assert(c); + + if (c->capabilities) + s = t = cap_to_text(c->capabilities, NULL); + else + s = ""; + + if (!t) + return -ENOMEM; + + b = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &s); + cap_free(t); + + if (!b) + return -ENOMEM; + + return 0; +} + +int bus_execute_append_rlimits(Manager *m, DBusMessageIter *i, const char *property, void *data) { + ExecContext *c = data; + int r; + uint64_t u; + + assert(m); + assert(i); + assert(property); + assert(c); + + assert_se((r = rlimit_from_string(property)) >= 0); + + if (c->rlimit[r]) + u = (uint64_t) c->rlimit[r]->rlim_max; + else { + struct rlimit rl; + + zero(rl); + getrlimit(r, &rl); + + u = (uint64_t) rl.rlim_max; + } + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u)) + return -ENOMEM; + + return 0; +} diff --git a/src/dbus-execute.h b/src/dbus-execute.h index 1e83caca5a..f6cca9c493 100644 --- a/src/dbus-execute.h +++ b/src/dbus-execute.h @@ -29,8 +29,31 @@ #define BUS_EXEC_CONTEXT_INTERFACE \ " \n" \ " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ " \n" \ " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -39,27 +62,50 @@ " \n" \ " \n" \ " \n" \ + " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ " \n" \ " \n" \ " \n" \ - " \n" + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ #define BUS_EXEC_CONTEXT_PROPERTIES(interface, context) \ { interface, "Environment", bus_property_append_strv, "as", (context).environment }, \ { interface, "UMask", bus_property_append_mode, "u", &(context).umask }, \ - /* RLimits */ \ + { interface, "LimitCPU", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitFSIZE", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitDATA", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitSTACK", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitCORE", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitRSS", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitNOFILE", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitAS", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitNPROC", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitMEMLOCK", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitLOCKS", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitSIGPENDING", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitMSGQUEUE", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitNICE", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitRTPRIO", bus_execute_append_rlimits, "t", &(context) }, \ + { interface, "LimitRTTIME", bus_execute_append_rlimits, "t", &(context) }, \ { interface, "WorkingDirectory", bus_property_append_string, "s", (context).working_directory }, \ { interface, "RootDirectory", bus_property_append_string, "s", (context).root_directory }, \ - /* OOM Adjust */ \ - /* Nice */ \ - /* IOPrio */ \ - /* CPUSchedPolicy */ \ - /* CPUSchedPriority */ \ - /* CPUAffinity */ \ - /* TimerSlackNS */ \ + { interface, "OOMAdjust", bus_execute_append_oom_adjust, "i", &(context) }, \ + { interface, "Nice", bus_execute_append_nice, "i", &(context) }, \ + { interface, "IOScheduling", bus_execute_append_ioprio, "i", &(context) }, \ + { interface, "CPUSchedulingPolicy", bus_execute_append_cpu_sched_policy, "i", &(context) }, \ + { interface, "CPUSchedulingPriority", bus_execute_append_cpu_sched_priority, "i", &(context) }, \ + { interface, "CPUAffinity", bus_execute_append_affinity,"ay", &(context) }, \ + { interface, "TimerSlackNS", bus_execute_append_timer_slack_ns, "t", &(context) }, \ { interface, "CPUSchedulingResetOnFork", bus_property_append_bool, "b", &(context).cpu_sched_reset_on_fork }, \ { interface, "NonBlocking", bus_property_append_bool, "b", &(context).non_blocking }, \ { interface, "StandardInput", bus_execute_append_input, "s", &(context).std_input }, \ @@ -68,16 +114,32 @@ { interface, "TTYPath", bus_property_append_string, "s", (context).tty_path }, \ { interface, "SyslogPriority", bus_property_append_int, "i", &(context).syslog_priority }, \ { interface, "SyslogIdentifier", bus_property_append_string, "s", (context).syslog_identifier }, \ - /* CAPABILITIES */ \ + { interface, "SyslogNoPrefix", bus_property_append_bool, "b", &(context).syslog_no_prefix }, \ + { interface, "Capabilities", bus_property_append_string, "s", (context).capabilities }, \ { interface, "SecureBits", bus_property_append_int, "i", &(context).secure_bits }, \ { interface, "CapabilityBoundingSetDrop", bus_property_append_uint64, "t", &(context).capability_bounding_set_drop }, \ { interface, "User", bus_property_append_string, "s", (context).user }, \ { interface, "Group", bus_property_append_string, "s", (context).group }, \ { interface, "SupplementaryGroups", bus_property_append_strv, "as", (context).supplementary_groups }, \ { interface, "TCPWrapName", bus_property_append_string, "s", (context).tcpwrap_name }, \ - { interface, "PAMName", bus_property_append_string, "s", (context).pam_name } + { interface, "PAMName", bus_property_append_string, "s", (context).pam_name }, \ + { interface, "ReadWriteDirectories", bus_property_append_strv, "as", (context).read_write_dirs }, \ + { interface, "ReadOnlyDirectories", bus_property_append_strv, "as", (context).read_only_dirs }, \ + { interface, "InaccessibleDirectories", bus_property_append_strv, "as", (context).inaccessible_dirs }, \ + { interface, "MountFlags", bus_property_append_ul, "t", &(context).mount_flags }, \ + { interface, "PrivateTmp", bus_property_append_bool, "b", &(context).private_tmp }, \ + { interface, "NoSetSID", bus_property_append_bool, "b", &(context).no_setsid } int bus_execute_append_output(Manager *m, DBusMessageIter *i, const char *property, void *data); int bus_execute_append_input(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_oom_adjust(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_nice(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_ioprio(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_cpu_sched_policy(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_cpu_sched_priority(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_affinity(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_timer_slack_ns(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_capabilities(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_rlimits(Manager *m, DBusMessageIter *i, const char *property, void *data); #endif diff --git a/src/dbus.c b/src/dbus.c index 3a6d79f3d9..405ea46cda 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -1473,6 +1473,22 @@ int bus_property_append_size(Manager *m, DBusMessageIter *i, const char *propert return 0; } +int bus_property_append_ul(Manager *m, DBusMessageIter *i, const char *property, void *data) { + uint64_t u; + + assert(m); + assert(i); + assert(property); + assert(data); + + u = (uint64_t) *(unsigned long*) data; + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u)) + return -ENOMEM; + + return 0; +} + int bus_parse_strv(DBusMessage *m, char ***_l) { DBusMessageIter iter, sub; unsigned n = 0, i = 0; diff --git a/src/dbus.h b/src/dbus.h index 91132fd93c..ccee74f99f 100644 --- a/src/dbus.h +++ b/src/dbus.h @@ -78,6 +78,7 @@ int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *proper int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data); int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data); int bus_property_append_size(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_property_append_ul(Manager *m, DBusMessageIter *i, const char *property, void *data); #define bus_property_append_int bus_property_append_int32 #define bus_property_append_pid bus_property_append_uint32 diff --git a/src/execute.c b/src/execute.c index 9ded1c7788..0bdd60000b 100644 --- a/src/execute.c +++ b/src/execute.c @@ -1071,8 +1071,8 @@ int exec_spawn(ExecCommand *command, } } - if (context->cpu_affinity_set) - if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) { + if (context->cpuset) + if (sched_setaffinity(0, CPU_ALLOC_SIZE(context->cpuset_ncpus), context->cpuset) < 0) { r = EXIT_CPUAFFINITY; goto fail; } @@ -1350,6 +1350,9 @@ void exec_context_done(ExecContext *c) { strv_free(c->inaccessible_dirs); c->inaccessible_dirs = NULL; + + if (c->cpuset) + CPU_FREE(c->cpuset); } void exec_command_done(ExecCommand *c) { @@ -1458,10 +1461,10 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { prefix, c->cpu_sched_priority, prefix, yes_no(c->cpu_sched_reset_on_fork)); - if (c->cpu_affinity_set) { + if (c->cpuset) { fprintf(f, "%sCPUAffinity:", prefix); - for (i = 0; i < CPU_SETSIZE; i++) - if (CPU_ISSET(i, &c->cpu_affinity)) + for (i = 0; i < c->cpuset_ncpus; i++) + if (CPU_ISSET_S(i, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset)) fprintf(f, " %i", i); fputs("\n", f); } diff --git a/src/execute.h b/src/execute.h index 841670aa6d..9fb48e6446 100644 --- a/src/execute.h +++ b/src/execute.h @@ -93,7 +93,8 @@ struct ExecContext { int cpu_sched_policy; int cpu_sched_priority; - cpu_set_t cpu_affinity; + cpu_set_t *cpuset; + unsigned cpuset_ncpus; unsigned long timer_slack_ns; ExecInput std_input; @@ -134,7 +135,6 @@ struct ExecContext { bool nice_set:1; bool ioprio_set:1; bool cpu_sched_set:1; - bool cpu_affinity_set:1; bool timer_slack_ns_set:1; /* This is not exposed to the user but available @@ -142,7 +142,7 @@ struct ExecContext { * /bin/mount it is run in the same process group as us so * that the autofs logic detects that it belongs to us and we * don't enter a trigger loop. */ - bool no_setsid:1; + bool no_setsid; }; typedef enum ExitStatus { diff --git a/src/load-fragment.c b/src/load-fragment.c index e5c7ba24ab..a5ea0e41b0 100644 --- a/src/load-fragment.c +++ b/src/load-fragment.c @@ -725,19 +725,21 @@ static int config_parse_cpu_affinity( if (!(t = strndup(w, l))) return -ENOMEM; + if (!(c->cpuset)) + if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus))) + return -ENOMEM; + r = safe_atou(t, &cpu); free(t); - if (r < 0 || cpu >= CPU_SETSIZE) { + if (r < 0 || cpu >= c->cpuset_ncpus) { log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue); return -EBADMSG; } - CPU_SET(cpu, &c->cpu_affinity); + CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset); } - c->cpu_affinity_set = true; - return 0; } diff --git a/src/missing.h b/src/missing.h index 3b4cb9cdeb..602d44a979 100644 --- a/src/missing.h +++ b/src/missing.h @@ -27,6 +27,7 @@ #include #include #include +#include #ifndef RLIMIT_RTTIME #define RLIMIT_RTTIME 15 diff --git a/src/systemctl.c b/src/systemctl.c index 66d6ef038b..392ecf2c92 100644 --- a/src/systemctl.c +++ b/src/systemctl.c @@ -1111,7 +1111,29 @@ static int print_property(const char *name, DBusMessageIter *iter) { } return 0; + } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) { + DBusMessageIter sub; + + dbus_message_iter_recurse(iter, &sub); + + if (arg_all || + dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { + printf("%s=", name); + + while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { + uint8_t u; + assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE); + dbus_message_iter_get_basic(&sub, &u); + printf("%02x", u); + + dbus_message_iter_next(&sub); + } + + puts(""); + } + + return 0; } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) { DBusMessageIter sub, sub2; diff --git a/src/util.c b/src/util.c index b66eb466b6..ea8bfc1984 100644 --- a/src/util.c +++ b/src/util.c @@ -2538,6 +2538,34 @@ int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { return 0; } +cpu_set_t* cpu_set_malloc(unsigned *ncpus) { + cpu_set_t *r; + unsigned n = 1024; + + /* Allocates the cpuset in the right size */ + + for (;;) { + if (!(r = CPU_ALLOC(n))) + return NULL; + + if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) { + CPU_ZERO_S(CPU_ALLOC_SIZE(n), r); + + if (ncpus) + *ncpus = n; + + return r; + } + + CPU_FREE(r); + + if (errno != EINVAL) + return NULL; + + n *= 2; + } +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime", diff --git a/src/util.h b/src/util.h index eda8e5c406..50bac6edf8 100644 --- a/src/util.h +++ b/src/util.h @@ -29,6 +29,7 @@ #include #include #include +#include #include "macro.h" @@ -284,6 +285,8 @@ int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid); int rm_rf(const char *path, bool only_dirs, bool delete_root); +cpu_set_t* cpu_set_malloc(unsigned *ncpus); + const char *ioprio_class_to_string(int i); int ioprio_class_from_string(const char *s); -- cgit v1.2.3-54-g00ecf