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 LwSmParseArguments( int argc, char** ppszArgv ) { DWORD dwError = 0; int i = 0; if (!strcmp(ppszArgv[0], CONTAINER_PROCESS_NAME)) { if (argc != 2) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); } gState.bContainer = TRUE; gState.pName = ppszArgv[1]; gState.bSyslog = TRUE; } else { gState.pName = ppszArgv[0]; for (i = 1; i < argc; i++) { if (!strcmp(ppszArgv[i], "--start-as-daemon")) { gState.bStartAsDaemon = TRUE; gState.bSyslog = TRUE; gState.logLevel = LW_SM_LOG_LEVEL_INFO; } else if (!strcmp(ppszArgv[i], "--syslog")) { gState.bSyslog = TRUE; gState.logLevel = LW_SM_LOG_LEVEL_INFO; } else if (!strcmp(ppszArgv[i], "--loglevel")) { if (++i >= argc) { dwError = LW_ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); } dwError = LwSmLogLevelNameToLogLevel( ppszArgv[i], &gState.logLevel); BAIL_ON_ERROR(dwError); } else if (!strcmp(ppszArgv[i], "--logfile")) { if (++i >= argc) { dwError = LW_ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); } gState.pszLogFilePath = ppszArgv[i]; } else if (!strcmp(ppszArgv[i], "--container")) { if (++i >= argc) { dwError = LW_ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); } dwError = LwMbsToWc16s(ppszArgv[i], &gState.pGroup); BAIL_ON_ERROR(dwError); gState.bContainer = TRUE; } else if (!strcmp(ppszArgv[i], "--help")) { Usage(argc, ppszArgv); exit(0); } } } error: return dwError; }
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; }