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

Set pipe size issue322 #324

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions include/boost/process/detail/posix/basic_pipe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#include <fcntl.h>
#include <memory>

#if !(defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__MACH__))
#include <fstream>
#include <string>
#endif

namespace boost { namespace process { namespace detail { namespace posix {


Expand Down Expand Up @@ -117,6 +122,48 @@ class basic_pipe
_source = -1;
_sink = -1;
}

#if !(defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__MACH__))

int_type set_pipe_capacity_max()
{
std::ifstream file("/proc/sys/fs/pipe-max-size");
std::string content;
if(file.is_open())
{
std::getline(file, content);
file.close();
}
else
{
return -1;
}

int_type max_capacity = std::stoi(content);
return set_pipe_capacity(max_capacity);
}

int_type set_pipe_capacity(int_type capacity)
{
if (!is_open())
{
return -1;
}
int_type fd = _source != -1 ? _source : _sink;

int_type return_value;
while(((return_value = fcntl(fd, F_SETPIPE_SZ, capacity)) == -1))
{
//Try again if interrupted
auto err = errno;
if (err != EINTR)
::boost::process::detail::throw_last_error();
}

return return_value;
}

#endif
};

template<class CharT, class Traits>
Expand Down
29 changes: 29 additions & 0 deletions test/pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,34 @@ BOOST_AUTO_TEST_CASE(stream_close_scope, *boost::unit_test::timeout(5))
BOOST_CHECK_EQUAL(i, j);
}

#if !(defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__MACH__))

BOOST_AUTO_TEST_CASE(set_pipe_capacity_max, *boost::unit_test::timeout(5))
{
std::ifstream file("/proc/sys/fs/pipe-max-size");
std::string content;
std::getline(file, content);
file.close();
int max_capacity = std::stoi(content);

bp::pipe pipe;
int capacity = pipe.set_pipe_capacity_max();
BOOST_CHECK_EQUAL(capacity, max_capacity);
}

BOOST_AUTO_TEST_CASE(set_pipe_capacity, *boost::unit_test::timeout(5))
{
int min_capacity = 80000;
bp::pipe pipe;

int capacity = pipe.set_pipe_capacity(min_capacity);
BOOST_REQUIRE_NO_THROW(capacity >= min_capacity);

min_capacity = capacity + 10000;
capacity = pipe.set_pipe_capacity(min_capacity);
BOOST_REQUIRE_NO_THROW(capacity >= min_capacity);
}

#endif

BOOST_AUTO_TEST_SUITE_END();