static void semVerInit() { if (!semVer) { mprGlobalLock(); if (!semVer) { semVer = srcompile(SEM_VER); semCriteria = srcompile(SEM_CRITERIA); semExpr = srcompile(SEM_EXPR); mprAddTerminator((MprTerminator) versionTerminate); } mprGlobalUnlock(); } }
int main(int argc, char *argv[]) { char *argp, *value; int err, nextArg, status; err = 0; mprCreate(argc, argv, MPR_USER_EVENTS_THREAD); app = mprAllocObj(App, manageApp); mprAddRoot(app); mprAddTerminator(terminating); mprAddStandardSignals(); setAppDefaults(); for (nextArg = 1; nextArg < argc && !err; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--args") == 0) { /* Args to pass to service */ if (nextArg >= argc) { err++; } else { app->serviceArgs = argv[++nextArg]; } } else if (strcmp(argp, "--console") == 0) { /* Does nothing. Here for compatibility with windows watcher */ } else if (strcmp(argp, "--daemon") == 0) { app->runAsDaemon++; #if FUTURE } else if (strcmp(argp, "--heartBeat") == 0) { /* Set the frequency to check on the program. */ if (nextArg >= argc) { err++; } else { app->heartBeatPeriod = atoi(argv[++nextArg]); } #endif } else if (strcmp(argp, "--home") == 0) { /* Change to this directory before starting the service */ if (nextArg >= argc) { err++; } else { app->serviceHome = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--log") == 0) { /* Pass the log directive through to the service */ if (nextArg >= argc) { err++; } else { app->logSpec = sclone(argv[++nextArg]); mprStartLogging(app->logSpec, 0); mprSetCmdlineLogging(1); } } else if (strcmp(argp, "--name") == 0) { if (nextArg >= argc) { err++; } else { app->serviceName = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--pidfile") == 0) { if (nextArg >= argc) { err++; } else { app->pidPath = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--program") == 0) { if (nextArg >= argc) { err++; } else { app->serviceProgram = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--retries") == 0) { if (nextArg >= argc) { err++; } else { app->retries = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--signal") == 0) { if (nextArg >= argc) { err++; } else { value = argv[++nextArg]; if (smatch(value, "SIGABRT")) { app->signal = SIGABRT; } else if (smatch(value, "SIGINT")) { app->signal = SIGINT; } else if (smatch(value, "SIGHUP")) { app->signal = SIGHUP; } else if (smatch(value, "SIGQUIT")) { app->signal = SIGQUIT; } else if (smatch(value, "SIGTERM")) { app->signal = SIGTERM; } else if (smatch(value, "SIGUSR1")) { app->signal = SIGUSR1; } else if (smatch(value, "SIGUSR2")) { app->signal = SIGUSR2; } else { app->signal = atoi(argv[++nextArg]); } } } else if (strcmp(argp, "--verbose") == 0 || strcmp(argp, "-v") == 0) { mprSetLogLevel(1); } else { err++; break; } } if (nextArg >= argc) { err++; } if (err) { mprUserError("Bad command line: \n" " Usage: %s [commands]\n" " Switches:\n" " --args # Args to pass to service\n" " --daemon # Run manager as a daemon\n" " --home path # Home directory for service\n" " --log logFile:level # Log directive for service\n" " --retries count # Max count of app restarts\n" " --name name # Name of the service to manage\n" " --pidfile path # Location of the pid file\n" " --program path # Service program to start\n" " --signal signo # Signal number to terminate service\n" " --verbose # Show command feedback\n" #if FUTURE " --heartBeat interval # Heart beat interval period (secs) \n" #endif " Commands:\n" " disable # Disable the service\n" " enable # Enable the service\n" " install # Install the service\n" " run # Run and watch over the service\n" " start # Start the service\n" " stop # Start the service\n" " uninstall # Uninstall the service\n" , app->appName); return -1; } if (!app->pidPath) { app->pidPath = sjoin(app->pidDir, "/", app->serviceName, ".pid", NULL); } if (app->runAsDaemon) { makeDaemon(); } status = 0; if (getuid() != 0) { mprUserError("Must run with administrator privilege. Use sudo."); status = 1; } else if (mprStart() < 0) { mprUserError("Can't start MPR for %s", mprGetAppName()); status = 2; } else { mprStartEventsThread(); for (; nextArg < argc; nextArg++) { if (!process(argv[nextArg], 0)) { status = 3; break; } } } mprDestroy(MPR_EXIT_DEFAULT); return status; }
int APIENTRY WinMain(HINSTANCE inst, HINSTANCE junk, char *args, int junk2) { char **argv, *argp; int argc, err, nextArg; mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD); app = mprAllocObj(App, manageApp); mprAddRoot(app); mprAddTerminator(terminating); err = 0; app->appInst = inst; app->heartBeatPeriod = HEART_BEAT_PERIOD; setAppDefaults(); mprSetAppName(BLD_PRODUCT "Manager", BLD_NAME "Manager", BLD_VERSION); app->appName = mprGetAppName(); app->appTitle = mprGetAppTitle(mpr); mprSetLogHandler(logHandler); mprSetWinMsgCallback((MprMsgCallback) msgProc); if ((argc = mprMakeArgv(args, &argv, MPR_ARGV_ARGS_ONLY)) < 0) { return FALSE; } for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-' || strcmp(argp, "--") == 0) { break; } if (strcmp(argp, "--args") == 0) { /* Args to pass to service when it starts */ if (nextArg >= argc) { err++; } else { app->serviceArgs = argv[++nextArg]; } } else if (strcmp(argp, "--console") == 0) { /* Allow the service to interact with the console */ app->createConsole++; } else if (strcmp(argp, "--daemon") == 0) { /* Ignored on windows */ } else if (strcmp(argp, "--heartBeat") == 0) { /* Set the heart beat period */ if (nextArg >= argc) { err++; } else { app->heartBeatPeriod = atoi(argv[++nextArg]) * 1000; } } else if (strcmp(argp, "--home") == 0) { /* Change to this directory before starting the service */ if (nextArg >= argc) { err++; } else { app->serviceHome = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--log") == 0) { /* Pass the log directive through to the service */ if (nextArg >= argc) { err++; } else { app->logSpec = sclone(argv[++nextArg]); mprStartLogging(app->logSpec, 0); mprSetCmdlineLogging(1); } } else if (strcmp(argp, "--name") == 0) { if (nextArg >= argc) { err++; } else { app->serviceName = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--program") == 0) { if (nextArg >= argc) { err++; } else { app->serviceProgram = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--verbose") == 0 || strcmp(argp, "-v") == 0) { mprSetLogLevel(1); } else { err++; } if (err) { mprUserError("Bad command line: %s\n" " Usage: %s [options] [program args]\n" " Switches:\n" " --args # Args to pass to service\n" " --console # Display the service console\n" " --heartBeat interval # Heart beat interval period (secs)\n" " --home path # Home directory for service\n" " --log logFile:level # Log directive for service\n" " --name name # Name of the service to manage\n" " --program path # Service program to start\n" " --verbose # Show command feedback\n" " Commands:\n" " disable # Disable the service\n" " enable # Enable the service\n" " start # Start the service\n" " stop # Start the service\n" " run # Run and watch over the service\n", args, app->appName); return -1; } } if (mprStart() < 0) { mprUserError("Can't start MPR for %s", mprGetAppName()); } else { mprStartEventsThread(); if (nextArg >= argc) { return process("run"); } else for (; nextArg < argc; nextArg++) { if (!process(argv[nextArg])) { return FALSE; } } } return TRUE; }
PUBLIC Http *httpCreate(int flags) { Http *http; HttpStatusCode *code; mprGlobalLock(); if (MPR->httpService) { mprGlobalUnlock(); return MPR->httpService; } if ((http = mprAllocObj(Http, manageHttp)) == 0) { mprGlobalUnlock(); return 0; } MPR->httpService = HTTP = http; http->software = sclone(ME_HTTP_SOFTWARE); http->protocol = sclone("HTTP/1.1"); http->mutex = mprCreateLock(); http->stages = mprCreateHash(-1, MPR_HASH_STABLE); http->hosts = mprCreateList(-1, MPR_LIST_STABLE); http->connections = mprCreateList(-1, MPR_LIST_STATIC_VALUES); http->authTypes = mprCreateHash(-1, MPR_HASH_CASELESS | MPR_HASH_UNIQUE | MPR_HASH_STABLE); http->authStores = mprCreateHash(-1, MPR_HASH_CASELESS | MPR_HASH_UNIQUE | MPR_HASH_STABLE); http->routeSets = mprCreateHash(-1, MPR_HASH_STATIC_VALUES | MPR_HASH_STABLE); http->booted = mprGetTime(); http->flags = flags; http->monitorPeriod = ME_HTTP_MONITOR_PERIOD; http->secret = mprGetRandomString(HTTP_MAX_SECRET); http->trace = httpCreateTrace(0); http->startLevel = 2; http->localPlatform = slower(sfmt("%s-%s-%s", ME_OS, ME_CPU, ME_PROFILE)); httpSetPlatform(http->localPlatform); httpSetPlatformDir(NULL); updateCurrentDate(); http->statusCodes = mprCreateHash(41, MPR_HASH_STATIC_VALUES | MPR_HASH_STATIC_KEYS | MPR_HASH_STABLE); for (code = HttpStatusCodes; code->code; code++) { mprAddKey(http->statusCodes, code->codeString, code); } httpGetUserGroup(); httpInitParser(); httpInitAuth(); httpOpenNetConnector(); httpOpenSendConnector(); httpOpenRangeFilter(); httpOpenChunkFilter(); #if ME_HTTP_WEB_SOCKETS httpOpenWebSockFilter(); #endif mprSetIdleCallback(isIdle); mprAddTerminator(terminateHttp); if (flags & HTTP_SERVER_SIDE) { http->endpoints = mprCreateList(-1, MPR_LIST_STABLE); http->counters = mprCreateList(-1, MPR_LIST_STABLE); http->monitors = mprCreateList(-1, MPR_LIST_STABLE); http->routeTargets = mprCreateHash(-1, MPR_HASH_STATIC_VALUES | MPR_HASH_STABLE); http->routeConditions = mprCreateHash(-1, MPR_HASH_STATIC_VALUES | MPR_HASH_STABLE); http->routeUpdates = mprCreateHash(-1, MPR_HASH_STATIC_VALUES | MPR_HASH_STABLE); http->sessionCache = mprCreateCache(MPR_CACHE_SHARED | MPR_HASH_STABLE); http->addresses = mprCreateHash(-1, MPR_HASH_STABLE); http->defenses = mprCreateHash(-1, MPR_HASH_STABLE); http->remedies = mprCreateHash(-1, MPR_HASH_CASELESS | MPR_HASH_STATIC_VALUES | MPR_HASH_STABLE); httpOpenUploadFilter(); httpOpenCacheHandler(); httpOpenPassHandler(); httpOpenActionHandler(); httpOpenDirHandler(); httpOpenFileHandler(); http->serverLimits = httpCreateLimits(1); httpDefineRouteBuiltins(); httpAddCounters(); httpAddRemedies(); httpCreateDefaultHost(); } if (flags & HTTP_CLIENT_SIDE) { http->defaultClientHost = sclone("127.0.0.1"); http->defaultClientPort = 80; http->clientLimits = httpCreateLimits(0); http->clientRoute = httpCreateConfiguredRoute(0, 0); http->clientHandler = httpCreateHandler("client", 0); } mprGlobalUnlock(); return http; }