static DWORD LwSmQueryServiceDependencyClosureHelper( LW_SERVICE_HANDLE hHandle, PWSTR** pppwszServiceList ) { DWORD dwError = 0; PLW_SERVICE_INFO pInfo = NULL; LW_SERVICE_HANDLE hDepHandle = NULL; PWSTR pwszDepName = NULL; size_t i = 0; dwError = LwSmQueryServiceInfo(hHandle, &pInfo); BAIL_ON_ERROR(dwError); for (i = 0; pInfo->ppwszDependencies[i]; i++) { dwError = LwSmAcquireServiceHandle(pInfo->ppwszDependencies[i], &hDepHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceDependencyClosureHelper(hDepHandle, pppwszServiceList); BAIL_ON_ERROR(dwError); if (!LwSmStringListContains(*pppwszServiceList, pInfo->ppwszDependencies[i])) { dwError = LwAllocateWc16String(&pwszDepName, pInfo->ppwszDependencies[i]); BAIL_ON_ERROR(dwError); dwError = LwSmStringListAppend(pppwszServiceList, pwszDepName); BAIL_ON_ERROR(dwError); pwszDepName = NULL; } LwSmReleaseServiceHandle(hDepHandle); hDepHandle = NULL; } cleanup: LW_SAFE_FREE_MEMORY(pwszDepName); if (pInfo) { LwSmCommonFreeServiceInfo(pInfo); } if (hDepHandle) { LwSmReleaseServiceHandle(hDepHandle); } return dwError; error: goto cleanup; }
static DWORD LwSmStatus( int argc, char** pArgv, int* pRet ) { DWORD dwError = 0; PWSTR pwszServiceName = NULL; LW_SERVICE_HANDLE hHandle = NULL; LW_SERVICE_STATUS status = {0}; dwError = LwMbsToWc16s(pArgv[1], &pwszServiceName); BAIL_ON_ERROR(dwError); dwError = LwSmAcquireServiceHandle(pwszServiceName, &hHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceStatus(hHandle, &status); BAIL_ON_ERROR(dwError); if (!gState.bQuiet) { switch (status.state) { case LW_SERVICE_STATE_RUNNING: printf("%s (%s: %li)\n", LwSmStateToString(status.state), LwSmHomeToString(status.home), (long) status.pid); break; default: printf("%s\n", LwSmStateToString(status.state)); break; } } *pRet = status.state; cleanup: LW_SAFE_FREE_MEMORY(pwszServiceName); if (hHandle) { LwSmReleaseServiceHandle(hHandle); } return dwError; error: goto cleanup; }
static DWORD LwSmDoRefresh( int argc, char** pArgv ) { DWORD dwError = 0; PWSTR pwszServiceName = NULL; LW_SERVICE_HANDLE hHandle = NULL; if (argc > 1) { dwError = LwMbsToWc16s(pArgv[1], &pwszServiceName); BAIL_ON_ERROR(dwError); dwError = LwSmAcquireServiceHandle(pwszServiceName, &hHandle); BAIL_ON_ERROR(dwError); if (!gState.bQuiet) { printf("Refreshing service: %s\n", pArgv[1]); } dwError = LwSmRefreshService(hHandle); BAIL_ON_ERROR(dwError); } else { if (!gState.bQuiet) { printf("Refreshing service manager\n"); } dwError = LwSmRefresh(); BAIL_ON_ERROR(dwError); } cleanup: LW_SAFE_FREE_MEMORY(pwszServiceName); if (hHandle) { LwSmReleaseServiceHandle(hHandle); } return dwError; error: goto cleanup; }
static DWORD LwSmCmdSetLogLevel( int argc, char** pArgv ) { DWORD dwError = 0; LW_SM_LOG_LEVEL level = 0; PSTR pFacility = NULL; LW_SERVICE_HANDLE hHandle = NULL; PWSTR pServiceName = NULL; if (argc < 4) { dwError = LW_ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); } if (strcmp(pArgv[1], "-")) { dwError = LwMbsToWc16s(pArgv[1], &pServiceName); BAIL_ON_ERROR(dwError); dwError = LwSmAcquireServiceHandle(pServiceName, &hHandle); BAIL_ON_ERROR(dwError); } if (strcmp(pArgv[2], "-")) { pFacility = pArgv[2]; } dwError = LwSmLogLevelNameToLogLevel(pArgv[3], &level); BAIL_ON_ERROR(dwError); dwError = LwSmSetServiceLogLevel(hHandle, pFacility, level); BAIL_ON_ERROR(dwError); error: return dwError; }
static DWORD LwSmStopOnly( int argc, char** pArgv ) { DWORD dwError = 0; PWSTR pwszServiceName = NULL; LW_SERVICE_HANDLE hHandle = NULL; dwError = LwMbsToWc16s(pArgv[1], &pwszServiceName); BAIL_ON_ERROR(dwError); dwError = LwSmAcquireServiceHandle(pwszServiceName, &hHandle); BAIL_ON_ERROR(dwError); if (!gState.bQuiet) { printf("Stopping service: %s\n", pArgv[1]); } dwError = LwSmStopService(hHandle); BAIL_ON_ERROR(dwError); cleanup: LW_SAFE_FREE_MEMORY(pwszServiceName); if (hHandle) { LwSmReleaseServiceHandle(hHandle); } return dwError; error: goto cleanup; }
DWORD VmwDeployStopService( PCSTR pszName ) { DWORD dwError = 0; PWSTR pwszName = NULL; LW_SERVICE_HANDLE hService = NULL; dwError = VmwDeployAllocateStringWFromA( pszName, &pwszName); BAIL_ON_DEPLOY_ERROR(dwError); dwError = LwSmAcquireServiceHandle( pwszName, &hService); BAIL_ON_DEPLOY_ERROR(dwError); dwError = LwSmStopService(hService); BAIL_ON_DEPLOY_ERROR(dwError); cleanup: if (hService) { LwSmReleaseServiceHandle(hService); } if (pwszName) { VmwDeployFreeMemory(pwszName); } return dwError; error: goto cleanup; }
static DWORD LwSmProxy( int argc, char** pArgv ) { DWORD dwError = 0; LW_SERVICE_HANDLE hHandle = NULL; PWSTR pwszServiceName = NULL; pthread_t waitThread; int sig = 0; dwError = LwSmWaitForLwsmd(); BAIL_ON_ERROR(dwError); dwError = LwSmConfigureSignals(); BAIL_ON_ERROR(dwError); dwError = LwSmStart(argc, pArgv); BAIL_ON_ERROR(dwError); dwError = LwMbsToWc16s(pArgv[1], &pwszServiceName); BAIL_ON_ERROR(dwError); dwError = LwSmAcquireServiceHandle(pwszServiceName, &hHandle); BAIL_ON_ERROR(dwError); dwError = LwMapErrnoToLwError(pthread_create( &waitThread, NULL, LwSmWaitThread, hHandle)); BAIL_ON_ERROR(dwError); dwError = LwMapErrnoToLwError(pthread_detach(waitThread)); BAIL_ON_ERROR(dwError); if (!gState.bQuiet) { printf("Proxying for service: %s\n", pArgv[1]); } dwError = LwSmWaitSignals(&sig); BAIL_ON_ERROR(dwError); switch (sig) { case SIGTERM: dwError = LwSmStop(argc, pArgv); BAIL_ON_ERROR(dwError); break; default: break; } cleanup: LW_SAFE_FREE_MEMORY(pwszServiceName); if (hHandle) { LwSmReleaseServiceHandle(hHandle); } return dwError; error: goto cleanup; }
static DWORD LwSmCmdTapLog( int argc, char** pArgv ) { DWORD error = ERROR_SUCCESS; PLW_THREAD_POOL pPool = NULL; PLW_TASK pTask = NULL; BOOLEAN bResetLogger = FALSE; BOOLEAN bRmPipe = FALSE; LW_SM_LOGGER_TYPE oldLogger = 0; PSTR pOldTarget = NULL; LW_SM_LOG_LEVEL oldLevel = 0; LW_SM_LOG_LEVEL newLevel = 0; PSTR pFacility = NULL; LW_SERVICE_HANDLE hHandle = NULL; PWSTR pServiceName = NULL; PSTR pFifo = NULL; int FifoFd = -1; if (argc < 4) { error = LW_ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(error); } if (strcmp(pArgv[1], "-")) { error = LwMbsToWc16s(pArgv[1], &pServiceName); BAIL_ON_ERROR(error); error = LwSmAcquireServiceHandle(pServiceName, &hHandle); BAIL_ON_ERROR(error); } if (strcmp(pArgv[2], "-")) { pFacility = pArgv[2]; } error = LwSmLogLevelNameToLogLevel(pArgv[3], &newLevel); BAIL_ON_ERROR(error); error = LwSmGetServiceLogState(hHandle, pFacility, &oldLogger, &pOldTarget, &oldLevel); BAIL_ON_ERROR(error); error = LwAllocateStringPrintf(&pFifo, "/tmp/.lwsm-log-tap-%lu", (unsigned long) getpid()); BAIL_ON_ERROR(error); LwRtlBlockSignals(); if (mknod(pFifo, S_IRUSR | S_IWUSR | S_IFIFO, 0) < 0) { error = LwErrnoToWin32Error(errno); BAIL_ON_ERROR(error); } bRmPipe = TRUE; if ((FifoFd = open(pFifo, O_RDONLY | O_NONBLOCK)) < 0) { error = LwErrnoToWin32Error(errno); BAIL_ON_ERROR(error); } if (fcntl(FifoFd, F_SETFL, O_NONBLOCK) < 0) { error = LwErrnoToWin32Error(errno); BAIL_ON_ERROR(error); } error = LwSmSetServiceLogTarget(hHandle, pFacility, LW_SM_LOGGER_FILE, pFifo); BAIL_ON_ERROR(error); bResetLogger = TRUE; error = LwSmSetServiceLogLevel(hHandle, pFacility, newLevel); BAIL_ON_ERROR(error); error = LwNtStatusToWin32Error(LwRtlCreateThreadPool(&pPool, NULL)); BAIL_ON_ERROR(error); error = LwNtStatusToWin32Error(LwRtlCreateTask(pPool, &pTask, NULL, LogTapper, &FifoFd)); BAIL_ON_ERROR(error); LwRtlWakeTask(pTask); error = LwNtStatusToWin32Error(LwRtlMain()); BAIL_ON_ERROR(error); error: if (pTask) { LwRtlCancelTask(pTask); LwRtlWaitTask(pTask); LwRtlReleaseTask(&pTask); } LwRtlFreeThreadPool(&pPool); if (bResetLogger) { error = LwSmSetServiceLogLevel(hHandle, pFacility, oldLevel); BAIL_ON_ERROR(error); error = LwSmSetServiceLogTarget(hHandle, pFacility, oldLogger, pOldTarget); BAIL_ON_ERROR(error); } if (pOldTarget) { LwSmFreeLogTarget(pOldTarget); } if (FifoFd >= 0) { close(FifoFd); } if (bRmPipe) { unlink(pFifo); } LW_SAFE_FREE_MEMORY(pFifo); return error; }
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 LwSmInfo( int argc, char** pArgv ) { DWORD dwError = 0; PWSTR pwszServiceName = NULL; LW_SERVICE_HANDLE hHandle = NULL; PLW_SERVICE_INFO pInfo = NULL; PSTR pszTemp = NULL; size_t i = 0; dwError = LwMbsToWc16s(pArgv[1], &pwszServiceName); BAIL_ON_ERROR(dwError); dwError = LwSmAcquireServiceHandle(pwszServiceName, &hHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceInfo(hHandle, &pInfo); BAIL_ON_ERROR(dwError); printf("Service: %s\n", pArgv[1]); dwError = LwWc16sToMbs(pInfo->pwszDescription, &pszTemp); BAIL_ON_ERROR(dwError); printf("Description: %s\n", pszTemp); LW_SAFE_FREE_MEMORY(pszTemp); printf("Type: %s\n", LwSmTypeToString(pInfo->type)); printf("Autostart: %s\n", pInfo->bAutostart ? "yes" : "no"); dwError = LwWc16sToMbs(pInfo->pwszPath, &pszTemp); BAIL_ON_ERROR(dwError); printf("Path: %s\n", pszTemp); LW_SAFE_FREE_MEMORY(pszTemp); printf("Arguments:"); for (i = 0; pInfo->ppwszArgs[i]; i++) { dwError = LwWc16sToMbs(pInfo->ppwszArgs[i], &pszTemp); BAIL_ON_ERROR(dwError); printf(" '%s'", pszTemp); LW_SAFE_FREE_MEMORY(pszTemp); } printf("\n"); printf("Environment:"); for (i = 0; pInfo->ppwszEnv[i]; i++) { dwError = LwWc16sToMbs(pInfo->ppwszEnv[i], &pszTemp); BAIL_ON_ERROR(dwError); printf(" '%s'", pszTemp); LW_SAFE_FREE_MEMORY(pszTemp); } printf("\n"); printf("Dependencies:"); for (i = 0; pInfo->ppwszDependencies[i]; i++) { dwError = LwWc16sToMbs(pInfo->ppwszDependencies[i], &pszTemp); BAIL_ON_ERROR(dwError); printf(" %s", pszTemp); LW_SAFE_FREE_MEMORY(pszTemp); } printf("\n"); dwError = LwWc16sToMbs(pInfo->pwszGroup, &pszTemp); BAIL_ON_ERROR(dwError); printf("Service Group: %s\n", pszTemp); LW_SAFE_FREE_MEMORY(pszTemp); if (pInfo->dwFdLimit) { printf("File descriptor limit: %lu\n", (unsigned long) pInfo->dwFdLimit); } else { printf("File descriptor limit: inherit\n"); } if (pInfo->dwCoreSize) { printf("Core dump size limit: %lu\n", (unsigned long) pInfo->dwCoreSize); } else { printf("Core dump size limit: inherit\n"); } cleanup: LW_SAFE_FREE_MEMORY(pwszServiceName); LW_SAFE_FREE_MEMORY(pszTemp); if (hHandle) { LwSmReleaseServiceHandle(hHandle); } return dwError; error: goto cleanup; }
static DWORD LwSmStop( int argc, char** pArgv ) { DWORD dwError = 0; PWSTR pwszServiceName = NULL; LW_SERVICE_HANDLE hHandle = NULL; LW_SERVICE_HANDLE hDepHandle = NULL; LW_SERVICE_STATUS status = {0}; PWSTR* ppwszDependencies = NULL; PSTR pszTemp = NULL; size_t i = 0; dwError = LwMbsToWc16s(pArgv[1], &pwszServiceName); BAIL_ON_ERROR(dwError); dwError = LwSmAcquireServiceHandle(pwszServiceName, &hHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceReverseDependencyClosure(hHandle, &ppwszDependencies); BAIL_ON_ERROR(dwError); for (i = 0; ppwszDependencies[i]; i++) { dwError = LwSmAcquireServiceHandle(ppwszDependencies[i], &hDepHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceStatus(hDepHandle, &status); BAIL_ON_ERROR(dwError); if (status.state != LW_SERVICE_STATE_STOPPED) { if (!gState.bQuiet) { dwError = LwWc16sToMbs(ppwszDependencies[i], &pszTemp); BAIL_ON_ERROR(dwError); printf("Stopping service reverse dependency: %s\n", pszTemp); LW_SAFE_FREE_MEMORY(pszTemp); } dwError = LwSmStopService(hDepHandle); BAIL_ON_ERROR(dwError); } dwError = LwSmReleaseServiceHandle(hDepHandle); hDepHandle = NULL; BAIL_ON_ERROR(dwError); } if (!gState.bQuiet) { printf("Stopping service: %s\n", pArgv[1]); } dwError = LwSmStopService(hHandle); BAIL_ON_ERROR(dwError); cleanup: LW_SAFE_FREE_MEMORY(pwszServiceName); LW_SAFE_FREE_MEMORY(pszTemp); if (ppwszDependencies) { LwSmFreeServiceNameList(ppwszDependencies); } if (hHandle) { LwSmReleaseServiceHandle(hHandle); } if (hDepHandle) { LwSmReleaseServiceHandle(hDepHandle); } return dwError; error: goto cleanup; }
static DWORD LwSmRestart( int argc, char** pArgv ) { DWORD dwError = 0; PWSTR pwszServiceName = NULL; LW_SERVICE_HANDLE hHandle = NULL; PWSTR* ppwszReverseDeps = NULL; PLW_SERVICE_STATUS pStatus = NULL; PLW_SERVICE_HANDLE phDepHandles = NULL; PWSTR* ppwszDependencies = NULL; LW_SERVICE_HANDLE hDepHandle = NULL; LW_SERVICE_STATUS status; PSTR pszTemp = NULL; size_t count = 0; size_t i = 0; dwError = LwMbsToWc16s(pArgv[1], &pwszServiceName); BAIL_ON_ERROR(dwError); dwError = LwSmAcquireServiceHandle(pwszServiceName, &hHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceReverseDependencyClosure(hHandle, &ppwszReverseDeps); BAIL_ON_ERROR(dwError); count = LwSmStringListLength(ppwszReverseDeps); dwError = LwAllocateMemory(sizeof(*pStatus) * count, OUT_PPVOID(&pStatus)); BAIL_ON_ERROR(dwError); dwError = LwAllocateMemory(sizeof(*phDepHandles) * count, OUT_PPVOID(&phDepHandles)); BAIL_ON_ERROR(dwError); for (i = 0; i < count; i++) { dwError = LwSmAcquireServiceHandle(ppwszReverseDeps[i], &phDepHandles[i]); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceStatus(phDepHandles[i], &pStatus[i]); BAIL_ON_ERROR(dwError); if (pStatus[i].state != LW_SERVICE_STATE_STOPPED) { if (!gState.bQuiet) { dwError = LwWc16sToMbs(ppwszReverseDeps[i], &pszTemp); BAIL_ON_ERROR(dwError); printf("Stopping service reverse dependency: %s\n", pszTemp); LW_SAFE_FREE_MEMORY(pszTemp); } dwError = LwSmStopService(phDepHandles[i]); BAIL_ON_ERROR(dwError); } } if (!gState.bQuiet) { printf("Stopping service: %s\n", pArgv[1]); } dwError = LwSmStopService(hHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceDependencyClosure(hHandle, &ppwszDependencies); BAIL_ON_ERROR(dwError); for (i = 0; ppwszDependencies[i]; i++) { dwError = LwSmAcquireServiceHandle(ppwszDependencies[i], &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[i], &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 (!gState.bQuiet) { printf("Starting service: %s\n", pArgv[1]); } dwError = LwSmStartService(hHandle); BAIL_ON_ERROR(dwError); for (i = 0; i < count; i++) { if (pStatus[count - 1 - i].state == LW_SERVICE_STATE_RUNNING) { if (!gState.bQuiet) { dwError = LwWc16sToMbs(ppwszReverseDeps[count - 1 - i], &pszTemp); BAIL_ON_ERROR(dwError); printf("Starting service reverse dependency: %s\n", pszTemp); LW_SAFE_FREE_MEMORY(pszTemp); } dwError = LwSmStartService(phDepHandles[count - 1 - i]); BAIL_ON_ERROR(dwError); } } cleanup: LW_SAFE_FREE_MEMORY(pwszServiceName); LW_SAFE_FREE_MEMORY(pStatus); if (hHandle) { LwSmReleaseServiceHandle(hHandle); } if (phDepHandles) { for (i = 0; i < count; i++) { if (phDepHandles[i]) { LwSmReleaseServiceHandle(phDepHandles[i]); } } LW_SAFE_FREE_MEMORY(phDepHandles); } if (ppwszReverseDeps) { LwSmFreeServiceNameList(ppwszReverseDeps); ppwszReverseDeps = NULL; } if (hDepHandle) { LwSmReleaseServiceHandle(hDepHandle); hDepHandle = NULL; } if (ppwszDependencies) { LwSmFreeServiceNameList(ppwszDependencies); ppwszDependencies = NULL; } return dwError; error: goto cleanup; }
static DWORD LwSmGdb( int argc, char** pArgv ) { DWORD dwError = 0; PWSTR pwszServiceName = NULL; LW_SERVICE_HANDLE hHandle = NULL; LW_SERVICE_STATUS status = {0}; PLW_SERVICE_INFO pInfo = NULL; PSTR pszExecutablePath = NULL; PSTR pszPid = NULL; dwError = LwMbsToWc16s(pArgv[1], &pwszServiceName); BAIL_ON_ERROR(dwError); dwError = LwSmAcquireServiceHandle(pwszServiceName, &hHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceStatus(hHandle, &status); BAIL_ON_ERROR(dwError); if (status.state != LW_SERVICE_STATE_RUNNING) { dwError = LwSmStart(argc, pArgv); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceStatus(hHandle, &status); BAIL_ON_ERROR(dwError); } dwError = LwSmQueryServiceInfo(hHandle, &pInfo); BAIL_ON_ERROR(dwError); if (pInfo->type == LW_SERVICE_TYPE_DRIVER) { /* Find non-driver service so we can figure out what to attach to */ LwSmReleaseServiceHandle(hHandle); hHandle = NULL; LwSmFreeServiceInfo(pInfo); pInfo = NULL; dwError = LwSmFindServiceWithPid(status.pid, &hHandle); BAIL_ON_ERROR(dwError); dwError = LwSmQueryServiceInfo(hHandle, &pInfo); if (dwError == LW_ERROR_INVALID_PARAMETER) { printf("Service type is not supported\n"); } BAIL_ON_ERROR(dwError); } switch (pInfo->type) { case LW_SERVICE_TYPE_EXECUTABLE: case LW_SERVICE_TYPE_LEGACY_EXECUTABLE: dwError = LwWc16sToMbs(pInfo->pwszPath, &pszExecutablePath); BAIL_ON_ERROR(dwError); break; case LW_SERVICE_TYPE_MODULE: dwError = LwAllocateString(SBINDIR "/lwsmd", &pszExecutablePath); BAIL_ON_ERROR(dwError); break; default: printf("Service type is not supported\n"); break; } dwError = LwAllocateStringPrintf(&pszPid, "%lu", (unsigned long) status.pid); BAIL_ON_ERROR(dwError); if (execlp("gdb", "gdb", pszExecutablePath, pszPid, NULL) < 0) { dwError = LwMapErrnoToLwError(errno); BAIL_ON_ERROR(dwError); } error: LW_SAFE_FREE_MEMORY(pszPid); LW_SAFE_FREE_MEMORY(pszExecutablePath); if (pInfo) { LwSmFreeServiceInfo(pInfo); } if (hHandle) { LwSmReleaseServiceHandle(hHandle); } return dwError; }
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; }
static DWORD LwSmSetLog( int argc, char** pArgv ) { DWORD dwError = 0; LW_SERVICE_HANDLE hHandle = NULL; LW_SM_LOGGER_TYPE type = 0; PSTR pFacility = NULL; PSTR pszTarget = NULL; PWSTR pServiceName = NULL; if (argc < 4) { dwError = LW_ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); } if (strcmp(pArgv[1], "-")) { dwError = LwMbsToWc16s(pArgv[1], &pServiceName); BAIL_ON_ERROR(dwError); dwError = LwSmAcquireServiceHandle(pServiceName, &hHandle); BAIL_ON_ERROR(dwError); } if (strcmp(pArgv[2], "-")) { pFacility = pArgv[2]; } if (!strcasecmp(pArgv[3], "-")) { type = LW_SM_LOGGER_DEFAULT; } else if (!strcasecmp(pArgv[3], "none")) { type = LW_SM_LOGGER_NONE; } else if (!strcasecmp(pArgv[3], "file")) { if (argc < 5) { dwError = LW_ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); } type = LW_SM_LOGGER_FILE; pszTarget = pArgv[4]; } else if (!strcasecmp(pArgv[3], "syslog")) { type = LW_SM_LOGGER_SYSLOG; pszTarget = NULL; } else { dwError = LW_ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); } dwError = LwSmSetServiceLogTarget(hHandle, pFacility, type, pszTarget); BAIL_ON_ERROR(dwError); error: return dwError; }
static DWORD LwSmGetLog( int argc, char** pArgv ) { DWORD dwError = 0; LW_SM_LOGGER_TYPE type = 0; LW_SM_LOG_LEVEL level = 0; PSTR pszTarget = NULL; PSTR pFacility = NULL; LW_SERVICE_HANDLE hHandle = NULL; PWSTR pServiceName = NULL; PWSTR* ppFacilities = NULL; DWORD index = 0; if (argc < 2) { dwError = LW_ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); } if (strcmp(pArgv[1], "-")) { dwError = LwMbsToWc16s(pArgv[1], &pServiceName); BAIL_ON_ERROR(dwError); dwError = LwSmAcquireServiceHandle(pServiceName, &hHandle); BAIL_ON_ERROR(dwError); } if (argc == 2) { dwError = LwSmGetServiceLogState(hHandle, NULL, &type, &pszTarget, &level); BAIL_ON_ERROR(dwError); PrintLogInfo("<default>", type, pszTarget, level); if (pszTarget) { LwSmFreeLogTarget(pszTarget); pszTarget = NULL; } dwError = LwSmEnumerateServiceLogFacilities(hHandle, &ppFacilities); BAIL_ON_ERROR(dwError); for (index = 0; ppFacilities[index]; index++) { dwError = LwWc16sToMbs(ppFacilities[index], &pFacility); BAIL_ON_ERROR(dwError); dwError = LwSmGetServiceLogState(hHandle, pFacility, &type, &pszTarget, &level); BAIL_ON_ERROR(dwError); PrintLogInfo(pFacility, type, pszTarget, level); if (pszTarget) { LwSmFreeLogTarget(pszTarget); pszTarget = NULL; } } } else if (argc == 3) { if (strcmp(pArgv[2], "-")) { dwError = LwAllocateString(pArgv[2], &pFacility); BAIL_ON_ERROR(dwError); } dwError = LwSmGetServiceLogState(hHandle, pFacility, &type, &pszTarget, &level); BAIL_ON_ERROR(dwError); PrintLogInfo(pFacility, type, pszTarget, level); } error: LW_SAFE_FREE_MEMORY(pFacility); if (pszTarget) { LwSmFreeLogTarget(pszTarget); } if (ppFacilities) { LwSmFreeLogFacilityList(ppFacilities); } return dwError; }