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

cleanup(userspace/libscap): avoid the usage of non-portable (glibc specific) __gnu_cxx::stdio_filebuf #2037

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Changes from 1 commit
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
55 changes: 26 additions & 29 deletions userspace/libscap/engine/gvisor/runsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ limitations under the License.
*/

#include <unistd.h>
#include <sys/wait.h>
#include <ext/stdio_filebuf.h>
#include <iostream>
#include <fstream>
#include <string>

#include <libscap/engine/gvisor/gvisor.h>
Expand All @@ -30,36 +26,37 @@ namespace runsc {

result runsc(char *argv[]) {
result res;
int pipefds[2];

int ret = pipe(pipefds);
if(ret) {
return res;
std::string full_command;
int i = 0;
while(true) {
if(argv[i] == nullptr) {
break;
}
full_command.append(argv[i]);
full_command.append(" ");
i++;
}

pid_t pid = vfork();
if(pid > 0) {
int status;

close(pipefds[1]);
wait(&status);
FILE *cmd_out = popen(full_command.c_str(), "r");
LucaGuerra marked this conversation as resolved.
Show resolved Hide resolved
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) {
// getline() returns string comprehensive of newline char;
// drop it if found.
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;
}

__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("runsc", argv);
exit(1);
}

return res;
Expand Down
Loading