Skip to content

Commit

Permalink
cleanup(userspace/libscap): avoid the usage of non-portable (glibc sp…
Browse files Browse the repository at this point in the history
…ecific) `__gnu_cxx::stdio_filebuf`.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Aug 29, 2024
1 parent e2c5174 commit f49119f
Showing 1 changed file with 29 additions and 37 deletions.
66 changes: 29 additions & 37 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 @@ -31,43 +27,39 @@ namespace runsc {
result runsc(char *argv[])
{
result res;
int pipefds[2];

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

pid_t pid = vfork();
if(pid > 0)
FILE *cmd_out = popen(full_command.c_str(), "r");
if (cmd_out == nullptr)
{
int status;

close(pipefds[1]);
wait(&status);
if(!WIFEXITED(status) || WEXITSTATUS(status) != 0)
res.error = -errno;
}
else
{
char *out = nullptr;
size_t len = 0;
while (getline(&out, &len, cmd_out) >= 0)
{
res.error = status;
return res;
res.output.emplace_back(out);
}

__gnu_cxx::stdio_filebuf<char> filebuf(pipefds[0], std::ios::in);
std::string line;
std::istream is(&filebuf);

while(std::getline(is, line))
free(out);
int status = pclose(cmd_out);
if(!WIFEXITED(status) || WEXITSTATUS(status) != 0)
{
res.output.emplace_back(std::string(line));
res.error = status;
}
}
else
{
close(pipefds[0]);
dup2(pipefds[1], STDOUT_FILENO);
execvp("runsc", argv);
exit(1);
}

return res;
}
Expand All @@ -77,7 +69,7 @@ result version()
const char *argv[] = {
"runsc",
"--version",
NULL
nullptr
};

return runsc((char **)argv);
Expand All @@ -93,7 +85,7 @@ result list(const std::string &root_path)
"--root",
root_path.c_str(),
"list",
NULL
nullptr
};

res = runsc((char **)argv);
Expand Down Expand Up @@ -127,7 +119,7 @@ result trace_create(const std::string &root_path, const std::string &trace_sessi
"--config",
trace_session_path.c_str(),
sandbox_id.c_str(),
NULL
nullptr
};

return runsc((char **)argv);
Expand All @@ -144,7 +136,7 @@ result trace_delete(const std::string &root_path, const std::string &session_nam
"--name",
session_name.c_str(),
sandbox_id.c_str(),
NULL
nullptr
};

return runsc((char **)argv);
Expand All @@ -159,7 +151,7 @@ result trace_procfs(const std::string &root_path, const std::string &sandbox_id)
"trace",
"procfs",
sandbox_id.c_str(),
NULL,
nullptr,
};

return runsc((char **)argv);
Expand Down

0 comments on commit f49119f

Please sign in to comment.