From ead340a66039096c7b4bf12dcd65e189769c6653 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Mon, 18 Oct 2021 17:51:11 -0700 Subject: feat(rc): job control prototype working for basic commands --- sys/cmd/rc/main.c | 74 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 27 deletions(-) (limited to 'sys/cmd/rc/main.c') diff --git a/sys/cmd/rc/main.c b/sys/cmd/rc/main.c index 46a4e0f..d1bbd06 100644 --- a/sys/cmd/rc/main.c +++ b/sys/cmd/rc/main.c @@ -2,9 +2,53 @@ #include "parse.h" #include "exec.h" -int rcpid; +#include +#include + +// ----------------------------------------------------------------------- +// globals + Io *errio = nil; -Thread *shell = nil; +Thread *proc = nil; +Shell shell = { 0 }; + +// ----------------------------------------------------------------------- +// functions + +void +initshell(void) +{ + if((shell.interactive=isatty(0))){ + while(tcgetpgrp(0) != (shell.pid = getpgrp())) + kill(-shell.pid, SIGTTIN); + + /* ignore job control signals */ + signal(SIGINT, SIG_IGN); + signal(SIGQUIT, SIG_IGN); + signal(SIGTSTP, SIG_IGN); + signal(SIGTTIN, SIG_IGN); + signal(SIGTTOU, SIG_IGN); + /* + * NOTE: if SIGCHLD is set to SIG_IGN then + * 1. children that terminate do not become zombies + * 2. call a to wait() will block until all children have terminated + * 3. the call to wait will fail with errno == ECHILD + * see for discussion: + * https://stackoverflow.com/questions/1608017/no-child-process-error-from-waitpid-when-waiting-for-process-group + */ + // signal(SIGCHLD, SIG_IGN); + + /* take control */ + shell.pid = getpid(); + if(setpgid(shell.pid, shell.pid)<0) + fatal("could not put shell in its own process group"); + + tcsetpgrp(0, shell.pid); + } +} + +// ----------------------------------------------------------------------- +// main point of entry int main(int argc, char *argv[]) @@ -17,31 +61,7 @@ main(int argc, char *argv[]) initenv(); initpath(); - - itoa(num, rcpid = getpid()); - setvar("pid", makeword(num, nil)); + initshell(); xboot(argc, argv); -#if 0 - Thread root = { - .cmd = { - .path = "", - .io = openfd(0), - }, - .line = 0, - .flag = { - .i = 1, - .eof = 0, - }, - }; - - shell = &root; - errio = openfd(2); -#if 1 - while(!yyparse()) - ; -#else - printkeycode() -#endif -#endif } -- cgit v1.2.1