Select Git revision
-
Adrien Béraud authored
Change-Id: If2dbf456f0f2dbc7f04e1dec4db7986ad10f6f54
Adrien Béraud authoredChange-Id: If2dbf456f0f2dbc7f04e1dec4db7986ad10f6f54
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
dsh.cpp 9.81 KiB
/*
* Copyright (C) 2023 Savoir-faire Linux Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dsh.h"
#include "../common.h"
#include <opendht/log.h>
#include <opendht/crypto.h>
#include <asio/io_context.hpp>
#include <sys/types.h>
#include <sys/wait.h>
namespace dhtnet {
const int READ_END = 0;
const int WRITE_END = 1;
void
create_pipe(int apipe[2])
{
#ifdef __APPLE__
if (pipe(apipe) < 0)
perror("pipe");
if (fcntl(apipe[0], F_SETFD, FD_CLOEXEC) < 0)
perror("unable to set pipe FD_CLOEXEC");
if (fcntl(apipe[1], F_SETFD, FD_CLOEXEC) < 0)
perror("unable to set pipe FD_CLOEXEC");
#else
if (pipe2(apipe, O_CLOEXEC) == -1) {
perror("pipe2");
exit(EXIT_FAILURE);
}
#endif
}
void
child_proc(const int in_pipe[2],
const int out_pipe[2],
const int error_pipe[2],
const std::string& name)
{
// close unused write end of input pipe and read end of output pipe
close(in_pipe[WRITE_END]);
close(out_pipe[READ_END]);
close(error_pipe[READ_END]);
// replace stdin with input pipe
if (dup2(in_pipe[READ_END], STDIN_FILENO) == -1) {
perror("dup2: error replacing stdin");
exit(EXIT_FAILURE);
}
// replace stdout with output pipe
if (dup2(out_pipe[WRITE_END], STDOUT_FILENO) == -1) {
perror("dup2: error replacing stdout");
exit(EXIT_FAILURE);