Beispiel #1
0
int32_t get_rel_time(const struct timespec* abs_time)
{
    struct timespec now = get_time_tick();

    assert(abs_time);

    return S_2_MS(abs_time->tv_sec - now.tv_sec) + NS_2_MS(abs_time->tv_nsec - now.tv_nsec);
}
Beispiel #2
0
struct timespec get_abs_time(int32_t time_affter)
{
    struct timespec ts = get_time_tick();

    ts.tv_sec  += MS_2_S(time_affter);
    ts.tv_nsec += MS_2_NS(time_affter);

    return ts;
}
Beispiel #3
0
int main(int argc, char **argv)
{
	int ch, longindex, ret;
	unsigned long flags;
	struct option *long_options;
	const struct command *commands;
	const char *short_options;
	char *p, *env;
	const struct sd_option *sd_opts;
	uint8_t sdhost[16];
	int sdport;
	struct timespec start, end;

	start = get_time_tick();

	log_dog_operation(argc, argv);

	install_crash_handler(crash_handler);

	init_commands(&commands);

	if (argc < 2)
		usage(commands, 0);

	flags = setup_commands(commands, argv[1], argv[2]);

	optind = 3;

	sd_opts = build_sd_options(command_opts);
	long_options = build_long_options(sd_opts);
	short_options = build_short_options(sd_opts);

	env = getenv("SHEEPDOG_DOG_ADDR");
	if (env) {
		if (!str_to_addr(env, sdhost)) {
			sd_err("Invalid ip address %s", env);
			return EXIT_FAILURE;
		}
		memcpy(sd_nid.addr, sdhost, sizeof(sdhost));
	}

	env = getenv("SHEEPDOG_DOG_PORT");
	if (env) {
		sdport = strtol(env, &p, 10);
		if (env == p || sdport < 1 || sdport > UINT16_MAX
		    || !is_numeric(env)) {
			sd_err("Invalid port number '%s'", env);
			exit(EXIT_USAGE);
		}
		sd_nid.port = sdport;
	}

	while ((ch = getopt_long(argc, argv, short_options, long_options,
				&longindex)) >= 0) {

		switch (ch) {
		case 'a':
			if (!str_to_addr(optarg, sdhost)) {
				sd_err("Invalid ip address %s", optarg);
				return EXIT_FAILURE;
			}
			memcpy(sd_nid.addr, sdhost, sizeof(sdhost));
			break;
		case 'p':
			sdport = strtol(optarg, &p, 10);
			if (optarg == p || sdport < 1 || sdport > UINT16_MAX
					|| !is_numeric(optarg)) {
				sd_err("Invalid port number '%s'", optarg);
				exit(EXIT_USAGE);
			}
			sd_nid.port = sdport;
			break;
		case 'r':
			raw_output = true;
			break;
		case 'v':
			verbose = true;
			break;
		case 'h':
			subcommand_usage(argv[1], argv[2], EXIT_SUCCESS);
			break;
		case 't':
			elapsed_time = true;
			break;
		case '?':
			usage(commands, EXIT_USAGE);
			break;
		default:
			if (command_parser)
				command_parser(ch, optarg);
			else
				usage(commands, EXIT_USAGE);
			break;
		}
	}

	if (sd_inode_actor_init(dog_bnode_writer, dog_bnode_reader) < 0)
		exit(EXIT_SYSFAIL);

	if (!is_stdout_console() || raw_output)
		highlight = false;

	if (flags & CMD_NEED_NODELIST) {
		ret = update_node_list(SD_MAX_NODES);
		if (ret < 0) {
			sd_err("Failed to get node list");
			exit(EXIT_SYSFAIL);
		}
	}

	if (flags & CMD_NEED_ARG && argc == optind)
		subcommand_usage(argv[1], argv[2], EXIT_USAGE);

	if (init_event(EPOLL_SIZE) < 0)
		exit(EXIT_SYSFAIL);

	if (init_work_queue(get_nr_nodes) != 0) {
		sd_err("Failed to init work queue");
		exit(EXIT_SYSFAIL);
	}

	if (sockfd_init()) {
		sd_err("sockfd_init() failed");
		exit(EXIT_SYSFAIL);
	}

	ret = command_fn(argc, argv);
	if (ret == EXIT_USAGE)
		subcommand_usage(argv[1], argv[2], EXIT_USAGE);

	if (elapsed_time) {
		end = get_time_tick();
		printf("\nElapsed time: %.3lf seconds\n",
				get_time_interval(&start, &end));
	}

	return ret;
}