-
Notifications
You must be signed in to change notification settings - Fork 165
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
cleanup(userspace/libscap): avoid the usage of non-portable (glibc specific) __gnu_cxx::stdio_filebuf
#2037
Conversation
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: FedeDP The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/milestone 0.19.0 |
Perf diff from master - unit tests
Heap diff from master - unit tests
Heap diff from master - scap file
Benchmarks diff from master
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2037 +/- ##
=======================================
Coverage 73.57% 73.58%
=======================================
Files 253 253
Lines 31860 31867 +7
Branches 5640 5641 +1
=======================================
+ Hits 23442 23450 +8
+ Misses 8407 8392 -15
- Partials 11 25 +14
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
X64 kernel testing matrix
ARM64 kernel testing matrix
|
f49119f
to
cc7d73f
Compare
@LucaGuerra tested with this small test file: #include <sys/wait.h>
#include <ext/stdio_filebuf.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string>
#include <vector>
struct result {
int error = 0;
std::vector<std::string> output;
};
// Old method
result runsc(char *argv[])
{
result res;
int pipefds[2];
int ret = pipe(pipefds);
if(ret)
{
return res;
}
pid_t pid = vfork();
if(pid > 0)
{
int status;
close(pipefds[1]);
wait(&status);
if(!WIFEXITED(status) || WEXITSTATUS(status) != 0)
{
res.error = status;
return res;
}
__gnu_cxx::stdio_filebuf<char> filebuf(pipefds[0], std::ios::in);
std::string line;
std::istream is(&filebuf);
while(std::getline(is, line))
{
res.output.emplace_back(std::string(line));
}
}
else
{
close(pipefds[0]);
dup2(pipefds[1], STDOUT_FILENO);
execvp("docker", argv);
exit(1);
}
return res;
}
// New method
result runsc_popen(char *argv[])
{
result res;
std::string full_command;
int i = 0;
while (true)
{
if (argv[i] == nullptr)
{
break;
}
full_command.append(argv[i]);
full_command.append(" ");
i++;
}
FILE *cmd_out = popen(full_command.c_str(), "r");
if (cmd_out == nullptr)
{
res.error = -errno;
}
else
{
char *out = nullptr;
size_t len = 0;
ssize_t readbytes;
while ((readbytes = getline(&out, &len, cmd_out)) >= 0)
{
if (out[readbytes - 1] == '\n')
out[readbytes - 1] = '\0';
res.output.emplace_back(out);
}
free(out);
int status = pclose(cmd_out);
if(!WIFEXITED(status) || WEXITSTATUS(status) != 0)
{
res.error = status;
}
}
return res;
}
int main()
{
const char *argv[] = {
"docker",
"--version",
nullptr
};
auto res = runsc((char **)argv);
printf("run with exit code: %d\n", res.error);
for (const auto &o : res.output) {
printf("%s\n", o.c_str());
}
auto res_popen = runsc_popen((char **)argv);
printf("run_popen with exit code: %d\n", res_popen.error);
for (const auto &o : res_popen.output) {
printf("%s\n", o.c_str());
}
return 0;
} |
…ecific) `__gnu_cxx::stdio_filebuf`. Signed-off-by: Federico Di Pierro <[email protected]>
cc7d73f
to
28ba006
Compare
New function test:
|
} | ||
} else { | ||
close(pipefds[0]); | ||
dup2(pipefds[1], STDOUT_FILENO); | ||
execvp("runsc", argv); | ||
execvp("docker", argv); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leftover? :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh! ops ahha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
greatest catch ever 🤣
Signed-off-by: Federico Di Pierro <[email protected]>
2d067ff
to
24b3164
Compare
What type of PR is this?
/kind cleanup
Any specific area of the project related to this PR?
/area libscap-engine-gvisor
Does this PR require a change in the driver versions?
What this PR does / why we need it:
Using the glibc specific
__gnu_cxx::stdio_filebuf
breaks while trying to build Falco/libs with zig compiler (see falcosecurity/falco#3307).In this specific case, we can make use of POSIX standard
popen
.Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
There are a couple more usages of
__gnu_cxx::stdio_filebuf
; both are inside libsinsp_e2e tests framework, but we don't really need to build libs tests when we build Falco (with zig) therefore i am skipping them for now.Does this PR introduce a user-facing change?: