Skip to content

Commit

Permalink
Default to service dir as working directory for services
Browse files Browse the repository at this point in the history
  • Loading branch information
q66 authored and davmac314 committed Sep 13, 2023
1 parent 7aaa4d0 commit b74c526
Show file tree
Hide file tree
Showing 33 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion doc/manpages/dinit-service.5.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/baseproc-service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/igr-tests/basic/sd/basic
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type = process
command = ./basic.sh
command = ../basic.sh
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/before-after/sd/service1
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type = scripted
command = scripts/service.sh one 0.5
command = ../scripts/service.sh one 0.5
before = service2
2 changes: 1 addition & 1 deletion src/igr-tests/before-after/sd/service2
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
type = scripted
command = scripts/service.sh two 0.2
command = ../scripts/service.sh two 0.2
2 changes: 1 addition & 1 deletion src/igr-tests/before-after/sd/service3
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type = scripted
command = scripts/service.sh three 0.1
command = ../scripts/service.sh three 0.1
after = service2
2 changes: 1 addition & 1 deletion src/igr-tests/before-after2/sd/service1
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type = scripted
command = scripts/service.sh one 0.5
command = ../scripts/service.sh one 0.5
before = service2
2 changes: 1 addition & 1 deletion src/igr-tests/before-after2/sd/service2
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type = scripted
command = scripts/service.sh two 0.2
command = ../scripts/service.sh two 0.2
depends-on = service1
2 changes: 1 addition & 1 deletion src/igr-tests/catlog/sd/output
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type = scripted
command = ./output.sh
command = ../output.sh
restart = false
log-type = buffer
2 changes: 1 addition & 1 deletion src/igr-tests/chain-to/sd/part1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type = process
command = ./record-param.sh part1
command = ../record-param.sh part1
chain-to = part2
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/chain-to/sd/part2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type = process
command = ./record-param.sh part2
command = ../record-param.sh part2
chain-to = part3
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/chain-to/sd/part3
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type = process
options = always-chain
command = ./record-param.sh part3
command = ../record-param.sh part3
chain-to = part4
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/chain-to/sd/part4
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type = process
#options = always-chain
command = ./record-param.sh part4
command = ../record-param.sh part4
chain-to = part5
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/chain-to/sd/part5
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type = process
command = ./record-param.sh part5
command = ../record-param.sh part5
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/environ/sd/checkenv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/igr-tests/environ/sd/setenv1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type = process
command = ./setenv.sh setenv1
command = ../setenv.sh setenv1
depends-on = setenv2
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/environ/sd/setenv2
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type = scripted
command = ./setenv.sh setenv2
command = ../setenv.sh setenv2
depends-on = setenv3
2 changes: 1 addition & 1 deletion src/igr-tests/environ/sd/setenv3
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
type = scripted
command = ./setenv.sh setenv3
command = ../setenv.sh setenv3
2 changes: 2 additions & 0 deletions src/igr-tests/environ/setenv.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

cd ..

case "$1" in
setenv1)
if [ "$FOO" = "foo" ] && [ "$BAR" = "bar" ] && [ "$BAZ" = "baz" ]; then
Expand Down
2 changes: 1 addition & 1 deletion src/igr-tests/environ2/sd/checkenv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/igr-tests/log-via-pipe/sd/consumer
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type = process
command = scripts/consumer.sh
command = ../scripts/consumer.sh
consumer-of = producer
2 changes: 1 addition & 1 deletion src/igr-tests/log-via-pipe/sd/producer
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type = scripted
command = scripts/producer.sh
command = ../scripts/producer.sh
log-type = pipe
2 changes: 1 addition & 1 deletion src/igr-tests/ps-environ/sd/checkenv1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type = process
command = ./checkenv.sh
command = ../checkenv.sh
env-file = ./environment1
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/ps-environ/sd/checkenv2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type = process
command = ./checkenv.sh
command = ../checkenv.sh
env-file = ./environment2
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/ps-environ/sd/checkenv3
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type = process
command = ./checkenv2.sh
command = ../checkenv2.sh
env-file = ./environment3
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/ps-environ/sd/checkenv4
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type = process
command = ./checkenv2.sh
command = ../checkenv2.sh
env-file = ./environment4
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/pseudo-cycle/sd/service
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type = process
command = ./svc-script.sh
command = ../svc-script.sh
restart = false
before = boot
2 changes: 1 addition & 1 deletion src/igr-tests/restart/sd/basic
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type = process
command = ./basic.sh
command = ../basic.sh
restart = false
2 changes: 1 addition & 1 deletion src/igr-tests/svc-start-fail/sd/timeout-command
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# timeout-command
type = scripted
command = ./timeout.sh
command = ../timeout.sh
start-timeout = 0.01
2 changes: 1 addition & 1 deletion src/igr-tests/var-subst/sd/checkargs
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions src/includes/proc-service.h
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/load-service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/proc-service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b74c526

Please sign in to comment.