X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Finit_src%2Fmain.c;h=ac1e84892bb8e06a474cb39ed9c13191ca912601;hb=470e38ce75ff3e33390c78690faaa84f833b1cf3;hp=a3d737d94d63524f73535e32c82956b515b1a496;hpb=7f05e2b2f029c1e9709a9be14dbeae159bb2f1cc;p=tpg%2Facess2.git diff --git a/Usermode/Applications/init_src/main.c b/Usermode/Applications/init_src/main.c index a3d737d9..ac1e8489 100644 --- a/Usermode/Applications/init_src/main.c +++ b/Usermode/Applications/init_src/main.c @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) // No inittab file found, default to: _SysDebug("inittab '%s' is invalid, falling back to one VT", gsInittabPath); - #if 0 + #if 1 for( ;; ) { int pid = SpawnCommand(0, 1, 1, (char *[]){DEFAULT_SHELL, NULL}); @@ -157,7 +157,8 @@ char *ReadQuotedString(FILE *FP) retstr[pos++] = ch; } } - retstr[pos] = '\0'; + if( retstr ) + retstr[pos] = '\0'; return retstr; } @@ -179,6 +180,12 @@ char **ReadCommand(FILE *FP) } ret[pos++] = arg; } while(arg != NULL); + + if( pos == 0 ) + { + free(ret); + return NULL; + } ret[pos] = NULL; return ret; } @@ -196,6 +203,7 @@ void FreeCommand(char **Command) int ProcessInittab(const char *Path) { + int line_num = 0; FILE *fp = fopen(Path, "r"); if( !fp ) @@ -205,8 +213,10 @@ int ProcessInittab(const char *Path) { char cmdbuf[64+1]; + line_num ++; + int rv; - if( (rv = fscanf(fp, "%64s%*[ \t]", &cmdbuf)) != 1 ) { + if( (rv = fscanf(fp, "%64s%*[ \t]", cmdbuf)) != 1 ) { _SysDebug("fscanf rv %i != exp 1", rv); break; } @@ -242,13 +252,12 @@ int ProcessInittab(const char *Path) 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 ) { + if( fscanf(fp, "%32s %10s ", path_seg, modespec) != 2 ) { goto lineError; } char **command = ReadCommand(fp); @@ -261,8 +270,14 @@ int ProcessInittab(const char *Path) // - Runs a daemon (respawning) that logs to the specified files // - Will append a header whenever the daemon starts char *stdout_path = ReadQuotedString(fp); + if( !stdout_path ) + goto lineError; char *stderr_path = ReadQuotedString(fp); + if( !stderr_path ) + goto lineError; char **command = ReadCommand(fp); + if( !command ) + goto lineError; AddDaemon(stdout_path, stderr_path, command); } @@ -274,7 +289,7 @@ int ProcessInittab(const char *Path) if(!command) goto lineError; - int handles[] = {0, 1, 2}; + int handles[] = {0, 1, 1}; int pid = _SysSpawn(command[0], (const char **)command, NULL, 3, handles, NULL); int retstatus; _SysWaitTID(pid, &retstatus); @@ -290,13 +305,17 @@ int ProcessInittab(const char *Path) fscanf(fp, " "); continue; lineError: - _SysDebug("label lineError: goto'd"); + _SysDebug("label lineError: goto'd - line %i, cmdbuf='%s'", line_num, cmdbuf); while( !feof(fp) && fgetc(fp) != '\n' ) ; continue ; } fclose(fp); + + if( gpInitPrograms == NULL ) + return 2; + return 0; } @@ -354,8 +373,9 @@ int AddSerialTerminal(const char *DevPathSegment, const char *ModeStr, char **Co int baud; // Parse mode string - if( sscanf(ModeStr, "%1[78]%1[NOE]%1[012]%*1[bB]%d", &dbit, &parity, &sbit, &baud) != 5 ) { + if( sscanf(ModeStr, "%1[78]%1[NOE]%1[012]%*1[bB]%d", &dbit, &parity, &sbit, &baud) != 4 ) { // Oops? + _SysDebug("Serial mode string is invalid ('%s')", ModeStr); return -1; } @@ -367,7 +387,7 @@ int AddSerialTerminal(const char *DevPathSegment, const char *ModeStr, char **Co modeword |= (sbit - '0') << 3; // Create info - const char DEVPREFIX[] = "/Devices/"; + const char DEVPREFIX[] = "/Devices/pts/serial"; int pathlen = sizeof(DEVPREFIX) + strlen(DevPathSegment); tInitProgram *ent = AllocateProgram(Command, PT_STERM, sizeof(struct sSTerm)+pathlen); ent->TypeInfo.STerm.FormatBits = modeword; @@ -393,6 +413,21 @@ int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV) { int handles[] = {c_stdin, c_stdout, c_stderr}; + _SysDebug("Spawning '%s'", ArgV[0]); + + if( c_stdin < 0 ) { + _SysDebug("SpawnCommand: stdin is invalid"); + return 1; + } + if( c_stdout < 0 ) { + _SysDebug("SpawnCommand: stdout is invalid"); + return 1; + } + if( c_stderr < 0 ) { + _SysDebug("SpawnCommand: stderr is invalid"); + return 1; + } + int rv = _SysSpawn(ArgV[0], (const char **)ArgV, NULL, 3, handles, NULL); _SysClose(c_stdin); @@ -406,7 +441,7 @@ int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV) int SpawnKTerm(tInitProgram *Program) { - const char fmt[] = "/Devices/VTerm/%i"; + const char fmt[] = "/Devices/pts/vt%i"; char path[sizeof(fmt)]; snprintf(path, sizeof(path), fmt, Program->TypeInfo.KTerm.ID); @@ -422,6 +457,11 @@ int SpawnSTerm(tInitProgram *Program) int in = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_READ); int out = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_WRITE); + if(in == -1 || out == -1 ) { + _SysDebug("Unable to open serial port '%s'", Program->TypeInfo.STerm.Path); + return -1; + } + #if 0 if( _SysIOCtl(in, 0, NULL) != DRV_TYPE_SERIAL ) { @@ -441,13 +481,30 @@ int SpawnDaemon(tInitProgram *Program) 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 ) { + if( in < 0 || out < 0 || err < 0 ) { + perror("SpawnDaemon"); _SysClose(in); _SysClose(out); _SysClose(err); return -2; } + // Log spawn header + { + char buffer[101]; + size_t len = snprintf(buffer, 100, "[%lli] init spawning ", _SysTimestamp()); + _SysWrite(out, buffer, len); + char ch = '\''; + for( int i = 0; Program->Command[i]; i ++ ) + { + _SysWrite(out, &ch, 1); + _SysWrite(out, Program->Command[i], strlen(Program->Command[i])); + _SysWrite(out, &ch, 1); + } + ch = '\n'; + _SysWrite(out, &ch, 1); + } + return SpawnCommand(in, out, err, Program->Command); }