-- Leo's gemini proxy
-- Connecting to git.thebackupbox.net:1965...
-- Connected
-- Sending request
-- Meta line: 20 text/gemini
repo: music action: commit revision: path_from: revision_from: fafd8aa38e37f3e14647eaad453dce4e65609c61: path_to: revision_to:
commit fafd8aa38e37f3e14647eaad453dce4e65609c61 Author: epoch <epoch@thebackupbox.net> Date: Mon Apr 8 03:19:54 2024 -0500 use a process group ID to kill children and descendents diff --git a/qargs.c b/qargs.c
--- a/qargs.c +++ b/qargs.c @@ -7,8 +7,9 @@ #include <string.h> #include <stdlib.h> #include <sys/wait.h> +#include <errno.h> -int pid;//THE child process +pid_t child; int direction; #define STOP_WITH SIGTERM @@ -20,22 +21,22 @@ int direction; char exit_please=0; void sig_handler(int sig) { + fprintf(stderr,"\x1b[31m[!] ENTERING SIGNAL HANDLER\n"); fprintf(stderr,"got a signal. %d\n",sig); switch(sig) { + case SIGINT: case SIGTERM: - if(pid > 0) kill(pid,STOP_WITH); - else fprintf(stderr,"shit. pid is %d\n",pid); + kill(-child,STOP_WITH);// unstop whatever direction=DIR_STOP; + fprintf(stderr,"got SIGINT or SIGTERM. stopping...\n"); exit_please=1; break; case SIGUSR1: - if(pid > 0) kill(pid,STOP_WITH); - else fprintf(stderr,"shit. pid is %d\n",pid); + kill(-child,STOP_WITH);// unstop whatever direction=DIR_PREV; break; case SIGUSR2: - if(pid > 0) kill(pid,STOP_WITH); - else fprintf(stderr,"shit. pid is %d\n",pid); + kill(-child,STOP_WITH);// unstop whatever direction=DIR_NEXT; break; case SIGHUP: @@ -55,6 +56,7 @@ void sig_handler(int sig) { //should NOT happen. break; } + fprintf(stderr,"\x1b[31m[!] LEAVING SIGNAL HANDLER\x1b[0m\n"); } //this is used for how far back you want to be able to go before you error out. @@ -68,13 +70,28 @@ int main(int argc,char *argv[]) { int lines_read=0; char line[LINES][PATH_MAX+1];//rather be off by one in the safe direction. FILE *fp; - printf("%d\n",getpid()); + pid_t pid=getpid(); + printf("%d\n",pid); fflush(stdout); + if(getenv("QARGS_PIDFILE")) { + if((fp=fopen(getenv("QARGS_PIDFILE"),"w+"))) { + fprintf(fp,"%d\n",pid); + fclose(fp); + } else { + fprintf(stderr,"couldn't open pid file: %s\n",getenv("QARGS_PIDFILE")); + } + } else { + fprintf(stderr,"QARGS_PIDFILE is not set in env. can't write pidfile for qargs.\n"); + } while(!exit_please) {//I should come up with a better condition for the main loop + // just in case the signal handlers get reset signal(SIGUSR1,sig_handler); signal(SIGUSR2,sig_handler); signal(SIGHUP,sig_handler); signal(SIGTERM,sig_handler); + signal(SIGINT,sig_handler); + signal(SIGPIPE, SIG_IGN); + signal(SIGCHLD, SIG_IGN); if(index == lines_read) { if(fgets(line[index % LINES],sizeof(line[index % LINES])-1,stdin) == 0) { fprintf(stderr,"Reached EOF on stdin.\n"); @@ -86,15 +103,17 @@ int main(int argc,char *argv[]) { } lines_read++; } - switch(pid=fork()) { + switch(child=fork()) { case -1://fuck me perror("fork"); return -1; case 0://we're the child - fprintf(stderr,"running line %d in pid %d: %s %s\n",index,getpid(),argv[1],line[index % LINES]); + child=getpid(); + setpgid(0,0);// descendents of this child process will inherit the pgid + fprintf(stderr,"running line %d in pid %d: %s %s\n",index,child,argv[1],line[index % LINES]); if(getenv("QARGS_CHILD_PIDFILE")) { if((fp=fopen(getenv("QARGS_CHILD_PIDFILE"),"w+"))) { - fprintf(fp,"%d\n",getpid()); + fprintf(fp,"%d\n",child); fclose(fp); } else { fprintf(stderr,"couldn't open child pid file: %s\n",getenv("QARGS_CHILD_PIDFILE")); @@ -105,6 +124,7 @@ int main(int argc,char *argv[]) { signal(SIGUSR1,SIG_IGN); signal(SIGUSR2,SIG_IGN); signal(SIGTERM,SIG_DFL); + signal(SIGPIPE,SIG_IGN); execlp(argv[1],argv[1],line[index % LINES],NULL); perror("execlp"); return -2;//child failed to exec. tell it to fuck off. @@ -112,10 +132,17 @@ int main(int argc,char *argv[]) { default://we're the parent break; } - while(wait(&status) == -1) {//wait for child process to finish... + fprintf(stderr,"waiting on child to exit...\n"); + while(waitpid(child,&status,0) == -1) { perror("wait"); + if(errno == ECHILD) { + fprintf(stderr,"no children. moving on.\n"); + break; // good enough. no children. + } + fprintf(stderr,"trying the waitpid() again.\n"); } - fprintf(stderr,"child %d exited. going to %d the index.\n",pid,direction); + errno=0; + fprintf(stderr,"child %d exited. going to %d the index.\n",child,direction); index+=direction; if(index < start) { fprintf(stderr,"tried to previous to before start. exiting with code of 1.\n"); @@ -124,4 +151,6 @@ int main(int argc,char *argv[]) { if(index < 0) index+=LINES; direction=DIR_NEXT; } + fprintf(stderr,"qargs exiting!\n"); + return 0; }
-----END OF PAGE-----
-- Response ended
-- Page fetched on Sun Jun 2 18:27:21 2024