Exemplo n.º 1
0
static int CommandLineInterpreter(scr_t screenID, const char *commandLine)
{
    char *szCommand = "APACHE2 ";
    int iCommandLen = 8;
    char szcommandLine[256];
    char *pID;
    screenID = screenID;


    if (commandLine == NULL)
        return NOTMYCOMMAND;
    if (strlen(commandLine) <= strlen(szCommand))
        return NOTMYCOMMAND;

    apr_cpystrn(szcommandLine, commandLine, sizeof(szcommandLine));

    /*  All added commands begin with "APACHE2 " */

    if (!strnicmp(szCommand, szcommandLine, iCommandLen)) {
        ActivateScreen (getscreenhandle());

        /* If an instance id was not given but the nlm is loaded in
            protected space, then the the command belongs to the
            OS address space instance to pass it on. */
        pID = strstr (szcommandLine, "-p");
        if ((pID == NULL) && nlmisloadedprotected())
            return NOTMYCOMMAND;

        /* If we got an instance id but it doesn't match this
            instance of the nlm, pass it on. */
        if (pID) {
            pID = &pID[2];
            while (*pID && (*pID == ' '))
                pID++;
        }
        if (pID && ap_my_addrspace && strnicmp(pID, ap_my_addrspace, strlen(ap_my_addrspace)))
            return NOTMYCOMMAND;

        /* If we have determined that this command belongs to this
            instance of the nlm, then handle it. */
        if (!strnicmp("RESTART",&szcommandLine[iCommandLen],3)) {
            printf("Restart Requested...\n");
            restart();
        }
        else if (!strnicmp("VERSION",&szcommandLine[iCommandLen],3)) {
            printf("Server version: %s\n", ap_get_server_description());
            printf("Server built:   %s\n", ap_get_server_built());
        }
        else if (!strnicmp("MODULES",&szcommandLine[iCommandLen],3)) {
            ap_show_modules();
        }
        else if (!strnicmp("DIRECTIVES",&szcommandLine[iCommandLen],3)) {
                ap_show_directives();
        }
        else if (!strnicmp("SHUTDOWN",&szcommandLine[iCommandLen],3)) {
            printf("Shutdown Requested...\n");
            shutdown_pending = 1;
        }
        else if (!strnicmp("SETTINGS",&szcommandLine[iCommandLen],3)) {
            if (show_settings) {
                show_settings = 0;
                ClearScreen (getscreenhandle());
                show_server_data();
            }
            else {
                show_settings = 1;
                display_settings();
            }
        }
        else {
            show_settings = 0;
            if (strnicmp("HELP",&szcommandLine[iCommandLen],3))
                printf("Unknown APACHE2 command %s\n", &szcommandLine[iCommandLen]);
            printf("Usage: APACHE2 [command] [-p <instance ID>]\n");
            printf("Commands:\n");
            printf("\tDIRECTIVES - Show directives\n");
            printf("\tHELP       - Display this help information\n");
            printf("\tMODULES    - Show a list of the loaded modules\n");
            printf("\tRESTART    - Reread the configuration file and restart Apache\n");
            printf("\tSETTINGS   - Show current thread status\n");
            printf("\tSHUTDOWN   - Shutdown Apache\n");
            printf("\tVERSION    - Display the server version information\n");
        }

        /*  Tell NetWare we handled the command */
        return HANDLEDCOMMAND;
    }

    /*  Tell NetWare that the command isn't mine */
    return NOTMYCOMMAND;
}
Exemplo n.º 2
0
int main(int argc, const char * const argv[])
{
    char c;
    const char *ap_confname = "httpd.conf";
    const char *ngx_confname = "nginx.conf";
    const char *def_server_root = NULL;
    const char *temp_error_log = NULL;
    const char *error;
    process_rec *process;
    server_rec *server_conf;
    apr_pool_t *pglobal;
    apr_pool_t *pconf;
    apr_pool_t *plog; /* Pool of log streams, reset _after_ each read of conf */
    apr_pool_t *ptemp; /* Pool for temporary config stuff, reset often */
    apr_pool_t *pcommands; /* Pool for -D, -C and -c switches */
    apr_getopt_t *opt;
    apr_status_t rv;
    const char *optarg;

    AP_MONCONTROL(0); /* turn off profiling of startup */

    process = init_process(&argc, &argv);
    pglobal = process->pool;
    pconf = process->pconf;
    ap_server_argv0 = process->short_name;

#if APR_CHARSET_EBCDIC
    if (ap_init_ebcdic(pglobal) != APR_SUCCESS) {
        destroy_and_exit_process(process, 1);
    }
#endif

    apr_pool_create(&pcommands, pglobal);
    apr_pool_tag(pcommands, "pcommands");
    ap_server_pre_read_config  = apr_array_make(pcommands, 1, sizeof(char *));
    ap_server_post_read_config = apr_array_make(pcommands, 1, sizeof(char *));
    ap_server_config_defines   = apr_array_make(pcommands, 1, sizeof(char *));

    error = ap_setup_prelinked_modules(process);
    if (error) {
        ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_EMERG, 0, NULL, "%s: %s",
                     ap_server_argv0, error);
        destroy_and_exit_process(process, 1);
    }

    ap_run_rewrite_args(process);

    /* Maintain AP_SERVER_BASEARGS list in http_main.h to allow the MPM
     * to safely pass on our args from its rewrite_args() handler.
     */
    apr_getopt_init(&opt, pcommands, process->argc, process->argv);
    
    while ((rv = apr_getopt(opt, APN_SERVER_BASEARGS, &c, &optarg))
            == APR_SUCCESS) {
        switch (c) {

        case 'f':
            ap_confname = optarg;
            break;

        case 'o':
            ngx_confname = optarg;
            break;

        case 'd':
            def_server_root = optarg;
            break;

        case 'l':
            ap_show_modules();
            destroy_and_exit_process(process, 0);

        case 'L':
            ap_show_directives();
            destroy_and_exit_process(process, 0);

        case 'h':
        case '?':
            usage(process);
            destroy_and_exit_process(process, 0);
            
        case 'H':
            setconf = 1;
            confname = optarg;
            break;
                
        case 'i':
            defindex = 1;
            break;
  
        case 'G':
            defgzip = 1;
            break;
            
        case 'D':
            cleancfg = 1;
            break;
            
        case 'R':
            forcerm = 1;
            break;
        }    
    }

    if (rv != APR_EOF || argc < 3) {
        if (c != 'h' && c != '?') {
            usage(process);
        }
        destroy_and_exit_process(process, 1);
    }

    apr_pool_create(&plog, pglobal);
    apr_pool_tag(plog, "plog");
    apr_pool_create(&ptemp, pconf);
    apr_pool_tag(ptemp, "ptemp");

	/** we need the real path */
	char* fullpath = NULL;
	rv = apr_get_realpath(&fullpath, ap_confname, plog);
	if (rv != APR_SUCCESS){
		apn_error("Apache conf file is not found " 
				"or the given path is invalid! Exit.\n");
		destroy_and_exit_process(process, 1);
	}
	ap_confname = apr_pstrdup(plog, fullpath);

    /* Note that we preflight the config file once
     * before reading it _again_ in the main loop.
     * This allows things, log files configuration
     * for example, to settle down.
     */
    ap_server_root = def_server_root;
    if (!ap_server_root){ // no specify serverroot by -d in commandline.

        if (!ap_confname) {
            apn_error("Apache conf file name is null!\n");
            destroy_and_exit_process(process, 1);
        }

        /**
         * if ap_confname is absolute path, get the prefix as serverroot.
         * if it is not, set the current path as serverroot.
         */
        char* basedir;
        rv = apr_get_basedir(&basedir, ap_confname, process->pool);
        if(rv!=APR_SUCCESS){
            apn_error("Apache conf file is not found " 
                    "or the given path is invalid! Exit.\n");
            destroy_and_exit_process(process, 1);
        }

        ap_server_root = def_server_root = basedir;

		/**
		 * Sometimes, ap_server_root should be set more intelligence.
		 * Because of apache conf depend on the ServerRoot.
		 * when not in localhost, maybe ServerRoot is not valid,
		 * and here need to guess the ap_server_root.
		 */
		apn_fixed_server_root(plog);
    }
    if (temp_error_log) {
        ap_replace_stderr_log(process->pool, temp_error_log);
    }
 
    char *ngx_fullpath = NULL;
    rv = apr_get_realpath(&ngx_fullpath, ngx_confname, plog);
    
    if (rv == APR_SUCCESS && forcerm != 1) {
             apn_error("Config file exists: %s. Exit.\n", ngx_fullpath);
             destroy_and_exit_process(process, 1);
    } else {
        /* Create a empty nginx.conf, because mod_mime needs this file. */
        apr_file_t *f;
        rv = apr_file_open(&f, ngx_confname, 
                (forcerm == 1 ? APR_TRUNCATE : APR_CREATE ) |APR_APPEND|APR_WRITE,
                APR_OS_DEFAULT, plog);
        if (rv != APR_SUCCESS) {
            apn_error("Create file error: %s\n", ngx_fullpath);
            destroy_and_exit_process(process, 1);
        }
        apr_file_close(f);
    }

    /*
     * here just create the main server, and the vhost is not created.
     */
    server_conf = ap_read_config(process, ptemp, ap_confname, &ap_conftree);
    if (!server_conf) {
        destroy_and_exit_process(process, 1);
    }
    /* sort hooks here to make sure pre_config hooks are sorted properly */
    apr_hook_sort_all();

    if (ap_run_pre_config(pconf, plog, ptemp) != OK) {
        ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
                     NULL, "Pre-configuration failed");
        destroy_and_exit_process(process, 1);
    }

    /* Lijinhu added : check the configuration validation */
    if (ap_conftree == NULL) {
        apn_error("The apache conf file is invalid! Please check it. Exit.\n");
        destroy_and_exit_process(process, 1);
    }

    rv = ap_process_config_tree(server_conf, ap_conftree,
                                process->pconf, ptemp);
    if (rv == OK) {
        /*
         * 1. merge server configs.
         * 2. merge per dir configs for each server.
         * 3. re-order the directorise.
         */
        ap_fixup_virtual_hosts(pconf, server_conf);

        /* compile the tables and such we need to do the run-time vhost lookups */
        ap_fini_vhost_config(pconf, server_conf);

        /*
         * Sort hooks again because ap_process_config_tree may have added
         * modules and hence hooks. This happens with mod_perl and modules
         * written in perl.
         */
        apr_hook_sort_all();

        ap_run_test_config(pconf, server_conf);
        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "Syntax OK");

        if ( ap_run_post_config(pconf, plog, ptemp, server_conf) != OK) {
            ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
                    NULL, "Failed when merge some configurations");
            destroy_and_exit_process(process, 1);
        }


        /*
         * migrate the config to nginx conf format.
         * generate the conf file of nginx.
         */
        rv = apn_migrate_to_nginx(plog, server_conf, ngx_confname);
        if (rv != OK) {
            ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "Migrate Error!");
        }
        destroy_and_exit_process(process, 0);
    }

   return 0; /* Termination 'ok' */
}