Example #1
0
// Nits Installation reads the nitsinj.dll name from registry key on windows
// On linux, we use the file based mechanism where we require that NitsInstalled file is present
// at specific location. The windows way of doing this was required so that we are fine with layermap tool
void LoadInjectorIfRequired()
{    
#ifdef _MSC_VER        
#define InjectorStrLength (sizeof(L"nitsinj.dll")/sizeof(wchar_t))

    HKEY key;
    wchar_t value[InjectorStrLength];    
    DWORD valueSize = sizeof(value);
    DWORD valueType = 0;
    DWORD queryError = 0;

    RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WSMAN", 0, KEY_QUERY_VALUE, &key);

    queryError = RegQueryValueEx(key, L"NitsInjector", NULL, &valueType, (BYTE *)&value, &valueSize);
    
    if((queryError == 0) && (InjectorStrLength*sizeof(wchar_t) == valueSize))
    {        
        PAL_Char injectorLibName[InjectorStrLength];
        value[InjectorStrLength - 1] = L'\0';
#ifdef CONFIG_ENABLE_WCHAR        
        Wcslcpy(injectorLibName, value, InjectorStrLength);
#else
        StrWcslcpy(injectorLibName, value, InjectorStrLength)
#endif        
        LoadInjector(injectorLibName);                
    }

    RegCloseKey(key);
#else
    FILE* fp = File_Open(CONFIG_TMPDIR "/NitsInstalled", "rb");
    if(fp)
    {
        PAL_Char injectorLibName[NAME_MAX];
        TcsStrlcpy(injectorLibName, CONFIG_LIBDIR "/libnitsinj.so", NAME_MAX);

        LoadInjector(injectorLibName);
        File_Close(fp);        
    }
#endif    
}
Example #2
0
PAL_Char* Shlib_Err()
{
#if defined(_MSC_VER)
    PAL_Char* err = NULL;
    
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER|
        FORMAT_MESSAGE_FROM_SYSTEM|
        FORMAT_MESSAGE_IGNORE_INSERTS, 
        NULL, 
        GetLastError(), 
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
        (LPTSTR)&err, 
        0, 
        NULL);
    return err;
#else
    char* err = dlerror();
    size_t len = strlen(err) + 1;
    PAL_Char* copy = PAL_Malloc(len * sizeof(PAL_Char));
    TcsStrlcpy(copy, err, len);
    return copy;
