ncfdplane: on non-following planes, break out on 0 read

pull/555/head
nick black 4 years ago
parent b34fa9be41
commit c8322e6cb1
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -21,7 +21,7 @@ typedef struct ncfdplane_options {
} ncfdplane_options;
typedef struct ncsubproc_options {
ncfdplane_options popts;
void* curry; // parameter provided to callbacks
uint64_t restart_period; // restart after exit
} ncsubproc_options;
```

@ -2685,7 +2685,7 @@ API struct ncplane* ncfdplane_plane(struct ncfdplane* n);
API int ncfdplane_destroy(struct ncfdplane* n);
typedef struct ncsubproc_options {
ncfdplane_options popts;
void* curry;
uint64_t restart_period; // restart this many seconds after an exit (watch)
} ncsubproc_options;

@ -460,7 +460,7 @@ struct ncfdplane* ncfdplane_create(struct ncplane* n, const ncfdplane_options* o
struct ncplane* ncfdplane_plane(struct ncfdplane* n);
int ncfdplane_destroy(struct ncfdplane* n);
typedef struct ncsubproc_options {
ncfdplane_options popts;
void* curry;
uint64_t restart_period; // restart this many seconds after an exit (watch)
} ncsubproc_options;
struct ncsubproc* ncsubproc_createv(struct ncplane* n, const ncsubproc_options* opts, const char* bin, char* const arg[], ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn);

@ -45,6 +45,10 @@ fdthread(ncfdplane* ncfp, int pidfd){
break;
}
}
// if we're not doing follow, break out on a zero-byte read
if(r == 0 && !ncfp->follow){
break;
}
}
if(fdcount > 1 && pfds[1].revents & POLLIN){
r = 0;
@ -63,6 +67,22 @@ ncfdplane_thread(void* vncfp){
return NULL;
}
static int
set_fd_nonblocking(int fd){
int flags = fcntl(fd, F_GETFL, 0);
if(flags < 0){
return -1;
}
if(flags & O_NONBLOCK){
return 0;
}
flags |= O_NONBLOCK;
if(fcntl(fd, F_SETFL, flags)){
return -1;
}
return 0;
}
static ncfdplane*
ncfdplane_create_internal(ncplane* n, const ncfdplane_options* opts, int fd,
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn,
@ -146,14 +166,7 @@ launch_pipe_process(int* pipe, int* pidfd){
}
}else if(p > 0){ // parent
*pipe = pipes[0];
int flags = fcntl(*pipe, F_GETFL, 0);
if(flags < 0){
// FIXME
}
flags |= O_NONBLOCK;
if(fcntl(*pipe, F_SETFL, flags)){
// FIXME
}
set_fd_nonblocking(*pipe);
}
return p;
}
@ -184,7 +197,11 @@ ncsubproc_thread(void* vncsp){
static ncfdplane*
ncsubproc_launch(ncplane* n, ncsubproc* ret, const ncsubproc_options* opts, int fd,
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn){
ret->nfp = ncfdplane_create_internal(n, &opts->popts, fd, cbfxn, donecbfxn, false);
ncfdplane_options popts = {
.curry = opts->curry,
.follow = true,
};
ret->nfp = ncfdplane_create_internal(n, &popts, fd, cbfxn, donecbfxn, false);
if(ret->nfp == NULL){
return NULL;
}

@ -103,7 +103,7 @@ TEST_CASE("FdsAndSubprocs") {
char * const argv[] = { strdup("/dev/nope"), NULL, };
bool outofline_cancelled = false;
ncsubproc_options opts{};
opts.popts.curry = &outofline_cancelled;
opts.curry = &outofline_cancelled;
auto ncsubp = ncsubproc_createvp(n_, &opts, argv[0], argv, testfdcb, testfdeof);
REQUIRE(ncsubp);
std::unique_lock<std::mutex> lck(lock);
@ -123,7 +123,7 @@ TEST_CASE("FdsAndSubprocs") {
char * const argv[] = { strdup("/bin/cat"), strdup("/dev/null"), NULL, };
bool outofline_cancelled = false;
ncsubproc_options opts{};
opts.popts.curry = &outofline_cancelled;
opts.curry = &outofline_cancelled;
auto ncsubp = ncsubproc_createvp(n_, &opts, argv[0], argv, testfdcb, testfdeof);
REQUIRE(ncsubp);
std::unique_lock<std::mutex> lck(lock);
@ -140,7 +140,7 @@ TEST_CASE("FdsAndSubprocs") {
char * const argv[] = { strdup("/bin/cat"), strdup("/dev/nope"), NULL, };
bool outofline_cancelled = false;
ncsubproc_options opts{};
opts.popts.curry = &outofline_cancelled;
opts.curry = &outofline_cancelled;
auto ncsubp = ncsubproc_createvp(n_, &opts, argv[0], argv, testfdcb, testfdeof);
REQUIRE(ncsubp);
std::unique_lock<std::mutex> lck(lock);
@ -157,7 +157,7 @@ TEST_CASE("FdsAndSubprocs") {
char * const argv[] = { strdup("/bin/cat"), NULL, };
bool outofline_cancelled = false;
ncsubproc_options opts{};
opts.popts.curry = &outofline_cancelled;
opts.curry = &outofline_cancelled;
auto ncsubp = ncsubproc_createvp(n_, &opts, argv[0], argv, testfdcb, testfdeof);
REQUIRE(ncsubp);
CHECK(0 == ncsubproc_destroy(ncsubp));

Loading…
Cancel
Save