From 689a1ed8581f76b88c28ae0cd889b01e2623b290 Mon Sep 17 00:00:00 2001 From: James Prestwood Date: Thu, 2 Apr 2020 09:48:48 -0700 Subject: [PATCH] test-runner: fix segfault running with --log non-root When running test-runner as non-root the environment variables SUDO_GID/SUDO_UID were unset, causing atoi to segfault. This replaces atoi with strtol, and checks the existance of SUDO_GID/SUDO_UID before trying to turn it into an integer. This patch also allows the uid/gid to be read from the user if running as non-root. Note: running as non-root does require the users permissions to be setup properly. Directories and files are created when running with logging, so if the user running test-runner does not have these permissions the creation of these files will fail. --- tools/test-runner.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tools/test-runner.c b/tools/test-runner.c index 54a3bbde..44f08a0e 100644 --- a/tools/test-runner.c +++ b/tools/test-runner.c @@ -3093,6 +3093,8 @@ int main(int argc, char *argv[]) uint8_t actions = 0; struct tm *timeinfo; time_t t; + char *gid; + char *uid; l_log_set_stderr(); @@ -3193,8 +3195,16 @@ int main(int argc, char *argv[]) time(&t); timeinfo = localtime(&t); - log_gid = atoi(getenv("SUDO_GID")); - log_uid = atoi(getenv("SUDO_UID")); + gid = getenv("SUDO_GID"); + uid = getenv("SUDO_UID"); + + if (!gid || !uid) { + log_gid = getgid(); + log_uid = getuid(); + } else { + log_gid = strtol(gid, NULL, 10); + log_uid = strtol(uid, NULL, 10); + } snprintf(log_dir, sizeof(log_dir), "%s/run-%d-%d-%d-%d", optarg, timeinfo->tm_year + 1900,