void MI_CALL MSFT_DSCLocalConfigurationManager_Invoke_RollBack( _In_opt_ MSFT_DSCLocalConfigurationManager_Self* self, _In_ MI_Context* context, _In_opt_z_ const MI_Char* nameSpace, _In_opt_z_ const MI_Char* className, _In_opt_z_ const MI_Char* methodName, _In_ const MSFT_DSCLocalConfigurationManager* instanceName, _In_opt_ const MSFT_DSCLocalConfigurationManager_RollBack* in) { MI_Instance *cimErrorDetails = NULL; SetJobId(); DSC_EventWriteMSFTMethodParameters(__WFUNCTION__,className,methodName,nameSpace); if (in && in->configurationNumber.exists) { GetCimMIError(MI_RESULT_INVALID_PARAMETER, &cimErrorDetails, ID_LCM_CONF_NUMBEREXIST); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); ResetJobId(); return; } //Show up an error if confirm is used. Whatif is supported but not confirm if (IsConfirmUsed(context)) { GetCimMIError(MI_RESULT_INVALID_PARAMETER, &cimErrorDetails, ID_LCM_CONFIRM_NOT_SUPPORTED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); ResetJobId(); return; } Invoke_RollBack(self, context, nameSpace, className, methodName, instanceName, in); }
void MI_CALL MSFT_DSCLocalConfigurationManager_Invoke_TestConfiguration( _In_opt_ MSFT_DSCLocalConfigurationManager_Self* self, _In_ MI_Context* context, _In_opt_z_ const MI_Char* nameSpace, _In_opt_z_ const MI_Char* className, _In_opt_z_ const MI_Char* methodName, _In_ const MSFT_DSCLocalConfigurationManager* instanceName, _In_opt_ const MSFT_DSCLocalConfigurationManager_TestConfiguration* in) { MI_Instance *cimErrorDetails = NULL; SetJobId(); // Debug Log DSC_EventWriteMSFTMethodParameters(__WFUNCTION__,className,methodName,nameSpace); // If configuration data exists, output error mentioning this is reserved for future use if (in && in->configurationData.exists) { GetCimMIError(MI_RESULT_INVALID_PARAMETER, &cimErrorDetails, ID_LCM_CONF_DATAEXIST); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); ResetJobId(); return; } Invoke_TestConfiguration(self, context, nameSpace, className, methodName, instanceName, in); }
static MI_Result WriteError(_In_ MI_Context* context, MI_Uint32 resultCode, _In_z_ const MI_Char* resultType, _In_z_ const MI_Char* errorMessage, _Out_ MI_Boolean *flag) { MI_Result returnValue = MI_RESULT_OK; MI_Instance* extendedError = NULL; NativeResourceProvider* nativeResourceProvider = (NativeResourceProvider*)context; if (!nativeResourceProvider->_private.resourceProviderClassLoaded) { DSC_EventUnSupportedHostMethodCalled(MI_T("PostIndication")); return MI_RESULT_NOT_SUPPORTED; } *flag = MI_TRUE; // TRUE indicates the provider can continue (this is a non-terminating error) returnValue = MI_Utilities_CimErrorFromErrorCode(resultCode, resultType, errorMessage, &extendedError); EH_CheckResult(returnValue); DoWriteError(NULL, nativeResourceProvider->_private.callbackContext, extendedError, NULL); EH_UNWIND; if (extendedError != NULL) MI_Instance_Delete(extendedError); return returnValue; }
void LifecycleEvent_Free(_In_ LifecycleEvent* event) { if (event) { if (event->sourceInstance) { MI_Instance_Delete(event->sourceInstance); } if (event->previousInstance) { MI_Instance_Delete(event->previousInstance); } if (event->methodParam) { MI_Instance_Delete(event->methodParam); } PAL_Free(event); } }
static MI_Result MI_CALL _SetIndicationProperties( _In_ void* cfg, _Outptr_ MI_Instance** indication) { MI_Result r = _SetIndicationProperties_Impl(cfg, indication); if (r != MI_RESULT_OK) { if (*indication) MI_Instance_Delete(*indication); } return r; }
void CleanUpInstanceCache(_Inout_ MI_InstanceA *instanceArray) { MI_Uint32 xCount = 0; if( instanceArray == NULL || instanceArray->size == 0 ) { return; } for(xCount = 0; xCount < instanceArray->size; xCount++) { MI_Instance_Delete(instanceArray->data[xCount]); } DSC_free(instanceArray->data); }
void MI_CALL L_LifecycleC1_Unload( L_LifecycleC1_Self* self, MI_Context* context) { CHKMIRESULTPOST(lifecycle_StopThread(&cfgL_LifecycleC1), "L_LifecycleC1_Unload"); CHKMIRESULTPOST(lifecycle_Unload(&cfgL_LifecycleC1), "lifecycle_Unload(L_LifecycleC1)"); CHKMIRESULTPOST(class_Unload((void*)self, &cfgL_LifecycleC1), "L_LifecycleC1_Unload"); if (cfgL_LifecycleC1.lifecycleInstance) { MI_Instance_Delete( cfgL_LifecycleC1.lifecycleInstance ); cfgL_LifecycleC1.lifecycleInstance = NULL; } MI_PostResult(context, MI_RESULT_OK); }
void MI_CALL MSFT_DSCLocalConfigManager_Unload( _In_opt_ MSFT_DSCLocalConfigManager_Self* self, _In_ MI_Context* context) { MI_Result miResult; MI_Instance *cimErrorDetails = NULL; MI_UNREFERENCED_PARAMETER(self); miResult = UnInitHandler(&cimErrorDetails); if(miResult != MI_RESULT_OK) { MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); return; } MI_Context_PostResult(context, MI_RESULT_OK); }
void MI_CALL MSFT_nxGroupResource_Invoke_GetTargetResource( _In_opt_ MSFT_nxGroupResource_Self* self, _In_ MI_Context* context, _In_opt_z_ const MI_Char* nameSpace, _In_opt_z_ const MI_Char* className, _In_opt_z_ const MI_Char* methodName, _In_ const MSFT_nxGroupResource* instanceName, _In_opt_ const MSFT_nxGroupResource_GetTargetResource* in) { SCX_BOOKEND_EX ("Get", " name=\"nxGroup\""); MI_Result result = MI_RESULT_FAILED; if (self) { MI_Instance* retInstance; MI_Instance_Clone (&in->InputResource.value->__instance, &retInstance); result = self->get (in->InputResource.value->__instance, context, retInstance); if (MI_RESULT_OK == result) { SCX_BOOKEND_PRINT ("packing succeeded!"); MSFT_nxGroupResource_GetTargetResource out; MSFT_nxGroupResource_GetTargetResource_Construct (&out, context); MSFT_nxGroupResource_GetTargetResource_Set_MIReturn (&out, 0); MI_Value value; value.instance = retInstance; MI_Instance_SetElement (&out.__instance, "OutputResource", &value, MI_INSTANCE, 0); result = MSFT_nxGroupResource_GetTargetResource_Post (&out, context); if (MI_RESULT_OK != result) { SCX_BOOKEND_PRINT ("post Failed"); } MSFT_nxGroupResource_GetTargetResource_Destruct (&out); } else { SCX_BOOKEND_PRINT ("get FAILED"); } MI_Instance_Delete (retInstance); } MI_Context_PostResult (context, result); }
MI_Result RegisterRebootTaskIfNeeded( _In_ MI_Instance *metaConfigInstance, _In_ ModuleManager *moduleManager, _Outptr_result_maybenull_ MI_Instance **cimErrorDetails) { MI_Uint32 flags; MI_Value value; MI_Result result = MI_RESULT_OK; MI_Instance* tempErrorDetail = NULL; if (cimErrorDetails == NULL) { return MI_RESULT_INVALID_PARAMETER; } *cimErrorDetails = NULL; // Explicitly set *cimErrorDetails to NULL as _Outptr_ requires setting this at least once. result = MI_Instance_GetElement(metaConfigInstance, MSFT_DSCMetaConfiguration_RebootNodeIfNeeded, &value, NULL, &flags, NULL); if (result == MI_RESULT_OK) { if (flags & MI_FLAG_NULL) { value.boolean = DEFAULT_RebootNodeIfNeeded; } } result = RegisterStartAtBootTask(MI_TRUE, cimErrorDetails); if (result == MI_RESULT_OK && value.boolean == MI_TRUE) { result = ScheduleRestart(cimErrorDetails); if (result != MI_RESULT_OK) { // Deregister the task RegisterStartAtBootTask(MI_FALSE, &tempErrorDetail); if (tempErrorDetail) { MI_Instance_Delete(tempErrorDetail); } } } return result; }
static MI_Result MI_CALL CreateOMIError_shared( _In_z_ const MI_Char* message, MI_Uint32 errorCode, _In_z_ const MI_Char* errorType, MI_Uint16 errorCategory, const MI_ClassDecl *classDecl, _Outptr_ MI_Instance **omiError) { MI_Result result = MI_RESULT_OK; OMI_Error *err = NULL; MI_Application application = {0}; *omiError = NULL; result = MI_Application_Initialize(0, NULL, omiError, &application); if (result != MI_RESULT_OK) return result; result = MI_Application_NewInstance(&application, classDecl->name, classDecl, (MI_Instance**)&err); if (result == MI_RESULT_OK) { result = OMI_Error_Set_Message(err, message); if (result != MI_RESULT_OK) goto Exit; result = OMI_Error_Set_error_Code(err, errorCode); if (result != MI_RESULT_OK) goto Exit; result = OMI_Error_Set_error_Type(err, errorType); if (result != MI_RESULT_OK) goto Exit; result = OMI_Error_Set_error_Category(err, errorCategory); if (result != MI_RESULT_OK) goto Exit; result = OMI_Error_Set_CIMStatusCode(err, MI_RESULT_FAILED); if (result != MI_RESULT_OK) goto Exit; *omiError = (MI_Instance*)err; } Exit: if ((result != MI_RESULT_OK) && err) MI_Instance_Delete((MI_Instance*)err); MI_Application_Close(&application); return result; }
void MI_CALL MSFT_DSCLocalConfigManager_Load( _Outptr_result_maybenull_ MSFT_DSCLocalConfigManager_Self** self, _In_opt_ MI_Module_Self* selfModule, _In_ MI_Context* context) { MI_Result miResult; MI_Instance *cimErrorDetails = NULL; MI_UNREFERENCED_PARAMETER(selfModule); *self = NULL; //load will not be called by multiple threads miResult = InitHandler(&cimErrorDetails); if(miResult != MI_RESULT_OK) { MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); return; } MI_Context_PostResult(context, MI_RESULT_OK); }
/*Function to perform registration*/ MI_Result Register( _In_ RegistrationManager* self, _In_ RegistrationRequest* request, _Outptr_result_maybenull_ MI_Instance **cimErrorDetails) { MI_Result result = MI_RESULT_OK; MI_Instance* registrationPayload = NULL; MI_Uint32 getActionStatusCode; MI_Char* resultStatus = NULL; MI_Char* thumbprint = NULL; MI_Value value; MI_Uint32 flags; int systemResult = 0; if (cimErrorDetails) { *cimErrorDetails = NULL; } // Check if RegistrationKey is specified. If not specified, do not attempt to register. result = MI_Instance_GetElement(request->registrationData, MSFT_RegistrationKey_Name, &value, NULL, &flags, NULL); if (result != MI_RESULT_OK || flags & MI_FLAG_NULL || value.string == NULL || value.string[0] == '\0') { return MI_RESULT_OK; } if ( (access(OAAS_KEYPATH, F_OK) == -1) || (access(OAAS_CERTPATH, F_OK) == -1) ) { system("touch " OAAS_KEYPATH "; chmod 0600 " OAAS_KEYPATH); system("touch " OAAS_KEYPATH "_old; chmod 0600 " OAAS_KEYPATH "_old"); systemResult = system("openssl req -subj '/CN=DSC-OaaS' -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout " OAAS_KEYPATH "_old -out " OAAS_CERTPATH " && openssl rsa -in " OAAS_KEYPATH "_old -out " OAAS_KEYPATH " && rm -f " OAAS_KEYPATH "_old"); if (systemResult != 0 && errno != 10) { DSC_EventWriteLCMServerRegCertGenFailed(g_ConfigurationDetails.jobGuidString, self->agentId); return GetCimMIError(MI_RESULT_FAILED, cimErrorDetails, ID_PULL_FAILEDTOGENERATECERT); } systemResult = system("openssl x509 -noout -in " OAAS_CERTPATH " -fingerprint | sed 's/^.*=//' > " OAAS_THUMBPRINTPATH); if (systemResult != 0 && errno != 10) { DSC_EventWriteLCMServerRegCertGenFailed(g_ConfigurationDetails.jobGuidString, self->agentId); return GetCimMIError(MI_RESULT_FAILED, cimErrorDetails, ID_PULL_FAILEDTOGENERATECERT); } { long length; FILE * fingerprint_file = fopen (OAAS_THUMBPRINTPATH, "r"); if (fingerprint_file) { fseek (fingerprint_file, 0, SEEK_END); length = ftell (fingerprint_file); fseek (fingerprint_file, 0, SEEK_SET); thumbprint = DSC_malloc (length * sizeof(MI_Char), NitsHere()); fread (thumbprint, 1, length, fingerprint_file); // There's a newline at the end, so null terminate overwriting it. thumbprint[length-1] = '\0'; fclose (fingerprint_file); } else { DSC_EventWriteLCMServerRegCertGenFailed(g_ConfigurationDetails.jobGuidString, self->agentId); return MI_RESULT_FAILED; } } // Cache this URL. // result = CacheServerURL(self, request->registrationData, thumbprint, cimErrorDetails); //EH_CheckResult(result); // Write this cache to DSC Cache //result = UpdateServerURLsToDSCCache(self, cimErrorDetails); //EH_CheckResult(result); } result = GetAgentInformation(®istrationPayload); EH_CheckResult(result); result = LCM_Do_Register((MI_Instance *)g_metaConfig, request->registrationData, self->agentId, thumbprint, registrationPayload, &request->configurationNames, request->typeOfManagerInstance, &resultStatus, &getActionStatusCode, cimErrorDetails); EH_CheckResult(result); EH_UNWIND: if (registrationPayload != NULL) { MI_Instance_Delete(registrationPayload); } if (thumbprint != NULL) { DSC_free(thumbprint); } #ifdef _MSC_VER DSC_GlobalFree(resultStatus); #else DSC_free(resultStatus); #endif return result; }
void MI_CALL MSFT_DSCLocalConfigManager_Invoke_ApplyConfiguration( _In_opt_ MSFT_DSCLocalConfigManager_Self* self, _In_ MI_Context* context, _In_opt_z_ const MI_Char* nameSpace, _In_opt_z_ const MI_Char* className, _In_opt_z_ const MI_Char* methodName, _In_ const MSFT_DSCLocalConfigManager* instanceName, _In_opt_ const MSFT_DSCLocalConfigManager_ApplyConfiguration* in) { MI_Result miResult; MI_Instance *cimErrorDetails = NULL; MSFT_DSCLocalConfigManager_ApplyConfiguration outputObject; HANDLE m_clientThreadToken; MI_UNREFERENCED_PARAMETER(self); MI_UNREFERENCED_PARAMETER(nameSpace); MI_UNREFERENCED_PARAMETER(className); MI_UNREFERENCED_PARAMETER(methodName); MI_UNREFERENCED_PARAMETER(instanceName); MI_UNREFERENCED_PARAMETER(in); if(!OpenThreadToken(GetCurrentThread(), TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE, TRUE, &m_clientThreadToken)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_THREADIMPERSONATION_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); return; } if (!SetThreadToken(NULL, NULL)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_REVERTSELF_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); CloseHandle(m_clientThreadToken); return; } miResult = MSFT_DSCLocalConfigManager_ApplyConfiguration_Construct(&outputObject, context); if(miResult != MI_RESULT_OK) { GetCimMIError(miResult, &cimErrorDetails, ID_LCMHELPER_CONSTRUCTAPPLY_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); SetThreadToken(NULL, m_clientThreadToken); CloseHandle(m_clientThreadToken); return; } miResult = CallConsistencyEngine(context, &cimErrorDetails); if(miResult != MI_RESULT_OK) { MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); SetThreadToken(NULL, m_clientThreadToken); CloseHandle(m_clientThreadToken); return; } miResult = MSFT_DSCLocalConfigManager_ApplyConfiguration_Set_MIReturn(&outputObject, 0); if(miResult != MI_RESULT_OK) { GetCimMIError(miResult, &cimErrorDetails, ID_LCMHELPER_SETAPPLY_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); SetThreadToken(NULL, m_clientThreadToken); CloseHandle(m_clientThreadToken); return; } miResult = MSFT_DSCLocalConfigManager_ApplyConfiguration_Post(&outputObject, context); MSFT_DSCLocalConfigManager_ApplyConfiguration_Destruct(&outputObject); if (!SetThreadToken(NULL, m_clientThreadToken)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_RESUMEIMPERSONATION_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); CloseHandle(m_clientThreadToken); return; } CloseHandle(m_clientThreadToken); if(miResult != MI_RESULT_OK) { GetCimMIError(miResult, &cimErrorDetails, ID_LCMHELPER_POSTAPPLY_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); return; } MI_Context_PostResult(context, MI_RESULT_OK); }
void MI_CALL MSFT_nxGroupResource_Invoke_InventoryTargetResource( _In_opt_ MSFT_nxGroupResource_Self* self, _In_ MI_Context* context, _In_opt_z_ const MI_Char* nameSpace, _In_opt_z_ const MI_Char* className, _In_opt_z_ const MI_Char* methodName, _In_ const MSFT_nxGroupResource* instanceName, _In_opt_ const MSFT_nxGroupResource_InventoryTargetResource* in) { SCX_BOOKEND_EX ("Inventory", " name=\"nxGroup\""); MI_Result result = MI_RESULT_FAILED; if (self) { MI_Instance* retInstance; MI_NewDynamicInstance ( context, className, 0, &retInstance); result = self->inventory (in->InputResource.value->__instance, context, retInstance); if (MI_RESULT_OK == result) { SCX_BOOKEND_PRINT ("packing succeeded!"); MSFT_nxGroupResource_InventoryTargetResource out; MSFT_nxGroupResource_InventoryTargetResource_Construct (&out, context); MSFT_nxGroupResource_InventoryTargetResource_Set_MIReturn (&out, 0); { const MI_Uint32 c_initBufferLength = 1000000; MI_Application application; MI_Serializer serializer; MI_Uint8 *clientBuffer; MI_Uint32 clientBufferLength = c_initBufferLength; MI_Uint32 clientBufferNeeded = 0; const char * reportTemplateBase = DSC_ETC_PATH "/InventoryReports/nxGroup_XXXXXX"; FILE *fp = NULL; clientBuffer = (MI_Uint8*)malloc(clientBufferLength + 1); MI_Application_Initialize(0,NULL,NULL, &application); result = XmlSerializer_Create(&application, 0, "MI_XML", &serializer); if (result != MI_RESULT_OK) { MI_Application_Close(&application); MI_Context_PostResult (context, result); return; } result = XmlSerializer_SerializeInstance( &serializer, 0, retInstance, clientBuffer, clientBufferLength, &clientBufferNeeded); if (result != MI_RESULT_OK) { free(clientBuffer); if (clientBufferNeeded > 0) { // Try again with a buffer given to us by the clientBufferNeeded field clientBufferLength = clientBufferNeeded; clientBuffer = (MI_Uint8*)malloc(clientBufferLength + 1); result = XmlSerializer_SerializeInstance( &serializer, 0, retInstance, clientBuffer, clientBufferLength, &clientBufferNeeded); } else { XmlSerializer_Close(&serializer); MI_Application_Close(&application); MI_Context_PostResult (context, result); return; } } XmlSerializer_Close(&serializer); MI_Application_Close(&application); if (result == MI_RESULT_OK) { clientBuffer[clientBufferNeeded] = '\0'; printf((char*)clientBuffer); } { char * reportTemplate = (char*)malloc(strlen(reportTemplateBase)); strcpy(reportTemplate, reportTemplateBase); int fd = mkstemp(reportTemplate); if (fd == -1) { std::cerr << std::endl << "Error running mkstemp, errno = " << errno << std::endl; } fp = fdopen(fd, "w"); if( fp != NULL ) { fwrite(clientBuffer, 1, clientBufferNeeded, fp); fclose(fp); } else { std::cerr << std::endl << "Error opening file descriptor for reportTemplate, errno = " << errno << std::endl; } free(reportTemplate); } free(clientBuffer); } result = MSFT_nxGroupResource_InventoryTargetResource_Post (&out, context); if (MI_RESULT_OK != result) { SCX_BOOKEND_PRINT ("post Failed"); } MSFT_nxGroupResource_InventoryTargetResource_Destruct (&out); } else { SCX_BOOKEND_PRINT ("inventory FAILED"); } MI_Instance_Delete (retInstance); } MI_Context_PostResult (context, result); }
void MI_CALL MSFT_DSCLocalConfigManager_Invoke_SendMetaConfiguration( _In_opt_ MSFT_DSCLocalConfigManager_Self* self, _In_ MI_Context* context, _In_opt_z_ const MI_Char* nameSpace, _In_opt_z_ const MI_Char* className, _In_opt_z_ const MI_Char* methodName, _In_ const MSFT_DSCLocalConfigManager* instanceName, _In_opt_ const MSFT_DSCLocalConfigManager_SendMetaConfiguration* in) { MI_Result miResult; MI_Instance *cimErrorDetails = NULL; HANDLE m_clientThreadToken; MI_UNREFERENCED_PARAMETER(self); MI_UNREFERENCED_PARAMETER(nameSpace); MI_UNREFERENCED_PARAMETER(className); MI_UNREFERENCED_PARAMETER(methodName); MI_UNREFERENCED_PARAMETER(instanceName); MI_UNREFERENCED_PARAMETER(in); if(!in->MetaConfiguration.exists) { MI_Context_PostResult(context, MI_RESULT_INVALID_PARAMETER); return; } if(!OpenThreadToken(GetCurrentThread(), TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE, TRUE, &m_clientThreadToken)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_THREADIMPERSONATION_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); return; } if (!SetThreadToken(NULL, NULL)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_REVERTSELF_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); CloseHandle(m_clientThreadToken); return; } miResult = SetMetaConfig(in->MetaConfiguration.value, &cimErrorDetails); if (!SetThreadToken(NULL, m_clientThreadToken)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_RESUMEIMPERSONATION_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); CloseHandle(m_clientThreadToken); return; } CloseHandle(m_clientThreadToken); if(miResult != MI_RESULT_OK) { MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); return; } MI_Context_PostResult(context, MI_RESULT_OK); }
/* Do_Association() creates a key instance for the association and retrieves some extra optional filtering information * used for the assoociation, then selects from synchronous or asynchronous. */ void Do_Association(MI_Session *miSession, _In_z_ const wchar_t *namespaceName, const wchar_t *className) { wchar_t synchronous; wchar_t keysOnly; MI_Result miResult; MI_Instance *keyInstance; MI_Char _associationClass[50]; MI_Char *associationClass = NULL; MI_Char _resultClass[50]; MI_Char *resultClass = NULL; MI_Char _roleProperty[50]; MI_Char *roleProperty = NULL; MI_Char _resultRoleProperty[50]; MI_Char *resultRoleProperty = NULL; /* Helper function to create an MI_Instance which contains the classes keys to pass into the association operation. * Delete the instance once done with MI_Instance_Delete(). */ miResult = CreateInboundInstance(miSession, namespaceName, className, MI_TRUE, &keyInstance); if (miResult != MI_RESULT_OK) { wprintf(L"Failed to create a keyed instance for the operation, error %s\n", MI_Result_To_String(miResult)); return; } /* Asks for the association class filter. This allows the results to be filtered to just one association class. */ wprintf(L"Enter associator filters (all optional)\n"); GetUserInputString(L"association class", _associationClass, sizeof(_associationClass)/sizeof(_associationClass[0]), L""); if (_associationClass[0] != 0) { associationClass = _associationClass; } /* Ask for the result class filter. This allows results to be filtered to just this final class type. */ GetUserInputString(L"result class", _resultClass, sizeof(_resultClass)/sizeof(_resultClass[0]), L""); if (_resultClass[0] != 0) { resultClass = _resultClass; } /* Asks for association role property filter. This allows for filtering based on the property name within the * association class itself that points to the initial key instance. */ GetUserInputString(L"role property", _roleProperty, sizeof(_roleProperty)/sizeof(_roleProperty[0]), L""); if (_roleProperty[0] != 0) { roleProperty = _roleProperty; } /* Ask for the association result role property filter. This allows for filtering based on the property name within * the association class itself that points to the result class. */ GetUserInputString(L"result role property", _resultRoleProperty, sizeof(_resultRoleProperty)/sizeof(_resultRoleProperty[0]), L""); if (_resultRoleProperty[0] != 0) { resultRoleProperty = _resultRoleProperty; } /* Asks user if full result instances are required, or just the key properties */ keysOnly = GetUserSelection( L"How do you want the Associator operation to return property keys only?\n" L"\t[1] All instance properties\n" L"\t[2] Instance key properties only\n" L"\t[0] back to operation choice\n", L"012"); /* ASk user if synchronous or asynchronous operation is required. */ synchronous = GetUserSelection( L"How do you want the Associator operation to be carried out?\n" L"\t[1] Synchronous\n" L"\t[2] Asynchronous\n" L"\t[0] back to operation choice\n", L"012"); switch(synchronous) { case L'1': Do_Association_Synchronous(miSession, namespaceName, keyInstance, keysOnly==L'2' ? MI_TRUE : MI_FALSE, associationClass, resultClass, roleProperty, resultRoleProperty); break; case L'2': Do_Association_Asynchronous(miSession, namespaceName, keyInstance, keysOnly==L'2' ? MI_TRUE : MI_FALSE, associationClass, resultClass, roleProperty, resultRoleProperty); break; } /* Delete instance from CreateInboundInstance() call. */ miResult = MI_Instance_Delete(keyInstance); if (miResult != MI_RESULT_OK) { /* Invalid parameter is the only likely error which would imply a programming error. */ wprintf(L"MI_Instance_Delete, error %s\n", MI_Result_To_String(miResult)); return; } }
void MI_CALL MSFT_nxServiceResource_Invoke_GetTargetResource( _In_opt_ MSFT_nxServiceResource_Self* self, _In_ MI_Context* context, _In_opt_z_ const MI_Char* nameSpace, _In_opt_z_ const MI_Char* className, _In_opt_z_ const MI_Char* methodName, _In_ const MSFT_nxServiceResource* instanceName, _In_opt_ const MSFT_nxServiceResource_GetTargetResource* in) { std::cerr << "Get" << std::endl; MI_Result r = MI_RESULT_OK; MI_Boolean res = MI_TRUE; MSFT_nxServiceResource_GetTargetResource out; MI_Instance *newInstance; MI_Value value; r = MSFT_nxServiceResource_GetTargetResource_Construct(&out, context); r = MSFT_nxServiceResource_GetTargetResource_Set_MIReturn(&out, 0); const MSFT_nxServiceResource * service = in->InputResource.value; r = MI_Instance_Clone(&service->__instance, &newInstance); std::vector<std::string> ret_strings; long exit_code = callPythonFunction( ret_strings, self->pGetFn, 4, PassString(service->Name), PassString(service->Controller), PassBoolean(service->Enabled), PassString(service->State)); // Expecting 4+1 parameters in return if (ret_strings.size() == (4+1) && exit_code == 0) { res = MI_TRUE; } else { MI_Context_PostResult(context, MI_RESULT_FAILED); return; } if (SetElement(newInstance, "Name", ret_strings[0], MI_STRING) != 0 || SetElement(newInstance, "Controller", ret_strings[1], MI_STRING) != 0 || SetElement(newInstance, "Enabled", ret_strings[2], MI_BOOLEAN) != 0 || SetElement(newInstance, "State", ret_strings[3], MI_STRING) != 0 || SetElement(newInstance, "Path", ret_strings[4], MI_STRING)) { MI_Context_PostResult(context, MI_RESULT_FAILED); return; } value.instance = newInstance; r = MI_Instance_SetElement(&out.__instance, "OutputResource", &value, MI_INSTANCE, 0); if ( r != MI_RESULT_OK ) { MI_Context_PostResult(context, r); return; } MI_Instance_Delete(newInstance); r = MSFT_nxServiceResource_GetTargetResource_Post(&out, context); if ( r != MI_RESULT_OK ) { MI_Context_PostResult(context, r); return; } r = MSFT_nxServiceResource_GetTargetResource_Destruct(&out); if ( r != MI_RESULT_OK ) { MI_Context_PostResult(context, r); return; } MI_Context_PostResult(context, MI_RESULT_OK); }
void MI_CALL MSFT_DSCLocalConfigManager_Invoke_GetConfiguration( _In_opt_ MSFT_DSCLocalConfigManager_Self* self, _In_ MI_Context* context, _In_opt_z_ const MI_Char* nameSpace, _In_opt_z_ const MI_Char* className, _In_opt_z_ const MI_Char* methodName, _In_ const MSFT_DSCLocalConfigManager* instanceName, _In_opt_ const MSFT_DSCLocalConfigManager_GetConfiguration* in) { MI_Result miResult; MI_Instance *cimErrorDetails = NULL; MI_InstanceA outInstances = {0}; MI_Value val; MI_Uint32 bufferIndex = 0; MSFT_DSCLocalConfigManager_GetConfiguration outputObject; HANDLE m_clientThreadToken; MI_UNREFERENCED_PARAMETER(self); MI_UNREFERENCED_PARAMETER(nameSpace); MI_UNREFERENCED_PARAMETER(className); MI_UNREFERENCED_PARAMETER(methodName); MI_UNREFERENCED_PARAMETER(instanceName); MI_UNREFERENCED_PARAMETER(in); if(!in->configurationData.exists) { MI_Context_PostResult(context, MI_RESULT_INVALID_PARAMETER); return; } if(!OpenThreadToken(GetCurrentThread(), TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE, TRUE, &m_clientThreadToken)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_THREADIMPERSONATION_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); return; } if (!SetThreadToken(NULL, NULL)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_REVERTSELF_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); CloseHandle(m_clientThreadToken); return; } miResult = MSFT_DSCLocalConfigManager_GetConfiguration_Construct(&outputObject, context); if(miResult != MI_RESULT_OK) { GetCimMIError(miResult, &cimErrorDetails, ID_LCMHELPER_CONSTRUCTGET_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); return; } GetRealBufferIndex( &(in->configurationData.value), &bufferIndex); miResult = CallGetConfiguration(in->configurationData.value.data + bufferIndex, in->configurationData.value.size - bufferIndex, &outInstances, context, &cimErrorDetails); if(miResult != MI_RESULT_OK) { MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); MSFT_DSCLocalConfigManager_GetConfiguration_Destruct(&outputObject); SetThreadToken(NULL, m_clientThreadToken); CloseHandle(m_clientThreadToken); return; } val.instancea.data = outInstances.data; val.instancea.size = outInstances.size; miResult = MI_Context_WriteStreamParameter(context, L"configurations", &val, MI_INSTANCEA, 0); CleanUpInstanceCache(&outInstances); if(miResult != MI_RESULT_OK) { GetCimMIError(miResult, &cimErrorDetails, ID_LCMHELPER_WRITEGET_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); MSFT_DSCLocalConfigManager_GetConfiguration_Destruct(&outputObject); SetThreadToken(NULL, m_clientThreadToken); CloseHandle(m_clientThreadToken); return; } miResult = MSFT_DSCLocalConfigManager_GetConfiguration_Set_MIReturn(&outputObject, 0); if(miResult != MI_RESULT_OK) { GetCimMIError(miResult, &cimErrorDetails, ID_LCMHELPER_SETGET_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); MSFT_DSCLocalConfigManager_GetConfiguration_Destruct(&outputObject); SetThreadToken(NULL, m_clientThreadToken); CloseHandle(m_clientThreadToken); return; } miResult = MSFT_DSCLocalConfigManager_GetConfiguration_Post(&outputObject, context); MSFT_DSCLocalConfigManager_GetConfiguration_Destruct(&outputObject); if (!SetThreadToken(NULL, m_clientThreadToken)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_RESUMEIMPERSONATION_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); CloseHandle(m_clientThreadToken); return; } CloseHandle(m_clientThreadToken); if(miResult != MI_RESULT_OK) { GetCimMIError(miResult, &cimErrorDetails, ID_LCMHELPER_POSTGET_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); return; } MI_Context_PostResult(context, MI_RESULT_OK); }
PAL_Uint32 THREAD_API fireindication(void* param) { Config* config = (Config*)param; MI_Result r = MI_RESULT_OK; MI_Uint32 failAfterCount = config->failAfterCount; MI_Result failResult = config->failResult; Atomic_Swap( &config->threadrunning, 1); LOGMSG(("Set threadrunning to 1 for config (%p)", config)); config->count = 0; LOGMSG(("Start to fireindication for class (%s)", config->className)); /* wait for the semaphore */ LOGMSG(("Wait for semaphore to fire indication")); Sem_Wait(&config->sem); LOGMSG(("Received semaphore and start firing indication")); if (config->testGroup == (MI_Uint32)TestGroup_Misc) { switch (config->miscTestSubGroup) { case MiscTestGroup_GetExpression: case MiscTestGroup_Evaluate: case MiscTestGroup_SelfTest: failAfterCount = 1; failResult = MI_RESULT_FAILED; break; default: break; } } while((MI_FALSE == config->disabled) && (config->count < failAfterCount)) { config->count++; if (config->intervalMS > 0) { Sleep_Milliseconds(config->intervalMS); } if (r!= MI_RESULT_OK) { LOGMSG(("Set property value of indication failed (%d); Ignore #%d indication", r, config->count)); continue; } if (config->lifecycleThreadControl == 0) { const MI_Char* bookmark = NULL; DEBUG_ASSERT( NULL != config->setprop ); r = config->setprop(config->context, &config->indication); if (r == MI_RESULT_OK) { DEBUG_ASSERT( NULL != config->indication ); if (config->subscribeBookmark && 0 < Strlen(config->subscribeBookmark) && 0 != Strcmp(config->subscribeBookmark, ".")) { // Only set bookmark if it is non-NULL and non-default bookmark = ansiToMI(config->subscribeBookmark); } r = MI_Context_PostIndication(config->postctx, config->indication, 0, bookmark); if (r!= MI_RESULT_OK) { LOGMSG(("Post #%d Indication failed (%d)", config->count, r)); } /* Delete indication instance */ MI_Instance_Delete( config->indication ); config->indication = NULL; } else { LOGMSG(("config(%p:%s)->setprop failed to create indication instance(%d)", config, config->className, r)); } } else { if (config->lifecycleContext && config->currentSubscriptionTypes != 0 && config->supportedSubscriptionTypes != 0) { // lifeCycleThreadControl other than 0 and a running thread indicates that we should schedule lifecycle events if (config->lifecycleThreadControl & MI_LIFECYCLE_INDICATION_CREATE) { r = lifecycle_CreateInstance(config, config->lifecycleInstance); if (r != MI_RESULT_OK) { LOGMSG(("Lifecycle_CreateInstance for class (%s) failed with result (%d)", config->className, r)); } } if (config->lifecycleThreadControl & MI_LIFECYCLE_INDICATION_MODIFY) { r = lifecycle_ModifyInstance(config->lifecycleInstance, config); if (r != MI_RESULT_OK) { LOGMSG(("Lifecycle_ModifyInstance for class (%s) failed with result (%d)", config->className, r)); } } if (config->lifecycleThreadControl & MI_LIFECYCLE_INDICATION_DELETE) { r = lifecycle_DeleteInstance(config); if (r != MI_RESULT_OK) { LOGMSG(("Lifecycle_DeleteInstance for class (%s) failed with result (%d)", config->className, r)); } } if (config->lifecycleThreadControl & MI_LIFECYCLE_INDICATION_READ) { r = lifecycle_EnumerateInstances(config); if (r != MI_RESULT_OK) { LOGMSG(("Lifecycle_EnumerateInstances for class (%s) failed with result (%d)", config->className, r)); } } if (config->lifecycleThreadControl & MI_LIFECYCLE_INDICATION_METHODCALL) { r = lifecycle_InvokeMethod(config); if (r != MI_RESULT_OK) { LOGMSG(("Lifecycle_InvokeMethod for class (%s) failed with result (%d)", config->className, r)); } } } } LOGMSG(("Fired #%d indication for class (%s)", config->count, config->className)); } Atomic_Swap( &config->threadrunning, 0); LOGMSG(("Set threadrunning to 0 for config (%p)", config)); if (MI_FALSE == config->disabled) { LOGMSG(("fireindication stopped due to class (%s) need to fail after firing (%d) indicaitons", config->className, failAfterCount)); #if !defined(_MSC_VER) // if not joined yet, release thread resources pthread_detach(config->thread.__impl); # endif if (failResult != MI_RESULT_OK) { if (config->lifecycleThreadControl == 0) { MI_Context_PostResult(config->postctx, failResult); } else { MI_LifecycleIndicationContext_PostResult(config->lifecycleContext, failResult); } LOGMSG(("Stop the fireindication and failed the operation with error (%s:%d)", config->className, failResult)); } } else LOGMSG(("fireindication stopped due to disable indication called")); LOGMSG(("Done fireindication for class (%s)", config->className)); return 0; }
int main(int argc, char *argv[]) { MI_Application miApp = MI_APPLICATION_NULL; MI_Session miSess = MI_SESSION_NULL; MI_Operation miOperation = MI_OPERATION_NULL; MI_Instance *parameter = NULL; MI_Result r = MI_RESULT_OK; MI_Value value; const MI_Instance *result; MI_Boolean moreResults; MI_Result resultCode; const MI_Char *errorMessage; MI_Instance *cimErrorDetails = NULL; MI_Uint32 argValue = TASK_REGULAR; // if( argc >= 2 ) { // the argument is the value of the parameter passed to function PerformRequiredConfigurationChecks argValue = atoi(argv[1]); if( argValue == 0 || !(argValue == TASK_REGULAR || argValue== TASK_REBOOT || argValue == TASK_BOOTSTRAP)) argValue = TASK_REGULAR; } r = DSC_MI_Application_Initialize(0, NULL, NULL, &miApp); if( r != MI_RESULT_OK) { return r; } r = DSC_MI_Application_NewSession(&miApp, NULL, NULL, NULL, NULL, NULL, &miSess); if (r != MI_RESULT_OK) { MI_Application_Close(&miApp); return r; } r = DSC_MI_Application_NewInstance(&miApp, MI_T("__Parameter"), NULL, ¶meter); if (r != MI_RESULT_OK) { MI_Session_Close(&miSess, NULL, NULL); MI_Application_Close(&miApp); return r; } value.uint32 = TASK_REGULAR; r = DSC_MI_Instance_AddElement(parameter, PerformRequiredConfigurationChecks_PARAMETER_NAME, &value, MI_UINT32, 0); if (r != MI_RESULT_OK) { MI_Instance_Delete(parameter); MI_Session_Close(&miSess, NULL, NULL); MI_Application_Close(&miApp); return r; } MI_Session_Invoke(&miSess, 0, 0, DSCENGINE_NAMESPACE, MSFT_DSCLocalConfigManager_CLASSNAME, MSFT_DSCLocalConfigManager_PerformRequiredConfigurationChecks, NULL, parameter, NULL, &miOperation); r = MI_Operation_GetInstance(&miOperation, &result, &moreResults, &resultCode, &errorMessage, (const MI_Instance **)&cimErrorDetails); if (resultCode != MI_RESULT_OK) { //write error message r = MI_RESULT_FAILED; } // Free resources MI_Instance_Delete(parameter); MI_Operation_Close(&miOperation); MI_Session_Close(&miSess, NULL, NULL); MI_Application_Close(&miApp); return r; }
/* Do_Create() prompts the user to input the key properties to identify the object to get, then selects from * synchronous or asynchronous. */ void Do_Create(MI_Session *miSession, _In_z_ const wchar_t *namespaceName, const wchar_t *className) { wchar_t synchronous; MI_Instance *createInstance; MI_Char inputBuffer[50]; MI_Result miResult; /* Helper function to create an MI_Instance which contains the classes keys to pass into the Create operation. * This object is strongly typed, so we have all property names and types included, although only key properties * will be populated at this point. * Delete the instance once done with MI_Instance_Delete(). */ miResult = CreateInboundInstance(miSession, namespaceName, className, MI_TRUE, &createInstance); if (miResult != MI_RESULT_OK) { wprintf(L"Failed to create a keyed instance for the operation, error %s\n", MI_Result_To_String(miResult)); return; } /* Allow user to select which other property to change and set to a new value */ for (;;) { const MI_Char *elementName; MI_Value elementValue; MI_Type elementType; MI_Uint32 elementIndex; Dump_MI_Instance(createInstance, MI_FALSE, 0); /* Get the property name that user wants to change */ GetUserInputString(L"Input property name to change value, blank to continue", inputBuffer, sizeof(inputBuffer)/sizeof(inputBuffer[0]), L""); if (inputBuffer[0] == L'\0') { /* Blank property name means user wants to carry out operation now */ break; } /* Note: Doing GetElement followed by GetElementAt in order to retrieve element name pointer from instance so we can reuse the input buffer */ miResult = MI_Instance_GetElement(createInstance, inputBuffer, NULL, NULL, NULL, &elementIndex); if (miResult != MI_RESULT_OK) { wprintf(L"Failed to get property, error %s\n", MI_Result_To_String(miResult)); } miResult = MI_Instance_GetElementAt(createInstance, elementIndex, &elementName, &elementValue, &elementType, NULL); if (miResult != MI_RESULT_OK) { wprintf(L"Failed to get property, error %s\n", MI_Result_To_String(miResult)); } /* Helper function to input a new value and set it on the instance */ miResult = SetInstanceProperty(createInstance, elementName, elementType, 0, &elementValue, inputBuffer, sizeof(inputBuffer)/sizeof(inputBuffer[0]), MI_FALSE); if (miResult != MI_RESULT_OK) { wprintf(L"Failed to set property value, error %s\n", MI_Result_To_String(miResult)); } }; /* Select synchronous or asynchronous operation */ synchronous = GetUserSelection( L"How do you want the Create operation to be carried out?\n" L"\t[1] Synchronous\n" L"\t[2] Asynchronous\n" L"\t[0] back to operation choice\n", L"012"); if (synchronous == L'0') { goto errorCleanup; } switch(synchronous) { case L'1': Do_Create_Synchronous(miSession, namespaceName, createInstance); break; case L'2': Do_Create_Asynchronous(miSession, namespaceName, createInstance); break; } errorCleanup: /* Delete instance from CreateInboundInstance() call. */ if (createInstance) { miResult = MI_Instance_Delete(createInstance); if (miResult != MI_RESULT_OK) { /* Invalid parameter is the only likely error which would imply a programming error. */ wprintf(L"MI_Instance_Delete, error %s\n", MI_Result_To_String(miResult)); } } }
void MI_CALL MSFT_DSCLocalConfigManager_Invoke_SendConfigurationApply( _In_opt_ MSFT_DSCLocalConfigManager_Self* self, _In_ MI_Context* context, _In_opt_z_ const MI_Char* nameSpace, _In_opt_z_ const MI_Char* className, _In_opt_z_ const MI_Char* methodName, _In_ const MSFT_DSCLocalConfigManager* instanceName, _In_opt_ const MSFT_DSCLocalConfigManager_SendConfigurationApply* in) { MI_Result miResult; MI_Instance *cimErrorDetails = NULL; MI_Uint32 bufferIndex = 0; HANDLE m_clientThreadToken; MI_UNREFERENCED_PARAMETER(self); MI_UNREFERENCED_PARAMETER(nameSpace); MI_UNREFERENCED_PARAMETER(className); MI_UNREFERENCED_PARAMETER(methodName); MI_UNREFERENCED_PARAMETER(instanceName); MI_UNREFERENCED_PARAMETER(in); if(!in->ConfigurationData.exists) { MI_Context_PostResult(context, MI_RESULT_INVALID_PARAMETER); return; } if(!OpenThreadToken(GetCurrentThread(), TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE, TRUE, &m_clientThreadToken)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_THREADIMPERSONATION_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); return; } if (!SetThreadToken(NULL, NULL)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_REVERTSELF_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); CloseHandle(m_clientThreadToken); return; } GetRealBufferIndex( &(in->ConfigurationData.value), &bufferIndex); miResult = CallSetConfiguration(in->ConfigurationData.value.data + bufferIndex, in->ConfigurationData.value.size - bufferIndex, LCM_SETFLAGS_DEFAULT, context, &cimErrorDetails); if (!SetThreadToken(NULL, m_clientThreadToken)) { GetCimWin32Error(GetLastError(), &cimErrorDetails, ID_LCMHELPER_RESUMEIMPERSONATION_FAILED); MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); CloseHandle(m_clientThreadToken); return; } CloseHandle(m_clientThreadToken); if(miResult != MI_RESULT_OK) { MI_PostCimError(context, cimErrorDetails); MI_Instance_Delete(cimErrorDetails); return; } MI_Context_PostResult(context, MI_RESULT_OK); }