Example #1
0
int main(int argc, char **argv)
{
    struct cmd_spec *cspec;

    if (argc < 2) {
        help(NULL);
        exit(1);
    }

    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
        fprintf(stderr, "cannot init xl context\n");
        exit(1);
    }
    if (libxl_ctx_set_log(&ctx, log_callback, NULL)) {
        fprintf(stderr, "cannot set xl log callback\n");
        exit(-ERROR_FAIL);
    }

    srand(time(0));

    optind = 2;

    cspec = cmdtable_lookup(argv[1]);
    if (cspec)
        return cspec->cmd_impl(argc, argv);
    else if (!strcmp(argv[1], "help")) {
        help(argv[1]);
        exit(0);
    } else {
        fprintf(stderr, "command not implemented\n");
        exit(1);
    }
}
Example #2
0
File: xl.c Project: Chong-Li/xen
int main(int argc, char **argv)
{
    int opt = 0;
    char *cmd = 0;
    struct cmd_spec *cspec;
    int ret;
    void *config_data = 0;
    int config_len = 0;

    while ((opt = getopt(argc, argv, "+vftN")) >= 0) {
        switch (opt) {
        case 'v':
            if (minmsglevel > 0) minmsglevel--;
            break;
        case 'N':
            dryrun_only = 1;
            break;
        case 'f':
            force_execution = 1;
            break;
        case 't':
            progress_use_cr = 1;
            break;
        default:
            fprintf(stderr, "unknown global option\n");
            exit(EXIT_FAILURE);
        }
    }

    cmd = argv[optind];

    if (!cmd) {
        help(NULL);
        exit(EXIT_FAILURE);
    }
    opterr = 0;

    logger = xtl_createlogger_stdiostream(stderr, minmsglevel,
        (progress_use_cr ? XTL_STDIOSTREAM_PROGRESS_USE_CR : 0));
    if (!logger) exit(EXIT_FAILURE);

    atexit(xl_ctx_free);

    xl_ctx_alloc();

    ret = libxl_read_file_contents(ctx, XL_GLOBAL_CONFIG,
            &config_data, &config_len);
    if (ret)
        fprintf(stderr, "Failed to read config file: %s: %s\n",
                XL_GLOBAL_CONFIG, strerror(errno));
    parse_global_config(XL_GLOBAL_CONFIG, config_data, config_len);
    free(config_data);

    /* Reset options for per-command use of getopt. */
    argv += optind;
    argc -= optind;
    optind = 1;

    cspec = cmdtable_lookup(cmd);
    if (cspec) {
        if (dryrun_only && !cspec->can_dryrun) {
            fprintf(stderr, "command does not implement -N (dryrun) option\n");
            ret = EXIT_FAILURE;
            goto xit;
        }
        ret = cspec->cmd_impl(argc, argv);
    } else if (!strcmp(cmd, "help")) {
        help(argv[1]);
        ret = EXIT_SUCCESS;
    } else {
        fprintf(stderr, "command not implemented\n");
        ret = EXIT_FAILURE;
    }

 xit:
    return ret;
}
Example #3
0
File: xl.c Project: HackLinux/xen
int main(int argc, char **argv)
{
    int opt = 0;
    char *cmd = 0;
    struct cmd_spec *cspec;
    int ret;
    void *config_data = 0;
    int config_len = 0;
    const char *locks[] = XEND_LOCK;

    while ((opt = getopt(argc, argv, "+vfN")) >= 0) {
        switch (opt) {
        case 'v':
            if (minmsglevel > 0) minmsglevel--;
            break;
        case 'N':
            dryrun_only = 1;
            break;
        case 'f':
            force_execution = 1;
            break;
        default:
            fprintf(stderr, "unknown global option\n");
            exit(2);
        }
    }

    cmd = argv[optind];

    if (!cmd) {
        help(NULL);
        exit(1);
    }
    opterr = 0;

    logger = xtl_createlogger_stdiostream(stderr, minmsglevel,  0);
    if (!logger) exit(1);

    atexit(xl_ctx_free);

    xl_ctx_alloc();

    ret = libxl_read_file_contents(ctx, XL_GLOBAL_CONFIG,
            &config_data, &config_len);
    if (ret)
        fprintf(stderr, "Failed to read config file: %s: %s\n",
                XL_GLOBAL_CONFIG, strerror(errno));
    parse_global_config(XL_GLOBAL_CONFIG, config_data, config_len);
    free(config_data);

    /* Reset options for per-command use of getopt. */
    argv += optind;
    argc -= optind;
    optind = 1;

    cspec = cmdtable_lookup(cmd);
    if (cspec) {
        if (dryrun_only && !cspec->can_dryrun) {
            fprintf(stderr, "command does not implement -N (dryrun) option\n");
            ret = 1;
            goto xit;
        }
        if (cspec->modifies && !dryrun_only) {
            for (int i = 0; i < sizeof(locks)/sizeof(locks[0]); i++) {
                if (!access(locks[i], F_OK) && !force_execution) {
                    fprintf(stderr,
"xend is running, which may cause unpredictable results when using\n"
"this xl command.  Please shut down xend before continuing.\n\n"
"(This check can be overridden with the -f option.)\n"
                            );
                    ret = 1;
                    goto xit;
                }
            }
        }
        ret = cspec->cmd_impl(argc, argv);
    } else if (!strcmp(cmd, "help")) {
        help(argv[1]);
        ret = 0;
    } else {
        fprintf(stderr, "command not implemented\n");
        ret = 1;
    }

 xit:
    return ret;
}