Ejemplo n.º 1
0
static char *
capture_vsctl_valist(const char *arg0, va_list args)
{
    char *stdout_log, *stderr_log;
    enum vlog_level log_level;
    struct svec argv;
    int status;
    char *msg;

    /* Compose arguments. */
    svec_init(&argv);
    svec_add(&argv, arg0);
    for (;;) {
        const char *arg = va_arg(args, const char *);
        if (!arg) {
            break;
        }
        svec_add(&argv, arg);
    }
    svec_terminate(&argv);

    /* Run process. */
    if (process_run_capture(argv.names, &stdout_log, &stderr_log, SIZE_MAX,
                            &status)) {
        svec_destroy(&argv);
        return NULL;
    }

    /* Log results. */
    if (WIFEXITED(status)) {
        int code = WEXITSTATUS(status);
        log_level = code == 0 ? VLL_DBG : code == 1 ? VLL_WARN : VLL_ERR;
    } else {
        log_level = VLL_ERR;
    }
    msg = process_status_msg(status);
    VLOG(log_level, "ovs-vsctl exited (%s)", msg);
    if (stdout_log && *stdout_log) {
        VLOG(log_level, "ovs-vsctl wrote to stdout:\n%s\n", stdout_log);
    }
    if (stderr_log && *stderr_log) {
        VLOG(log_level, "ovs-vsctl wrote to stderr:\n%s\n", stderr_log);
    }
    free(msg);

    svec_destroy(&argv);

    free(stderr_log);
    if (WIFEXITED(status) && !WEXITSTATUS(status)) {
        return stdout_log;
    } else {
        free(stdout_log);
        return NULL;
    }
}
int
main(void)
{
    extern struct vlog_module VLM_reconnect;
    struct reconnect_stats prev;
    unsigned int old_max_tries;
    int old_time;
    char line[128];

    vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_OFF);

    now = 1000;
    reconnect = reconnect_create(now);
    reconnect_set_name(reconnect, "remote");
    reconnect_get_stats(reconnect, now, &prev);
    printf("### t=%d ###\n", now);
    old_time = now;
    old_max_tries = reconnect_get_max_tries(reconnect);
    while (fgets(line, sizeof line, stdin)) {
        struct reconnect_stats cur;
        struct svec args;

        fputs(line, stdout);
        if (line[0] == '#') {
            continue;
        }

        svec_init(&args);
        svec_parse_words(&args, line);
        svec_terminate(&args);
        if (!svec_is_empty(&args)) {
            run_command(args.n, args.names, commands);
        }
        svec_destroy(&args);

        if (old_time != now) {
            printf("\n### t=%d ###\n", now);
        }

        reconnect_get_stats(reconnect, now, &cur);
        diff_stats(&prev, &cur, now - old_time);
        prev = cur;
        if (reconnect_get_max_tries(reconnect) != old_max_tries) {
            old_max_tries = reconnect_get_max_tries(reconnect);
            printf("  %u tries left\n", old_max_tries);
        }

        old_time = now;
    }

    return 0;
}
Ejemplo n.º 3
0
void
svec_unique(struct svec *svec)
{
    assert(svec_is_sorted(svec));
    if (svec->n > 1) {
        /* This algorithm is lazy and sub-optimal, but it's "obviously correct"
         * and asymptotically optimal . */
        struct svec tmp;
        size_t i;

        svec_init(&tmp);
        svec_add(&tmp, svec->names[0]);
        for (i = 1; i < svec->n; i++) {
            if (strcmp(svec->names[i - 1], svec->names[i])) {
                svec_add(&tmp, svec->names[i]);
            }
        }
        svec_swap(&tmp, svec);
        svec_destroy(&tmp);
    }
}
Ejemplo n.º 4
0
int
main(int argc, char *argv[])
{
    const struct ovsdb_client_command *command;
    char *database;
    struct jsonrpc *rpc;

    ovs_cmdl_proctitle_init(argc, argv);
    set_program_name(argv[0]);
    parse_options(argc, argv);
    fatal_ignore_sigpipe();

    daemon_become_new_user(false);
    if (optind >= argc) {
        ovs_fatal(0, "missing command name; use --help for help");
    }

    for (command = get_all_commands(); ; command++) {
        if (!command->name) {
            VLOG_FATAL("unknown command '%s'; use --help for help",
                       argv[optind]);
        } else if (!strcmp(command->name, argv[optind])) {
            break;
        }
    }
    optind++;

    if (command->need != NEED_NONE) {
        if (argc - optind > command->min_args
            && (isalpha((unsigned char) argv[optind][0])
                && strchr(argv[optind], ':'))) {
            rpc = open_jsonrpc(argv[optind++]);
        } else {
            char *sock = xasprintf("unix:%s/db.sock", ovs_rundir());
            rpc = open_jsonrpc(sock);
            free(sock);
        }
    } else {
        rpc = NULL;
    }

    if (command->need == NEED_DATABASE) {
        struct svec dbs;

        svec_init(&dbs);
        fetch_dbs(rpc, &dbs);
        if (argc - optind > command->min_args
            && svec_contains(&dbs, argv[optind])) {
            database = xstrdup(argv[optind++]);
        } else if (dbs.n == 1) {
            database = xstrdup(dbs.names[0]);
        } else if (svec_contains(&dbs, "Open_vSwitch")) {
            database = xstrdup("Open_vSwitch");
        } else {
            jsonrpc_close(rpc);
            ovs_fatal(0, "no default database for `%s' command, please "
                      "specify a database name", command->name);
        }
        svec_destroy(&dbs);
    } else {
        database = NULL;
    }

    if (argc - optind < command->min_args ||
        argc - optind > command->max_args) {
        free(database);
        VLOG_FATAL("invalid syntax for '%s' (use --help for help)",
                    command->name);
    }

    command->handler(rpc, database, argc - optind, argv + optind);

    free(database);
    jsonrpc_close(rpc);

    if (ferror(stdout)) {
        VLOG_FATAL("write to stdout failed");
    }
    if (ferror(stderr)) {
        VLOG_FATAL("write to stderr failed");
    }

    return 0;
}
Ejemplo n.º 5
0
int main() {
	int i;
	ivec_t* vec = new_ivec(100);
	for (i = 0; i < 10000; ++i) {
		assert(ivec_push_back(vec, i));
	}

	printf("size = %u, empty = %d, capacity = %u\n", ivec_size(vec), ivec_empty(vec), ivec_capacity(vec));
	printf("popback = %d\n", ivec_pop_back(vec));
	printf("size = %u, empty = %d, capacity = %u\n", ivec_size(vec), ivec_empty(vec), ivec_capacity(vec));

	int* d = ivec_data(vec);
	for (i = 0; i < 9999; ++i) {
		assert(*(d+i) == i && i == ivec_at(vec, i));
	}

	printf("resize to 10, and shrink_to_fit\n");
	ivec_resize_default(vec, 10);
	ivec_shrink_to_fit(vec);
	printf("size = %u, empty = %d, capacity = %u\n", ivec_size(vec), ivec_empty(vec), ivec_capacity(vec));
	
	char buf[1000];
	ivec_dump(buf, 1000, vec);
	printf("%s\n", buf);

	printf("resize to 15\n", ivec_resize_default(vec, 15));
	printf("size = %u, empty = %d, capacity = %u\n", ivec_size(vec), ivec_empty(vec), ivec_capacity(vec));
	ivec_dump(buf, 1000, vec);
	printf("%s\n", buf);

	ivec_destroy(vec);

	svec_t* svec = new_svec(100);
	for (i = 0; i < 100; ++i) {
		snprintf(buf, 1000, "%d", i);
		assert(svec_push_back(svec, buf));
	}

	printf("size = %u, empty = %d, capacity = %u\n", svec_size(svec), svec_empty(svec), svec_capacity(svec));
	char* ret = svec_pop_back(svec);
	printf("popback = %s\n", ret);
	free(ret);
	printf("size = %u, empty = %d, capacity = %u\n", svec_size(svec), svec_empty(svec), svec_capacity(svec));

	char** s = svec_data(svec);
	for (i = 0; i < 99; ++i) {
		snprintf(buf, 1000, "%d", i);
		assert(!strcmp(*(s+i), buf) && !strcmp(buf, svec_at(svec, i)));
	}

	printf("resize to 10, and shrink to fit\n");
	svec_resize_default(svec, 10);
	svec_shrink_to_fit(svec);
	printf("size = %u, empty = %d, capacity = %u\n", svec_size(svec), svec_empty(svec), svec_capacity(svec));
	
	svec_dump(buf, 1000, svec);
	printf("%s\n", buf);

	printf("resize to 15\n", svec_resize_default(svec, 15));
	printf("size = %u, empty = %d, capacity = %u\n", svec_size(svec), svec_empty(svec), svec_capacity(svec));
	svec_dump(buf, 1000, svec);
	printf("%s\n", buf);

	svec_destroy(svec);

	return 0;
}