///////////////////////////////////////////////////////////////////////////////////////
// File   :  init.c
// Date   :  January 2018
// Author :  Alain Greiner
///////////////////////////////////////////////////////////////////////////////////////
// This single thread application implement the "init" process for ALMOS-MKH. 
// It uses the fork/exec syscalls to create N KSH child processes 
// (one child process per user terminal).
// It includes the hard_config.h file to get th NB_TXT_CHANNELS parameter.
//
// TODO : Register the PIDs for all KSH[i] in a ksh_pid[] array. 
// Then calls the wait() function to block, and reactivate any child KSH process
// that has been deleted, using a new fork/exec.
///////////////////////////////////////////////////////////////////////////////////////

#include <hard_config.h>

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

//////////
int main()
{
    int     i;
    int     ret_fork;      // fork return value  
    int     ret_exec;      // fork return value  
    int     rcv_pid;       // pid received from the wait syscall
    int     status;        // used by the wait syscall
    char    string[64];

    // check number of TXT channels
    assert( NB_TXT_CHANNELS > 1 );

    // create the KSH processes (one per user terminal)
    for( i = 1 ; i <  NB_TXT_CHANNELS ; i++ )
    {
        // INIT process fork process CHILD[i]
        ret_fork = fork();

        if( ret_fork < 0 )   // error in fork
        {
            // INIT display error message on TXT0 terminal 
            snprintf( string , 64 , "INIT cannot fork child[%d]" , i );
            display_string( string );

            // INIT suicide
            exit( 0 );
        }
        else if( ret_fork == 0 )                    // we are in CHILD[i] process
        {
            // CHILD[i] process exec process KSH[i]
            ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); 

            if ( ret_exec )   // error in exec              
            {
                // CHILD[i] display error message on TXT0 terminal
                snprintf( string , 64 , 
                "CHILD[%d] cannot exec KSH[%d] / ret_exec = %d" , i , i , ret_exec );
                display_string( string );
            }
        }
        else                                      // we are in INIT process
        {
             // INIT display CHILD[i] process PID
             snprintf( string , 64 , "INIT created KSH[%d] / pid = %x", i , ret_fork ); 
             display_string( string );
        }
    } 

    // This loop detects the termination of the KSH[i] processes,
    // and recreate a new KSH[i] process when required.
    while( 1 )
    {
        // block on child processes termination
        rcv_pid = wait( &status );

        if( WIFSTOPPED( status ) )                         // stopped => unblock it
        {
            // display string to report unexpected KSH process block
            snprintf( string , 64 , "KSH process %x stopped => unblock it" , rcv_pid );
            display_string( string ); 

            // TODO : unblock KSH

        }  // end KSH stopped handling

        if( WIFSIGNALED( status ) || WIFEXITED( status ) )  // killed => recreate it
        {
            // display string to report unexpected KSH process termination
            snprintf( string , 64 , "KSH process %x terminated => recreate KSH", rcv_pid );
            display_string( string ); 

            // INIT process fork a new CHILD process
            ret_fork = fork();

            if( ret_fork < 0 )                          // error in fork
            {
                // INIT display error message on TXT0 terminal 
                snprintf( string , 64 , "INIT cannot fork child");
                display_string( string );

                // INIT suicide
                exit( 0 );
            }
            else if( ret_fork == 0 )                    // we are in CHILD process
            {
                // CHILD process exec process KSH
                ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); 

                if ( ret_exec )   // error in exec              
                {
                    // CHILD display error message on TXT0 terminal
                    snprintf( string , 64 , "CHILD cannot exec KSH" );
                    display_string( string );
                }
            }
            else                                       // we are in INIT process
            {
                // INIT display new CHILD process PID
                snprintf( string , 64 , "INIT forked CHILD / pid = %x", ret_fork ); 
                display_string( string );
            }
        }  // end KSH kill handling
    }

} // end main()

