Exemple #1
0
void CVMWebAPISession::thread_start( const FB::variant& cfg ) {
    CRASH_REPORT_BEGIN;

    int ans;
    std::map<std::string, std::string> vmUserData;
    std::map<std::string, std::string> * dataPtr = NULL;
    
    // Update user data if they are specified
    if ( cfg.is_of_type<FB::JSObjectPtr>() ) {
        
        // Convert object values to key/value pairs
        CVMWA_LOG("Debug", "CFG argument is JSObjectPtr");
        FB::JSObject::GetObjectValues< std::map<std::string, std::string> >(
            cfg.cast<FB::JSObjectPtr>(),
            vmUserData);
        dataPtr = &vmUserData;
        
    }
    
    // Start session
    ans = this->session->start( dataPtr );
    if (ans == 0) {

        // Tell daemon to reload sessions
        if (isDaemonRunning())
            daemonGet( DIPC_RELOAD );

        // Notify browser that the session has started
        WHEN_SAFE this->fire_start();

    } else {
        WHEN_SAFE this->fire_startError(hypervisorErrorStr(ans), ans);
        WHEN_SAFE this->fire_error(hypervisorErrorStr(ans), ans, "start");
    }
    
    /* Start probing timer */
    this->probeTimer->start();

    CRASH_REPORT_END;
}
Int main (Int argc, Char * argv [])
{
    pid_t   child_pid;
    pid_t   child_sid;
    Int     status      = 0;
    Bool    daemon      = TRUE;
    Int     o;
    Int     i;
    Char  * images []   = {NULL, NULL};
    Int     numImages   = sizeof (images) / sizeof (images [0]);

    if (isDaemonRunning (argv [0])) {
        Osal_printf ("Multiple instances of syslink_daemon.out are not "
                     "supported!\n");
        return (-1);
    }

    /* Determine args */
    while ((o = getopt (argc, argv, ":fs:a:")) != -1) {
        switch (o) {
        case 's':
            images [0] = optarg;
            break;
        case 'a':
            images [1] = optarg;
            break;
        case 'f':
            daemon = FALSE;
            break;
        case ':':
            status = -1;
            Osal_printf ("Option -%c requires an operand\n", optopt);
            break;
        case '?':
            status = -1;
            Osal_printf ("Unrecognized option: -%c\n", optopt);
            break;
        }
    }

    for (i = 0; optind < argc; optind++) {
        while (i < numImages && images [i]) i++;
        if (i == numImages) {
            printUsage ();
        }
        images [i++] = argv [optind];
    }

    for (i = 0; i < numImages; i++) {
        if (images [i] && access (images [i], R_OK)) {
            Osal_printf ("Error opening %s\n", images [i]);
            printUsage ();
        }
    }

    if (status || optind < argc || !images [0]) {
        printUsage ();
    }

    /* Process will be daemonised if daemon flag is true */
    if (daemon) {
        Osal_printf ("Spawning SysLink Daemon...\n");
        /* Fork off the parent process */
        child_pid = fork ();
        if (child_pid < 0) {
            Osal_printf ("Spawn daemon failed!\n");
            exit (EXIT_FAILURE);     /* Failure */
        }
        /* If we got a good PID, then we can exit the parent process. */
        if (child_pid > 0) {
            exit (EXIT_SUCCESS);    /* Success */
        }

        /* Change file mode mask */
        umask (0);

        /* Create a new SID for the child process */
        child_sid = setsid ();
        if (child_sid < 0) {
            exit (EXIT_FAILURE);     /* Failure */
        }

        /* Change the current working directory */
        if ((chdir("/")) < 0) {
            exit (EXIT_FAILURE);     /* Failure */
        }
    }

    /* Setup the signal handlers */
    if (signal (SIGINT, signal_handler) == SIG_ERR) {
        Osal_printf ("SIGINT registration failed!");
    }
    if (signal (SIGTERM, signal_handler) == SIG_ERR) {
        Osal_printf ("SIGTERM registration failed!");
    }

    while (restart) {
        restart = FALSE;
        isSysM3Event = FALSE;
        isAppM3Event = FALSE;

        sem_init (&semDaemonWait, 0, 0);

        status = ipcSetup (images [0], images [1]);
        if (status < 0) {
            Osal_printf ("ipcSetup failed!\n");
            sem_destroy (&semDaemonWait);
            return (-1);
        }
        Osal_printf ("ipcSetup succeeded!\n");

        /* Create the SysM3 fault handler thread */
        Osal_printf ("Create SysM3 event handler thread\n");
        status = pthread_create (&sysM3EvtHandlerThrd, NULL,
                                    (Void *)&sysM3EventHandler, NULL);

        if (status) {
            Osal_printf ("Error Creating SysM3 event handler thread:%d\n",
                            status);
            ipcCleanup ();
            sem_destroy (&semDaemonWait);
            exit (EXIT_FAILURE);
        }
        /* Only if AppM3 image is specified */
        if (images [1]) {
            /* Create an AppM3 fault handler thread */
            Osal_printf ("Create AppM3 event handler thread\n");
            status = pthread_create (&appM3EvtHandlerThrd, NULL,
                                        (Void *)&appM3EventHandler, NULL);
            if (status) {
                Osal_printf ("Error Creating AppM3 event handler thread:%d\n",
                                status);
                pthread_kill (sysM3EvtHandlerThrd, SIGTERM);
                sysM3EvtHandlerThrd = 0;
                ipcCleanup ();
                sem_destroy (&semDaemonWait);
                exit (EXIT_FAILURE);
            }
        }

        /* Wait for any event handler thread to be unblocked */
        sem_wait (&semDaemonWait);

        /* Clean up event handler threads */
        if (sysM3EvtHandlerThrd) {
            if (isSysM3Event) {
                status = pthread_join (sysM3EvtHandlerThrd, NULL);
                Osal_printf ("pthread_join SysM3 Thread = %d\n", status);
            }
            else {
                if (pthread_kill (sysM3EvtHandlerThrd, SIGTERM) == 0) {
                    status = pthread_join (sysM3EvtHandlerThrd, NULL);
                    Osal_printf ("pthread_kill & join SysM3 Thread = %d\n",
                                    status);
                }
            }
            sysM3EvtHandlerThrd = 0;
        }
        if (appM3EvtHandlerThrd) {
            if (isAppM3Event) {
                status = pthread_join (appM3EvtHandlerThrd, NULL);
                Osal_printf ("pthread_join AppM3 Thread = %d\n", status);
            }
            else {
                if (pthread_kill (appM3EvtHandlerThrd, SIGTERM) == 0) {
                    status = pthread_join (appM3EvtHandlerThrd, NULL);
                    Osal_printf ("pthread_kill & join AppM3 Thread = %d\n",
                                    status);
                }
            }
            appM3EvtHandlerThrd = 0;
        }

        /* IPC_Cleanup function */
        ipcCleanup ();

        sem_destroy (&semDaemonWait);
    }

    return 0;
}