From e85db9f7972449d1ebfb5d5c61fb2f7744c49358 Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 13 Sep 2023 00:46:01 +0200 Subject: [PATCH] Default to service dir as working directory for services --- src/baseproc-service.cc | 2 +- src/igr-tests/basic/sd/basic | 2 +- src/igr-tests/before-after/sd/service1 | 2 +- src/igr-tests/before-after/sd/service2 | 2 +- src/igr-tests/before-after/sd/service3 | 2 +- src/igr-tests/before-after2/sd/service1 | 2 +- src/igr-tests/before-after2/sd/service2 | 2 +- src/igr-tests/catlog/sd/output | 2 +- src/igr-tests/chain-to/sd/part1 | 2 +- src/igr-tests/chain-to/sd/part2 | 2 +- src/igr-tests/chain-to/sd/part3 | 2 +- src/igr-tests/chain-to/sd/part4 | 2 +- src/igr-tests/chain-to/sd/part5 | 2 +- src/igr-tests/environ/sd/checkenv | 2 +- src/igr-tests/environ/sd/setenv1 | 2 +- src/igr-tests/environ/sd/setenv2 | 2 +- src/igr-tests/environ/sd/setenv3 | 2 +- src/igr-tests/environ2/sd/checkenv | 2 +- src/igr-tests/log-via-pipe/sd/consumer | 2 +- src/igr-tests/log-via-pipe/sd/producer | 2 +- src/igr-tests/ps-environ/sd/checkenv1 | 2 +- src/igr-tests/ps-environ/sd/checkenv2 | 2 +- src/igr-tests/ps-environ/sd/checkenv4 | 2 +- src/igr-tests/pseudo-cycle/sd/service | 2 +- src/igr-tests/restart/sd/basic | 2 +- src/igr-tests/svc-start-fail/sd/timeout-command | 2 +- src/igr-tests/var-subst/sd/checkargs | 2 +- src/includes/proc-service.h | 7 +++++-- src/load-service.cc | 6 +++--- src/proc-service.cc | 2 +- 30 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/baseproc-service.cc b/src/baseproc-service.cc index aace4434..7fa5d420 100644 --- a/src/baseproc-service.cc +++ b/src/baseproc-service.cc @@ -242,7 +242,7 @@ bool base_process_service::start_ps_process(const std::vector &cmd } if (forkpid == 0) { - const char * working_dir_c = nullptr; + const char * working_dir_c = working_dir_def; if (!working_dir.empty()) working_dir_c = working_dir.c_str(); after_fork(getpid()); run_proc_params run_params{cmd.data(), working_dir_c, logfile, pipefd[1], run_as_uid, run_as_gid, rlimits}; diff --git a/src/igr-tests/basic/sd/basic b/src/igr-tests/basic/sd/basic index 1a46ea97..7fd47c6c 100644 --- a/src/igr-tests/basic/sd/basic +++ b/src/igr-tests/basic/sd/basic @@ -1,3 +1,3 @@ type = process -command = ./basic.sh +command = ../basic.sh restart = false diff --git a/src/igr-tests/before-after/sd/service1 b/src/igr-tests/before-after/sd/service1 index 5ae9130c..ed386132 100644 --- a/src/igr-tests/before-after/sd/service1 +++ b/src/igr-tests/before-after/sd/service1 @@ -1,3 +1,3 @@ type = scripted -command = scripts/service.sh one 0.5 +command = ../scripts/service.sh one 0.5 before = service2 diff --git a/src/igr-tests/before-after/sd/service2 b/src/igr-tests/before-after/sd/service2 index b3e5b82d..8bef8fa2 100644 --- a/src/igr-tests/before-after/sd/service2 +++ b/src/igr-tests/before-after/sd/service2 @@ -1,2 +1,2 @@ type = scripted -command = scripts/service.sh two 0.2 +command = ../scripts/service.sh two 0.2 diff --git a/src/igr-tests/before-after/sd/service3 b/src/igr-tests/before-after/sd/service3 index aa9066d4..52af7c56 100644 --- a/src/igr-tests/before-after/sd/service3 +++ b/src/igr-tests/before-after/sd/service3 @@ -1,3 +1,3 @@ type = scripted -command = scripts/service.sh three 0.1 +command = ../scripts/service.sh three 0.1 after = service2 diff --git a/src/igr-tests/before-after2/sd/service1 b/src/igr-tests/before-after2/sd/service1 index 5ae9130c..ed386132 100644 --- a/src/igr-tests/before-after2/sd/service1 +++ b/src/igr-tests/before-after2/sd/service1 @@ -1,3 +1,3 @@ type = scripted -command = scripts/service.sh one 0.5 +command = ../scripts/service.sh one 0.5 before = service2 diff --git a/src/igr-tests/before-after2/sd/service2 b/src/igr-tests/before-after2/sd/service2 index 78b68535..affed033 100644 --- a/src/igr-tests/before-after2/sd/service2 +++ b/src/igr-tests/before-after2/sd/service2 @@ -1,3 +1,3 @@ type = scripted -command = scripts/service.sh two 0.2 +command = ../scripts/service.sh two 0.2 depends-on = service1 diff --git a/src/igr-tests/catlog/sd/output b/src/igr-tests/catlog/sd/output index 93f53db2..c561cbb4 100644 --- a/src/igr-tests/catlog/sd/output +++ b/src/igr-tests/catlog/sd/output @@ -1,4 +1,4 @@ type = scripted -command = ./output.sh +command = ../output.sh restart = false log-type = buffer diff --git a/src/igr-tests/chain-to/sd/part1 b/src/igr-tests/chain-to/sd/part1 index 6d14e6eb..55573101 100644 --- a/src/igr-tests/chain-to/sd/part1 +++ b/src/igr-tests/chain-to/sd/part1 @@ -1,4 +1,4 @@ type = process -command = ./record-param.sh part1 +command = ../record-param.sh part1 chain-to = part2 restart = false diff --git a/src/igr-tests/chain-to/sd/part2 b/src/igr-tests/chain-to/sd/part2 index a2ac8b35..5b5c8e80 100644 --- a/src/igr-tests/chain-to/sd/part2 +++ b/src/igr-tests/chain-to/sd/part2 @@ -1,4 +1,4 @@ type = process -command = ./record-param.sh part2 +command = ../record-param.sh part2 chain-to = part3 restart = false diff --git a/src/igr-tests/chain-to/sd/part3 b/src/igr-tests/chain-to/sd/part3 index 1ae00ef5..4a8d91b8 100644 --- a/src/igr-tests/chain-to/sd/part3 +++ b/src/igr-tests/chain-to/sd/part3 @@ -1,5 +1,5 @@ type = process options = always-chain -command = ./record-param.sh part3 +command = ../record-param.sh part3 chain-to = part4 restart = false diff --git a/src/igr-tests/chain-to/sd/part4 b/src/igr-tests/chain-to/sd/part4 index b7fcef4c..6d0a9d65 100644 --- a/src/igr-tests/chain-to/sd/part4 +++ b/src/igr-tests/chain-to/sd/part4 @@ -1,5 +1,5 @@ type = process #options = always-chain -command = ./record-param.sh part4 +command = ../record-param.sh part4 chain-to = part5 restart = false diff --git a/src/igr-tests/chain-to/sd/part5 b/src/igr-tests/chain-to/sd/part5 index 4cb8c936..954219ed 100644 --- a/src/igr-tests/chain-to/sd/part5 +++ b/src/igr-tests/chain-to/sd/part5 @@ -1,3 +1,3 @@ type = process -command = ./record-param.sh part5 +command = ../record-param.sh part5 restart = false diff --git a/src/igr-tests/environ/sd/checkenv b/src/igr-tests/environ/sd/checkenv index c91502f1..d63c8113 100644 --- a/src/igr-tests/environ/sd/checkenv +++ b/src/igr-tests/environ/sd/checkenv @@ -1,4 +1,4 @@ type = process -command = ./checkenv.sh $TEST_VAR_ONE $DINIT_SERVICE +command = ../checkenv.sh $TEST_VAR_ONE $DINIT_SERVICE restart = false load-options = export-service-name diff --git a/src/igr-tests/environ/sd/setenv1 b/src/igr-tests/environ/sd/setenv1 index 61a930b1..d75689be 100644 --- a/src/igr-tests/environ/sd/setenv1 +++ b/src/igr-tests/environ/sd/setenv1 @@ -1,4 +1,4 @@ type = process -command = ./setenv.sh setenv1 +command = ../setenv.sh setenv1 depends-on = setenv2 restart = false diff --git a/src/igr-tests/environ/sd/setenv2 b/src/igr-tests/environ/sd/setenv2 index 2bdbed6c..e30e6732 100644 --- a/src/igr-tests/environ/sd/setenv2 +++ b/src/igr-tests/environ/sd/setenv2 @@ -1,3 +1,3 @@ type = scripted -command = ./setenv.sh setenv2 +command = ../setenv.sh setenv2 depends-on = setenv3 diff --git a/src/igr-tests/environ/sd/setenv3 b/src/igr-tests/environ/sd/setenv3 index 5e27081c..cae8631f 100644 --- a/src/igr-tests/environ/sd/setenv3 +++ b/src/igr-tests/environ/sd/setenv3 @@ -1,2 +1,2 @@ type = scripted -command = ./setenv.sh setenv3 +command = ../setenv.sh setenv3 diff --git a/src/igr-tests/environ2/sd/checkenv b/src/igr-tests/environ2/sd/checkenv index 7739f1fb..10de31d4 100644 --- a/src/igr-tests/environ2/sd/checkenv +++ b/src/igr-tests/environ2/sd/checkenv @@ -1,5 +1,5 @@ type = process -command = ./checkenv.sh $TEST_VAR_ONE $TEST_VAR_BASE +command = ../checkenv.sh $TEST_VAR_ONE $TEST_VAR_BASE restart = false env-file = env-service load-options = export-passwd-vars diff --git a/src/igr-tests/log-via-pipe/sd/consumer b/src/igr-tests/log-via-pipe/sd/consumer index a1a035ae..5ccb9afd 100644 --- a/src/igr-tests/log-via-pipe/sd/consumer +++ b/src/igr-tests/log-via-pipe/sd/consumer @@ -1,3 +1,3 @@ type = process -command = scripts/consumer.sh +command = ../scripts/consumer.sh consumer-of = producer diff --git a/src/igr-tests/log-via-pipe/sd/producer b/src/igr-tests/log-via-pipe/sd/producer index 8b3a8ffc..c148842b 100644 --- a/src/igr-tests/log-via-pipe/sd/producer +++ b/src/igr-tests/log-via-pipe/sd/producer @@ -1,3 +1,3 @@ type = scripted -command = scripts/producer.sh +command = ../scripts/producer.sh log-type = pipe diff --git a/src/igr-tests/ps-environ/sd/checkenv1 b/src/igr-tests/ps-environ/sd/checkenv1 index 0a18427b..653d7890 100644 --- a/src/igr-tests/ps-environ/sd/checkenv1 +++ b/src/igr-tests/ps-environ/sd/checkenv1 @@ -1,4 +1,4 @@ type = process -command = ./checkenv.sh +command = ../checkenv.sh env-file = ./environment1 restart = false diff --git a/src/igr-tests/ps-environ/sd/checkenv2 b/src/igr-tests/ps-environ/sd/checkenv2 index fb929180..1583600d 100644 --- a/src/igr-tests/ps-environ/sd/checkenv2 +++ b/src/igr-tests/ps-environ/sd/checkenv2 @@ -1,4 +1,4 @@ type = process -command = ./checkenv.sh +command = ../checkenv.sh env-file = ./environment2 restart = false diff --git a/src/igr-tests/ps-environ/sd/checkenv4 b/src/igr-tests/ps-environ/sd/checkenv4 index a8dae800..362e7991 100644 --- a/src/igr-tests/ps-environ/sd/checkenv4 +++ b/src/igr-tests/ps-environ/sd/checkenv4 @@ -1,4 +1,4 @@ type = process -command = ./checkenv2.sh +command = ../checkenv2.sh env-file = ./environment4 restart = false diff --git a/src/igr-tests/pseudo-cycle/sd/service b/src/igr-tests/pseudo-cycle/sd/service index 867a7b92..320f236a 100644 --- a/src/igr-tests/pseudo-cycle/sd/service +++ b/src/igr-tests/pseudo-cycle/sd/service @@ -1,4 +1,4 @@ type = process -command = ./svc-script.sh +command = ../svc-script.sh restart = false before = boot diff --git a/src/igr-tests/restart/sd/basic b/src/igr-tests/restart/sd/basic index 1a46ea97..7fd47c6c 100644 --- a/src/igr-tests/restart/sd/basic +++ b/src/igr-tests/restart/sd/basic @@ -1,3 +1,3 @@ type = process -command = ./basic.sh +command = ../basic.sh restart = false diff --git a/src/igr-tests/svc-start-fail/sd/timeout-command b/src/igr-tests/svc-start-fail/sd/timeout-command index c9350681..8052e658 100644 --- a/src/igr-tests/svc-start-fail/sd/timeout-command +++ b/src/igr-tests/svc-start-fail/sd/timeout-command @@ -1,4 +1,4 @@ # timeout-command type = scripted -command = ./timeout.sh +command = ../timeout.sh start-timeout = 0.01 diff --git a/src/igr-tests/var-subst/sd/checkargs b/src/igr-tests/var-subst/sd/checkargs index a59a4cdd..c580d354 100644 --- a/src/igr-tests/var-subst/sd/checkargs +++ b/src/igr-tests/var-subst/sd/checkargs @@ -1,3 +1,3 @@ type = process -command = ./checkargs.sh xxx$TEST_VAR_ONE/yyy ${TEST_VAR_TWO}$TEST_VAR_THREE ${TEST_VAR_FOUR:-varfour} +command = ../checkargs.sh xxx$TEST_VAR_ONE/yyy ${TEST_VAR_TWO}$TEST_VAR_THREE ${TEST_VAR_FOUR:-varfour} restart = false diff --git a/src/includes/proc-service.h b/src/includes/proc-service.h index 972b82ae..032e7bc9 100644 --- a/src/includes/proc-service.h +++ b/src/includes/proc-service.h @@ -177,6 +177,7 @@ class base_process_service : public service_record string working_dir; // working directory (or empty) string env_file; // file with environment settings for this service + const char *working_dir_def; // working directory default (service dir) log_type_id log_type = log_type_id::NONE; string logfile; // log file name, empty string specifies /dev/null int logfile_perms = 0; // logfile permissions("mode") @@ -500,14 +501,16 @@ class base_process_service : public service_record } // Set the working directory - void set_working_dir(const string &working_dir_p) + void set_working_dir(const string &working_dir_p, const char *def) { working_dir = working_dir_p; + working_dir_def = def; } - void set_working_dir(string &&working_dir_p) noexcept + void set_working_dir(string &&working_dir_p, const char *def) noexcept { working_dir = std::move(working_dir_p); + working_dir_def = def; } // Set the notification fd number that the service process will use diff --git a/src/load-service.cc b/src/load-service.cc index 63982217..6a9e67da 100644 --- a/src/load-service.cc +++ b/src/load-service.cc @@ -712,7 +712,7 @@ service_record * dirload_service_set::load_reload_service(const char *name, serv rval = rvalps; // All of the following should be noexcept or must perform rollback on exception rvalps->set_stop_command(std::move(settings.stop_command), std::move(stop_arg_parts)); - rvalps->set_working_dir(std::move(settings.working_dir)); + rvalps->set_working_dir(std::move(settings.working_dir), sdir); rvalps->set_env_file(std::move(settings.env_file)); #if SUPPORT_CGROUPS rvalps->set_cgroup(std::move(settings.run_in_cgroup)); @@ -755,7 +755,7 @@ service_record * dirload_service_set::load_reload_service(const char *name, serv rval = rvalps; // All of the following should be noexcept or must perform rollback on exception rvalps->set_stop_command(std::move(settings.stop_command), std::move(stop_arg_parts)); - rvalps->set_working_dir(std::move(settings.working_dir)); + rvalps->set_working_dir(std::move(settings.working_dir), sdir); rvalps->set_env_file(std::move(settings.env_file)); #if SUPPORT_CGROUPS rvalps->set_cgroup(std::move(settings.run_in_cgroup)); @@ -794,7 +794,7 @@ service_record * dirload_service_set::load_reload_service(const char *name, serv rval = rvalps; // All of the following should be noexcept or must perform rollback on exception rvalps->set_stop_command(std::move(settings.stop_command), std::move(stop_arg_parts)); - rvalps->set_working_dir(std::move(settings.working_dir)); + rvalps->set_working_dir(std::move(settings.working_dir), sdir); rvalps->set_env_file(std::move(settings.env_file)); #if SUPPORT_CGROUPS rvalps->set_cgroup(std::move(settings.run_in_cgroup)); diff --git a/src/proc-service.cc b/src/proc-service.cc index a24d7a37..a7b71e13 100644 --- a/src/proc-service.cc +++ b/src/proc-service.cc @@ -918,7 +918,7 @@ bool process_service::start_stop_process(const std::vector &cmd) n if (forkpid == 0) { close(pipefd[0]); - const char * working_dir_c = nullptr; + const char * working_dir_c = working_dir_def; if (! working_dir.empty()) working_dir_c = working_dir.c_str(); run_proc_params run_params{cmd.data(), working_dir_c, logfile, pipefd[1], run_as_uid, run_as_gid, rlimits}; run_params.on_console = false;