#endif
}
MI_Result UpdateTask(
    _In_z_ MI_Char* taskName,
    _In_z_ MI_Char* taskTime,
    _In_ MI_Uint32 refreshFrequencyInSeconds,
    _Outptr_result_maybenull_ MI_Instance **extendedError)
{
    int retValue = -1;
    ssize_t read = 0;
    size_t readLength = 0, writeLength = 0;
	MI_Char *line = NULL, *lineToWrite = NULL, *taskInCrontab = NULL;
	FILE *cronFile = NULL, *cronFileTmp = NULL;
	MI_Uint32 errorCode = 0;

	if (extendedError == NULL)
	{
		return MI_RESULT_INVALID_PARAMETER;
	}
	*extendedError = NULL;


    // Preparation
    if (refreshFrequencyInSeconds > THIRTY_ONE_DAYS_IN_SECONDS)
    {
		refreshFrequencyInSeconds = THIRTY_ONE_DAYS_IN_SECONDS;
    }
    if (refreshFrequencyInSeconds < FIFTEEN_MINUTES_IN_SECONDS)
    {
		refreshFrequencyInSeconds = FIFTEEN_MINUTES_IN_SECONDS;
    }

    // PLAN 1 -- process each line of crontab file to a tmp file and then copy tmp file back
    // taskTime string is not used in Linux since we can simply register a task with frequency

	// Allocate memory
	line = (MI_Char *)DSC_malloc(UNIT_LINE_SIZE * sizeof(MI_Char), NitsHere());
	if (line == NULL)
	{
		errorCode = ID_LCMHELPER_MEMORY_ERROR;
		goto ExitWithError;
	}
    lineToWrite = (MI_Char *)DSC_malloc(UNIT_LINE_SIZE * sizeof(MI_Char), NitsHere());
	if (lineToWrite == NULL)
	{
		errorCode = ID_LCMHELPER_MEMORY_ERROR;
		goto ExitWithError;
	}
	taskInCrontab = (MI_Char *)DSC_malloc(UNIT_LINE_SIZE * sizeof(MI_Char), NitsHere());
	if (taskInCrontab == NULL)
	{
		errorCode = ID_LCMHELPER_MEMORY_ERROR;
		goto ExitWithError;
	}

	// Open files
	if (File_ExistT(CRON_FILE) != -1)
	{
		cronFile = File_OpenT(CRON_FILE, MI_T("r"));
		if (cronFile == NULL)
		{
			// Fail to open existing cron file should lead to error exit
			errorCode = ID_ENGINEHELPER_OPENFILE_ERROR;
			goto ExitWithError;
		}
	}
    cronFileTmp = File_OpenT(CRON_FILE_TMP, MI_T("w"));
    if (cronFileTmp == NULL)
	{
		// Fail to create tmp file should lead to error exit
		errorCode = ID_LCMHELPER_CREATEFILE_ERROR;
		goto ExitWithError;
    }

	// Read and process crontab file if it exists and opens appropriately
    while (cronFile != NULL && (read = readline(&line, &readLength, cronFile)) != -1)
    {
        retValue = TcsStrlcpy(lineToWrite, line, Tcslen(line)+1);
		retValue = sscanf(line, MI_T("%*s %*s %*s %*s %*s %*s %s"), taskInCrontab);
		if (retValue == 0)
		{
			// Ignore the bad line that does not comply with crontab file format
			continue;
		}
		else
		{
			if (Tcsncasecmp(taskName, taskInCrontab, Tcslen(taskName)) == 0)
			{
				// Ignore entry that duplicates registration of task
				continue;
			}
			else
			{
				// Write the entry to tmp file
				writeLength = fwrite(lineToWrite, 1, Tcslen(lineToWrite), cronFileTmp);
				if (writeLength != read)
				{
					errorCode = ID_LCMHELPER_WRITEFILE_ERROR;
					goto ExitWithError;
				}
			}
		}
    }
    if (readLength == -1)
	{
		// Deal memory failure in readline function
		errorCode = ID_ENGINEHELPER_READFILE_ERROR;
		goto ExitWithError;
	}

    // Add the task entry
    retValue = Stprintf(lineToWrite, UNIT_LINE_SIZE, MI_T("*/%d * * * * root %T\n"), refreshFrequencyInSeconds / 60, taskName);
	if (retValue == -1)
	{
		errorCode = ID_LCMHELPER_PRINTF_ERROR;
		goto ExitWithError;
	}
    writeLength = fwrite(lineToWrite, 1, Tcslen(lineToWrite), cronFileTmp);
	if (writeLength != Tcslen(lineToWrite))
	{
		errorCode = ID_LCMHELPER_WRITEFILE_ERROR;
		goto ExitWithError;
	}

	if (cronFile != NULL)
		File_Close(cronFile);
    File_Close(cronFileTmp);

    File_CopyT(CRON_FILE_TMP, CRON_FILE);
    File_RemoveT(CRON_FILE_TMP);    
    
    DSC_free(line);
    DSC_free(lineToWrite);
	DSC_free(taskInCrontab);

    return MI_RESULT_OK;

ExitWithError:
	// Close files and remove temp file if it exists
	if (cronFile)
		File_Close(cronFile);
	if (cronFileTmp)
		File_Close(cronFileTmp);
    if (File_ExistT(CRON_FILE_TMP) != -1)
        File_RemoveT(CRON_FILE_TMP);
	
	// Free memory allocations
	if (line != NULL)
		DSC_free(line);
	if (lineToWrite != NULL)
		DSC_free(lineToWrite);
	if (taskInCrontab != NULL)
		DSC_free(taskInCrontab);

	// Form rich CIM error
	if (errorCode != 0)
		return GetCimMIError(MI_RESULT_SERVER_LIMITS_EXCEEDED, extendedError, errorCode);
	else
		return MI_RESULT_FAILED;
}