2 * Acess2 System Init Task
12 #define DEFAULT_SHELL "/Acess/SBin/login"
13 #define INITTAB_FILE "/Acess/Conf/inittab"
15 #define ARRAY_SIZE(x) ((sizeof(x))/(sizeof((x)[0])))
18 int ProcessInittab(const char *Path);
19 char *ReadQuotedString(FILE *FP);
20 char **ReadCommand(FILE *FP);
21 void FreeCommand(char **Command);
23 tInitProgram *AllocateProgram(char **Command, enum eProgType Type, size_t ExtraSpace);
24 void RespawnProgam(tInitProgram *Program);
26 int AddKTerminal(int TerminalSpec, char **Command);
27 int AddSerialTerminal(const char *DevPathSegment, const char *ModeStr, char **Command);
28 int AddDaemon(char *StdoutPath, char *StderrPath, char **Command);
30 int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV);
31 int SpawnKTerm(tInitProgram *Program);
32 int SpawnSTerm(tInitProgram *Program);
33 int SpawnDaemon(tInitProgram *Program);
36 const char *gsInittabPath = INITTAB_FILE;
37 tInitProgram *gpInitPrograms;
41 * \fn int main(int argc, char *argv[])
44 int main(int argc, char *argv[])
46 // Parse commandline args
49 // TODO: Behave differently if not invoked as first process?
52 if( ProcessInittab(gsInittabPath) != 0 )
54 // No inittab file found, default to:
55 _SysDebug("inittab '%s' is invalid, falling back to one VT", gsInittabPath);
60 int pid = SpawnCommand(0, 1, 1, (char *[]){DEFAULT_SHELL, NULL});
61 // TODO: Detect errors
62 _SysWaitTID(pid, NULL);
69 // TODO: Implement message watching
73 pid = _SysWaitTID(-1, &status);
74 _SysDebug("PID %i died, looking for respawn entry", pid);
80 char *ReadQuotedString(FILE *FP)
84 while( isblank(ch = fgetc(FP)) )
93 int pos = 0, space = 0;
94 for( ; !feof(FP); ch = fgetc(FP) )
99 // Add escaped/special character
106 fseek(FP, -1, SEEK_CUR);
109 else if( ch == '\\' )
120 // Double-quoted string
125 else if( ch == '\\' )
132 // Single-quoted string
137 else if( ch == '\\' )
149 void *tmp = realloc(retstr, space+1);
151 _SysDebug("ReadQuotedString - realloc(%i) failure", space+1);
165 char **ReadCommand(FILE *FP)
169 char **ret = malloc(space*sizeof(char*));
172 arg = ReadQuotedString(FP);
175 if( pos == space - 1 ) {
176 _SysDebug("Too many arguments %i", pos);
182 } while(arg != NULL);
193 void FreeCommand(char **Command)
196 while( Command[pos] )
204 int ProcessInittab(const char *Path)
207 FILE *fp = fopen(Path, "r");
219 if( (rv = fscanf(fp, "%64s%*[ \t]", cmdbuf)) != 1 ) {
220 _SysDebug("fscanf rv %i != exp 1", rv);
225 if( cmdbuf[0] == '#' ) {
226 while( !feof(fp) && fgetc(fp) != '\n' )
230 if( cmdbuf[0] == '\0' ) {
232 if( ch != '\n' && ch != -1 ) {
234 _SysDebug("Unexpected char 0x%x, expecting EOL", ch);
235 return 2; // Unexpected character?
241 if( strcmp(cmdbuf, "ktty") == 0 ) {
242 // ktty <ID> <command...>
243 // - Spins off a console on the specified kernel TTY
245 if( fscanf(fp, "%d ", &id) != 1 ) {
246 _SysDebug("init[ktty] - TTY ID read failed");
249 char **command = ReadCommand(fp);
251 _SysDebug("init[ktty] - Command read failure");
254 AddKTerminal(id, command);
257 else if(strcmp(cmdbuf, "stty") == 0 ) {
258 // stty <devpath> [78][NOE][012][bB]<baud> <command...>
260 char modespec[4+6+1];
261 if( fscanf(fp, "%32s %10s ", path_seg, modespec) != 2 ) {
264 char **command = ReadCommand(fp);
267 AddSerialTerminal(path_seg, modespec, command);
269 else if(strcmp(cmdbuf, "daemon") == 0 ) {
270 // daemon <stdout> <stderr> <command...>
271 // - Runs a daemon (respawning) that logs to the specified files
272 // - Will append a header whenever the daemon starts
273 char *stdout_path = ReadQuotedString(fp);
276 char *stderr_path = ReadQuotedString(fp);
279 char **command = ReadCommand(fp);
283 AddDaemon(stdout_path, stderr_path, command);
285 else if(strcmp(cmdbuf, "exec") == 0 ) {
287 // - Runs a command and waits for it to complete before continuing
288 // - NOTE: No other commands will respawn while this is running
289 char **command = ReadCommand(fp);
293 int handles[] = {0, 1, 2};
294 int pid = _SysSpawn(command[0], (const char **)command, NULL, 3, handles, NULL);
296 _SysWaitTID(pid, &retstatus);
297 _SysDebug("Command '%s' returned %i", command[0], retstatus);
299 FreeCommand(command);
303 _SysDebug("Unknown command '%s'", cmdbuf);
309 _SysDebug("label lineError: goto'd - line %i, cmdbuf='%s'", line_num, cmdbuf);
310 while( !feof(fp) && fgetc(fp) != '\n' )
317 if( gpInitPrograms == NULL )
323 tInitProgram *AllocateProgram(char **Command, enum eProgType Type, size_t ExtraSpace)
327 ret = malloc( sizeof(tInitProgram) - sizeof(union uProgTypes) + ExtraSpace );
331 ret->Command = Command;
334 ret->Next = gpInitPrograms;
335 gpInitPrograms = ret;
340 void RespawnProgam(tInitProgram *Program)
343 switch(Program->Type)
345 case PT_KTERM: rv = SpawnKTerm(Program); break;
346 case PT_STERM: rv = SpawnSTerm(Program); break;
347 case PT_DAEMON: rv = SpawnDaemon(Program); break;
349 _SysDebug("BUGCHECK - Program Type %i unknown", Program->Type);
353 _SysDebug("Respawn failure!");
354 // TODO: Remove from list?
358 int AddKTerminal(int TerminalID, char **Command)
360 // TODO: Smarter validation
361 if( TerminalID < 0 || TerminalID > 7 )
364 tInitProgram *ent = AllocateProgram(Command, PT_KTERM, sizeof(struct sKTerm));
365 ent->TypeInfo.KTerm.ID = TerminalID;
371 int AddSerialTerminal(const char *DevPathSegment, const char *ModeStr, char **Command)
373 char dbit, parity, sbit;
377 if( sscanf(ModeStr, "%1[78]%1[NOE]%1[012]%*1[bB]%d", &dbit, &parity, &sbit, &baud) != 4 ) {
379 _SysDebug("Serial mode string is invalid ('%s')", ModeStr);
383 // Validate baud rate / build mode word
384 // TODO: Does baud rate need validation?
385 uint32_t modeword = 0;
386 modeword |= (dbit == '7' ? 1 : 0) << 0;
387 modeword |= (parity == 'O' ? 1 : (parity == 'E' ? 2 : 0)) << 1;
388 modeword |= (sbit - '0') << 3;
391 const char DEVPREFIX[] = "/Devices/";
392 int pathlen = sizeof(DEVPREFIX) + strlen(DevPathSegment);
393 tInitProgram *ent = AllocateProgram(Command, PT_STERM, sizeof(struct sSTerm)+pathlen);
394 ent->TypeInfo.STerm.FormatBits = modeword;
395 ent->TypeInfo.STerm.BaudRate = baud;
396 strcpy(ent->TypeInfo.STerm.Path, DEVPREFIX);
397 strcat(ent->TypeInfo.STerm.Path, DevPathSegment);
403 int AddDaemon(char *StdoutPath, char *StderrPath, char **Command)
405 tInitProgram *ent = AllocateProgram(Command, PT_DAEMON, sizeof(struct sDaemon));
406 ent->TypeInfo.Daemon.StdoutPath = StdoutPath;
407 ent->TypeInfo.Daemon.StderrPath = StderrPath;
413 int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV)
415 int handles[] = {c_stdin, c_stdout, c_stderr};
417 int rv = _SysSpawn(ArgV[0], (const char **)ArgV, NULL, 3, handles, NULL);
420 if( c_stdout != c_stdin )
422 if( c_stderr != c_stdin && c_stderr != c_stdout )
428 int SpawnKTerm(tInitProgram *Program)
430 const char fmt[] = "/Devices/VTerm/%i";
431 char path[sizeof(fmt)];
433 snprintf(path, sizeof(path), fmt, Program->TypeInfo.KTerm.ID);
435 int in = _SysOpen(path, OPENFLAG_READ);
436 int out = _SysOpen(path, OPENFLAG_WRITE);
438 return SpawnCommand(in, out, out, Program->Command);
441 int SpawnSTerm(tInitProgram *Program)
443 int in = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_READ);
444 int out = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_WRITE);
446 if(in == -1 || out == -1 ) {
447 _SysDebug("Unable to open serial '%s' for '%s'", Program->TypeInfo.STerm.Path, Program->Command);
452 if( _SysIOCtl(in, 0, NULL) != DRV_TYPE_SERIAL )
457 _SysIOCtl(in, SERIAL_IOCTL_GETSETBAUD, &Program->TypeInfo.STerm.BaudRate);
458 _SysIOCtl(in, SERIAL_IOCTL_GETSETFORMAT, &Program->TypeInfo.STerm.FormatBits);
461 return SpawnCommand(in, out, out, Program->Command);
464 int SpawnDaemon(tInitProgram *Program)
466 int in = _SysOpen("/Devices/null", OPENFLAG_READ);
467 int out = _SysOpen(Program->TypeInfo.Daemon.StdoutPath, OPENFLAG_WRITE);
468 int err = _SysOpen(Program->TypeInfo.Daemon.StderrPath, OPENFLAG_WRITE);
470 if( in == -1 || out == -1 || err == -1 ) {
480 size_t len = snprintf(buffer, 100, "[%i] init spawning '%s'\n", _SysTimestamp(), Program->Command);
481 _SysWrite(out, buffer, len);
484 return SpawnCommand(in, out, err, Program->Command);