in src/native/windows/apps/prunsrv/prunsrv.c [2019:2199]
void __cdecl main(int argc, char **argv)
{
UINT rv = 0;
LPAPXCMDLINE lpCmdline;
if (argc > 1) {
DWORD ss = 0;
if (strncmp(argv[1], "//PP", 4) == 0) {
/* Handy sleep routine defaulting to 1 minute */
if (argv[1][4] && argv[1][5] && argv[1][6]) {
int us = atoi(argv[1] + 6);
if (us > 0)
ss = (DWORD)us;
}
Sleep(ss * 1000);
ExitProcess(0);
return;
}
else if (strcmp(argv[1], "pause") == 0) {
/* Handy sleep routine defaulting to 1 minute */
if (argc > 2) {
int us = atoi(argv[2]);
if (us > 0)
ss = (DWORD)us;
}
}
if (ss) {
Sleep(ss * 1000);
ExitProcess(0);
return;
}
}
apxHandleManagerInitialize();
/* Create the main Pool */
gPool = apxPoolCreate(NULL, 0);
/* Parse the command line */
if ((lpCmdline = apxCmdlineParse(gPool, _options, _commands, _altcmds)) == NULL) {
apxLogWrite(APXLOG_MARK_ERROR "Invalid command line arguments.");
rv = 1;
goto cleanup;
}
apxCmdlineLoadEnvVars(lpCmdline);
if (lpCmdline->dwCmdIndex < 6) {
if (!loadConfiguration(lpCmdline) &&
lpCmdline->dwCmdIndex < 5) {
apxLogWrite(APXLOG_MARK_ERROR "Load configuration failed.");
rv = 2;
goto cleanup;
}
}
/* Only configure logging to a file when running as a service */
if (lpCmdline->dwCmdIndex == 2) {
apxLogOpen(gPool, SO_LOGPATH, SO_LOGPREFIX, SO_LOGROTATE);
apxLogLevelSetW(NULL, SO_LOGLEVEL);
apxLogWrite(APXLOG_MARK_DEBUG "Apache Commons Daemon procrun log initialized.");
if (SO_LOGROTATE) {
apxLogWrite(APXLOG_MARK_DEBUG "Log will rotate each %d seconds.", SO_LOGROTATE);
}
} else {
/* Not running as a service, just set the log level */
apxLogLevelSetW(NULL, SO_LOGLEVEL);
}
apxLogWrite(APXLOG_MARK_INFO "Apache Commons Daemon procrun (%s %d-bit) started.",
PRG_VERSION, PRG_BITS);
AplZeroMemory(&gStdwrap, sizeof(APX_STDWRAP));
gStartPath = lpCmdline->szExePath;
gStdwrap.szLogPath = SO_LOGPATH;
/* Only redirect when running as a service */
if (lpCmdline->dwCmdIndex == 2) {
gStdwrap.szStdOutFilename = SO_STDOUTPUT;
gStdwrap.szStdErrFilename = SO_STDERROR;
}
redirectStdStreams(&gStdwrap, lpCmdline);
if (lpCmdline->dwCmdIndex == 2) {
SYSTEMTIME t;
GetLocalTime(&t);
fprintf(stdout, "\n%d-%02d-%02d %02d:%02d:%02d "
"Apache Commons Daemon procrun stdout initialized.\n",
t.wYear, t.wMonth, t.wDay,
t.wHour, t.wMinute, t.wSecond);
fprintf(stderr, "\n%d-%02d-%02d %02d:%02d:%02d "
"Apache Commons Daemon procrun stderr initialized.\n",
t.wYear, t.wMonth, t.wDay,
t.wHour, t.wMinute, t.wSecond);
}
if (lpCmdline->dwCmdIndex > 2 && lpCmdline->dwCmdIndex < 8) {
/* Command requires elevation */
BOOL bElevated;
if (!isRunningAsAdministrator(&bElevated)) {
apxDisplayError(FALSE, NULL, 0, "Unable to determine if process has administrator privileges. Continuing as if it has.\n");
} else {
if (!bElevated) {
DWORD dwExitCode;
if (!restartCurrentProcessWithElevation(&dwExitCode)) {
apxDisplayError(FALSE, NULL, 0, "Failed to elevate current process.\n");
rv = lpCmdline->dwCmdIndex + 2;
} else {
if (dwExitCode) {
apxDisplayError(FALSE, NULL, 0, "Running from a command prompt with administrative privileges may show further error details.\n");
}
rv = dwExitCode;
}
goto cleanup;
}
}
}
switch (lpCmdline->dwCmdIndex) {
case 1: /* Run Service as console application */
if (!docmdDebugService(lpCmdline))
rv = 3;
break;
case 2: /* Run Service */
if (!docmdRunService(lpCmdline))
rv = 4;
break;
case 3: /* Start service - requires elevation */
if (!docmdStartService(lpCmdline))
rv = 5;
break;
case 4: /* Stop Service - requires elevation */
if (!docmdStopService(lpCmdline))
rv = 6;
break;
case 5: /* Update Service parameters - requires elevation */
if (!docmdUpdateService(lpCmdline))
rv = 7;
break;
case 6: /* Install Service - requires elevation */
if (!docmdInstallService(lpCmdline))
rv = 8;
break;
case 7: /* Delete Service - requires elevation */
if (!docmdDeleteService(lpCmdline))
rv = 9;
break;
case 8: /* Print Configuration and exit */
printConfig(lpCmdline);
break;
case 9: /* Print Usage and exit */
printUsage(lpCmdline, TRUE);
break;
case 10: /* Print version and exit */
printVersion();
break;
default:
/* Unknown command option */
apxLogWrite(APXLOG_MARK_ERROR "Unknown command line option.");
printUsage(lpCmdline, FALSE);
rv = 99;
break;
}
cleanup:
if (rv) {
int ix = 0;
if (rv > 0 && rv < 10)
ix = rv;
apxLogWrite(APXLOG_MARK_ERROR "Apache Commons Daemon procrun failed "
"with exit value: %d (failed to %s).",
rv, gSzProc[ix]);
if (ix > 2 && !_service_mode) {
/* Print something to the user console */
apxDisplayError(FALSE, NULL, 0, "Failed to %s.\n", gSzProc[ix]);
}
}
else
apxLogWrite(APXLOG_MARK_INFO "Apache Commons Daemon procrun finished.");
if (lpCmdline)
apxCmdlineFree(lpCmdline);
_service_status_handle = NULL;
_service_mode = FALSE;
_flushall();
apxLogClose(NULL);
apxHandleManagerDestroy();
ExitProcess(rv);
}