Skip to content

Commit

Permalink
runguard: Allow passing -V multiple times.
Browse files Browse the repository at this point in the history
The previous behavior was that the last arg wins. IMHO, this is
confusing and we even fell into this trap ourselves in
https://github.com/DOMjudge/domjudge/blame/main/judge/compile.sh#L145
where we would shadow any previously passed env var (such as the entry
point).
  • Loading branch information
meisterT committed Nov 17, 2024
1 parent cd74bb3 commit 3651a70
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 8 additions & 5 deletions judge/runguard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
#include <libcgroup.h>
#include <sched.h>
#include <sys/sysinfo.h>
#include <vector>
#include <string>

#define PROGRAM "runguard"
#define VERSION DOMJUDGE_VERSION "/" REVISION
Expand Down Expand Up @@ -106,7 +108,7 @@ char *rootchdir;
char *stdoutfilename;
char *stderrfilename;
char *metafilename;
char *environment_variables;
std::vector<std::string> environment_variables;
FILE *metafile;

char cgroupname[255];
Expand Down Expand Up @@ -363,7 +365,8 @@ Run COMMAND with restrictions.\n\
-s, --streamsize=SIZE truncate COMMAND stdout/stderr streams at SIZE kB\n\
-E, --environment preserve environment variables (default only PATH)\n\
-V, --variable add additional environment variables\n\
(in form KEY=VALUE;KEY2=VALUE2)\n\
(in form KEY=VALUE;KEY2=VALUE2); may be passed\n\
multiple times\n\
-M, --outmeta=FILE write metadata (runtime, exitcode, etc.) to FILE\n\
-U, --runpipepid=PID process ID of runpipe to send SIGUSR1 signal when\n\
timelimit is reached\n");
Expand Down Expand Up @@ -802,8 +805,8 @@ void setrestrictions()
}

/* Set additional environment variables. */
if (environment_variables != nullptr) {
char *token = strtok(environment_variables, ";");
for (const auto &env_var : environment_variables) {
char *token = strtok(strdup(env_var.c_str()), ";");
while (token != nullptr) {
verbose("setting environment variable: %s", token);
putenv(token);
Expand Down Expand Up @@ -1175,7 +1178,7 @@ int main(int argc, char **argv)
preserve_environment = 1;
break;
case 'V': /* set environment variable */
environment_variables = strdup(optarg);
environment_variables.push_back(optarg);
break;
case 'M': /* outputmeta option */
outputmeta = 1;
Expand Down
5 changes: 3 additions & 2 deletions judge/runguard_test/runguard_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,11 @@ test_envvars() {
expect_stdout "USER="
expect_stdout "SHELL="

exec_check_success sudo $RUNGUARD -u domjudge-run-0 -V"DOMjudgeA=A;DOMjudgeB=BB" ./print_envvars.py
expect_stdout "COUNT: 4."
exec_check_success sudo $RUNGUARD -u domjudge-run-0 -V"DOMjudgeA=A;DOMjudgeB=BB" -V"DOMjudgeC=CCC" ./print_envvars.py
expect_stdout "COUNT: 5."
expect_stdout "DOMjudgeA=A"
expect_stdout "DOMjudgeB=BB"
expect_stdout "DOMjudgeC=CCC"
not_expect_stdout "HOME="
not_expect_stdout "USER="
not_expect_stdout "SHELL="
Expand Down

0 comments on commit 3651a70

Please sign in to comment.