//------------------------------------------------------------------------------
void MobiCoreDriverDaemon::run(
	void
) {
	LOG_I("Daemon starting up...");
	LOG_I("Socket interface version is %u.%u", DAEMON_VERSION_MAJOR, DAEMON_VERSION_MINOR);
#ifdef MOBICORE_COMPONENT_BUILD_TAG
	LOG_I("%s", MOBICORE_COMPONENT_BUILD_TAG);
#else
	#warning "MOBICORE_COMPONENT_BUILD_TAG is not defined!"
#endif
	LOG_I("Build timestamp is %s %s", __DATE__, __TIME__);

	int i;

	mobiCoreDevice = getDeviceInstance();

	LOG_I("Daemon scheduler is %s", enableScheduler? "enabled" : "disabled");
	if(!mobiCoreDevice->initDevice(
		MC_DRV_MOD_DEVNODE_FULLPATH,
		loadMobicore,
		mobicoreImage.c_str(),
		enableScheduler)) {
		LOG_E("%s: Failed to initialize MobiCore!", __FUNCTION__);
		return;
	}
	mobiCoreDevice->start();

	checkMobiCoreVersion(mobiCoreDevice);

	if (donateRamSize > 0) {
		// Donate additional RAM to MC
		LOG_I("Donating %u Kbytes to Mobicore", donateRamSize / 1024);
		mobiCoreDevice->donateRam(donateRamSize);
	}

	// Load device driver if requested
	if (loadDriver) {
		loadDeviceDriver(driverPath);
	}

	LOG_I("Servers will be created!");
	// Start listening for incoming TLC connections
	servers[0] = new NetlinkServer(this);
	servers[1] = new Server(this, SOCK_PATH);
	LOG_I("Servers created!");

	// Start all the servers
	for (i = 0; i < MAX_SERVERS; i++) {
		servers[i]->start();
	}

	// then wait for them to exit
	for (i = 0; i < MAX_SERVERS; i++) {
		servers[i]->join();
	}
}
//------------------------------------------------------------------------------
void MobiCoreDriverDaemon::run(
    void
)
{
    const char *devNode = "/dev/" MC_ADMIN_DEVNODE;

    LOG_I_RELEASE("Daemon starting up...");
    LOG_I_RELEASE("Socket interface version is %u.%u", DAEMON_VERSION_MAJOR, DAEMON_VERSION_MINOR);

#ifdef MOBICORE_COMPONENT_BUILD_TAG
    LOG_I_RELEASE("%s", MOBICORE_COMPONENT_BUILD_TAG);
#else
#warning "MOBICORE_COMPONENT_BUILD_TAG is not defined!"
#endif

    LOG_I_RELEASE("Build timestamp is %s %s", __DATE__, __TIME__);

    int i;

    mobiCoreDevice = getDeviceInstance();

    LOG_I("Initializing Device, Daemon sheduler is %s",
          enableScheduler ? "enabled" : "disabled");

    // initialize device (setupo MCI)
    if (!mobiCoreDevice->initDevice(
                devNode,
                enableScheduler)) {
        LOG_E("Could not initialize <t-base (because %s could not be openend)!", devNode);
        return;
    }

    // start device (scheduler)
    mobiCoreDevice->start();

    // Load device driver if requested
    if (loadDriver) {
        for (unsigned int i = 0; i < drivers.size(); i++)
            loadDeviceDriver(drivers[i]);
    }

    /* Look for tokens in the registry and pass them to <t-base for endorsement
     * purposes.
     */
    LOG_I("Looking for suitable tokens");

    mcSoAuthTokenCont_t authtoken;
    mcSoRootCont_t rootcont;
    uint32_t sosize;
    uint8_t *p = NULL;

    mcResult_t ret = mcRegistryReadAuthToken(&authtoken);
    if (ret != MC_DRV_OK) {
        LOG_I("Failed to read AuthToken (ret=%u). Trying Root Container", ret);

        sosize = sizeof(rootcont);
        ret = mcRegistryReadRoot(&rootcont, &sosize);
        if (ret != MC_DRV_OK) {
            LOG_I("Failed to read Root Cont, (ret=%u)", ret);
            LOG_W("Device endorsements not supported!");
            sosize = 0;
        }
        else {
            LOG_I("Found Root Cont.");
            p = (uint8_t *) &rootcont;
        }
    } else {
        LOG_I("Found AuthToken.");
        p = (uint8_t *) &authtoken;
        sosize = sizeof(authtoken);
    }

    if (sosize) {
        LOG_I("Found token of size: %u", sosize);
        if (!loadToken(p, sosize)) {
            LOG_E("Failed to pass token to <t-base. "
                  "Device endorsements disabled");
        }
    }

    LOG_I("Creating socket servers");
    // Start listening for incoming TLC connections
    servers[0] = new NetlinkServer(this);
    servers[1] = new Server(this, SOCK_PATH);
    LOG_I("Successfully created servers");

    // Start all the servers
    for (i = 0; i < MAX_SERVERS; i++) {
        servers[i]->start(i ? "McDaemon.Server" : "NetlinkServer");
    }

    // Create the <t-base File Storage Daemon
    FSD *FileStorageDaemon = new FSD();
    // Start File Storage Daemon
    FileStorageDaemon->start("McDaemon.FSD");

    // then wait for them to exit
    for (i = 0; i < MAX_SERVERS; i++) {
        servers[i]->join();
    }
    //Wait for File Storage Daemon to exit
	FileStorageDaemon->join();
	delete FileStorageDaemon;
}