int options_init( struct options **result, const char *default_mode, int default_packet_size, int default_packets_per_burst, int default_shared_sockets, int default_spin, int default_num_threads) { struct options *options = NULL; options = calloc(1, sizeof(options[0])); if (options == NULL) { fprintf(stderr, "Failed to malloc space for options struct: %s\n", strerror(errno)); goto fail; } if (str_dup(&options->mode, default_mode) != 0) { fprintf(stderr, "Failed to copy string: %s\n", strerror(errno)); goto fail; } options->packet_size = default_packet_size; options->packets_per_burst = default_packets_per_burst; options->shared_sockets = default_shared_sockets; options->spin = default_spin; options->num_threads = default_num_threads; *result = options; return 0; fail: options_destroy(options); return 1; }
/** * RHash program entry point. * * @param argc number of program arguments including the program path * @param argv program arguments * @return the program exit code, zero on success and 1 on error */ int main(int argc, char *argv[]) { find_file_options search_opt; timedelta_t timer; int sfv; i18n_initialize(); /* initialize locale and translation */ memset(&rhash_data, 0, sizeof(rhash_data)); rhash_data.out = stdout; /* set initial output streams */ rhash_data.log = stderr; /* can be altered by options later */ rhash_data.search_opt = &search_opt; init_hash_info_table(); read_options(argc, argv); /* load config and parse command line options */ prev_sigint_handler = signal(SIGINT, ctrl_c_handler); /* install SIGINT handler */ rhash_library_init(); /* in benchmark mode just run benchmark and exit */ if(opt.mode & MODE_BENCHMARK) { unsigned flags = (opt.flags & OPT_BENCH_RAW ? RHASH_BENCHMARK_CPB | RHASH_BENCHMARK_RAW : RHASH_BENCHMARK_CPB); if((opt.flags & OPT_BENCH_RAW) == 0) { fprintf(rhash_data.out, _("%s v%s benchmarking...\n"), PROGRAM_NAME, VERSION); } rhash_run_benchmark(opt.sum_flags, flags, rhash_data.out); rsh_exit(0); } if(opt.n_files == 0) { if(argc > 1) { log_warning(_("no files/directories were specified at command line\n")); } /* print short usage help */ log_msg(_("Usage: %s [OPTION...] <FILE>...\n\n" "Run `%s --help' for more help.\n"), CMD_FILENAME, CMD_FILENAME); rsh_exit(0); } /* setup printf formating string */ rhash_data.printf_str = opt.printf_str; if(opt.template_file) { if(!load_printf_template()) rsh_exit(2); } else if(!rhash_data.printf_str && !(opt.mode & (MODE_CHECK | MODE_CHECK_EMBEDDED))) { /* initialize printf output format according to '--<hashname>' options */ init_printf_format( (rhash_data.template_text = rsh_str_new()) ); rhash_data.printf_str = rhash_data.template_text->str; if(opt.flags & OPT_VERBOSE) { char* str = rsh_strdup(rhash_data.printf_str); log_msg(_("Format string is: %s\n"), str_trim(str)); free(str); } } if(rhash_data.printf_str) { rhash_data.print_list = parse_print_string(rhash_data.printf_str, &opt.sum_flags); } memset(&search_opt, 0, sizeof(search_opt)); search_opt.max_depth = (opt.flags & OPT_RECURSIVE ? opt.find_max_depth : 0); search_opt.options = FIND_SKIP_DIRS; search_opt.call_back = find_file_callback; if((sfv = (opt.fmt == FMT_SFV && !opt.mode))) { print_sfv_banner(rhash_data.out); } /* pre-process files */ if(sfv || opt.bt_batch_file) { /* note: errors are not reported on pre-processing */ search_opt.call_back_data = (void*)1; process_files((const char**)opt.files, opt.n_files, &search_opt); fflush(rhash_data.out); } /* measure total processing time */ rhash_timer_start(&timer); rhash_data.processed = 0; /* process files */ search_opt.options |= FIND_LOG_ERRORS; search_opt.call_back_data = (void*)0; process_files((const char**)opt.files, opt.n_files, &search_opt); if((opt.mode & MODE_CHECK_EMBEDDED) && rhash_data.processed > 1) { print_check_stats(); } if(!rhash_data.interrupted) { if(opt.bt_batch_file && rhash_data.rctx) { rhash_final(rhash_data.rctx, 0); save_torrent_to(opt.bt_batch_file, rhash_data.rctx); } if((opt.flags & OPT_SPEED) && !(opt.mode & (MODE_CHECK | MODE_UPDATE)) && rhash_data.processed > 1) { double time = rhash_timer_stop(&timer); print_time_stats(time, rhash_data.total_size, 1); } } else { /* check if interruption was not reported yet */ if(rhash_data.interrupted == 1) report_interrupted(); } options_destroy(&opt); rhash_destroy(&rhash_data); /* return non-zero error code if error occurred */ return (rhash_data.error_flag ? 1 : search_opt.errors_count ? 2 : rhash_data.interrupted ? 3 : 0); }
int main(int argc, char **argv) { int option_index = 0; if (argc < 2) { show_help(argv); return EXIT_FAILURE; } gru_status_t status = gru_status_new(); options_t *options = options_new(&status); if (!options) { fprintf(stderr, "Unable to create options object: %s", status.message); return EXIT_FAILURE; } set_options_object(options); gru_logger_set(gru_logger_timed_printer); while (1) { static struct option long_options[] = {{"log-level", required_argument, 0, 'l'}, {"log-dir", required_argument, 0, 'L'}, {"maestro-url", required_argument, 0, 'm'}, {"name", required_argument, 0, 'n'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; int c = getopt_long(argc, argv, "l:L:m:n:h", long_options, &option_index); if (c == -1) { break; } switch (c) { case 'l': options_set_log_level(options, optarg); break; case 'L': if (!options_set_logdir(options, optarg)) { fprintf(stderr, "Unable to allocate memory for setting the log directory\n"); goto err_exit; } break; case 'm': if (!options_set_maestro_uri(options, optarg, &status)) { fprintf(stderr, "%s\n", status.message); goto err_exit; } break; case 'n': if (!options_set_name(options, optarg)) { fprintf(stderr, "Unable to allocate memory for setting the node name\n"); goto err_exit; } break; case 'h': show_help(argv); options_destroy(&options); return EXIT_SUCCESS; default: printf("Invalid or missing option\n"); show_help(argv); options_destroy(&options); return EXIT_FAILURE; } } if (!options_get_log_dir()) { fprintf(stderr, "Log directory is mandatory for the sender daemon\n"); goto err_exit; } if (!gru_path_mkdirs(options_get_log_dir(), &status)) { fprintf(stderr, "Unable to create log directory: %s\n", status.message); goto err_exit; } if (!options_get_maestro_host()) { fprintf(stderr, "Maestro host is mandatory for the sender daemon\n"); goto err_exit; } int cret = init_controller(options_get_log_dir(), "mpt-sender-daemon"); if (cret == 0) { if (senderd_worker_start(options) != 0) { logger_t logger = gru_logger_get(); logger(GRU_ERROR, "Unable to start the sender worker"); goto err_exit; } #ifdef __linux__ fcloseall(); #endif options_destroy(&options); return EXIT_SUCCESS; } if (cret > 0) { options_destroy(&options); return EXIT_SUCCESS; } err_exit: options_destroy(&options); return EXIT_FAILURE; }
/** * RHash program entry point. * * @param argc number of program arguments including the program path * @param argv program arguments * @return the program exit code, zero on success and 1 on error */ int main(int argc, char *argv[]) { i18n_initialize(); /* initialize locale and translation */ memset(&rhash_data, 0, sizeof(rhash_data)); rhash_data.out = stdout; /* set initial output streams */ rhash_data.log = stderr; /* can be altered by options later */ init_hash_info_table(); read_options(argc, argv); /* load config and parse command line options */ prev_sigint_handler = signal(SIGINT, ctrl_c_handler); /* install SIGINT handler */ rhash_library_init(); /* in benchmark mode just run benchmark and exit */ if(opt.mode & MODE_BENCHMARK) { unsigned flags = (opt.flags & OPT_BENCH_RAW ? RHASH_BENCHMARK_CPB | RHASH_BENCHMARK_RAW : RHASH_BENCHMARK_CPB); if((opt.flags & OPT_BENCH_RAW) == 0) { fprintf(rhash_data.out, _("%s v%s benchmarking...\n"), PROGRAM_NAME, VERSION); } rhash_run_benchmark(opt.sum_flags, flags, rhash_data.out); rsh_exit(0); } if(opt.n_files == 0) { if(argc > 1) { log_warning(_("no files/directories were specified at command line\n")); } /* print short usage help */ log_msg(_("Usage: %s [OPTION...] <FILE>...\n\n" "Run `%s --help' for more help.\n"), CMD_FILENAME, CMD_FILENAME); rsh_exit(0); } /* setup printf formating string */ rhash_data.printf_str = opt.printf_str; if(opt.template_file) { if(!load_printf_template()) rsh_exit(2); } else if(!rhash_data.printf_str && !(opt.mode & (MODE_CHECK | MODE_CHECK_EMBEDDED))) { /* initialize printf output format according to '--<hashname>' options */ init_printf_format( (rhash_data.template_text = rsh_str_new()) ); rhash_data.printf_str = rhash_data.template_text->str; if(opt.flags & OPT_VERBOSE) { char* str = rsh_strdup(rhash_data.printf_str); log_msg(_("Format string is: %s\n"), str_trim(str)); free(str); } } if(rhash_data.printf_str) { rhash_data.print_list = parse_print_string(rhash_data.printf_str, &opt.sum_flags); } preprocess_files(); process_files(); options_destroy(&opt); rhash_destroy(&rhash_data); return (rhash_data.error_flag ? 1 : 0); }
int main(int argc, char **argv) { int option_index = 0; if (argc < 2) { show_help(argv); return EXIT_FAILURE; } gru_status_t status = gru_status_new(); options_t *options = options_new(&status); if (!options) { fprintf(stderr, "Unable to create options object: %s", status.message); return EXIT_FAILURE; } set_options_object(options); gru_logger_set(gru_logger_default_printer); while (1) { static struct option long_options[] = {{"broker-url", required_argument, 0, 'b'}, {"duration", required_argument, 0, 'd'}, {"log-level", required_argument, 0, 'l'}, {"log-dir", required_argument, 0, 'L'}, {"parallel-count", required_argument, 0, 'p'}, {"message-size", required_argument, 0, 's'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; int c = getopt_long(argc, argv, "b:d:l:L:p:s:h", long_options, &option_index); if (c == -1) { break; } switch (c) { case 'b': if (!options_set_broker_uri(options, optarg, &status)) { fprintf(stderr, "%s\n", status.message); goto err_exit_0; } break; case 'd': if (!options_set_duration(options, optarg)) { fprintf(stderr, "Invalid duration: %s\n", optarg); goto err_exit_0; } break; case 'l': options_set_log_level(options, optarg); break; case 'p': options_set_parallel_count(options, optarg); break; case 's': options_set_message_size(options, optarg); break; case 'L': if (!options_set_logdir(options, optarg)) { fprintf(stderr, "Unable to allocate memory for setting the log directory\n"); goto err_exit_0; } break; case 'h': show_help(argv); options_destroy(&options); return EXIT_SUCCESS; default: printf("Invalid or missing option\n"); show_help(argv); options_destroy(&options); return EXIT_FAILURE; } } if (options_get_log_dir()) { remap_log_with_link(options_get_log_dir(), "mpt-receiver", 0, getpid(), stderr, &status); } else { if (options_get_parallel_count() > 1) { fprintf(stderr, "Multiple concurrent process require a log directory\n"); goto err_exit_0; } } logger_t logger = gru_logger_get(); vmsl_t vmsl = vmsl_init(); const gru_uri_t broker_uri = options_get_broker_uri(); if (!vmsl_assign_by_url(&broker_uri, &vmsl)) { goto err_exit_1; } logger(GRU_INFO, "Starting test"); if (receiver_start(&vmsl, options) == 0) { logger(GRU_INFO, "Test execution with process ID %d finished successfully\n", getpid()); options_destroy(&options); return EXIT_SUCCESS; } err_exit_1: logger(GRU_INFO, "Test execution with process ID %d finished with errors\n", getpid()); err_exit_0: options_destroy(&options); return EXIT_FAILURE; }