Browse Source

Default to service dir as working directory for services

q66 7 months ago
parent
commit
b74c5266fd

+ 1 - 1
doc/manpages/dinit-service.5.m4

@@ -157,7 +157,7 @@ The value is subject to variable substitution (see \fBVARIABLE SUBSTITUTION\fR).
 .TP
 \fBworking\-dir\fR = \fIdirectory\fR
 Specifies the working directory for this service. For a scripted service, this
-affects both the start command and the stop command.
+affects both the start command and the stop command. The default is the service directory.
 The value is subject to variable substitution (see \fBVARIABLE SUBSTITUTION\fR).
 .TP
 \fBrun\-as\fR = \fIuser-id\fR

+ 1 - 1
src/baseproc-service.cc

@@ -242,7 +242,7 @@ bool base_process_service::start_ps_process(const std::vector<const char *> &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};

+ 1 - 1
src/igr-tests/basic/sd/basic

@@ -1,3 +1,3 @@
 type = process
-command = ./basic.sh
+command = ../basic.sh
 restart = false

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
src/igr-tests/catlog/sd/output

@@ -1,4 +1,4 @@
 type = scripted
-command = ./output.sh
+command = ../output.sh
 restart = false
 log-type = buffer

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
src/igr-tests/environ/sd/setenv2

@@ -1,3 +1,3 @@
 type = scripted
-command = ./setenv.sh setenv2
+command = ../setenv.sh setenv2
 depends-on = setenv3

+ 1 - 1
src/igr-tests/environ/sd/setenv3

@@ -1,2 +1,2 @@
 type = scripted
-command = ./setenv.sh setenv3
+command = ../setenv.sh setenv3

+ 2 - 0
src/igr-tests/environ/setenv.sh

@@ -1,5 +1,7 @@
 #!/bin/sh
 
+cd ..
+
 case "$1" in
     setenv1)
         if [ "$FOO" = "foo" ] && [ "$BAR" = "bar" ] && [ "$BAZ" = "baz" ]; then

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
src/igr-tests/ps-environ/sd/checkenv1

@@ -1,4 +1,4 @@
 type = process
-command = ./checkenv.sh
+command = ../checkenv.sh
 env-file = ./environment1
 restart = false

+ 1 - 1
src/igr-tests/ps-environ/sd/checkenv2

@@ -1,4 +1,4 @@
 type = process
-command = ./checkenv.sh
+command = ../checkenv.sh
 env-file = ./environment2
 restart = false

+ 1 - 1
src/igr-tests/ps-environ/sd/checkenv3

@@ -1,4 +1,4 @@
 type = process
-command = ./checkenv2.sh
+command = ../checkenv2.sh
 env-file = ./environment3
 restart = false

+ 1 - 1
src/igr-tests/ps-environ/sd/checkenv4

@@ -1,4 +1,4 @@
 type = process
-command = ./checkenv2.sh
+command = ../checkenv2.sh
 env-file = ./environment4
 restart = false

+ 1 - 1
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

+ 1 - 1
src/igr-tests/restart/sd/basic

@@ -1,3 +1,3 @@
 type = process
-command = ./basic.sh
+command = ../basic.sh
 restart = false

+ 1 - 1
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

+ 1 - 1
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

+ 5 - 2
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

+ 3 - 3
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));

+ 1 - 1
src/proc-service.cc

@@ -918,7 +918,7 @@ bool process_service::start_stop_process(const std::vector<const char *> &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;