| 1 | /////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 2 | // File   :  init.c | 
|---|
| 3 | // Date   :  January 2018 | 
|---|
| 4 | // Author :  Alain Greiner | 
|---|
| 5 | /////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 6 | // This single thread application implement the "init" process for ALMOS-MKH.  | 
|---|
| 7 | // It uses the fork/exec syscalls to create N KSH child processes  | 
|---|
| 8 | // (one child process per user TXT terminal). | 
|---|
| 9 | // Then calls the wait() function to block, and recreate any child KSH process | 
|---|
| 10 | // that has been deleted, using a new fork/exec. | 
|---|
| 11 | /////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 12 |  | 
|---|
| 13 | #include <unistd.h> | 
|---|
| 14 | #include <stdlib.h> | 
|---|
| 15 | #include <stdio.h> | 
|---|
| 16 | #include <pthread.h> | 
|---|
| 17 | #include <almosmkh.h> | 
|---|
| 18 | #include <hal_macros.h> | 
|---|
| 19 | #include <sys/wait.h> | 
|---|
| 20 | #include <shared_syscalls.h> | 
|---|
| 21 |  | 
|---|
| 22 | #define DEBUG_PROCESS_INIT    0 | 
|---|
| 23 |  | 
|---|
| 24 | //////////////// | 
|---|
| 25 | int main( void ) | 
|---|
| 26 | { | 
|---|
| 27 |     unsigned int  i; | 
|---|
| 28 |     int           ret_fork;      // fork return value   | 
|---|
| 29 |     int           ret_exec;      // exec return value   | 
|---|
| 30 |     int           rcv_pid;       // pid received from the wait syscall | 
|---|
| 31 |     int           status;        // used by the wait syscall | 
|---|
| 32 |     char          string[64];    // log messages on kernel TXT0 | 
|---|
| 33 |  | 
|---|
| 34 | #if DEBUG_PROCESS_INIT | 
|---|
| 35 | display_string("[init] process enters"); | 
|---|
| 36 | #endif | 
|---|
| 37 |  | 
|---|
| 38 |     // get number of TXT channels from hard configuration | 
|---|
| 39 |     hard_config_t config;       | 
|---|
| 40 |  | 
|---|
| 41 |     get_config( &config ); | 
|---|
| 42 |  | 
|---|
| 43 |     unsigned int  txt_channels = config.txt_channels; | 
|---|
| 44 |  | 
|---|
| 45 |     // check number of TXT channels | 
|---|
| 46 |     if( txt_channels < 2 ) | 
|---|
| 47 |     { | 
|---|
| 48 |         snprintf( string , 64 , | 
|---|
| 49 |         "\n[init ERROR] number of TXT channels must be larger than 1"); | 
|---|
| 50 |         display_string( string ); | 
|---|
| 51 |         exit( EXIT_FAILURE ); | 
|---|
| 52 |     } | 
|---|
| 53 |  | 
|---|
| 54 |     // create the KSH processes (one per user terminal) | 
|---|
| 55 |     for( i = 1 ; i <  txt_channels ; i++ ) | 
|---|
| 56 |     { | 
|---|
| 57 |         // INIT process fork process CHILD[i] | 
|---|
| 58 |         ret_fork = fork(); | 
|---|
| 59 |  | 
|---|
| 60 |         if( ret_fork < 0 )   // error in fork | 
|---|
| 61 |         { | 
|---|
| 62 |             // INIT display error message   | 
|---|
| 63 |             snprintf( string , 64 ,  | 
|---|
| 64 |             "\n[init ERROR] cannot fork child[%d] => suicide" , i ); | 
|---|
| 65 |             display_string( string ); | 
|---|
| 66 |  | 
|---|
| 67 |             // INIT suicide | 
|---|
| 68 |             exit( EXIT_FAILURE ); | 
|---|
| 69 |         } | 
|---|
| 70 |         else if( ret_fork == 0 )                    // we are in CHILD[i] process | 
|---|
| 71 |         { | 
|---|
| 72 |  | 
|---|
| 73 | #if DEBUG_PROCESS_INIT | 
|---|
| 74 |             snprintf( string , 64 , | 
|---|
| 75 |             "\n[init] CHILD[%d] process forked / call execve", i ); | 
|---|
| 76 |             display_string( string ); | 
|---|
| 77 | #endif | 
|---|
| 78 |             // CHILD[i] process exec process KSH[i] | 
|---|
| 79 |             ret_exec = execve( "/bin/user/ksh.elf" , NULL , NULL );  | 
|---|
| 80 |  | 
|---|
| 81 |             if ( ret_exec )   // error in exec               | 
|---|
| 82 |             { | 
|---|
| 83 |                 // CHILD[i] display error message | 
|---|
| 84 |                 snprintf( string , 64 ,  | 
|---|
| 85 |                 "\n[init ERROR] CHILD[%d] cannot exec KSH" , i ); | 
|---|
| 86 |                 display_string( string ); | 
|---|
| 87 |  | 
|---|
| 88 |                 // CHILD[i] suicide | 
|---|
| 89 |                 exit( EXIT_FAILURE ); | 
|---|
| 90 |             } | 
|---|
| 91 |         } | 
|---|
| 92 |         else                                      // we are in INIT process | 
|---|
| 93 |         { | 
|---|
| 94 |             // INIT display CHILD[i] process PID | 
|---|
| 95 |             snprintf( string , 64 , | 
|---|
| 96 |             "\n[init] (pid 0x1) create ksh[%d] (pid %x)", i , ret_fork );  | 
|---|
| 97 |             display_string( string ); | 
|---|
| 98 |  | 
|---|
| 99 |             // wait signal from KSH[i] before creating KSH[i+1] | 
|---|
| 100 |             pause(); | 
|---|
| 101 |         } | 
|---|
| 102 |     }  | 
|---|
| 103 |    | 
|---|
| 104 | #if DEBUG_PROCESS_INIT | 
|---|
| 105 | { | 
|---|
| 106 |     unsigned int  x;             // cluster x coordinate | 
|---|
| 107 |     unsigned int  y;             // cluster y coordinate | 
|---|
| 108 |     unsigned int  cxy;           // cluster identifier | 
|---|
| 109 |     unsigned int  lid;           // core local index | 
|---|
| 110 |  | 
|---|
| 111 |     unsigned int  x_size       = config.x_size; | 
|---|
| 112 |     unsigned int  y_size       = config.y_size; | 
|---|
| 113 |     unsigned int  ncores       = config.ncores; | 
|---|
| 114 |  | 
|---|
| 115 |     // INIT displays processes and threads in all clusters | 
|---|
| 116 |     for( x = 0 ; x < x_size ; x++ ) | 
|---|
| 117 |     { | 
|---|
| 118 |         for( y = 0 ; y < y_size ; y++ ) | 
|---|
| 119 |         { | 
|---|
| 120 |             cxy = HAL_CXY_FROM_XY( x , y ); | 
|---|
| 121 |             display_cluster_processes( cxy , 0 ); | 
|---|
| 122 |             for( lid = 0 ; lid < ncores ; lid++ ) | 
|---|
| 123 |             {  | 
|---|
| 124 |                 display_sched( cxy , lid ); | 
|---|
| 125 |             } | 
|---|
| 126 |         } | 
|---|
| 127 |     } | 
|---|
| 128 | } | 
|---|
| 129 | #endif | 
|---|
| 130 |  | 
|---|
| 131 |     // This loop detects the termination of the KSH[i] processes, | 
|---|
| 132 |     // and recreate a new KSH[i] process when required. | 
|---|
| 133 |     while( 1 ) | 
|---|
| 134 |     { | 
|---|
| 135 |         // block on child processes termination | 
|---|
| 136 |         rcv_pid = wait( &status ); | 
|---|
| 137 |  | 
|---|
| 138 |         if( WIFSTOPPED( status ) )                         // stopped => unblock it | 
|---|
| 139 |         { | 
|---|
| 140 |             // display string to report unexpected KSH process block | 
|---|
| 141 |             snprintf( string , 64 , "[init] KSH process %x stopped => unblock it" , rcv_pid ); | 
|---|
| 142 |             display_string( string );  | 
|---|
| 143 |  | 
|---|
| 144 |             // TODO : unblock KSH [AG] | 
|---|
| 145 |  | 
|---|
| 146 |         }  // end KSH stopped handling | 
|---|
| 147 |  | 
|---|
| 148 |         if( WIFSIGNALED( status ) || WIFEXITED( status ) )  // killed => recreate it | 
|---|
| 149 |         { | 
|---|
| 150 |             // display string to report KSH process termination | 
|---|
| 151 |             snprintf( string , 64 , "[init] KSH process %x terminated => recreate", rcv_pid ); | 
|---|
| 152 |             display_string( string );  | 
|---|
| 153 |  | 
|---|
| 154 |             // INIT process fork a new CHILD process | 
|---|
| 155 |             ret_fork = fork(); | 
|---|
| 156 |  | 
|---|
| 157 |             if( ret_fork < 0 )                          // error in fork | 
|---|
| 158 |             { | 
|---|
| 159 |                 // INIT display error message | 
|---|
| 160 |                 snprintf( string , 64 , "[init ERROR] cannot fork child => suicide"); | 
|---|
| 161 |                 display_string( string ); | 
|---|
| 162 |  | 
|---|
| 163 |                 // INIT suicide | 
|---|
| 164 |                 exit( 0 ); | 
|---|
| 165 |             } | 
|---|
| 166 |             else if( ret_fork == 0 )                    // we are in CHILD process | 
|---|
| 167 |             { | 
|---|
| 168 |                 // CHILD process exec process KSH | 
|---|
| 169 |                 ret_exec = execve( "/bin/user/ksh.elf" , NULL , NULL );  | 
|---|
| 170 |  | 
|---|
| 171 |                 if ( ret_exec )   // error in exec               | 
|---|
| 172 |                 { | 
|---|
| 173 |                     // CHILD display error message on TXT0 terminal | 
|---|
| 174 |                     snprintf( string , 64 , "[init ERROR] CHILD cannot exec KSH" ); | 
|---|
| 175 |                     display_string( string ); | 
|---|
| 176 |                 } | 
|---|
| 177 |             } | 
|---|
| 178 |             else                                       // we are in INIT process | 
|---|
| 179 |             { | 
|---|
| 180 |                 // INIT display new KSH process PID | 
|---|
| 181 |                 snprintf( string , 64 , "[init] re-created KSH / pid = %x", ret_fork );  | 
|---|
| 182 |                 display_string( string ); | 
|---|
| 183 |             } | 
|---|
| 184 |         } // end KSH kill handling | 
|---|
| 185 |  | 
|---|
| 186 |     }  // end while waiting KSH[i] termination | 
|---|
| 187 |  | 
|---|
| 188 | } // end main() | 
|---|
| 189 |  | 
|---|