Skip to content

Commit

Permalink
Merge pull request #466 from jack-rann/458-bash-path
Browse files Browse the repository at this point in the history
Use $PATH to find Bash executable
  • Loading branch information
SimonKagstrom authored Sep 24, 2024
2 parents 3d57339 + e195cc8 commit 840b091
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doc/kcov.1
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Set the python parser to use for Python programs (the default is python). Can be
run with Python 3 on systems where Python 2 is the default.
.TP
\fB\-\-bash\-parser\fP=\fIPARSER\fP
Set the bash parser to use for shell scripts (the default is /bin/bash).
Set the bash parser to use for shell scripts (the default is bash).
.TP
\fB\-\-bash\-method\fP=\fIMETHOD\fP
Use collection method \fIMETHOD\fP for bash scripts. The method can be either PS4, for use of
Expand Down
2 changes: 1 addition & 1 deletion src/configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ class Configuration : public IConfiguration
#ifdef __FreeBSD__
setKey("bash-command", "/usr/local/bin/bash");
#else
setKey("bash-command", "/bin/bash");
setKey("bash-command", "bash");
#endif
setKey("kernel-coverage-path", "/sys/kernel/debug/kprobe-coverage");
setKey("path-strip-level", 2);
Expand Down
13 changes: 10 additions & 3 deletions src/engines/bash-engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ class BashEngine : public ScriptEngineBase
return false;
}

const std::string command = conf.keyAsString("bash-command");
if (!executable_exists_in_path(command))
{
error("Cannot find Bash parser '%s'", command.c_str());

return false;
}

m_bashSupportsXtraceFd = bashCanHandleXtraceFd();

std::string helperPath = IOutputHandler::getInstance().getBaseDirectory() + "bash-helper.sh";
Expand Down Expand Up @@ -165,7 +173,6 @@ class BashEngine : public ScriptEngineBase
xtraceFd = rlim.rlim_cur / 4;
#endif
}
const std::string command = conf.keyAsString("bash-command");
bool usePS4 = conf.keyAsInt("bash-use-ps4");

// Revert to stderr if this bash version can't handle BASH_XTRACE
Expand Down Expand Up @@ -228,13 +235,13 @@ class BashEngine : public ScriptEngineBase
char **vec;
int argcStart = 1;
vec = (char **) xmalloc(sizeof(char *) * (argc + 3));
vec[0] = xstrdup(conf.keyAsString("bash-command").c_str());
vec[0] = xstrdup(command.c_str());

for (unsigned i = 0; i < argc; i++)
vec[argcStart + i] = xstrdup(argv[i]);

/* Execute the script */
if (execv(vec[0], vec))
if (execvp(vec[0], vec))
{
perror("Failed to execute script");

Expand Down
5 changes: 3 additions & 2 deletions src/engines/bash-execve-redirector.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ int execve(const char *filename, char * const argv[], char * const envp[])
&& (strstr(startBytes, "/bin/sh") != NULL || strstr(startBytes, "/bin/bash") != NULL
|| strstr(startBytes, "/bin/ash") != NULL || strstr(startBytes, "/bin/dash") != NULL ))
{
char *bashExec = "/bin/bash";
char **replacementArgv;
unsigned i = 0;

Expand All @@ -63,15 +64,15 @@ int execve(const char *filename, char * const argv[], char * const envp[])
unsigned arg = 0;

replacementArgv = malloc(sizeof(char*) * (i + 4));
replacementArgv[arg++] = strdup(kcovBash);
replacementArgv[arg++] = strdup(bashExec);
if (!kcovUseDebugTrap)
replacementArgv[arg++] = strdup(bashArg);
replacementArgv[arg++] = strdup(filename);
for (i = 0; argv[i]; i++)
replacementArgv[i + arg] = strdup(argv[i]);
replacementArgv[i + arg] = NULL;

filename = strdup(kcovBash);
filename = strdup(bashExec);

argv = replacementArgv;
}
Expand Down

0 comments on commit 840b091

Please sign in to comment.