#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include bool keep_running = true; void asserno(bool ok, const char* name){ if(ok) return; perror(name); exit(2); } typedef struct epoll_handler_s { int fd; void(*callback)(void*); } epoll_handler_t; typedef struct handle_signalfd_data_s { epoll_handler_t eph; pid_t child_pid; } handle_signalfd_data_t; void handle_signalfd_data(void* x){ handle_signalfd_data_t*restrict d = x; struct signalfd_siginfo fdsi; asserno(read(d->eph.fd, &fdsi, sizeof(fdsi)) == sizeof(fdsi), "read"); if(fdsi.ssi_signo == SIGCHLD){ if((pid_t)fdsi.ssi_pid == d->child_pid){ puts("Child program exited"); keep_running = false; } } } typedef struct handle_pty_data_s { epoll_handler_t eph; } handle_pty_data_t; void handle_pty_data(void* x){ handle_pty_data_t*restrict pty = x; char buf[256]; int s = read(pty->eph.fd, buf, sizeof(buf)); if(s<0) return; for(int i=0; ifd); keep_running = false; break; } eh->callback(eh); } } puts("Stopping program!"); }