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);
164 char **ReadCommand(FILE *FP)
168 char **ret = malloc(space*sizeof(char*));
171 arg = ReadQuotedString(FP);
174 if( pos == space - 1 ) {
175 _SysDebug("Too many arguments %i", pos);
181 } while(arg != NULL);
186 void FreeCommand(char **Command)
189 while( Command[pos] )
197 int ProcessInittab(const char *Path)
199 FILE *fp = fopen(Path, "r");
209 if( (rv = fscanf(fp, "%64s%*[ \t]", &cmdbuf)) != 1 ) {
210 _SysDebug("fscanf rv %i != exp 1", rv);
215 if( cmdbuf[0] == '#' ) {
216 while( !feof(fp) && fgetc(fp) != '\n' )
220 if( cmdbuf[0] == '\0' ) {
222 if( ch != '\n' && ch != -1 ) {
224 _SysDebug("Unexpected char 0x%x, expecting EOL", ch);
225 return 2; // Unexpected character?
231 if( strcmp(cmdbuf, "ktty") == 0 ) {
232 // ktty <ID> <command...>
233 // - Spins off a console on the specified kernel TTY
235 if( fscanf(fp, "%d ", &id) != 1 ) {
236 _SysDebug("init[ktty] - TTY ID read failed");
239 char **command = ReadCommand(fp);
241 _SysDebug("init[ktty] - Command read failure");
244 AddKTerminal(id, command);
247 else if(strcmp(cmdbuf, "stty") == 0 ) {
248 // stty <devpath> [78][NOE][012][bB]<baud> <command...>
250 char modespec[4+6+1];
251 if( fscanf(fp, "%32s %6s ", &path_seg, &modespec) != 2 ) {
254 char **command = ReadCommand(fp);
257 AddSerialTerminal(path_seg, modespec, command);
259 else if(strcmp(cmdbuf, "daemon") == 0 ) {
260 // daemon <stdout> <stderr> <command...>
261 // - Runs a daemon (respawning) that logs to the specified files
262 // - Will append a header whenever the daemon starts
263 char *stdout_path = ReadQuotedString(fp);
264 char *stderr_path = ReadQuotedString(fp);
265 char **command = ReadCommand(fp);
267 AddDaemon(stdout_path, stderr_path, command);
269 else if(strcmp(cmdbuf, "exec") == 0 ) {
271 // - Runs a command and waits for it to complete before continuing
272 // - NOTE: No other commands will respawn while this is running
273 char **command = ReadCommand(fp);
277 int handles[] = {0, 1, 2};
278 int pid = _SysSpawn(command[0], (const char **)command, NULL, 3, handles, NULL);
280 _SysWaitTID(pid, &retstatus);
281 _SysDebug("Command '%s' returned %i", command[0], retstatus);
283 FreeCommand(command);
287 _SysDebug("Unknown command '%s'", cmdbuf);
293 _SysDebug("label lineError: goto'd");
294 while( !feof(fp) && fgetc(fp) != '\n' )
303 tInitProgram *AllocateProgram(char **Command, enum eProgType Type, size_t ExtraSpace)
307 ret = malloc( sizeof(tInitProgram) - sizeof(union uProgTypes) + ExtraSpace );
311 ret->Command = Command;
314 ret->Next = gpInitPrograms;
315 gpInitPrograms = ret;
320 void RespawnProgam(tInitProgram *Program)
323 switch(Program->Type)
325 case PT_KTERM: rv = SpawnKTerm(Program); break;
326 case PT_STERM: rv = SpawnSTerm(Program); break;
327 case PT_DAEMON: rv = SpawnDaemon(Program); break;
329 _SysDebug("BUGCHECK - Program Type %i unknown", Program->Type);
333 _SysDebug("Respawn failure!");
334 // TODO: Remove from list?
338 int AddKTerminal(int TerminalID, char **Command)
340 // TODO: Smarter validation
341 if( TerminalID < 0 || TerminalID > 7 )
344 tInitProgram *ent = AllocateProgram(Command, PT_KTERM, sizeof(struct sKTerm));
345 ent->TypeInfo.KTerm.ID = TerminalID;
351 int AddSerialTerminal(const char *DevPathSegment, const char *ModeStr, char **Command)
353 char dbit, parity, sbit;
357 if( sscanf(ModeStr, "%1[78]%1[NOE]%1[012]%*1[bB]%d", &dbit, &parity, &sbit, &baud) != 5 ) {
362 // Validate baud rate / build mode word
363 // TODO: Does baud rate need validation?
364 uint32_t modeword = 0;
365 modeword |= (dbit == '7' ? 1 : 0) << 0;
366 modeword |= (parity == 'O' ? 1 : (parity == 'E' ? 2 : 0)) << 1;
367 modeword |= (sbit - '0') << 3;
370 const char DEVPREFIX[] = "/Devices/";
371 int pathlen = sizeof(DEVPREFIX) + strlen(DevPathSegment);
372 tInitProgram *ent = AllocateProgram(Command, PT_STERM, sizeof(struct sSTerm)+pathlen);
373 ent->TypeInfo.STerm.FormatBits = modeword;
374 ent->TypeInfo.STerm.BaudRate = baud;
375 strcpy(ent->TypeInfo.STerm.Path, DEVPREFIX);
376 strcat(ent->TypeInfo.STerm.Path, DevPathSegment);
382 int AddDaemon(char *StdoutPath, char *StderrPath, char **Command)
384 tInitProgram *ent = AllocateProgram(Command, PT_DAEMON, sizeof(struct sDaemon));
385 ent->TypeInfo.Daemon.StdoutPath = StdoutPath;
386 ent->TypeInfo.Daemon.StderrPath = StderrPath;
392 int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV)
394 int handles[] = {c_stdin, c_stdout, c_stderr};
396 int rv = _SysSpawn(ArgV[0], (const char **)ArgV, NULL, 3, handles, NULL);
399 if( c_stdout != c_stdin )
401 if( c_stderr != c_stdin && c_stderr != c_stdout )
407 int SpawnKTerm(tInitProgram *Program)
409 const char fmt[] = "/Devices/VTerm/%i";
410 char path[sizeof(fmt)];
412 snprintf(path, sizeof(path), fmt, Program->TypeInfo.KTerm.ID);
414 int in = _SysOpen(path, OPENFLAG_READ);
415 int out = _SysOpen(path, OPENFLAG_WRITE);
417 return SpawnCommand(in, out, out, Program->Command);
420 int SpawnSTerm(tInitProgram *Program)
422 int in = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_READ);
423 int out = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_WRITE);
426 if( _SysIOCtl(in, 0, NULL) != DRV_TYPE_SERIAL )
431 _SysIOCtl(in, SERIAL_IOCTL_GETSETBAUD, &Program->TypeInfo.STerm.BaudRate);
432 _SysIOCtl(in, SERIAL_IOCTL_GETSETFORMAT, &Program->TypeInfo.STerm.FormatBits);
435 return SpawnCommand(in, out, out, Program->Command);
438 int SpawnDaemon(tInitProgram *Program)
440 int in = _SysOpen("/Devices/null", OPENFLAG_READ);
441 int out = _SysOpen(Program->TypeInfo.Daemon.StdoutPath, OPENFLAG_WRITE);
442 int err = _SysOpen(Program->TypeInfo.Daemon.StderrPath, OPENFLAG_WRITE);
444 if( in == -1 || out == -1 || err == -1 ) {
451 return SpawnCommand(in, out, err, Program->Command);