From 14068e17f32a06e6a1f8f72416018bc653b8ea2d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 11 Oct 2017 14:02:36 +0200 Subject: [PATCH 1/3] core: add support for expanding state/cache/log directory root in unit files This augments %t which already resolves to the runtime directory root, and should be useful for units that want to pass any of these paths in command line arguments. Example: ExecStart=/usr/bin/mydaemon --datadir=%S/mydaemon Why not expose a specifier resolving directly to the configured state/runtime/cache/log dir? Three reasons: 1. Specifiers should be independent of configuration of the unit itself, and StateDirectory= and friends are unit configuration. See 03fc9c723cfc59467a7fccc305f34273f8564b25 and related work. 2. We permit multiple StateDirectory= values per unit, and it hence wouldn't be clear which one is passed. 3. We already have %t for the runtime directory root, and we should continue with the same scheme. --- man/systemd.unit.xml | 17 ++++++++++++++++- src/core/unit-printf.c | 16 ++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml index 043c4ed16e..ab7613dcc4 100644 --- a/man/systemd.unit.xml +++ b/man/systemd.unit.xml @@ -1272,10 +1272,25 @@ %t - Runtime directory + Runtime directory root This is either /run (for the system manager) or the path $XDG_RUNTIME_DIR resolves to (for user managers). + %S + State directory root + This is either /var/lib (for the system manager) or the path $XDG_CONFIG_HOME resolves to (for user managers). + + + %C + Cache directory root + This is either /var/cache (for the system manager) or the path $XDG_CACHE_HOME resolves to (for user managers). + + + %L + Logs directory root + This is either /var/log (for the system manager) or the path $XDG_CONFIG_HOME resolves to with /log appended (for user managers). + + %u User name This is the name of the user running the service manager instance. In case of the system manager this resolves to root. diff --git a/src/core/unit-printf.c b/src/core/unit-printf.c index 0480ec6526..8088d016a3 100644 --- a/src/core/unit-printf.c +++ b/src/core/unit-printf.c @@ -143,13 +143,13 @@ static int specifier_cgroup_slice(char specifier, void *data, void *userdata, ch return 0; } -static int specifier_runtime(char specifier, void *data, void *userdata, char **ret) { +static int specifier_special_directory(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; char *n = NULL; assert(u); - n = strdup(u->manager->prefix[EXEC_DIRECTORY_RUNTIME]); + n = strdup(u->manager->prefix[PTR_TO_UINT(data)]); if (!n) return -ENOMEM; @@ -244,10 +244,15 @@ int unit_full_printf(Unit *u, const char *format, char **ret) { * (which are likely not suitable for unescaped inclusion in unit names): * * %f: the unescaped instance if set, otherwise the id unescaped as path + * * %c: cgroup path of unit (deprecated) * %r: where units in this slice are placed in the cgroup tree (deprecated) * %R: the root of this systemd's instance tree (deprecated) - * %t: the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR) + * + * %t: the runtime directory root (e.g. /run or $XDG_RUNTIME_DIR) + * %S: the state directory root (e.g. /var/lib or $XDG_CONFIG_HOME) + * %C: the cache directory root (e.g. /var/cache or $XDG_CACHE_HOME) + * %L: the log directory root (e.g. /var/log or $XDG_CONFIG_HOME/log) * * %h: the homedir of the running user * %s: the shell of the running user @@ -271,7 +276,10 @@ int unit_full_printf(Unit *u, const char *format, char **ret) { { 'c', specifier_cgroup, NULL }, { 'r', specifier_cgroup_slice, NULL }, { 'R', specifier_cgroup_root, NULL }, - { 't', specifier_runtime, NULL }, + { 't', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_RUNTIME) }, + { 'S', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_STATE) }, + { 'C', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_CACHE) }, + { 'L', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_LOGS) }, { 'U', specifier_user_id, NULL }, { 'u', specifier_user_name, NULL }, From 9672b5839875b058dedd16c3dbaf437cff37cea6 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 11 Oct 2017 14:07:51 +0200 Subject: [PATCH 2/3] test: add simple test for validating some of the unit specifiers we support (Also, sort list of test unit files in meson.build alphabetically, to make future additions more systematic) --- src/test/test-execute.c | 5 + test/meson.build | 211 ++++++++++++----------- test/test-execute/exec-specifier.service | 15 ++ 3 files changed, 126 insertions(+), 105 deletions(-) create mode 100644 test/test-execute/exec-specifier.service diff --git a/src/test/test-execute.c b/src/test/test-execute.c index 7e36900ce8..7905b5db9d 100644 --- a/src/test/test-execute.c +++ b/src/test/test-execute.c @@ -468,6 +468,10 @@ static void test_exec_unset_environment(Manager *m) { test(m, "exec-unset-environment.service", 0, CLD_EXITED); } +static void test_exec_specifier(Manager *m) { + test(m, "exec-specifier.service", 0, CLD_EXITED); +} + static int run_tests(UnitFileScope scope, const test_function_t *tests) { const test_function_t *test = NULL; Manager *m = NULL; @@ -529,6 +533,7 @@ int main(int argc, char *argv[]) { static const test_function_t system_tests[] = { test_exec_systemcall_system_mode_with_user, test_exec_dynamic_user, + test_exec_specifier, NULL, }; int r; diff --git a/test/meson.build b/test/meson.build index 2b523da725..5e98ec6e50 100644 --- a/test/meson.build +++ b/test/meson.build @@ -1,18 +1,21 @@ test_data_files = ''' a.service - basic.target b.service + basic.target c.service - daughter.service d.service - end.service + daughter.service e.service + end.service f.service - grandchild.service g.service + grandchild.service + h.service hello-after-sleep.target hello.service - h.service + hwdb/10-bad.hwdb + journal-data/journal-1.txt + journal-data/journal-2.txt parent-deep.slice parent.slice sched_idle_bad.service @@ -25,113 +28,111 @@ test_data_files = ''' sockets.target son.service sysinit.target - testsuite.target - timers.target - unstoppable.service - test-path/paths.target - test-path/basic.target - test-path/sysinit.target - test-path/path-changed.service - test-path/path-directorynotempty.service - test-path/path-existsglob.service - test-path/path-exists.service - test-path/path-makedirectory.service - test-path/path-modified.service - test-path/path-mycustomunit.service - test-path/path-service.service - test-path/path-changed.path - test-path/path-directorynotempty.path - test-path/path-existsglob.path - test-path/path-exists.path - test-path/path-makedirectory.path - test-path/path-modified.path - test-path/path-unit.path test-execute/exec-bind-paths.service - test-execute/exec-environment-empty.service - test-execute/exec-environment-multiple.service - test-execute/exec-environment.service - test-execute/exec-passenvironment-absent.service - test-execute/exec-passenvironment-empty.service - test-execute/exec-passenvironment-repeated.service - test-execute/exec-passenvironment.service - test-execute/exec-group.service - test-execute/exec-group-nfsnobody.service - test-execute/exec-supplementarygroups.service - test-execute/exec-supplementarygroups-single-group.service - test-execute/exec-supplementarygroups-single-group-user.service - test-execute/exec-supplementarygroups-multiple-groups-default-group-user.service - test-execute/exec-supplementarygroups-multiple-groups-withgid.service - test-execute/exec-supplementarygroups-multiple-groups-withuid.service - test-execute/exec-dynamicuser-fixeduser.service - test-execute/exec-dynamicuser-fixeduser-one-supplementarygroup.service - test-execute/exec-dynamicuser-supplementarygroups.service - test-execute/exec-dynamicuser-state-dir.service - test-execute/exec-ignoresigpipe-no.service - test-execute/exec-ignoresigpipe-yes.service - test-execute/exec-personality-x86-64.service - test-execute/exec-personality-x86.service - test-execute/exec-personality-s390.service - test-execute/exec-personality-ppc64.service - test-execute/exec-personality-ppc64le.service - test-execute/exec-personality-aarch64.service - test-execute/exec-privatedevices-no.service - test-execute/exec-privatedevices-yes.service - test-execute/exec-privatedevices-no-capability-mknod.service - test-execute/exec-privatedevices-yes-capability-mknod.service - test-execute/exec-protectkernelmodules-no-capabilities.service - test-execute/exec-protectkernelmodules-yes-capabilities.service - test-execute/exec-protectkernelmodules-yes-mount-propagation.service - test-execute/exec-privatetmp-no.service - test-execute/exec-privatetmp-yes.service - test-execute/exec-readonlypaths.service - test-execute/exec-readonlypaths-mount-propagation.service - test-execute/exec-readwritepaths-mount-propagation.service - test-execute/exec-inaccessiblepaths-mount-propagation.service - test-execute/exec-inaccessiblepaths-proc.service - test-execute/exec-spec-interpolation.service - test-execute/exec-systemcallerrornumber.service - test-execute/exec-systemcallfilter-failing2.service - test-execute/exec-systemcallfilter-failing.service - test-execute/exec-systemcallfilter-not-failing2.service - test-execute/exec-systemcallfilter-not-failing.service - test-execute/exec-systemcallfilter-system-user.service - test-execute/exec-systemcallfilter-system-user-nfsnobody.service - test-execute/exec-unset-environment.service - test-execute/exec-user.service - test-execute/exec-user-nfsnobody.service - test-execute/exec-workingdirectory.service - test-execute/exec-umask-0177.service - test-execute/exec-umask-default.service - test-execute/exec-privatenetwork-yes.service - test-execute/exec-environmentfile.service - test-execute/exec-oomscoreadjust-positive.service - test-execute/exec-oomscoreadjust-negative.service - test-execute/exec-ioschedulingclass-best-effort.service - test-execute/exec-ioschedulingclass-idle.service - test-execute/exec-ioschedulingclass-none.service - test-execute/exec-ioschedulingclass-realtime.service + test-execute/exec-capabilityambientset-merge-nfsnobody.service + test-execute/exec-capabilityambientset-merge.service + test-execute/exec-capabilityambientset-nfsnobody.service + test-execute/exec-capabilityambientset.service test-execute/exec-capabilityboundingset-invert.service test-execute/exec-capabilityboundingset-merge.service test-execute/exec-capabilityboundingset-reset.service test-execute/exec-capabilityboundingset-simple.service - test-execute/exec-capabilityambientset.service - test-execute/exec-capabilityambientset-nfsnobody.service - test-execute/exec-capabilityambientset-merge.service - test-execute/exec-capabilityambientset-merge-nfsnobody.service - test-execute/exec-runtimedirectory.service - test-execute/exec-runtimedirectory-mode.service - test-execute/exec-runtimedirectory-owner.service - test-execute/exec-runtimedirectory-owner-nfsnobody.service + test-execute/exec-dynamicuser-fixeduser-one-supplementarygroup.service + test-execute/exec-dynamicuser-fixeduser.service + test-execute/exec-dynamicuser-state-dir.service + test-execute/exec-dynamicuser-supplementarygroups.service + test-execute/exec-environment-empty.service + test-execute/exec-environment-multiple.service + test-execute/exec-environment.service + test-execute/exec-environmentfile.service + test-execute/exec-group-nfsnobody.service + test-execute/exec-group.service + test-execute/exec-ignoresigpipe-no.service + test-execute/exec-ignoresigpipe-yes.service + test-execute/exec-inaccessiblepaths-mount-propagation.service + test-execute/exec-inaccessiblepaths-proc.service + test-execute/exec-ioschedulingclass-best-effort.service + test-execute/exec-ioschedulingclass-idle.service + test-execute/exec-ioschedulingclass-none.service + test-execute/exec-ioschedulingclass-realtime.service + test-execute/exec-oomscoreadjust-negative.service + test-execute/exec-oomscoreadjust-positive.service + test-execute/exec-passenvironment-absent.service + test-execute/exec-passenvironment-empty.service + test-execute/exec-passenvironment-repeated.service + test-execute/exec-passenvironment.service + test-execute/exec-personality-aarch64.service + test-execute/exec-personality-ppc64.service + test-execute/exec-personality-ppc64le.service + test-execute/exec-personality-s390.service + test-execute/exec-personality-x86-64.service + test-execute/exec-personality-x86.service + test-execute/exec-privatedevices-no-capability-mknod.service + test-execute/exec-privatedevices-no-capability-sys-rawio.service + test-execute/exec-privatedevices-no.service + test-execute/exec-privatedevices-yes-capability-mknod.service + test-execute/exec-privatedevices-yes-capability-sys-rawio.service + test-execute/exec-privatedevices-yes.service + test-execute/exec-privatenetwork-yes.service + test-execute/exec-privatetmp-no.service + test-execute/exec-privatetmp-yes.service + test-execute/exec-protectkernelmodules-no-capabilities.service + test-execute/exec-protectkernelmodules-yes-capabilities.service + test-execute/exec-protectkernelmodules-yes-mount-propagation.service + test-execute/exec-read-only-path-succeed.service + test-execute/exec-readonlypaths-mount-propagation.service + test-execute/exec-readonlypaths.service + test-execute/exec-readwritepaths-mount-propagation.service + test-execute/exec-restrict-namespaces-mnt-blacklist.service + test-execute/exec-restrict-namespaces-mnt.service test-execute/exec-restrict-namespaces-no.service test-execute/exec-restrict-namespaces-yes.service - test-execute/exec-restrict-namespaces-mnt.service - test-execute/exec-restrict-namespaces-mnt-blacklist.service - test-execute/exec-read-only-path-succeed.service - test-execute/exec-privatedevices-yes-capability-sys-rawio.service - test-execute/exec-privatedevices-no-capability-sys-rawio.service - hwdb/10-bad.hwdb - journal-data/journal-1.txt - journal-data/journal-2.txt + test-execute/exec-runtimedirectory-mode.service + test-execute/exec-runtimedirectory-owner-nfsnobody.service + test-execute/exec-runtimedirectory-owner.service + test-execute/exec-runtimedirectory.service + test-execute/exec-spec-interpolation.service + test-execute/exec-specifier.service + test-execute/exec-supplementarygroups-multiple-groups-default-group-user.service + test-execute/exec-supplementarygroups-multiple-groups-withgid.service + test-execute/exec-supplementarygroups-multiple-groups-withuid.service + test-execute/exec-supplementarygroups-single-group-user.service + test-execute/exec-supplementarygroups-single-group.service + test-execute/exec-supplementarygroups.service + test-execute/exec-systemcallerrornumber.service + test-execute/exec-systemcallfilter-failing.service + test-execute/exec-systemcallfilter-failing2.service + test-execute/exec-systemcallfilter-not-failing.service + test-execute/exec-systemcallfilter-not-failing2.service + test-execute/exec-systemcallfilter-system-user-nfsnobody.service + test-execute/exec-systemcallfilter-system-user.service + test-execute/exec-umask-0177.service + test-execute/exec-umask-default.service + test-execute/exec-unset-environment.service + test-execute/exec-user-nfsnobody.service + test-execute/exec-user.service + test-execute/exec-workingdirectory.service + test-path/basic.target + test-path/path-changed.path + test-path/path-changed.service + test-path/path-directorynotempty.path + test-path/path-directorynotempty.service + test-path/path-exists.path + test-path/path-exists.service + test-path/path-existsglob.path + test-path/path-existsglob.service + test-path/path-makedirectory.path + test-path/path-makedirectory.service + test-path/path-modified.path + test-path/path-modified.service + test-path/path-mycustomunit.service + test-path/path-service.service + test-path/path-unit.path + test-path/paths.target + test-path/sysinit.target + testsuite.target + timers.target + unstoppable.service '''.split() if conf.get('ENABLE_RESOLVE') == 1 diff --git a/test/test-execute/exec-specifier.service b/test/test-execute/exec-specifier.service new file mode 100644 index 0000000000..228d7837ad --- /dev/null +++ b/test/test-execute/exec-specifier.service @@ -0,0 +1,15 @@ +[Service] +Type=oneshot +ExecStart=/usr/bin/test %n = exec-specifier.service +ExecStart=/usr/bin/test %N = exec-specifier +ExecStart=/usr/bin/test %p = exec-specifier +ExecStart=/usr/bin/test %P = exec/specifier +ExecStart=/usr/bin/test %f = /exec/specifier +ExecStart=/usr/bin/test %t = /run +ExecStart=/usr/bin/test %S = /var/lib +ExecStart=/usr/bin/test %C = /var/cache +ExecStart=/usr/bin/test %L = /var/log +ExecStart=/usr/bin/test %U = 0 + +# We don't test the other specifiers here, since they migh resolve to different +# things in different test environments From 1c8ac41c65c39fa8445b1aa2e0008b3a92edcc30 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 11 Oct 2017 14:09:10 +0200 Subject: [PATCH 3/3] update TODO --- TODO | 4 ---- 1 file changed, 4 deletions(-) diff --git a/TODO b/TODO index e1771c802f..2072f1e2da 100644 --- a/TODO +++ b/TODO @@ -44,10 +44,6 @@ Features: taken if multiple dirs are configured. Maybe avoid setting the env vars in that case? -* In a similar vein, consider adding unit specifiers that resolve to the root - directory used for state, logs, cache and configuration - directory. i.e. similar to %t, but for the root of the other special dirs. - * expose IO accounting data on the bus, show it in systemd-run --wait and log about it in the resource log message