Add supervisor stem process auto-revival

This commit is contained in:
Jon Siwek 2019-10-04 13:25:01 -07:00
parent 4959d438fa
commit 52f7647f25
5 changed files with 285 additions and 93 deletions

View file

@ -20,21 +20,52 @@ static void pipe_fail(int eno)
fprintf(stderr, "Pipe failure: %s", tmp);
}
static void set_flags(int fd, int flags)
static int set_flags(int fd, int flags)
{
auto rval = fcntl(fd, F_GETFD);
if ( flags )
if ( fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | flags) == -1 )
{
rval |= flags;
if ( fcntl(fd, F_SETFD, rval) == -1 )
pipe_fail(errno);
}
return rval;
}
static void set_status_flags(int fd, int flags)
static int unset_flags(int fd, int flags)
{
auto rval = fcntl(fd, F_GETFD);
if ( flags )
if ( fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | flags) == -1 )
{
rval &= ~flags;
if ( fcntl(fd, F_SETFD, rval) == -1 )
pipe_fail(errno);
}
return rval;
}
static int dup_or_fail(int fd, int flags)
static int set_status_flags(int fd, int flags)
{
auto rval = fcntl(fd, F_GETFL);
if ( flags )
{
rval |= flags;
if ( fcntl(fd, F_SETFL, rval) == -1 )
pipe_fail(errno);
}
return rval;
}
static int dup_or_fail(int fd, int flags, int status_flags)
{
int rval = dup(fd);
@ -42,22 +73,41 @@ static int dup_or_fail(int fd, int flags)
pipe_fail(errno);
set_flags(fd, flags);
set_status_flags(fd, status_flags);
return rval;
}
Pipe::Pipe(int flags0, int flags1, int status_flags0, int status_flags1)
Pipe::Pipe(int flags0, int flags1, int status_flags0, int status_flags1,
int* arg_fds)
{
// pipe2 can set flags atomically, but not yet available everywhere.
if ( ::pipe(fds) )
pipe_fail(errno);
if ( arg_fds )
{
fds[0] = arg_fds[0];
fds[1] = arg_fds[1];
}
else
{
// pipe2 can set flags atomically, but not yet available everywhere.
if ( ::pipe(fds) )
pipe_fail(errno);
}
flags[0] = flags0;
flags[1] = flags1;
flags[0] = set_flags(fds[0], flags[0]);
flags[1] = set_flags(fds[1], flags[1]);
status_flags[0] = set_status_flags(fds[0], status_flags0);
status_flags[1] = set_status_flags(fds[1], status_flags1);
}
set_flags(fds[0], flags[0]);
set_flags(fds[1], flags[1]);
set_status_flags(fds[0], status_flags0);
set_status_flags(fds[1], status_flags1);
void Pipe::SetFlags(int arg_flags)
{
flags[0] = set_flags(fds[0], arg_flags);
flags[1] = set_flags(fds[1], arg_flags);
}
void Pipe::UnsetFlags(int arg_flags)
{
flags[0] = unset_flags(fds[0], arg_flags);
flags[1] = unset_flags(fds[1], arg_flags);
}
Pipe::~Pipe()
@ -68,10 +118,12 @@ Pipe::~Pipe()
Pipe::Pipe(const Pipe& other)
{
fds[0] = dup_or_fail(other.fds[0], other.flags[0]);
fds[1] = dup_or_fail(other.fds[1], other.flags[1]);
fds[0] = dup_or_fail(other.fds[0], other.flags[0], other.status_flags[0]);
fds[1] = dup_or_fail(other.fds[1], other.flags[1], other.status_flags[1]);
flags[0] = other.flags[0];
flags[1] = other.flags[1];
status_flags[0] = other.status_flags[0];
status_flags[1] = other.status_flags[1];
}
Pipe& Pipe::operator=(const Pipe& other)
@ -81,9 +133,11 @@ Pipe& Pipe::operator=(const Pipe& other)
close(fds[0]);
close(fds[1]);
fds[0] = dup_or_fail(other.fds[0], other.flags[0]);
fds[1] = dup_or_fail(other.fds[1], other.flags[1]);
fds[0] = dup_or_fail(other.fds[0], other.flags[0], other.status_flags[0]);
fds[1] = dup_or_fail(other.fds[1], other.flags[1], other.status_flags[1]);
flags[0] = other.flags[0];
flags[1] = other.flags[1];
status_flags[0] = other.status_flags[0];
status_flags[1] = other.status_flags[1];
return *this;
}