Externals/cross-compiler - Fixed sysroot to be arch-specific
[tpg/acess2.git] / Usermode / Applications / init_src / main.c
1 /*
2  * Acess2 System Init Task
3  */
4 #include <acess/sys.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include "common.h"
9 #include <ctype.h>
10
11 // === CONSTANTS ===
12 #define DEFAULT_SHELL   "/Acess/SBin/login"
13 #define INITTAB_FILE    "/Acess/Conf/inittab"
14
15 #define ARRAY_SIZE(x)   ((sizeof(x))/(sizeof((x)[0])))
16
17 // === PROTOTYPES ===
18  int    ProcessInittab(const char *Path);
19 char    *ReadQuotedString(FILE *FP);
20 char    **ReadCommand(FILE *FP);
21 void    FreeCommand(char **Command);
22
23 tInitProgram    *AllocateProgram(char **Command, enum eProgType Type, size_t ExtraSpace);
24 void    RespawnProgam(tInitProgram *Program);
25
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);
29
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);
34
35 // === GLOBALS ===
36 const char      *gsInittabPath = INITTAB_FILE;
37 tInitProgram    *gpInitPrograms;
38
39 // === CODE ===
40 /**
41  * \fn int main(int argc, char *argv[])
42  * \brief Entrypoint
43  */
44 int main(int argc, char *argv[])
45 {
46         // Parse commandline args
47         // TODO: cmdline
48
49         // TODO: Behave differently if not invoked as first process?
50
51         // Begin init process
52         if( ProcessInittab(gsInittabPath) != 0 )
53         {
54                 // No inittab file found, default to:
55                 _SysDebug("inittab '%s' is invalid, falling back to one VT", gsInittabPath);
56                 
57                 #if 0
58                 for( ;; )
59                 {
60                         int pid = SpawnCommand(0, 1, 1, (char *[]){DEFAULT_SHELL, NULL});
61                         // TODO: Detect errors
62                         _SysWaitTID(pid, NULL);
63                 }
64                 #else
65                 return 41;
66                 #endif
67         }
68
69         // TODO: Implement message watching
70         for(;;)
71         {
72                  int    pid, status;
73                 pid = _SysWaitTID(-1, &status);
74                 _SysDebug("PID %i died, looking for respawn entry", pid);
75         }
76         
77         return 42;
78 }
79
80 char *ReadQuotedString(FILE *FP)
81 {
82         char    ch;
83         
84         while( isblank(ch = fgetc(FP)) )
85                 ;
86
87         if( ch == '\n' ) {
88                 return NULL;
89         }
90
91         char    *retstr = NULL;
92          int    mode = 0;
93          int    pos = 0, space = 0;
94         for( ; !feof(FP); ch = fgetc(FP) )
95         {
96                  int    skip;
97                 skip = 1;
98                 if( mode & 4 ) {
99                         // Add escaped/special character
100                         skip = 0;
101                         mode &= 3;
102                 }
103                 else if( mode == 0 )
104                 {
105                         if( isspace(ch) ) {
106                                 fseek(FP, -1, SEEK_CUR);
107                                 break;
108                         }
109                         else if( ch == '\\' )
110                                 mode |= 4;
111                         else if( ch == '"' )
112                                 mode = 1;
113                         else if( ch == '\'')
114                                 mode = 2;
115                         else {
116                                 // Add character
117                                 skip = 0;
118                         }
119                 }
120                 // Double-quoted string
121                 else if( mode == 1 )
122                 {
123                         if( ch == '"' )
124                                 mode = 0;
125                         else if( ch == '\\' )
126                                 mode |= 4;
127                         else {
128                                 // Add character
129                                 skip = 0;
130                         }
131                 }
132                 // Single-quoted string
133                 else if( mode == 2 )
134                 {
135                         if( ch == '\'' )
136                                 mode = 0;
137                         else if( ch == '\\' )
138                                 mode |= 4;
139                         else {
140                                 // Add character
141                                 skip = 0;
142                         }
143                 }
144         
145                 if( !skip )
146                 {
147                         if( pos == space ) {
148                                 space += 32;
149                                 void *tmp = realloc(retstr, space+1);
150                                 if( !tmp ) {
151                                         _SysDebug("ReadQuotedString - realloc(%i) failure", space+1);
152                                         free(retstr);
153                                         return NULL;
154                                 }
155                                 retstr = tmp;
156                         }
157                         retstr[pos++] = ch;
158                 }
159         }
160         retstr[pos] = '\0';
161         return retstr;
162 }
163
164 char **ReadCommand(FILE *FP)
165 {
166         const int       space = 8;
167          int    pos = 0;
168         char    **ret = malloc(space*sizeof(char*));
169         char    *arg;
170         do {
171                 arg = ReadQuotedString(FP);
172                 if(arg == NULL)
173                         break;
174                 if( pos == space - 1 ) {
175                         _SysDebug("Too many arguments %i", pos);
176                         ret[pos] = NULL;
177                         FreeCommand(ret);
178                         return NULL;
179                 }
180                 ret[pos++] = arg;
181         } while(arg != NULL);
182         ret[pos] = NULL;
183         return ret;
184 }
185
186 void FreeCommand(char **Command)
187 {
188         int pos = 0;
189         while( Command[pos] )
190         {
191                 free(Command[pos]);
192                 pos ++;
193         }
194         free(Command);
195 }
196
197 int ProcessInittab(const char *Path)
198 {
199         FILE    *fp = fopen(Path, "r");
200
201         if( !fp )
202                 return 1;       
203
204         while(!feof(fp))
205         {
206                 char cmdbuf[64+1];
207                 
208                  int    rv;
209                 if( (rv = fscanf(fp, "%64s%*[ \t]", &cmdbuf)) != 1 ) {
210                         _SysDebug("fscanf rv %i != exp 1", rv);
211                         break;
212                 }
213                 
214                 // Clear comments
215                 if( cmdbuf[0] == '#' ) {
216                         while( !feof(fp) && fgetc(fp) != '\n' )
217                                 ;
218                         continue ;
219                 }
220                 if( cmdbuf[0] == '\0' ) {
221                         char    ch = fgetc(fp);
222                         if( ch != '\n' && ch != -1 ) {
223                                 fclose(fp);
224                                 _SysDebug("Unexpected char 0x%x, expecting EOL", ch);
225                                 return 2;       // Unexpected character?
226                         }
227                         continue ;
228                 }
229
230                 // Check commands
231                 if( strcmp(cmdbuf, "ktty") == 0 ) {
232                         // ktty <ID> <command...>
233                         // - Spins off a console on the specified kernel TTY
234                          int    id = 0;
235                         if( fscanf(fp, "%d ", &id) != 1 ) {
236                                 _SysDebug("init[ktty] - TTY ID read failed");
237                                 goto lineError;
238                         }
239                         char    **command = ReadCommand(fp);
240                         if( !command ) {
241                                 _SysDebug("init[ktty] - Command read failure");
242                                 goto lineError;
243                         }
244                         AddKTerminal(id, command);
245                         free(command);
246                 }
247                 else if(strcmp(cmdbuf, "stty") == 0 ) {
248                         // stty <devpath> [78][NOE][012][bB]<baud> <command...>
249                         char    path_seg[32+1];
250                         char    modespec[4+6+1];
251                         if( fscanf(fp, "%32s %6s ", &path_seg, &modespec) != 2 ) {
252                                 goto lineError;
253                         }
254                         char **command = ReadCommand(fp);
255                         if( !command )
256                                 goto lineError;
257                         AddSerialTerminal(path_seg, modespec, command);
258                 }
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);
266                         
267                         AddDaemon(stdout_path, stderr_path, command);
268                 }
269                 else if(strcmp(cmdbuf, "exec") == 0 ) {
270                         // exec <command...>
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);
274                         if(!command)
275                                 goto lineError;
276
277                         int handles[] = {0, 1, 2};
278                         int pid = _SysSpawn(command[0], (const char **)command, NULL, 3, handles, NULL);
279                         int retstatus;
280                         _SysWaitTID(pid, &retstatus);
281                         _SysDebug("Command '%s' returned %i", command[0], retstatus);
282
283                         FreeCommand(command);
284                 }
285                 else {
286                         // Unknown command.
287                         _SysDebug("Unknown command '%s'", cmdbuf);
288                         goto lineError;
289                 }
290                 fscanf(fp, " ");
291                 continue;
292         lineError:
293                 _SysDebug("label lineError: goto'd");
294                 while( !feof(fp) && fgetc(fp) != '\n' )
295                         ;
296                 continue ;
297         }
298
299         fclose(fp);
300         return 0;
301 }
302
303 tInitProgram *AllocateProgram(char **Command, enum eProgType Type, size_t ExtraSpace)
304 {
305         tInitProgram    *ret;
306         
307         ret = malloc( sizeof(tInitProgram) - sizeof(union uProgTypes) + ExtraSpace );
308         ret->Next = NULL;
309         ret->CurrentPID = 0;
310         ret->Type = Type;
311         ret->Command = Command;
312         
313         // Append
314         ret->Next = gpInitPrograms;
315         gpInitPrograms = ret;
316         
317         return ret;
318 }
319
320 void RespawnProgam(tInitProgram *Program)
321 {
322          int    rv = 0;
323         switch(Program->Type)
324         {
325         case PT_KTERM:  rv = SpawnKTerm(Program);       break;
326         case PT_STERM:  rv = SpawnSTerm(Program);       break;
327         case PT_DAEMON: rv = SpawnDaemon(Program);      break;
328         default:
329                 _SysDebug("BUGCHECK - Program Type %i unknown", Program->Type);
330                 break;
331         }
332         if( !rv ) {
333                 _SysDebug("Respawn failure!");
334                 // TODO: Remove from list?
335         }
336 }
337
338 int AddKTerminal(int TerminalID, char **Command)
339 {
340         // TODO: Smarter validation
341         if( TerminalID < 0 || TerminalID > 7 )
342                 return -1;
343         
344         tInitProgram    *ent = AllocateProgram(Command, PT_KTERM, sizeof(struct sKTerm));
345         ent->TypeInfo.KTerm.ID = TerminalID;
346
347         RespawnProgam(ent);
348         return 0;
349 }
350
351 int AddSerialTerminal(const char *DevPathSegment, const char *ModeStr, char **Command)
352 {
353         char    dbit, parity, sbit;
354          int    baud;
355
356         // Parse mode string
357         if( sscanf(ModeStr, "%1[78]%1[NOE]%1[012]%*1[bB]%d", &dbit, &parity, &sbit, &baud) != 5 ) {
358                 // Oops?
359                 return -1;
360         }
361         
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;
368         
369         // Create info
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);
377
378         RespawnProgam(ent);
379         return 0;
380 }
381
382 int AddDaemon(char *StdoutPath, char *StderrPath, char **Command)
383 {
384         tInitProgram    *ent = AllocateProgram(Command, PT_DAEMON, sizeof(struct sDaemon));
385         ent->TypeInfo.Daemon.StdoutPath = StdoutPath;
386         ent->TypeInfo.Daemon.StderrPath = StderrPath;
387         
388         RespawnProgam(ent);
389         return 0;
390 }
391
392 int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV)
393 {
394          int    handles[] = {c_stdin, c_stdout, c_stderr};
395
396         int rv = _SysSpawn(ArgV[0], (const char **)ArgV, NULL, 3, handles, NULL);
397
398         _SysClose(c_stdin);
399         if( c_stdout != c_stdin )
400                 _SysClose(c_stdout);
401         if( c_stderr != c_stdin && c_stderr != c_stdout )
402                 _SysClose(c_stderr);
403
404         return rv;
405 }
406
407 int SpawnKTerm(tInitProgram *Program)
408 {
409         const char fmt[] = "/Devices/VTerm/%i";
410         char    path[sizeof(fmt)];
411         
412         snprintf(path, sizeof(path), fmt, Program->TypeInfo.KTerm.ID);
413         
414          int    in = _SysOpen(path, OPENFLAG_READ);
415          int    out = _SysOpen(path, OPENFLAG_WRITE);
416         
417         return SpawnCommand(in, out, out, Program->Command);
418 }
419
420 int SpawnSTerm(tInitProgram *Program)
421 {
422          int    in = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_READ);
423          int    out = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_WRITE);
424
425         #if 0
426         if( _SysIOCtl(in, 0, NULL) != DRV_TYPE_SERIAL )
427         {
428                 // Oops?
429                 return -2;
430         }
431         _SysIOCtl(in, SERIAL_IOCTL_GETSETBAUD, &Program->TypeInfo.STerm.BaudRate);
432         _SysIOCtl(in, SERIAL_IOCTL_GETSETFORMAT, &Program->TypeInfo.STerm.FormatBits);
433         #endif
434
435         return SpawnCommand(in, out, out, Program->Command);
436 }
437
438 int SpawnDaemon(tInitProgram *Program)
439 {
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);
443         
444         if( in == -1 || out == -1 || err == -1 ) {
445                 _SysClose(in);
446                 _SysClose(out);
447                 _SysClose(err);
448                 return -2;
449         }
450         
451         return SpawnCommand(in, out, err, Program->Command);
452 }
453

UCC git Repository :: git.ucc.asn.au