Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use $PATH to find Bash executable #466

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading