Notcurses 3.0.13
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
fd.c File Reference
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include "internal.h"
#include <spawn.h>
Include dependency graph for fd.c:

Go to the source code of this file.

Macros

#define NCPOLLEVENTS   (POLLIN)
 

Functions

ncfdplanencfdplane_create (ncplane *n, const ncfdplane_options *opts, int fd, ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
 
ncplanencfdplane_plane (ncfdplane *n)
 
int ncfdplane_destroy (ncfdplane *n)
 
ncsubprocncsubproc_createv (ncplane *n, const ncsubproc_options *opts, const char *bin, const char *const arg[], ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
 
ncsubprocncsubproc_createvp (ncplane *n, const ncsubproc_options *opts, const char *bin, const char *const arg[], ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
 
ncsubprocncsubproc_createvpe (ncplane *n, const ncsubproc_options *opts, const char *bin, const char *const arg[], const char *const env[], ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
 
int ncsubproc_destroy (ncsubproc *n)
 
ncplanencsubproc_plane (ncsubproc *n)
 
int get_tty_fd (FILE *ttyfp)
 

Macro Definition Documentation

◆ NCPOLLEVENTS

#define NCPOLLEVENTS   (POLLIN)

Definition at line 23 of file fd.c.

Function Documentation

◆ get_tty_fd()

int get_tty_fd ( FILE *  ttyfp)

Definition at line 455 of file fd.c.

455 {
456 int fd = -1;
457 if(ttyfp){
458 if((fd = fileno(ttyfp)) < 0){
459 logwarn("no file descriptor was available in outfp %p", ttyfp);
460 }else{
461 if(tty_check(fd)){
462 fd = dup(fd);
463 }else{
464 loginfo("fd %d is not a TTY", fd);
465 fd = -1;
466 }
467 }
468 }
469 if(fd < 0){
470 fd = open("/dev/tty", O_RDWR | O_CLOEXEC | O_NOCTTY);
471 if(fd < 0){
472 loginfo("couldn't open /dev/tty (%s)", strerror(errno));
473 }else{
474 if(!tty_check(fd)){
475 loginfo("file descriptor for /dev/tty (%d) is not actually a TTY", fd);
476 close(fd);
477 fd = -1;
478 }
479 }
480 }
481 if(fd >= 0){
482 loginfo("returning TTY fd %d", fd);
483 }
484 return fd;
485}
#define loginfo(fmt,...)
Definition logging.h:42
#define logwarn(fmt,...)
Definition logging.h:37
Here is the caller graph for this function:

◆ ncfdplane_create()

ncfdplane * ncfdplane_create ( ncplane n,
const ncfdplane_options opts,
int  fd,
ncfdplane_callback  cbfxn,
ncfdplane_done_cb  donecbfxn 
)

Definition at line 120 of file fd.c.

121 {
122 ncfdplane_options zeroed = {0};
123 if(!opts){
124 opts = &zeroed;
125 }
126 if(fd < 0 || !cbfxn || !donecbfxn){
127 return NULL;
128 }
129 return ncfdplane_create_internal(n, opts, fd, cbfxn, donecbfxn, true);
130}
const struct ncplane_options * opts
Definition notcurses.h:3483
vopts n
Definition notcurses.h:3502
return NULL
Definition termdesc.h:229

◆ ncfdplane_destroy()

int ncfdplane_destroy ( ncfdplane n)

Definition at line 136 of file fd.c.

136 {
137 int ret = 0;
138 if(n){
139 if(pthread_equal(pthread_self(), n->tid)){
140 n->destroyed = true; // ncfdplane_destroy_inner() is called on thread exit
141 }else{
142 void* vret = NULL;
143 ret |= cancel_and_join("fdplane", n->tid, &vret);
144 ret |= ncfdplane_destroy_inner(n);
145 }
146 }
147 return ret;
148}
Here is the caller graph for this function:

◆ ncfdplane_plane()

ncplane * ncfdplane_plane ( ncfdplane n)

Definition at line 132 of file fd.c.

132 {
133 return n->ncp;
134}
Here is the caller graph for this function:

◆ ncsubproc_createv()

ncsubproc * ncsubproc_createv ( ncplane n,
const ncsubproc_options opts,
const char *  bin,
const char *const  arg[],
ncfdplane_callback  cbfxn,
ncfdplane_done_cb  donecbfxn 
)

Definition at line 383 of file fd.c.

385 {
386 return ncexecvpe(n, opts, 0, bin, (char* const *)arg, NULL, cbfxn, donecbfxn);
387}

◆ ncsubproc_createvp()

ncsubproc * ncsubproc_createvp ( ncplane n,
const ncsubproc_options opts,
const char *  bin,
const char *const  arg[],
ncfdplane_callback  cbfxn,
ncfdplane_done_cb  donecbfxn 
)

Definition at line 389 of file fd.c.

391 {
392 return ncexecvpe(n, opts, 1, bin, (char* const *)arg, NULL, cbfxn, donecbfxn);
393}

◆ ncsubproc_createvpe()

ncsubproc * ncsubproc_createvpe ( ncplane n,
const ncsubproc_options opts,
const char *  bin,
const char *const  arg[],
const char *const  env[],
ncfdplane_callback  cbfxn,
ncfdplane_done_cb  donecbfxn 
)

Definition at line 395 of file fd.c.

398 {
399 return ncexecvpe(n, opts, 1, bin, (char* const *)arg, (char* const*)env, cbfxn, donecbfxn);
400}

◆ ncsubproc_destroy()

int ncsubproc_destroy ( ncsubproc n)

Definition at line 402 of file fd.c.

402 {
403 int ret = 0;
404 if(n){
405 void* vret = NULL;
406//fprintf(stderr, "pid: %u pidfd: %d waittid: %u\n", n->pid, n->pidfd, n->waittid);
407#ifndef __MINGW32__
408#ifdef USING_PIDFD
409 if(n->pidfd >= 0){
410 loginfo("sending SIGKILL to pidfd %d", n->pidfd);
411 if(syscall(__NR_pidfd_send_signal, n->pidfd, SIGKILL, NULL, 0)){
412 kill(n->pid, SIGKILL);
413 }
414 }
415#else
416 pthread_mutex_lock(&n->lock);
417 if(!n->waited){
418 loginfo("sending SIGKILL to PID %d", n->pid);
419 kill(n->pid, SIGKILL);
420 }
421 pthread_mutex_unlock(&n->lock);
422#endif
423#endif
424 // the thread waits on the subprocess via pidfd (iff pidfd >= 0), and
425 // then exits. don't try to cancel the thread in that case; rely instead on
426 // killing the subprocess.
427 if(n->pidfd < 0){
428 pthread_cancel(n->nfp->tid);
429 // shouldn't need a cancellation of waittid thanks to SIGKILL
430 pthread_join(n->waittid, &vret);
431 }
432 if(vret == NULL){
433 pthread_join(n->nfp->tid, &vret);
434 }else{
435 pthread_join(n->nfp->tid, NULL);
436 }
437 pthread_mutex_destroy(&n->lock);
438 free(n);
439 if(vret == NULL){
440 ret = -1;
441 }else if(vret != PTHREAD_CANCELED){
442 ret = *(int*)vret;
443 free(vret);
444 }
445 }
446 return ret;
447}
free(duplicated)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ncsubproc_plane()

ncplane * ncsubproc_plane ( ncsubproc n)

Definition at line 449 of file fd.c.

449 {
450 return n->nfp->ncp;
451}
Here is the caller graph for this function: