Skip to content

Commit 6adad0a

Browse files
committed
Add the --chdir option
1 parent 676c4c3 commit 6adad0a

File tree

6 files changed

+27
-0
lines changed

6 files changed

+27
-0
lines changed

doc/sio2jail.1.scd

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ of the hardware sio2jail runs on.
3131
Write the execution report to file descriptor _fd_,
3232
instead of stderr.
3333

34+
*-c* _dir_, *--chdir* _dir_
35+
Change the working directory to _dir_ before running the program.
36+
3437
*-s, --stderr*
3538
Pass stderr from the sandboxed program,
3639
instead of redirecting it to stderr.

src/executor/Executor.cc

+10
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ namespace executor {
3939
Executor::Executor(
4040
std::string childProgramName,
4141
std::vector<std::string> childProgramArgv,
42+
std::string childProgramWorkingDir,
4243
bool supportThreads)
4344
: childProgramName_(std::move(childProgramName))
4445
, childProgramArgv_(std::move(childProgramArgv))
46+
, childProgramWorkingDir_(std::move(childProgramWorkingDir))
4547
, childPid_(0)
4648
, supportThreads_{supportThreads} {}
4749

@@ -68,6 +70,14 @@ void Executor::executeChild() {
6870
listener->onPostForkChild();
6971
}
7072

73+
// "" is the default value
74+
if (!childProgramWorkingDir_.empty()) {
75+
withErrnoCheck(
76+
"chdir to " + childProgramWorkingDir_,
77+
chdir,
78+
childProgramWorkingDir_.c_str());
79+
}
80+
7181
// Create plain C arrays with program arguments
7282
char* programName = stringToCStr(childProgramName_);
7383
char** programArgv = new char*[childProgramArgv_.size() + 2];

src/executor/Executor.h

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Executor
2020
Executor(
2121
std::string childProgramName,
2222
std::vector<std::string> childProgramArgv,
23+
std::string childProgramWorkingDir,
2324
bool supportThreads = false);
2425

2526
template<typename ProgramNameType>
@@ -45,6 +46,7 @@ class Executor
4546

4647
std::string childProgramName_;
4748
std::vector<std::string> childProgramArgv_;
49+
std::string childProgramWorkingDir_;
4850

4951
pid_t childPid_;
5052
const bool supportThreads_;

src/s2japp/Application.cc

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Application::ExitCode Application::handleRun() {
7070
auto executor = std::make_shared<s2j::executor::Executor>(
7171
settings_.programName,
7272
settings_.programArgv,
73+
settings_.programWorkingDir,
7374
settings_.threadsLimit >= 0);
7475

7576
auto traceExecutor = createListener<tracer::TraceExecutor>();

src/s2japp/ApplicationSettings.cc

+10
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,15 @@ ApplicationSettings::ApplicationSettings(int argc, const char* argv[])
302302
cmd,
303303
false);
304304

305+
TCLAP::ValueArg<std::string> argProgramWorkingDir(
306+
"c",
307+
"chdir",
308+
"Where to chdir to before running the program",
309+
false,
310+
"",
311+
"dir",
312+
cmd);
313+
305314
TCLAP::ValueArg<std::string> argLoggerPath(
306315
"l",
307316
"log",
@@ -365,6 +374,7 @@ ApplicationSettings::ApplicationSettings(int argc, const char* argv[])
365374

366375
programName = argProgramName.getValue();
367376
programArgv = argProgramArgv.getValue();
377+
programWorkingDir = argProgramWorkingDir.getValue();
368378

369379
outputBuilderFactory = argOutputFormat.getValue().getFactory();
370380
syscallPolicyFactory = argSyscallPolicy.getValue().getFactory();

src/s2japp/ApplicationSettings.h

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct ApplicationSettings : public ns::MountNamespaceListener::Settings {
5454

5555
std::string programName;
5656
std::vector<std::string> programArgv;
57+
std::string programWorkingDir;
5758

5859
Factory<s2j::printer::OutputBuilder> outputBuilderFactory;
5960
Factory<s2j::seccomp::policy::BaseSyscallPolicy> syscallPolicyFactory;

0 commit comments

Comments
 (0)