static int setupServer(MaHttp *http, int poolThreads) { mprLog(MPR_CONFIG, "Using server root: %s\n", serverRoot); #if BLD_FEATURE_CONFIG_PARSE if (configFile) { if (configureViaFile() < 0) { return MPR_ERR_CANT_OPEN; } } else #endif { if (configureViaApi() < 0) { return MPR_ERR_CANT_OPEN; } } #if BLD_FEATURE_MULTITHREAD // // The default limits my be updated via the config file // MaLimits *limits = http->getLimits(); if (poolThreads >= 0) { limits->maxThreads = poolThreads; if (limits->minThreads > limits->maxThreads) { limits->minThreads = limits->maxThreads; } } if (limits->maxThreads > 0) { mprGetMpr()->setMaxPoolThreads(limits->maxThreads); mprGetMpr()->setMinPoolThreads(limits->minThreads); } #endif return 0; }
static void catchSignal(int signo, siginfo_t *info, void *arg) { mprLog(MPR_INFO, "Received signal %d\nExiting ...\n", signo); if (mprGetMpr()) { mprGetMpr()->terminate(1); } }
int MprCmd::waitForChild(int timeout) { lock(); // // Prevent this object from being deleted by another thread while we // are using it. // inUse++; if (! (flags & MPR_CMD_COMPLETE)) { mprGetMpr()->cmdService->startWatcher(); } unlock(); while (! (flags & MPR_CMD_COMPLETE)) { mprGetMpr()->serviceEvents(1, MPR_CMD_WATCHER_NAP); } lock(); // // If the object was deleted while we were using it, then delete now. // if (--inUse == 0 && flags & MPR_CMD_DISPOSED) { delete this; } else { unlock(); } return 0; }
/* * Complete the request and return true if there is a pipelined request following. */ bool maProcessCompletion(MaConn *conn) { MaRequest *req; MaPacket *packet; bool more; mprAssert(conn->state == MPR_HTTP_STATE_COMPLETE); req = conn->request; maLogRequest(conn); #if BLD_DEBUG mprLog(req, 4, "Request complete used %,d K, conn usage %,d K, mpr usage %,d K, page usage %,d K", req->arena->allocBytes / 1024, conn->arena->allocBytes / 1024, mprGetMpr(conn)->heap.allocBytes / 1024, mprGetMpr(conn)->pageHeap.allocBytes / 1024); /* mprPrintAllocReport(mprGetMpr(conn), "Before completing request"); */ #endif packet = conn->input; more = packet && (mprGetBufLength(packet->content) > 0); if (mprGetParent(packet) != conn) { if (more) { conn->input = maSplitPacket(conn, packet, 0); mprAssert(mprGetParent(conn->input) == conn); } else { conn->input = 0; } } /* * This will free the request, response, pipeline and call maPrepConnection to reset the state. */ mprFree(req->arena); return (conn->disconnected || conn->connectionFailed) ? 0 : more; }
void mprSetThreads(int low, int high) { #if BLD_FEATURE_MULTITHREAD if (low > 0) { mprGetMpr()->setMinPoolThreads(low); } if (high > 0) { mprGetMpr()->setMaxPoolThreads(high); } #endif }
HWND mprGetHwnd(MprCtx ctx) { Mpr *mpr; mpr = mprGetMpr(ctx); return mpr->waitService->hwnd; }
void mprSetSocketMessage(MprCtx ctx, int socketMessage) { Mpr *mpr; mpr = mprGetMpr(ctx); mpr->waitService->socketMessage = socketMessage; }
void mprSetHwnd(MprCtx ctx, HWND h) { Mpr *mpr; mpr = mprGetMpr(ctx); mpr->waitService->hwnd = h; }
int MprWinService::stop(int cmd) { int exitCode; svcStopped++; mprGetMpr()->terminate(1); if (cmd == SERVICE_CONTROL_SHUTDOWN) { return 0; } SetEvent(waitEvent); svcStatus.dwCurrentState = SERVICE_STOP_PENDING; tellScm(svcStatus.dwCurrentState, NO_ERROR, 1000); exitCode = 0; GetExitCodeThread(threadHandle, (ulong*) &exitCode); while (exitCode == STILL_ACTIVE) { GetExitCodeThread(threadHandle, (ulong*) &exitCode); mprSleep(100); tellScm(svcStatus.dwCurrentState, NO_ERROR, 125); } svcStatus.dwCurrentState = SERVICE_STOPPED; tellScm(svcStatus.dwCurrentState, exitCode, 0); return 0; }
/* * Load the ssl provider */ static MprModule *loadSsl(MprCtx ctx, bool lazy) { Mpr *mpr; MprModule *mp; mpr = mprGetMpr(ctx); if (mpr->flags & MPR_SSL_PROVIDER_LOADED) { return mprLookupModule(ctx, "sslModule"); } mprLog(ctx, MPR_CONFIG, "Activating the SSL provider"); #if BLD_FEATURE_OPENSSL /* * NOTE: preference given to open ssl if both are enabled */ mprLog(ctx, 2, "Loading OpenSSL module"); if (mprCreateOpenSslModule(ctx, lazy) < 0) { return 0; } #elif BLD_FEATURE_MATRIXSSL mprLog(ctx, 2, "Loading MatrixSSL module"); if (mprCreateMatrixSslModule(ctx, lazy) < 0) { return 0; } #endif if ((mp = mprCreateModule(ctx, "sslModule", BLD_VERSION, NULL, NULL, NULL)) == 0) { return 0; } mpr->flags |= MPR_SSL_PROVIDER_LOADED; return mp; }
MprThread::~MprThread() { lock(); mprFree(name); mprGetMpr()->threadService->removeThread(this); delete mutex; }
void mprSetServiceThread(MprCtx ctx, MprThread *thread) { Mpr *mpr; mpr = mprGetMpr(ctx); mpr->serviceThread = thread->osThread; }
static int windowsServiceOps() { winService = new MprWinService(APPWEB_SERVICE_NAME); switch (serviceOp) { case MPR_INSTALL_SERVICE: char path[MPR_MAX_FNAME], cmd[MPR_MAX_FNAME]; GetModuleFileName(0, path, sizeof(path)); mprSprintf(cmd, sizeof(cmd), "\"%s\" %s", path, serviceCmdLine); winService->install(APPWEB_SERVICE_DISPLAY, cmd); break; case MPR_UNINSTALL_SERVICE: winService->remove(1); break; case MPR_GO_SERVICE: winService->start(); // // Give time for service to actually start // mprSleep(2000); break; case MPR_STOP_SERVICE: winService->remove(0); break; } if (isService) { mprGetMpr()->setService(1); } return 0; }
static bool appwebIsIdle(MprCtx ctx) { MaHost *host; MaConn *conn; MaHttp *http; MprTime now; int nextHost, next; static MprTime lastTrace = 0; now = mprGetTime(ctx); http = (MaHttp*) mprGetMpr(ctx)->appwebHttpService; for (nextHost = 0; (host = mprGetNextItem(http->defaultServer->hosts, &nextHost)) != 0; ) { lock(host); for (next = 0; (conn = mprGetNextItem(host->connections, &next)) != 0; ) { if (conn->state != MPR_HTTP_STATE_BEGIN) { if (lastTrace < now) { mprLog(ctx, 0, "Waiting for request %s to complete", *conn->request->url ? conn->request->url : conn->request->pathInfo); lastTrace = now; } unlock(host); return 0; } } unlock(host); } if (!mprServicesAreIdle(ctx)) { if (lastTrace < now) { mprLog(ctx, 0, "Waiting for MPR services complete"); lastTrace = now; } return 0; } return 1; }
void MprLogModule::innerMprLogModule(char *s) { MprLogModule *def; Mpr *mpr; char namBuf[MPR_MAX_FNAME]; char *moduleSpecs, *cp; name = mprStrdup(s); enabled = 1; level = -1; mpr = mprGetMpr(); moduleSpecs = mpr->logService->getModuleSpecs(); if (moduleSpecs) { mprSprintf(namBuf, sizeof(namBuf), "%s:", name); if ((cp = strstr(moduleSpecs, namBuf)) != 0) { if ((cp = strchr(cp, ':')) != 0) { level = atoi(++cp); } } } if (level < 0) { def = mpr->logService->getDefaultModule(); if (def) { level = def->getLevel(); } } mpr->logService->insertModule(this); }
static int windowsInit() { #if ONLY_SINGLE_INSTANCE if (serviceOp == 0 && findInstance()) { mprError(MPR_L, MPR_USER, "Application %s is already active.", program); return MPR_ERR_BUSY; } #endif // // Create the window // if (initWindow() < 0) { mprError(MPR_L, MPR_ERROR, "Can't initialize application Window"); return MPR_ERR_CANT_INITIALIZE; } if (trayIcon > 0) { if (openTrayIcon() < 0 && mp->isService()) { trayTimer = new MprTimer(10 * 1000, trayIconProc, (void *) NULL); } } mprGetMpr()->setAsyncSelectMode(MPR_ASYNC_SELECT); return 0; }
void MprLogToFile::logEvent(char *module, int flags, int level, char *thread, char *msg) { char buf[MPR_MAX_LOG_STRING]; if (mprGetMpr() == 0) { mprStrcpy(buf, sizeof(buf), msg); } else if (timeStamps) { #if CYGWIN || LINUX || MACOSX || SOLARIS || VXWORKS || FREEBSD #if BLD_FEATURE_HIRES_TIME int64 elapsed = mprGetElapsedTime(); mprSprintf(buf, sizeof(buf), "%,14Ld %10s:%d %s %s", elapsed, module, level, thread, msg); #else static int last = 0; int now = mprGetTime(0); int elapsed = now - last; mprSprintf(buf, sizeof(buf), "%,14d %10s:%d %s %s", elapsed, module, level, thread, msg); last = now; #endif #endif // CYGWIN || LINUX || MACOSX || SOLARIS || VXWORKS || FREEBSD } else if (! (flags & MPR_RAW)) { mprSprintf(buf, sizeof(buf), "%10s:%d %4s %s", module, level, thread, msg); } else { // Raw output mprStrcpy(buf, sizeof(buf), msg); } if (logFd < 0) { if (level <= 1) { // // Always output fatal and error messages // mprFprintf(MPR_STDERR, buf); } return; } // OPT -- could get length above mprAssert(logFd >= 0); write(logFd, buf, strlen(buf)); if (logFd != MPR_STDOUT) { struct stat sbuf; if (maxSize <= 0 || fstat(logFd, &sbuf) < 0) { return; } // // Rotate logs when full // if (sbuf.st_mode & S_IFREG && (unsigned) sbuf.st_size > maxSize) { rotate(); } } }
void MprLogService::output(MprLogModule *module, int flags, int level, char *msg) { MprLogListener *lp; Mpr *mpr; char *threadName, *moduleName; mpr = mprGetMpr(); lock(); threadName = ""; #if BLD_FEATURE_MULTITHREAD if (mpr && mpr->threadService) { MprThread *tp = mpr->threadService->getCurrentThread(); if (tp) { threadName = tp->getName(); } } #endif moduleName = module ? module->getName() : (char*) "default"; lp = (MprLogListener*) listeners.getFirst(); while (lp) { lp->logEvent(moduleName, flags, level, threadName, msg); lp = (MprLogListener*) listeners.getNext(lp); } unlock(); }
cchar *mprGetAppVersion(MprCtx ctx) { Mpr *mpr; mpr = mprGetMpr(ctx); return mpr->version; }
int mprSetAppName(MprCtx ctx, cchar *name, cchar *title, cchar *version) { Mpr *mpr; char *cp; mpr = mprGetMpr(ctx); if (name) { mprFree(mpr->name); if ((mpr->name = (char*) mprGetPathBase(mpr, name)) == 0) { return MPR_ERR_CANT_ALLOCATE; } if ((cp = strrchr(mpr->name, '.')) != 0) { *cp = '\0'; } } if (title) { mprFree(mpr->title); if ((mpr->title = mprStrdup(ctx, title)) == 0) { return MPR_ERR_CANT_ALLOCATE; } } if (version) { mprFree(mpr->version); if ((mpr->version = mprStrdup(ctx, version)) == 0) { return MPR_ERR_CANT_ALLOCATE; } } return 0; }
MprCmd *mprCreateCmd(MprCtx ctx) { MprCmdService *cs; MprCmd *cmd; MprCmdFile *files; int i; cmd = mprAllocObjWithDestructorZeroed(ctx, MprCmd, cmdDestructor); if (cmd == 0) { return 0; } cmd->completeCond = mprCreateCond(cmd); cmd->timeoutPeriod = MPR_TIMEOUT_CMD; cmd->timestamp = mprGetTime(cmd); cmd->forkCallback = (MprForkCallback) closeFiles; #if VXWORKS cmd->startCond = semCCreate(SEM_Q_PRIORITY, SEM_EMPTY); cmd->exitCond = semCCreate(SEM_Q_PRIORITY, SEM_EMPTY); #endif files = cmd->files; for (i = 0; i < MPR_CMD_MAX_PIPE; i++) { files[i].clientFd = -1; files[i].fd = -1; } #if BLD_FEATURE_MULTITHREAD cmd->mutex = mprCreateLock(cmd); #endif cs = mprGetMpr(ctx)->cmdService; mprLock(cs->mutex); mprAddItem(cs->cmds, cmd); mprUnlock(cs->mutex); return cmd; }
MprThread::MprThread(MprThreadProc entry, int priority, void *data, char *name, int stackSize) { Mpr *mpr; mpr = mprGetMpr(); mprLog(7, "Create Thread %s\n", name); osThreadId = 0; pid = 0; this->priority = priority; this->entry = entry; this->data = data; this->name = mprStrdup(name); this->stackSize = stackSize; mutex = new MprMutex(); if (stackSize == 0) { this->stackSize = mpr->getConfigInt("stackSize", MPR_DEFAULT_STACK); } else { this->stackSize = stackSize; } if (mpr->threadService) { mpr->threadService->insertThread(this); } }
int MaClient::sendRetry(char *op, char *requestUrl, char *postData, int postLen) { int authCount, count, rc; authCount = count = 0; do { again: rc = sendCore(op, requestUrl, postData, postLen); if (rc == 0 && !(flags & MPR_HTTP_TERMINATED)) { if (responseCode == 401) { // // Carefull altering this code. Ran into GCC compiler bugs. // if (authCount++ == 0 && user && password && realm) { goto again; } count = retries; } else { break; } } } while (++count < retries && !mprGetMpr()->isExiting()); if (rc < 0 && count >= retries) { mprError(MPR_L, MPR_LOG, "sendRetry: failed to get %s %s, %d", op, requestUrl, rc); return MPR_ERR_TOO_MANY; } return 0; }
static void readEventWrapper(void *data, MprSocket *sp, int mask, int isPoolThread) { MaClient *cp; int moreData, loopCount; mprLog(5, "%d: readEventWrapper: mask %x, isPool %d\n", sp->getFd(), mask, isPoolThread); // // Make sure we are not being deleted // mprGetMpr()->lock(); cp = (MaClient*) clients.getFirst(); while (cp) { if (cp == (MaClient*) data) { break; } cp = (MaClient*) clients.getNext(cp); } if (cp == 0) { mprError(MPR_L, MPR_LOG, "Client deleted prematurely."); return; } cp->lock(); mprGetMpr()->unlock(); // // If we are multi-threaded and called on a pool thread, we can block and // read as much data as we can. If single threaded, just do 25 reads. // loopCount = 25; do { moreData = cp->readEvent(); if (cp->getState() == MPR_HTTP_CLIENT_DONE) { cp->signalComplete(); break; } } while (moreData > 0 && (isPoolThread || loopCount-- > 0)); cp->unlock(); }
void mprSetLogHandler(MprCtx ctx, MprLogHandler handler, void *handlerData) { Mpr *mpr; mpr = mprGetMpr(ctx); mpr->logHandler = handler; mpr->logHandlerData = handlerData; }
void mprConfigureSsl(MprSsl *ssl) { MprSocketProvider *provider; provider = mprGetMpr(ssl)->socketService->secureProvider; if (provider) { provider->configureSsl(ssl); } }
MprIdleCallback mprSetIdleCallback(MprCtx ctx, MprIdleCallback idleCallback) { MprIdleCallback old; Mpr *mpr; mpr = mprGetMpr(ctx); old = mpr->idleCallback; mpr->idleCallback = idleCallback; return old; }
bool mprIsExiting(MprCtx ctx) { Mpr *mpr; mpr = mprGetMpr(ctx); if (mpr == 0) { return 1; } return mpr->flags & MPR_EXITING; }
/* * Thread main for serviceEvents */ static void serviceEvents(void *data, MprThread *tp) { Mpr *mpr; mpr = mprGetMpr(tp); mpr->serviceThread = tp->osThread; mprServiceEvents(mpr->dispatcher, -1, MPR_SERVICE_EVENTS | MPR_SERVICE_IO); mpr->serviceThread = 0; mpr->hasDedicatedService = 1; }
bool mprIsComplete(MprCtx ctx) { Mpr *mpr; mpr = mprGetMpr(ctx); if (mpr == 0) { return 1; } return (mpr->flags & MPR_EXITING) && mprIsIdle(ctx); }