DWORD LwSmQueryServiceReverseDependencyClosure( LW_SERVICE_HANDLE hHandle, PWSTR** pppwszServiceList ) { DWORD dwError = 0; PWSTR* ppwszServiceList = NULL; PWSTR* ppwszAllServices = NULL; dwError = LwAllocateMemory(sizeof(*ppwszServiceList) * 1, OUT_PPVOID(&ppwszServiceList)); BAIL_ON_ERROR(dwError); dwError = LwSmEnumerateServices(&ppwszAllServices); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceReverseDependencyClosureHelper( hHandle, ppwszAllServices, &ppwszServiceList); BAIL_ON_ERROR(dwError); *pppwszServiceList = ppwszServiceList; cleanup: if (ppwszAllServices) { LwSmFreeStringList(ppwszAllServices); } return dwError; error: *pppwszServiceList = NULL; if (ppwszServiceList) { LwSmFreeStringList(ppwszServiceList); } goto cleanup; }
static DWORD LwSmFindServiceWithPid( pid_t pid, PLW_SERVICE_HANDLE phHandle ) { DWORD dwError = 0; PWSTR* ppwszServiceNames = NULL; DWORD i = 0; PLW_SERVICE_INFO pInfo = NULL; LW_SERVICE_HANDLE hHandle = NULL; LW_SERVICE_STATUS status = {0}; dwError = LwSmEnumerateServices(&ppwszServiceNames); BAIL_ON_ERROR(dwError); for (i = 0; ppwszServiceNames[i]; i++) { dwError = LwSmAcquireServiceHandle(ppwszServiceNames[i], &hHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceStatus(hHandle, &status); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceInfo(hHandle, &pInfo); BAIL_ON_ERROR(dwError); if (status.pid == pid && pInfo->type != LW_SERVICE_TYPE_DRIVER) { *phHandle = hHandle; hHandle = NULL; goto cleanup; } LwSmReleaseServiceHandle(hHandle); hHandle = NULL; LwSmFreeServiceInfo(pInfo); pInfo = NULL; } dwError = LW_ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); cleanup: if (hHandle) { LwSmReleaseServiceHandle(hHandle); } if (ppwszServiceNames) { LwSmFreeServiceNameList(ppwszServiceNames); } if (pInfo) { LwSmFreeServiceInfo(pInfo); } return dwError; error: goto cleanup; }
static DWORD LwSmWaitForLwsmd( void ) { DWORD dwError = 0; int try = 0; static const int maxTries = 4; static const int interval = 5; PWSTR* ppwszServices = NULL; do { dwError = LwSmEnumerateServices(&ppwszServices); if (dwError) { sleep(interval); } try++; } while (dwError != LW_ERROR_SUCCESS && try < maxTries); BAIL_ON_ERROR(dwError); cleanup: if (ppwszServices) { LwSmFreeServiceNameList(ppwszServices); } return dwError; error: goto cleanup; } static DWORD LwSmList( int argc, char** pArgv ) { DWORD dwError = 0; PWSTR* ppwszServiceNames = NULL; PSTR pszServiceName = NULL; LW_SERVICE_STATUS status; LW_SERVICE_HANDLE hHandle = NULL; size_t i = 0; size_t len = 0; size_t maxLen = 0; dwError = LwSmEnumerateServices(&ppwszServiceNames); BAIL_ON_ERROR(dwError); for (i = 0; ppwszServiceNames[i]; i++) { dwError = LwWc16sLen(ppwszServiceNames[i], &len); BAIL_ON_ERROR(dwError); if (len > maxLen) { maxLen = len; } } for (i = 0; ppwszServiceNames[i]; i++) { dwError = LwSmAcquireServiceHandle(ppwszServiceNames[i], &hHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceStatus(hHandle, &status); BAIL_ON_ERROR(dwError); dwError = LwWc16sToMbs(ppwszServiceNames[i], &pszServiceName); BAIL_ON_ERROR(dwError); if (!gState.bQuiet) { printf("%s", pszServiceName); dwError = LwWc16sLen(ppwszServiceNames[i], &len); BAIL_ON_ERROR(dwError); for (; len < maxLen; len++) { printf(" "); } printf(" "); switch (status.state) { case LW_SERVICE_STATE_RUNNING: BAIL_ON_ERROR(dwError); printf("%s (%s: %li)\n", LwSmStateToString(status.state), LwSmHomeToString(status.home), (long) status.pid); break; default: printf("%s\n", LwSmStateToString(status.state)); break; } } dwError = LwSmReleaseServiceHandle(hHandle); hHandle = NULL; BAIL_ON_ERROR(dwError); LW_SAFE_FREE_MEMORY(pszServiceName); } cleanup: LW_SAFE_FREE_MEMORY(pszServiceName); if (hHandle) { LwSmReleaseServiceHandle(hHandle); } if (ppwszServiceNames) { LwSmFreeServiceNameList(ppwszServiceNames); } return dwError; error: goto cleanup; }
static DWORD LwSmAutostart( int argc, char** pArgv ) { DWORD dwError = 0; PWSTR *ppwszAllServices = NULL; LW_SERVICE_HANDLE hHandle = NULL; PLW_SERVICE_INFO pInfo = NULL; size_t i = 0; PWSTR *ppwszDependencies = NULL; LW_SERVICE_HANDLE hDepHandle = NULL; LW_SERVICE_STATUS status = {0}; PSTR pszTemp = NULL; size_t j = 0; dwError = LwSmEnumerateServices(&ppwszAllServices); BAIL_ON_ERROR(dwError); for (i = 0; ppwszAllServices[i]; i++) { dwError = LwSmAcquireServiceHandle(ppwszAllServices[i], &hHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceInfo(hHandle, &pInfo); BAIL_ON_ERROR(dwError); if (pInfo->bAutostart) { dwError = LwSmQueryServiceDependencyClosure( hHandle, &ppwszDependencies); BAIL_ON_ERROR(dwError); for (j = 0; ppwszDependencies[j]; j++) { dwError = LwSmAcquireServiceHandle( ppwszDependencies[j], &hDepHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceStatus(hDepHandle, &status); BAIL_ON_ERROR(dwError); if (status.state != LW_SERVICE_STATE_RUNNING) { if (!gState.bQuiet) { dwError = LwWc16sToMbs(ppwszDependencies[j], &pszTemp); BAIL_ON_ERROR(dwError); printf("Starting service dependency: %s\n", pszTemp); LW_SAFE_FREE_MEMORY(pszTemp); } dwError = LwSmStartService(hDepHandle); BAIL_ON_ERROR(dwError); } dwError = LwSmReleaseServiceHandle(hDepHandle); hDepHandle = NULL; BAIL_ON_ERROR(dwError); } if (ppwszDependencies) { LwSmFreeServiceNameList(ppwszDependencies); ppwszDependencies = NULL; } if (!gState.bQuiet) { dwError = LwWc16sToMbs(ppwszAllServices[i], &pszTemp); BAIL_ON_ERROR(dwError); printf("Starting service: %s\n", pszTemp); LW_SAFE_FREE_MEMORY(pszTemp); } dwError = LwSmStartService(hHandle); BAIL_ON_ERROR(dwError); } dwError = LwSmReleaseServiceHandle(hHandle); hHandle = NULL; BAIL_ON_ERROR(dwError); } cleanup: if (hHandle) { LwSmReleaseServiceHandle(hHandle); hHandle = NULL; } if (ppwszAllServices) { LwSmFreeStringList(ppwszAllServices); ppwszAllServices = NULL; } if (ppwszDependencies) { LwSmFreeServiceNameList(ppwszDependencies); ppwszDependencies = NULL; } if (hDepHandle) { LwSmReleaseServiceHandle(hDepHandle); hDepHandle = NULL; } LW_SAFE_FREE_MEMORY(pszTemp); return dwError; error: goto cleanup; }