コード例 #1
0
ファイル: mpr.c プロジェクト: embedthis/mpr-3
/*
 *  Start the Mpr and all services
 */
int mprStart(Mpr *mpr, int startEventsThread)
{
    int     rc;

    rc = mprStartOsService(mpr->osService);
    rc += mprStartModuleService(mpr->moduleService);
#if BLD_FEATURE_MULTITHREAD
    rc += mprStartWorkerService(mpr->workerService);
#endif
    rc += mprStartSocketService(mpr->socketService);
#if BLD_FEATURE_HTTP
    rc += mprStartHttpService(mpr->httpService);
#endif

    if (rc != 0) {
        mprUserError(mpr, "Can't start MPR services");
        return MPR_ERR_CANT_INITIALIZE;
    }
    mpr->flags |= MPR_STARTED;
    mprLog(mpr, MPR_INFO, "MPR services are ready");
#if BLD_FEATURE_MULTITHREAD
    if (startEventsThread) {
        mprStartEventsThread(mpr);
    }
#endif
    return 0;
}
コード例 #2
0
ファイル: manager.c プロジェクト: childhood/appweb-4
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;
}
コード例 #3
0
ファイル: manager.c プロジェクト: childhood/appweb-4
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;                                                                    
}