root/user/init.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 // init: The initial user-level program
   2 
   3 #include "kernel/types.h"
   4 #include "kernel/stat.h"
   5 #include "kernel/spinlock.h"
   6 #include "kernel/sleeplock.h"
   7 #include "kernel/fs.h"
   8 #include "kernel/file.h"
   9 #include "user/user.h"
  10 #include "kernel/fcntl.h"
  11 
  12 char *argv[] = { "sh", 0 };
  13 
  14 int
  15 main(void)
  16 {
  17   int pid, wpid;
  18 
  19   if(open("console", O_RDWR) < 0){
  20     mknod("console", CONSOLE, 0);
  21     open("console", O_RDWR);
  22   }
  23   dup(0);  // stdout
  24   dup(0);  // stderr
  25 
  26   for(;;){
  27     printf("init: starting sh\n");
  28     pid = fork();
  29     if(pid < 0){
  30       printf("init: fork failed\n");
  31       exit(1);
  32     }
  33     if(pid == 0){
  34       exec("sh", argv);
  35       printf("init: exec sh failed\n");
  36       exit(1);
  37     }
  38 
  39     for(;;){
  40       // this call to wait() returns if the shell exits,
  41       // or if a parentless process exits.
  42       wpid = wait((int *) 0);
  43       if(wpid == pid){
  44         // the shell exited; restart it.
  45         break;
  46       } else if(wpid < 0){
  47         printf("init: wait returned an error\n");
  48         exit(1);
  49       } else {
  50         // it was a parentless process; do nothing.
  51       }
  52     }
  53   }
  54 }

/* [<][>][^][v][top][bottom][index][help] */