From 8c776690f72a3afb887a0e19751d9b7f5025bd1b Mon Sep 17 00:00:00 2001 From: "John Hodge (sonata)" Date: Sat, 19 Jan 2013 19:31:37 +0800 Subject: [PATCH] Usermode/init - Added (untested) inittab support - Falls back to single VT (whatever was provided by kernel) when initab is unusable --- Usermode/Applications/init_src/common.h | 55 ++++ Usermode/Applications/init_src/main.c | 417 ++++++++++++++++++++++-- 2 files changed, 449 insertions(+), 23 deletions(-) create mode 100644 Usermode/Applications/init_src/common.h diff --git a/Usermode/Applications/init_src/common.h b/Usermode/Applications/init_src/common.h new file mode 100644 index 00000000..406fe6fd --- /dev/null +++ b/Usermode/Applications/init_src/common.h @@ -0,0 +1,55 @@ +/* + * Acess2 Userland init(8) + * - Userland root process + * + * common.h + * - Common type definitions + */ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +typedef struct sInitProgram tInitProgram; + +struct sKTerm +{ + int ID; +}; + +struct sSTerm +{ + uint32_t FormatBits; + unsigned int BaudRate; + char Path[]; +}; + +struct sDaemon +{ + char *StdoutPath; // heap + char *StderrPath; // heap +}; + +union uProgTypes +{ + struct sKTerm KTerm; + struct sSTerm STerm; + struct sDaemon Daemon; +}; + +enum eProgType +{ + PT_KTERM, + PT_STERM, + PT_DAEMON +}; + +struct sInitProgram +{ + tInitProgram *Next; + enum eProgType Type; + int CurrentPID; + char **Command; + union uProgTypes TypeInfo; +}; + +#endif + diff --git a/Usermode/Applications/init_src/main.c b/Usermode/Applications/init_src/main.c index 51b91942..0d8713d1 100644 --- a/Usermode/Applications/init_src/main.c +++ b/Usermode/Applications/init_src/main.c @@ -4,16 +4,37 @@ #include #include #include -//#include "common.h" +#include +#include "common.h" +#include // === CONSTANTS === -#define NUM_TERMS 4 -#define DEFAULT_TERMINAL "/Devices/VTerm/0" #define DEFAULT_SHELL "/Acess/SBin/login" +#define INITTAB_FILE "/Acess/Config/inittab" #define ARRAY_SIZE(x) ((sizeof(x))/(sizeof((x)[0]))) // === PROTOTYPES === + int ProcessInittab(const char *Path); +char *ReadQuotedString(FILE *FP); +char **ReadCommand(FILE *FP); +void FreeCommand(char **Command); + +tInitProgram *AllocateProgram(char **Command, enum eProgType Type, size_t ExtraSpace); +void RespawnProgam(tInitProgram *Program); + + int AddKTerminal(int TerminalSpec, char **Command); + int AddSerialTerminal(const char *DevPathSegment, const char *ModeStr, char **Command); + int AddDaemon(char *StdoutPath, char *StderrPath, char **Command); + + int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV); + int SpawnKTerm(tInitProgram *Program); + int SpawnSTerm(tInitProgram *Program); + int SpawnDaemon(tInitProgram *Program); + +// === GLOBALS === +const char *gsInittabPath = INITTAB_FILE; +tInitProgram *gpInitPrograms; // === CODE === /** @@ -22,33 +43,383 @@ */ int main(int argc, char *argv[]) { - int tid; - int i; - char termpath[sizeof(DEFAULT_TERMINAL)] = DEFAULT_TERMINAL; - char *child_argv[2] = {DEFAULT_SHELL, 0}; + // Parse commandline args + // TODO: cmdline + + // TODO: Behave differently if not invoked as first process? + + // Begin init process + if( ProcessInittab(gsInittabPath) != 0 ) + { + // No inittab file found, default to: + _SysDebug("inittab '%s' is invalid, falling back to one VT", gsInittabPath); + + for( ;; ) + { + int pid = SpawnCommand(0, 1, 1, (char *[]){DEFAULT_SHELL, NULL}); + _SysWaitTID(pid, NULL); + } + // TODO: Detect errors + } + + // TODO: Implement message watching + for(;;) + { + int pid, status; + pid = _SysWaitTID(0, &status); + _SysDebug("PID %i died, looking for respawn entry", pid); + } - // - Parse init script + return 42; +} + +char *ReadQuotedString(FILE *FP) +{ + char ch; + while( isblank(ch = fgetc(FP)) ) + ; + + if( ch == '\n' ) + return NULL; + + char *retstr = NULL; + int mode = 0; + int pos = 0, space = 0; + for( ; !feof(FP); ch = fgetc(FP) ) + { + int skip; + skip = 1; + if( mode & 4 ) { + // Add escaped/special character + skip = 0; + mode &= 3; + } + else if( mode == 0 ) + { + if( isspace(ch) ) + break; + else if( ch == '\\' ) + mode |= 4; + else if( ch == '"' ) + mode = 1; + else if( ch == '\'') + mode = 2; + else { + // Add character + skip = 0; + } + } + // Double-quoted string + else if( mode == 1 ) + { + if( ch == '"' ) + mode = 0; + else if( ch == '\\' ) + mode |= 4; + else { + // Add character + skip = 0; + } + } + // Single-quoted string + else if( mode == 2 ) + { + if( ch == '\'' ) + mode = 0; + else if( ch == '\\' ) + mode |= 4; + else { + // Add character + skip = 0; + } + } - // - Start virtual terminals - for( i = 0; i < NUM_TERMS; i++ ) - { - tid = _SysClone(CLONE_VM, 0); - if(tid == 0) + if( !skip ) { - termpath[sizeof(DEFAULT_TERMINAL)-2] = '0' + i; + if( pos == space ) { + space += 32; + void *tmp = realloc(retstr, space+1); + if( !tmp ) { + free(retstr); + return NULL; + } + retstr = tmp; + } + retstr[pos++] = ch; + } + } + retstr[pos] = '\0'; + return retstr; +} + +char **ReadCommand(FILE *FP) +{ + const int space = 8; + int pos = 0; + char **ret = malloc(space*sizeof(char*)); + char *arg; + do { + arg = ReadQuotedString(FP); + if( pos == space - 1 ) { + ret[pos] = NULL; + FreeCommand(ret); + return NULL; + } + ret[pos++] = arg; + } while(arg != NULL); + ret[pos] = NULL; + return ret; +} + +void FreeCommand(char **Command) +{ + int pos = 0; + while( Command[pos] ) + { + free(Command[pos]); + pos ++; + } + free(Command); +} + +int ProcessInittab(const char *Path) +{ + FILE *fp = fopen(Path, "r"); + + if( !fp ) + return 1; + + while(!feof(fp)) + { + char cmdbuf[64+1]; + if( fscanf(fp, "%64s%*[ \t]", &cmdbuf) != 1 ) + break; + + // Clear comments + if( cmdbuf[0] == '#' ) { + while( !feof(fp) && fgetc(fp) != '\n' ) + ; + continue ; + } + if( cmdbuf[0] == '\0' ) { + if( fgetc(fp) != '\n' ) { + fclose(fp); + return 2; // Unexpected character? + } + continue ; + } + + // Check commands + if( strcmp(cmdbuf, "ktty") == 0 ) { + // ktty + // - Spins off a console on the specified kernel TTY + int id = 0; + if( fscanf(fp, "%d ", &id) != 1 ) + goto lineError; + char **command = ReadCommand(fp); + if( !command ) + goto lineError; + AddKTerminal(id, command); + free(command); + } + else if(strcmp(cmdbuf, "stty") == 0 ) { + // stty [78][NOE][012][bB] + char path_seg[32+1]; + char modespec[4+6+1]; + if( fscanf(fp, "%32s %6s ", &path_seg, &modespec) != 2 ) { + goto lineError; + } + char **command = ReadCommand(fp); + if( !command ) + goto lineError; + AddSerialTerminal(path_seg, modespec, command); + } + else if(strcmp(cmdbuf, "daemon") == 0 ) { + // daemon + // - Runs a daemon (respawning) that logs to the specified files + // - Will append a header whenever the daemon starts + char *stdout_path = ReadQuotedString(fp); + char *stderr_path = ReadQuotedString(fp); + char **command = ReadCommand(fp); - _SysOpen(termpath, OPENFLAG_READ); // Stdin - _SysOpen(termpath, OPENFLAG_WRITE); // Stdout - _SysOpen(termpath, OPENFLAG_WRITE); // Stderr - _SysExecVE(DEFAULT_SHELL, child_argv, NULL); - for(;;) ; + AddDaemon(stdout_path, stderr_path, command); } + else if(strcmp(cmdbuf, "exec") == 0 ) { + // exec + // - Runs a command and waits for it to complete before continuing + // - NOTE: No other commands will respawn while this is running + char **command = ReadCommand(fp); + if(!command) + goto lineError; + + int handles[] = {0, 1, 2}; + int pid = _SysSpawn(command[0], (const char **)command, NULL, 3, handles, NULL); + _SysWaitTID(pid, NULL); + + FreeCommand(command); + } + else { + // Unknown command. + } + continue; + lineError: + while( !feof(fp) && fgetc(fp) != '\n' ) + ; + continue ; } + + fclose(fp); + return 0; +} + +tInitProgram *AllocateProgram(char **Command, enum eProgType Type, size_t ExtraSpace) +{ + tInitProgram *ret; - // TODO: Implement message watching - for(;;) - _SysWaitEvent(THREAD_EVENT_IPCMSG); + ret = malloc( sizeof(tInitProgram) - sizeof(union uProgTypes) + ExtraSpace ); + ret->Next = NULL; + ret->CurrentPID = 0; + ret->Type = Type; + ret->Command = Command; - return 42; + // Append + ret->Next = gpInitPrograms; + gpInitPrograms = ret; + + return ret; +} + +void RespawnProgam(tInitProgram *Program) +{ + int rv = 0; + switch(Program->Type) + { + case PT_KTERM: rv = SpawnKTerm(Program); break; + case PT_STERM: rv = SpawnSTerm(Program); break; + case PT_DAEMON: rv = SpawnDaemon(Program); break; + default: + _SysDebug("BUGCHECK - Program Type %i unknown", Program->Type); + break; + } + if( !rv ) { + _SysDebug("Respawn failure!"); + // TODO: Remove from list? + } +} + +int AddKTerminal(int TerminalID, char **Command) +{ + // TODO: Smarter validation + if( TerminalID < 0 || TerminalID > 7 ) + return -1; + + tInitProgram *ent = AllocateProgram(Command, PT_KTERM, sizeof(struct sKTerm)); + ent->TypeInfo.KTerm.ID = TerminalID; + + RespawnProgam(ent); + return 0; +} + +int AddSerialTerminal(const char *DevPathSegment, const char *ModeStr, char **Command) +{ + char dbit, parity, sbit; + int baud; + + // Parse mode string + if( sscanf(ModeStr, "%1[78]%1[NOE]%1[012]%*1[bB]%d", &dbit, &parity, &sbit, &baud) != 5 ) { + // Oops? + return -1; + } + + // Validate baud rate / build mode word + // TODO: Does baud rate need validation? + uint32_t modeword = 0; + modeword |= (dbit == '7' ? 1 : 0) << 0; + modeword |= (parity == 'O' ? 1 : (parity == 'E' ? 2 : 0)) << 1; + modeword |= (sbit - '0') << 3; + + // Create info + const char DEVPREFIX[] = "/Devices/"; + int pathlen = sizeof(DEVPREFIX) + strlen(DevPathSegment); + tInitProgram *ent = AllocateProgram(Command, PT_STERM, sizeof(struct sSTerm)+pathlen); + ent->TypeInfo.STerm.FormatBits = modeword; + ent->TypeInfo.STerm.BaudRate = baud; + strcpy(ent->TypeInfo.STerm.Path, DEVPREFIX); + strcat(ent->TypeInfo.STerm.Path, DevPathSegment); + + RespawnProgam(ent); + return 0; +} + +int AddDaemon(char *StdoutPath, char *StderrPath, char **Command) +{ + tInitProgram *ent = AllocateProgram(Command, PT_DAEMON, sizeof(struct sDaemon)); + ent->TypeInfo.Daemon.StdoutPath = StdoutPath; + ent->TypeInfo.Daemon.StderrPath = StderrPath; + + RespawnProgam(ent); + return 0; +} + +int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV) +{ + int handles[] = {c_stdin, c_stdout, c_stderr}; + + int rv = _SysSpawn(ArgV[0], (const char **)ArgV, NULL, 3, handles, NULL); + + _SysClose(c_stdin); + if( c_stdout != c_stdin ) + _SysClose(c_stdout); + if( c_stderr != c_stdin && c_stderr != c_stdin ) + _SysClose(c_stderr); + + return rv; +} + +int SpawnKTerm(tInitProgram *Program) +{ + const char fmt[] = "/Devices/VTerm/%i"; + char path[sizeof(fmt)]; + + snprintf(path, sizeof(path), fmt, Program->TypeInfo.KTerm.ID); + + int in = _SysOpen(path, OPENFLAG_READ); + int out = _SysOpen(path, OPENFLAG_WRITE); + + return SpawnCommand(in, out, out, Program->Command); +} + +int SpawnSTerm(tInitProgram *Program) +{ + int in = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_READ); + int out = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_WRITE); + + #if 0 + if( _SysIOCtl(in, 0, NULL) != DRV_TYPE_SERIAL ) + { + // Oops? + return -2; + } + _SysIOCtl(in, SERIAL_IOCTL_GETSETBAUD, &Program->TypeInfo.STerm.BaudRate); + _SysIOCtl(in, SERIAL_IOCTL_GETSETFORMAT, &Program->TypeInfo.STerm.FormatBits); + #endif + + return SpawnCommand(in, out, out, Program->Command); +} + +int SpawnDaemon(tInitProgram *Program) +{ + int in = _SysOpen("/Devices/null", OPENFLAG_READ); + int out = _SysOpen(Program->TypeInfo.Daemon.StdoutPath, OPENFLAG_WRITE); + int err = _SysOpen(Program->TypeInfo.Daemon.StderrPath, OPENFLAG_WRITE); + + if( in == -1 || out == -1 || err == -1 ) { + _SysClose(in); + _SysClose(out); + _SysClose(err); + return -2; + } + + return SpawnCommand(in, out, err, Program->Command); } -- 2.20.1