void error_setup(ERRORVAL *errcon, NAMELIST_TEXT *nltext, RUN *run_cond, LINE_LIST *beamline) { long i; log_entry("error_setup"); /* reset namelist variables to defaults */ /* clear_error_settings = 1; summarize_error_settings = 0; error_log = NULL; */ /* process namelist text */ set_namelist_processing_flags(STICKY_NAMELIST_DEFAULTS); set_print_namelist_flags(0); if (processNamelist(&error_control, nltext)==NAMELIST_ERROR) bombElegant(NULL, NULL); if (echoNamelists) print_namelist(stdout, &error_control); if (summarize_error_settings) { fprintf(stdout, "summary of random error settings: \n"); fflush(stdout); if (errcon->no_errors_first_step) fprintf(stdout, "No errors will be generated for the first step.\n"); fflush(stdout); for (i=0; i<errcon->n_items; i++) { switch (errcon->error_type[i]) { case UNIFORM_ERRORS: case GAUSSIAN_ERRORS: fprintf(stdout, "%8s: %sadditive %s errors with amplitude %e %s and cutoff %e %s\n", errcon->quan_name[i], (errcon->flags[i]&NONADDITIVE_ERRORS?"non-":""), known_error_type[errcon->error_type[i]], (errcon->flags[i]&FRACTIONAL_ERRORS?100:1)*errcon->error_level[i], errcon->flags[i]&FRACTIONAL_ERRORS?"%":errcon->quan_unit[i], errcon->error_cutoff[i], (errcon->flags[i]&POST_CORRECTION?"(post-correction)":"")); fflush(stdout); break; case PLUS_OR_MINUS_ERRORS: fprintf(stdout, "%8s: %sadditive %s errors with amplitude %e\n", errcon->quan_name[i], (errcon->flags[i]&NONADDITIVE_ERRORS?"non-":""), known_error_type[errcon->error_type[i]], errcon->error_level[i]); fflush(stdout); break; } } log_exit("error_setup"); return; } errcon->no_errors_first_step = no_errors_for_first_step; compute_end_positions(beamline); if (errcon->fp_log) fclose(errcon->fp_log); #if USE_MPI if (isSlave) error_log = NULL; #endif if (error_log) { errcon->fp_log = fopen_e(compose_filename(error_log, run_cond->rootname), "w", 0); fprintf(errcon->fp_log, "SDDS1\n"); fprintf(errcon->fp_log, "&description text=\"Error log--input: %s lattice: %s\", contents=\"error log, elegant output\" &end\n", run_cond->runfile, run_cond->lattice); fprintf(errcon->fp_log, "&associate filename=\"%s\", path=\"%s\", contents=\"elegant input, parent\" &end\n", run_cond->runfile, getenv("PWD")); fprintf(errcon->fp_log, "&associate filename=\"%s\", path=\"%s\", contents=\"elegant lattice, parent\" &end\n", run_cond->lattice, getenv("PWD")); fprintf(errcon->fp_log, "¶meter name=Step, type=long, description=\"simulation step\" &end\n"); fprintf(errcon->fp_log, "¶meter name=When, type=string, description=\"phase of simulation when errors were asserted\" &end\n"); fprintf(errcon->fp_log, "&column name=ParameterValue, type=double, description=\"Perturbed value\" &end\n"); fprintf(errcon->fp_log, "&column name=ParameterError, type=double, description=\"Perturbation value\" &end\n"); fprintf(errcon->fp_log, "&column name=ElementParameter, type=string, description=\"Parameter name\" &end\n"); fprintf(errcon->fp_log, "&column name=ElementName, type=string, description=\"Element name\" &end\n"); fprintf(errcon->fp_log, "&column name=ElementOccurence, type=long, description=\"Element occurence\" &end\n"); fprintf(errcon->fp_log, "&column name=ElementType, type=string, description=\"Element type\" &end\n"); fprintf(errcon->fp_log, "&data mode=ascii, lines_per_row=1, no_row_counts=1 &end\n"); fflush(errcon->fp_log); } else errcon->fp_log = NULL; if (clear_error_settings) { /* indicate that no error data has been asserted */ errcon->new_data_read = 0; /* clean up the error control structure and flags on elements */ if (errcon->n_items) set_element_flags(beamline, errcon->name, NULL, NULL, NULL, errcon->n_items, PARAMETERS_ARE_STATIC, 0, 1, 0); errcon->n_items = 0; } fprintf(stdout, "\n*** Cleared error settings\n"); fflush(stdout); log_exit("error_setup"); }
int main(int argc, char **argv) { int exit_code = EXIT_FAILURE; int i; bool help = false; bool version = false; bool check_config = false; bool daemon = false; bool debug = false; bool libusb_debug = false; int pid_fd = -1; #ifdef BRICKD_WITH_LIBUDEV bool initialized_udev = false; #endif for (i = 1; i < argc; ++i) { if (strcmp(argv[i], "--help") == 0) { help = true; } else if (strcmp(argv[i], "--version") == 0) { version = true; } else if (strcmp(argv[i], "--check-config") == 0) { check_config = true; } else if (strcmp(argv[i], "--daemon") == 0) { daemon = true; } else if (strcmp(argv[i], "--debug") == 0) { debug = true; } else if (strcmp(argv[i], "--libusb-debug") == 0) { libusb_debug = true; } else { fprintf(stderr, "Unknown option '%s'\n\n", argv[i]); print_usage(); return EXIT_FAILURE; } } if (help) { print_usage(); return EXIT_SUCCESS; } if (version) { printf("%s\n", VERSION_STRING); return EXIT_SUCCESS; } if (prepare_paths() < 0) { return EXIT_FAILURE; } if (check_config) { return config_check(_config_filename) < 0 ? EXIT_FAILURE : EXIT_SUCCESS; } config_init(_config_filename); log_init(); if (daemon) { pid_fd = daemon_start(_log_filename, _pid_filename, true); } else { pid_fd = pid_file_acquire(_pid_filename, getpid()); if (pid_fd == PID_FILE_ALREADY_ACQUIRED) { fprintf(stderr, "Already running according to '%s'\n", _pid_filename); } } if (pid_fd < 0) { goto error_log; } log_set_debug_override(debug); log_set_level(LOG_CATEGORY_EVENT, config_get_option_value("log_level.event")->log_level); log_set_level(LOG_CATEGORY_USB, config_get_option_value("log_level.usb")->log_level); log_set_level(LOG_CATEGORY_NETWORK, config_get_option_value("log_level.network")->log_level); log_set_level(LOG_CATEGORY_HOTPLUG, config_get_option_value("log_level.hotplug")->log_level); log_set_level(LOG_CATEGORY_HARDWARE, config_get_option_value("log_level.hardware")->log_level); log_set_level(LOG_CATEGORY_WEBSOCKET, config_get_option_value("log_level.websocket")->log_level); #ifdef BRICKD_WITH_RED_BRICK log_set_level(LOG_CATEGORY_RED_BRICK, config_get_option_value("log_level.red_brick")->log_level); log_set_level(LOG_CATEGORY_SPI, config_get_option_value("log_level.spi")->log_level); log_set_level(LOG_CATEGORY_RS485, config_get_option_value("log_level.rs485")->log_level); #endif log_set_level(LOG_CATEGORY_OTHER, config_get_option_value("log_level.other")->log_level); if (config_has_error()) { log_error("Error(s) occurred while reading config file '%s'", _config_filename); goto error_config; } if (daemon) { log_info("Brick Daemon %s started (daemonized)", VERSION_STRING); } else { log_info("Brick Daemon %s started", VERSION_STRING); } if (config_has_warning()) { log_error("Warning(s) in config file '%s', run with --check-config option for details", _config_filename); } if (event_init() < 0) { goto error_event; } if (signal_init(handle_sigusr1) < 0) { goto error_signal; } if (hardware_init() < 0) { goto error_hardware; } if (usb_init(libusb_debug) < 0) { goto error_usb; } #ifdef BRICKD_WITH_LIBUDEV if (!usb_has_hotplug()) { if (udev_init() < 0) { goto error_udev; } initialized_udev = true; } #endif if (network_init() < 0) { goto error_network; } #ifdef BRICKD_WITH_RED_BRICK if (gpio_init() < 0) { goto error_gpio; } if (redapid_init() < 0) { goto error_redapid; } if (red_stack_init() < 0) { goto error_red_stack; } if (red_extension_init() < 0) { goto error_red_extension; } if (red_usb_gadget_init() < 0) { goto error_red_usb_gadget; } red_led_set_trigger(RED_LED_GREEN, config_get_option_value("led_trigger.green")->red_led_trigger); red_led_set_trigger(RED_LED_RED, config_get_option_value("led_trigger.red")->red_led_trigger); #endif if (event_run(network_cleanup_clients_and_zombies) < 0) { goto error_run; } #ifdef BRICKD_WITH_RED_BRICK hardware_announce_disconnect(); network_announce_red_brick_disconnect(); red_usb_gadget_announce_red_brick_disconnect(); #endif exit_code = EXIT_SUCCESS; error_run: #ifdef BRICKD_WITH_RED_BRICK red_usb_gadget_exit(); error_red_usb_gadget: red_extension_exit(); error_red_extension: red_stack_exit(); error_red_stack: redapid_exit(); error_redapid: //gpio_exit(); error_gpio: #endif network_exit(); error_network: #ifdef BRICKD_WITH_LIBUDEV if (initialized_udev) { udev_exit(); } error_udev: #endif usb_exit(); error_usb: hardware_exit(); error_hardware: signal_exit(); error_signal: event_exit(); error_event: log_info("Brick Daemon %s stopped", VERSION_STRING); error_config: error_log: log_exit(); if (pid_fd >= 0) { pid_file_release(_pid_filename, pid_fd); } config_exit(); return exit_code; }
void add_error_element(ERRORVAL *errcon, NAMELIST_TEXT *nltext, LINE_LIST *beamline) { long n_items, n_added, i_start, firstIndexInGroup; ELEMENT_LIST *context; double sMin = -DBL_MAX, sMax = DBL_MAX; log_entry("add_error_element"); if ((n_items = errcon->n_items)==0) { if (errcon->new_data_read) bombElegant("improper sequencing of error specifications and tracking", NULL); errcon->new_data_read = 1; } /* process namelist text */ set_namelist_processing_flags(STICKY_NAMELIST_DEFAULTS); set_print_namelist_flags(0); if (processNamelist(&error, nltext)==NAMELIST_ERROR) bombElegant(NULL, NULL); if (name==NULL) { if (!element_type) bombElegant("element name missing in error namelist", NULL); SDDS_CopyString(&name, "*"); } if (echoNamelists) print_namelist(stdout, &error); /* check for valid input and copy to errcon arrays */ if (item==NULL) bombElegant("item name missing in error namelist", NULL); if (match_string(type, known_error_type, N_ERROR_TYPES, 0)<0) bombElegant("unknown error type specified", NULL); if (bind_number<0) bombElegant("bind_number < 0", NULL); if (!additive && fractional) bombElegant("fractional errors must be additive", NULL); context = NULL; n_added = 0; i_start = n_items; context = NULL; if (after && strlen(after)) { if (!(context=find_element(after, &context, &(beamline->elem)))) { fprintf(stdout, "Element %s not found in beamline.\n", after); exitElegant(1); } sMin = context->end_pos; if (find_element(after, &context, &(beamline->elem))) { fprintf(stdout, "Element %s found in beamline more than once.\n", after); exitElegant(1); } fprintf(stdout, "%s found at s = %le m\n", after, sMin); fflush(stdout); } context = NULL; if (before && strlen(before)) { if (!(context=find_element(before, &context, &(beamline->elem)))) { fprintf(stdout, "Element %s not found in beamline.\n", before); exitElegant(1); } sMax = context->end_pos; if (find_element(before, &context, &(beamline->elem))) { fprintf(stdout, "Element %s found in beamline more than once.\n", after); exitElegant(1); } fprintf(stdout, "%s found at s = %le m\n", before, sMax); fflush(stdout); } if (after && before && sMin>sMax) { fprintf(stdout, "Element %s is not upstream of %s!\n", before, after); exitElegant(1); } if (element_type && has_wildcards(element_type) && strchr(element_type, '-')) element_type = expand_ranges(element_type); if (has_wildcards(name)) { if (strchr(name, '-')) name = expand_ranges(name); str_toupper(name); firstIndexInGroup = -1; while ((context=wfind_element(name, &context, &(beamline->elem)))) { if (element_type && !wild_match(entity_name[context->type], element_type)) continue; if (exclude && wild_match(context->name, exclude)) continue; if (i_start!=n_items && duplicate_name(errcon->name+i_start, n_items-i_start, context->name)) continue; if ((sMin>=0 && context->end_pos<sMin) || (sMax>=0 && context->end_pos>sMax)) continue; errcon->name = trealloc(errcon->name, sizeof(*errcon->name)*(n_items+1)); errcon->item = trealloc(errcon->item, sizeof(*errcon->item)*(n_items+1)); errcon->quan_name = trealloc(errcon->quan_name, sizeof(*errcon->quan_name)*(n_items+1)); errcon->quan_final_index = trealloc(errcon->quan_final_index, sizeof(*errcon->quan_final_index)*(n_items+1)); errcon->quan_unit = trealloc(errcon->quan_unit, sizeof(*errcon->quan_unit)*(n_items+1)); errcon->error_level = trealloc(errcon->error_level, sizeof(*errcon->error_level)*(n_items+1)); errcon->error_cutoff = trealloc(errcon->error_cutoff, sizeof(*errcon->error_cutoff)*(n_items+1)); errcon->error_type = trealloc(errcon->error_type, sizeof(*errcon->error_type)*(n_items+1)); errcon->elem_type = trealloc(errcon->elem_type, sizeof(*errcon->elem_type)*(n_items+1)); errcon->error_value = trealloc(errcon->error_value, sizeof(*errcon->error_value)*(n_items+1)); errcon->unperturbed_value = trealloc(errcon->unperturbed_value, sizeof(*errcon->unperturbed_value)*(n_items+1)); errcon->param_number = trealloc(errcon->param_number, sizeof(*errcon->param_number)*(n_items+1)); errcon->bind_number = trealloc(errcon->bind_number, sizeof(*errcon->bind_number)*(n_items+1)); errcon->flags = trealloc(errcon->flags, sizeof(*errcon->flags)*(n_items+1)); errcon->sMin = trealloc(errcon->sMin, sizeof(*errcon->sMin)*(n_items+1)); errcon->sMax = trealloc(errcon->sMax, sizeof(*errcon->sMax)*(n_items+1)); errcon->boundTo = trealloc(errcon->boundTo, sizeof(*errcon->boundTo)*(n_items+1)); cp_str(errcon->item+n_items, str_toupper(item)); cp_str(errcon->name+n_items, context->name); errcon->error_level[n_items] = amplitude*error_factor; errcon->error_cutoff[n_items] = cutoff; errcon->error_type[n_items] = match_string(type, known_error_type, N_ERROR_TYPES, 0); errcon->quan_name[n_items] = tmalloc(sizeof(char*)*(strlen(context->name)+strlen(item)+4)); errcon->quan_final_index[n_items] = -1; sprintf(errcon->quan_name[n_items], "d%s.%s", context->name, item); errcon->flags[n_items] = (fractional?FRACTIONAL_ERRORS:0); errcon->flags[n_items] += (bind==0?0:(bind==-1?ANTIBIND_ERRORS:BIND_ERRORS)); errcon->flags[n_items] += (post_correction?POST_CORRECTION:PRE_CORRECTION); errcon->flags[n_items] += (additive?0:NONADDITIVE_ERRORS); errcon->bind_number[n_items] = bind_number; /* boundTo must be -1 when there is no cross-name binding or when this element is the * first of a series */ errcon->boundTo[n_items] = bind_across_names?firstIndexInGroup:-1; /* if (errcon->boundTo[n_items]!=-1) fprintf(stdout, "%s bound to %s\n", errcon->name[n_items], errcon->name[errcon->boundTo[n_items]]); */ errcon->sMin[n_items] = sMin; errcon->sMax[n_items] = sMax; errcon->elem_type[n_items] = context->type; if ((errcon->param_number[n_items] = confirm_parameter(item, context->type))<0) { fprintf(stdout, "error: cannot vary %s--no such parameter for %s (wildcard name: %s)\n",item, context->name, name); fflush(stdout); exitElegant(1); } cp_str(&errcon->quan_unit[n_items], errcon->flags[n_items]&FRACTIONAL_ERRORS?"fr": entity_description[errcon->elem_type[n_items]].parameter[errcon->param_number[n_items]].unit ); errcon->unperturbed_value[n_items] = parameter_value(errcon->name[n_items], errcon->elem_type[n_items], errcon->param_number[n_items], beamline); if (errcon->unperturbed_value[n_items]==0 && errcon->flags[n_items]&FRACTIONAL_ERRORS) fprintf(stdout, "***\7\7\7 warning: you've specified fractional errors for %s.%s, but the unperturbed value is zero.\nThis may be an error.\n", errcon->name[n_items], errcon->item[n_items]); fflush(stdout); if (duplicate_name(errcon->quan_name, n_items, errcon->quan_name[n_items])) fprintf(stdout, "***\7\7\7 warning: you've specified errors for %s.%s more than once!\n", errcon->name[n_items], errcon->item[n_items]); fflush(stdout); errcon->n_items = ++n_items; n_added++; if (firstIndexInGroup==-1) firstIndexInGroup = n_items-1; } } else { str_toupper(name); if (!(context=find_element(name, &context, &(beamline->elem)))) { fprintf(stdout, "error: cannot add errors to element %s--not in beamline\n", name); fflush(stdout); exitElegant(1); } errcon->name = trealloc(errcon->name, sizeof(*errcon->name)*(n_items+1)); errcon->item = trealloc(errcon->item, sizeof(*errcon->item)*(n_items+1)); errcon->quan_name = trealloc(errcon->quan_name, sizeof(*errcon->quan_name)*(n_items+1)); errcon->quan_final_index = trealloc(errcon->quan_final_index, sizeof(*errcon->quan_final_index)*(n_items+1)); errcon->quan_unit = trealloc(errcon->quan_unit, sizeof(*errcon->quan_unit)*(n_items+1)); errcon->error_level = trealloc(errcon->error_level, sizeof(*errcon->error_level)*(n_items+1)); errcon->error_cutoff = trealloc(errcon->error_cutoff, sizeof(*errcon->error_cutoff)*(n_items+1)); errcon->error_type = trealloc(errcon->error_type, sizeof(*errcon->error_type)*(n_items+1)); errcon->elem_type = trealloc(errcon->elem_type, sizeof(*errcon->elem_type)*(n_items+1)); errcon->error_value = trealloc(errcon->error_value, sizeof(*errcon->error_value)*(n_items+1)); errcon->unperturbed_value = trealloc(errcon->unperturbed_value, sizeof(*errcon->unperturbed_value)*(n_items+1)); errcon->param_number = trealloc(errcon->param_number, sizeof(*errcon->param_number)*(n_items+1)); errcon->bind_number = trealloc(errcon->bind_number, sizeof(*errcon->bind_number)*(n_items+1)); errcon->flags = trealloc(errcon->flags, sizeof(*errcon->flags)*(n_items+1)); errcon->sMin = trealloc(errcon->sMin, sizeof(*errcon->sMin)*(n_items+1)); errcon->sMax = trealloc(errcon->sMax, sizeof(*errcon->sMax)*(n_items+1)); errcon->boundTo = trealloc(errcon->boundTo, sizeof(*errcon->boundTo)*(n_items+1)); cp_str(errcon->item+n_items, str_toupper(item)); cp_str(errcon->name+n_items, context->name); errcon->error_level[n_items] = amplitude; errcon->error_cutoff[n_items] = cutoff; errcon->error_type[n_items] = match_string(type, known_error_type, N_ERROR_TYPES, 0); errcon->quan_name[n_items] = tmalloc(sizeof(char*)*(strlen(context->name)+strlen(item)+4)); sprintf(errcon->quan_name[n_items], "d%s.%s", context->name, item); errcon->flags[n_items] = (fractional?FRACTIONAL_ERRORS:0); errcon->flags[n_items] += (bind==0?0:(bind==-1?ANTIBIND_ERRORS:BIND_ERRORS)); errcon->flags[n_items] += (post_correction?POST_CORRECTION:PRE_CORRECTION); errcon->flags[n_items] += (additive?0:NONADDITIVE_ERRORS); errcon->bind_number[n_items] = bind_number; errcon->sMin[n_items] = sMin; errcon->sMax[n_items] = sMax; errcon->boundTo[n_items] = -1; /* not used when there are no wildcards */ errcon->elem_type[n_items] = context->type; if ((errcon->param_number[n_items] = confirm_parameter(item, context->type))<0) { fprintf(stdout, "error: cannot vary %s--no such parameter for %s\n",item, name); fflush(stdout); exitElegant(1); } cp_str(&errcon->quan_unit[n_items], errcon->flags[n_items]&FRACTIONAL_ERRORS?"fr": entity_description[errcon->elem_type[n_items]].parameter[errcon->param_number[n_items]].unit ); errcon->unperturbed_value[n_items] = parameter_value(errcon->name[n_items], errcon->elem_type[n_items], errcon->param_number[n_items], beamline); if (errcon->unperturbed_value[n_items]==0 && errcon->flags[n_items]&FRACTIONAL_ERRORS) fprintf(stdout, "***\7\7\7 warning: you've specified fractional errors for %s.%s, but the unperturbed value is zero.\nThis may be an error.\n", errcon->name[n_items], errcon->item[n_items]); fflush(stdout); if (duplicate_name(errcon->quan_name, n_items, errcon->quan_name[n_items])) fprintf(stdout, "***\7\7\7 warning: you've specified errors for %s.%s more than once!\n", errcon->name[n_items], errcon->item[n_items]); fflush(stdout); errcon->n_items = ++n_items; n_added++; } if (!n_added && !allow_missing_elements) { fprintf(stdout, "error: no match for name %s\n", name); fflush(stdout); exitElegant(1); } log_exit("add_error_element"); }
int main(int argc, char *argv[]) { char *user = NULL; char *input = NULL; char *output = NULL; int input_location = 0; bool multiple_input = false; bool one_passed = false; bool output_is_dir = false; struct config cfg; int c; while((c=getopt(argc, argv, "hdu:")) != -1) { switch(c) { case 'u': user = optarg; break; case 'h': usage(argv[0]); exit(NO_ERROR); case 'd': _debug = 1; break; case '?': if(strchr("u", optopt) == NULL) fprintf(stderr, "Unknown option -%c encountered\n", optopt); else fprintf(stderr, "Option -%c requires an argument\n", optopt); exit(USAGE_ERROR); default: abort(); } } debug("optind: %d, argc: %d", optind, argc); if(optind + 2 == argc) { input = argv[optind]; output = argv[optind + 1]; multiple_input = false; } else if (optind + 2 < argc) { input_location = optind; output = argv[argc - 1]; multiple_input = true; } else { fprintf(stderr, "Invalid number of arguments\n"); usage(argv[0]); exit(USAGE_ERROR); } if(user == NULL) { fprintf(stderr, "Error, user must be supplied with -u <user> argument\n" "\tRerun with -h to see usage-details\n"); exit(USAGE_ERROR); } /* All of these functions exit the program unless everything is A-OK */ read_config(CONFIG_PATH, &cfg); /* If the user running the program doesn't match the config-file, exit */ die_unless_user(cfg.required_user); /* Try to become target user iff. they are a member of the right group */ if_valid_become(user, cfg.required_group); /* Append a '/' if the output is a dir and it doesn't have one */ output = dirify_output(output, &output_is_dir); debug("output now: %s (%d)", output, output_is_dir); /* If the output path isn't in the list of allowed outputs, exit */ validate_output(output, cfg.allowed_paths); destroy_config(&cfg); /* Do the actual copy, failing on any error condition */ if(!multiple_input) { copy_file(input, output); } else { struct stat sb; char *p; int i; if(!output_is_dir) log_exit(USAGE_ERROR, "Error: destination %s must be a directory", output); debug("Multiple inputs, %d files to %s", argc - input_location - 1, output); for(i = input_location; i < argc - 1; i++) { p = argv[i]; if(stat(p, &sb) < 0) log_exit_perror(FILEPERM_ERROR, "stat input: %s", p); switch(sb.st_mode & S_IFMT) { case S_IFREG: debug("************** Copy file %d ****************", i - input_location + 1); copy_file(p, output); one_passed = true; break; case S_IFDIR: log_msg("%s is a directory, skipping", p); break; default: log_msg("%s is not a regular file, skip", p); break; } } } /* changed by dirify() above */ free(output); if(multiple_input && !one_passed) { debug("No files copied, exit NO_ACTION_ERROR"); return NO_ACTION_ERROR; } else { debug("Finished, exit OK"); return NO_ERROR; } }
void tilt_matrices(VMATRIX *M, double tilt) { static VMATRIX Rot, IRot; static long initialized=0; VMATRIX Mr; double sin_tilt, cos_tilt; log_entry("tilt_matrices"); if (tilt==0) { log_exit("tilt_matrices"); return; } if (!initialized) { initialized = 1; initialize_matrices(&Rot, 1); initialize_matrices(&IRot, 1); } initialize_matrices(&Mr, M->order); if (fabs(tilt-PI/2)<1e-12) { sin_tilt = 1; cos_tilt = 0; } else if (fabs(tilt+PI/2)<1e-12) { sin_tilt = -1; cos_tilt = 0; } else if (fabs(tilt-PI)<1e-12 || fabs(tilt+PI)<1e-12) { sin_tilt = 0; cos_tilt = -1; } else { sin_tilt = sin(tilt); cos_tilt = cos(tilt); } /* Rotation matrix for (x, y) */ IRot.R[0][0] = Rot.R[0][0] = cos_tilt ; IRot.R[0][2] = -(Rot.R[0][2] = sin_tilt); IRot.R[2][0] = -(Rot.R[2][0] = -sin_tilt); IRot.R[2][2] = Rot.R[2][2] = cos_tilt ; /* Rotation matrix for (x', y') */ IRot.R[1][1] = Rot.R[1][1] = cos_tilt ; IRot.R[1][3] = -(Rot.R[1][3] = sin_tilt); IRot.R[3][1] = -(Rot.R[3][1] = -sin_tilt); IRot.R[3][3] = Rot.R[3][3] = cos_tilt ; IRot.R[4][4] = IRot.R[5][5] = Rot.R[4][4] = Rot.R[5][5] = 1; concat_matrices(&Mr, M, &Rot, 0); concat_matrices(M , &IRot, &Mr , 0); free_matrices(&Mr); log_exit("tilt_matrices"); }
/** * Eventually calls exit(), passing @p code, therefore does not return. * * @param code one of the RERR_* codes from errcode.h. **/ NORETURN void _exit_cleanup(int code, const char *file, int line) { static int switch_step = 0; static int exit_code = 0, exit_line = 0; static const char *exit_file = NULL; static int unmodified_code = 0; SIGACTION(SIGUSR1, SIG_IGN); SIGACTION(SIGUSR2, SIG_IGN); if (exit_code) { /* Preserve first exit info when recursing. */ code = exit_code; file = exit_file; line = exit_line; } /* If this is the exit at the end of the run, the server side * should not attempt to output a message (see log_exit()). */ if (am_server && code == 0) am_server = 2; /* Some of our actions might cause a recursive call back here, so we * keep track of where we are in the cleanup and never repeat a step. */ switch (switch_step) { #include "case_N.h" /* case 0: */ switch_step++; exit_code = unmodified_code = code; exit_file = file; exit_line = line; if (verbose > 3) { rprintf(FINFO, "[%s] _exit_cleanup(code=%d, file=%s, line=%d): entered\n", who_am_i(), code, file, line); } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (cleanup_child_pid != -1) { int status; int pid = wait_process(cleanup_child_pid, &status, WNOHANG); if (pid == cleanup_child_pid) { status = WEXITSTATUS(status); if (status > code) code = exit_code = status; } } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (cleanup_got_literal && cleanup_fname && cleanup_new_fname && keep_partial && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) { const char *fname = cleanup_fname; cleanup_fname = NULL; if (cleanup_fd_r != -1) bpc_close(cleanup_fd_r); if (cleanup_fd_w != -1) { flush_write_file(cleanup_fd_w); bpc_close(cleanup_fd_w); } finish_transfer(cleanup_new_fname, fname, NULL, NULL, cleanup_file, 0, !partial_dir); } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (!code || am_server || am_receiver) io_flush(FULL_FLUSH); /* FALLTHROUGH */ #include "case_N.h" switch_step++; bpc_sysCall_cleanup(); if (cleanup_fname) do_unlink(cleanup_fname); if (code) kill_all(SIGUSR1); if (cleanup_pid && cleanup_pid == getpid()) { char *pidf = lp_pid_file(); if (pidf && *pidf) unlink(lp_pid_file()); } if (code == 0) { if (io_error & IOERR_DEL_LIMIT) code = exit_code = RERR_DEL_LIMIT; if (io_error & IOERR_VANISHED) code = exit_code = RERR_VANISHED; if (io_error & IOERR_GENERAL || got_xfer_error) code = exit_code = RERR_PARTIAL; } if (code || am_daemon || (logfile_name && (am_server || !verbose))) log_exit(code, file, line); /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (verbose > 2) { rprintf(FINFO, "[%s] _exit_cleanup(code=%d, file=%s, line=%d): " "about to call exit(%d)\n", who_am_i(), unmodified_code, file, line, code); } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (am_server && code) msleep(100); close_all(); /* FALLTHROUGH */ default: break; } exit(code); }
void output_magnets(char *filename, char *line_name, LINE_LIST *beamline) { ELEMENT_LIST *eptr; QUAD *qptr; BEND *bptr; KQUSE *kqsptr; KQUAD *kqptr; KSBEND *kbptr; CSBEND *cbptr; CSRCSBEND *csrbptr; long iPhase; double start, end, total_length, dz, value; FILE *fpm; log_entry("output_magnets"); total_length = 0; eptr = &(beamline->elem); fpm = fopen_e(filename, "w", 0); start = end = 0; fprintf(fpm, "SDDS1\n&description text=\"magnet layout for beamline %s\" &end\n", line_name); fprintf(fpm, "&column name=ElementName, type=string &end\n"); fprintf(fpm, "&column name=ElementType, type=string &end\n"); fprintf(fpm, "&column name=s, units=m, type=double &end\n&column name=Profile, type=double &end\n"); fprintf(fpm, "&data mode=ascii, no_row_counts=1 &end\n"); eptr = &(beamline->elem); fprintf(fpm, "_BEGIN_ MARK 0 0\n"); while (eptr) { switch (eptr->type) { case T_QUAD: qptr = (QUAD*)eptr->p_elem; fprintf(fpm, "%s %s %e %d\n", eptr->name, entity_name[eptr->type], start, SIGN(qptr->k1)); end = start+qptr->length; fprintf(fpm, "%s %s %e %d\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], end, SIGN(qptr->k1), eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); start = end; break; case T_KQUAD: kqptr = (KQUAD*)eptr->p_elem; if (kqptr->bore) value = kqptr->B; else value = kqptr->k1; fprintf(fpm, "%s %s %e %d\n", eptr->name, entity_name[eptr->type], start, SIGN(value)); end = start+kqptr->length; fprintf(fpm, "%s %s %e %d\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], end, SIGN(kqptr->k1), eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); start = end; break; case T_KQUSE: kqsptr = (KQUSE*)eptr->p_elem; value = kqsptr->k1; fprintf(fpm, "%s %s %e %d\n", eptr->name, entity_name[eptr->type], start, SIGN(value)); end = start+kqsptr->length; fprintf(fpm, "%s %s %e %d\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], end, SIGN(value), eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); start = end; break; case T_RBEN: case T_SBEN: bptr = (BEND*)eptr->p_elem; end = start+bptr->length; if (bptr->angle>0) fprintf(fpm, "%s %s %e .33333333\n%s %s %e .33333333\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); else if (bptr->angle<0) fprintf(fpm, "%s %s %e -.33333333\n%s %s %e -.33333333\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); start = end; break; case T_KSBEND: kbptr = (KSBEND*)eptr->p_elem; end = start+kbptr->length; if (kbptr->angle>0) fprintf(fpm, "%s %s %e .33333333\n%s %s %e .33333333\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); else if (kbptr->angle<0) fprintf(fpm, "%s %s %e -.33333333\n%s %s %e -.33333333\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); start = end; break; case T_SEXT: end = start+((SEXT*)eptr->p_elem)->length; fprintf(fpm, "%s %s %e .5\n%s %s %e .5\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); start = end; break; case T_KSEXT: end = start+((KSEXT*)eptr->p_elem)->length; fprintf(fpm, "%s %s %e .5\n%s %s %e .5\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); start = end; break; case T_HCOR: end = start+((HCOR*)eptr->p_elem)->length; fprintf(fpm, "%s %s %e .25\n%s %s %e .25\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end); start = end; break; case T_VCOR: end = start+((VCOR*)eptr->p_elem)->length; fprintf(fpm, "%s %s %e -.25\n%s %s %e -.25\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end); start = end; break; case T_HVCOR: end = start+((HVCOR*)eptr->p_elem)->length; fprintf(fpm, "%s %s %e .25\n%s %s %e -.25\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end); start = end; break; case T_DRIF: start = (end = start+((DRIFT*)eptr->p_elem)->length); fprintf(fpm, "%s %s %e 0\n", eptr->name, entity_name[eptr->type], end); break; case T_HMON: dz = ((HMON*)eptr->p_elem)->length/2; fprintf(fpm, "%s %s %e 0.125\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start+dz, eptr->name, entity_name[eptr->type], start+2*dz, eptr->name, entity_name[eptr->type], start+dz, eptr->name, entity_name[eptr->type], start+2*dz); start += 2*dz; break; case T_VMON: dz = ((VMON*)eptr->p_elem)->length/2; fprintf(fpm, "%s %s %e -0.125\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start+dz, eptr->name, entity_name[eptr->type], start+2*dz, eptr->name, entity_name[eptr->type], start+dz, eptr->name, entity_name[eptr->type], start+2*dz); start += 2*dz; break; case T_MONI: dz = ((MONI*)eptr->p_elem)->length/2; fprintf(fpm, "%s %s %e 0.125\n%s %s %e 0\n%s %s %e -0.125\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start+dz, eptr->name, entity_name[eptr->type], start+2*dz, eptr->name, entity_name[eptr->type], start+dz, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], start+2*dz); start += 2*dz; break; case T_MULT: dz = ((MULT*)eptr->p_elem)->length/3; fprintf(fpm, "%s %s %e 0.6666\n%s %s %e 0.6666\n%s %s %e 0\n%s %s %e -0.6666\n%s %s %e -0.6666\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start+dz, eptr->name, entity_name[eptr->type], start+2*dz, eptr->name, entity_name[eptr->type], start+3*dz, eptr->name, entity_name[eptr->type], start+2*dz, eptr->name, entity_name[eptr->type], start+dz, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], start+3*dz); start += 3*dz; break; case T_MARK: /* zero-length drift */ break; case T_CSBEND: cbptr = (CSBEND*)eptr->p_elem; end = start+cbptr->length; if (cbptr->angle>0) fprintf(fpm, "%s %s %e .33333333\n%s %s %e .33333333\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); else if (cbptr->angle<0) fprintf(fpm, "%s %s %e -.33333333\n%s %s %e -.33333333\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); start = end; break; case T_CSRCSBEND: csrbptr = (CSRCSBEND*)eptr->p_elem; end = start+csrbptr->length; if (csrbptr->angle>0) fprintf(fpm, "%s %s %e .33333333\n%s %s %e .33333333\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); else if (csrbptr->angle<0) fprintf(fpm, "%s %s %e -.33333333\n%s %s %e -.33333333\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n%s %s %e 0\n", eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], end, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], start, eptr->name, entity_name[eptr->type], end); start = end; break; case T_RFCA: case T_TWLA: case T_RAMPRF: case T_RFCW: case T_MODRF: dz = ((DRIFT*)eptr->p_elem)->length; dz /= 8; for (iPhase=0; iPhase<9; iPhase++) { fprintf(fpm, "%s %s %e %e\n", eptr->name, entity_name[eptr->type], start+dz*iPhase, 0.5*sin((iPhase/8.0)*PIx2)); } start += dz*8; break; case T_MATR: dz = ((MATR*)eptr->p_elem)->length; fprintf(fpm, "%s %s %e %e\n", eptr->name, entity_name[eptr->type], start, 0.5); fprintf(fpm, "%s %s %e %e\n", eptr->name, entity_name[eptr->type], start+dz, -0.5); fprintf(fpm, "%s %s %e %e\n", eptr->name, entity_name[eptr->type], start+dz, 0.5); fprintf(fpm, "%s %s %e %e\n", eptr->name, entity_name[eptr->type], start, -0.5); fprintf(fpm, "%s %s %e %e\n", eptr->name, entity_name[eptr->type], start, 0.0); fprintf(fpm, "%s %s %e %e\n", eptr->name, entity_name[eptr->type], start+dz, 0.0); start += dz; break; default: if (entity_description[eptr->type].flags&HAS_LENGTH) { dz = ((DRIFT*)eptr->p_elem)->length; fprintf(fpm, "%s %s %e 0\n", eptr->name, entity_name[eptr->type], start+=dz); } break; } eptr = eptr->succ; } log_exit("output_magnets"); fclose(fpm); }
/** * Eventually calls exit(), passing @p code, therefore does not return. * * @param code one of the RERR_* codes from errcode.h. **/ NORETURN void _exit_cleanup(int code, const char *file, int line) { static int switch_step = 0; static int exit_code = 0, exit_line = 0; static const char *exit_file = NULL; static int first_code = 0; SIGACTION(SIGUSR1, SIG_IGN); SIGACTION(SIGUSR2, SIG_IGN); if (!exit_code) { /* Preserve first error exit info when recursing. */ exit_code = code; exit_file = file; exit_line = line < 0 ? -line : line; } /* If this is the exit at the end of the run, the server side * should not attempt to output a message (see log_exit()). */ if (am_server && code == 0) am_server = 2; /* Some of our actions might cause a recursive call back here, so we * keep track of where we are in the cleanup and never repeat a step. */ switch (switch_step) { #include "case_N.h" /* case 0: */ switch_step++; first_code = code; if (output_needs_newline) { fputc('\n', stdout); output_needs_newline = 0; } if (DEBUG_GTE(EXIT, 2)) { rprintf(FINFO, "[%s] _exit_cleanup(code=%d, file=%s, line=%d): entered\n", who_am_i(), code, file, line); } #include "case_N.h" switch_step++; if (cleanup_child_pid != -1) { int status; int pid = wait_process(cleanup_child_pid, &status, WNOHANG); if (pid == cleanup_child_pid) { status = WEXITSTATUS(status); if (status > exit_code) exit_code = status; } } #include "case_N.h" switch_step++; if (cleanup_got_literal && (cleanup_fname || cleanup_fd_w != -1)) { if (cleanup_fd_r != -1) { close(cleanup_fd_r); cleanup_fd_r = -1; } if (cleanup_fd_w != -1) { flush_write_file(cleanup_fd_w); close(cleanup_fd_w); cleanup_fd_w = -1; } if (cleanup_fname && cleanup_new_fname && keep_partial && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) { int tweak_modtime = 0; const char *fname = cleanup_fname; cleanup_fname = NULL; if (!partial_dir) { /* We don't want to leave a partial file with a modern time or it * could be skipped via --update. Setting the time to something * really old also helps it to stand out as unfinished in an ls. */ tweak_modtime = 1; cleanup_file->modtime = 0; } finish_transfer(cleanup_new_fname, fname, NULL, NULL, cleanup_file, tweak_modtime, !partial_dir); } } #include "case_N.h" switch_step++; if (flush_ok_after_signal) { flush_ok_after_signal = False; if (code == RERR_SIGNAL) io_flush(FULL_FLUSH); } if (!exit_code && !code) io_flush(FULL_FLUSH); #include "case_N.h" switch_step++; if (cleanup_fname) do_unlink(cleanup_fname); if (exit_code) kill_all(SIGUSR1); if (cleanup_pid && cleanup_pid == getpid()) { char *pidf = lp_pid_file(); if (pidf && *pidf) unlink(lp_pid_file()); } if (exit_code == 0) { if (code) exit_code = code; if (io_error & IOERR_DEL_LIMIT) exit_code = RERR_DEL_LIMIT; if (io_error & IOERR_VANISHED) exit_code = RERR_VANISHED; if (io_error & IOERR_GENERAL || got_xfer_error) exit_code = RERR_PARTIAL; } /* If line < 0, this exit is after a MSG_ERROR_EXIT event, so * we don't want to output a duplicate error. */ if ((exit_code && line > 0) || am_daemon || (logfile_name && (am_server || !INFO_GTE(STATS, 1)))) log_exit(exit_code, exit_file, exit_line); #include "case_N.h" switch_step++; if (DEBUG_GTE(EXIT, 1)) { rprintf(FINFO, "[%s] _exit_cleanup(code=%d, file=%s, line=%d): " "about to call exit(%d)%s\n", who_am_i(), first_code, exit_file, exit_line, exit_code, dry_run ? " (DRY RUN)" : ""); } #include "case_N.h" switch_step++; if (exit_code && exit_code != RERR_SOCKETIO && exit_code != RERR_STREAMIO && exit_code != RERR_SIGNAL1 && exit_code != RERR_TIMEOUT && !shutting_down && (protocol_version >= 31 || am_receiver)) { if (line > 0) { if (DEBUG_GTE(EXIT, 3)) { rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT with exit_code %d\n", who_am_i(), exit_code); } send_msg_int(MSG_ERROR_EXIT, exit_code); } noop_io_until_death(); } #include "case_N.h" switch_step++; if (am_server && exit_code) msleep(100); close_all(); /* FALLTHROUGH */ default: break; } exit(exit_code); }
int main (int argc, char **argv) { int s = -1; int fd = -1; Arguments arg; Tunnel *tunnel; int closed; parse_arguments (argc, argv, &arg); if ((debug_level == 0 || debug_file != NULL) && arg.use_daemon) daemon (0, 1); #ifdef DEBUG_MODE if (debug_level != 0 && debug_file == NULL) debug_file = stderr; #else openlog ("htc", LOG_PID, LOG_DAEMON); #endif log_notice ("htc (%s) %s started with arguments:", PACKAGE, VERSION); log_notice (" me = %s", arg.me); log_notice (" device = %s", arg.device ? arg.device : "(null)"); log_notice (" host_name = %s", arg.host_name ? arg.host_name : "(null)"); log_notice (" host_port = %d", arg.host_port); log_notice (" proxy_name = %s", arg.proxy_name ? arg.proxy_name : "(null)"); log_notice (" proxy_port = %d", arg.proxy_port); log_notice (" proxy_buffer_size = %d", arg.proxy_buffer_size); log_notice (" proxy_buffer_timeout = %d", arg.proxy_buffer_timeout); log_notice (" content_length = %d", arg.content_length); log_notice (" forward_port = %d", arg.forward_port); log_notice (" max_connection_age = %d", arg.max_connection_age); log_notice (" use_std = %d", arg.use_std); log_notice (" strict_content_length = %d", arg.strict_content_length); log_notice (" keep_alive = %d", arg.keep_alive); log_notice (" proxy_authorization = %s", arg.proxy_authorization ? arg.proxy_authorization : "(null)"); log_notice (" user_agent = %s", arg.user_agent ? arg.user_agent : "(null)"); log_notice (" debug_level = %d", debug_level); if (arg.forward_port != -1) { struct in_addr addr; addr.s_addr = INADDR_ANY; s = server_socket (addr, arg.forward_port, 0); log_debug ("server_socket (%d) = %d", arg.forward_port, s); if (s == -1) { log_error ("couldn't create server socket: %s", strerror (errno)); log_exit (1); } } #ifdef DEBUG_MODE signal (SIGPIPE, log_sigpipe); #else signal (SIGPIPE, SIG_IGN); #endif for (;;) { time_t last_tunnel_write; if (arg.device) { fd = open_device (arg.device); log_debug ("open_device (\"%s\") = %d", arg.device, fd); if (fd == -1) { log_error ("couldn't open %s: %s", arg.device, strerror (errno)); log_exit (1); } /* Check that fd is not 0 (clash with --stdin-stdout) */ if (fd == 0) { log_notice("changing fd from %d to 3",fd); if (dup2 (fd, 3) != 3) { log_error ("couldn't dup2 (%d, 3): %s",fd,strerror(errno)); log_exit (1); } } } else if (arg.forward_port != -1) { log_debug ("waiting for connection on port %d", arg.forward_port); fd = wait_for_connection_on_socket (s); log_debug ("wait_for_connection_on_socket (%d) = %d", s, fd); if (fd == -1) { log_error ("couldn't forward port %d: %s", arg.forward_port, strerror (errno)); log_exit (1); } /* Check that fd is not 0 (clash with --stdin-stdout) */ if (fd == 0) { log_notice ("changing fd from %d to 3",fd); if (dup2 (fd, 3) != 3) { log_error ("couldn't dup2 (%d, 3): %s",fd,strerror(errno)); log_exit (1); } } } else if (arg.use_std) { log_debug ("using stdin as fd"); fd = 0; if (fcntl(fd,F_SETFL,O_NONBLOCK)==-1) { log_error ("couldn't set stdin to non-blocking mode: %s", strerror(errno)); log_exit (1); } /* Usage of stdout (fd = 1) is checked later. */ } log_debug ("creating a new tunnel"); tunnel = tunnel_new_client (arg.host_name, arg.host_port, arg.proxy_name, arg.proxy_port, arg.content_length); if (tunnel == NULL) { log_error ("couldn't create tunnel"); log_exit (1); } if (tunnel_setopt (tunnel, "strict_content_length", &arg.strict_content_length) == -1) log_debug ("tunnel_setopt strict_content_length error: %s", strerror (errno)); if (tunnel_setopt (tunnel, "keep_alive", &arg.keep_alive) == -1) log_debug ("tunnel_setopt keep_alive error: %s", strerror (errno)); if (tunnel_setopt (tunnel, "max_connection_age", &arg.max_connection_age) == -1) log_debug ("tunnel_setopt max_connection_age error: %s", strerror (errno)); if (arg.proxy_authorization != NULL) { ssize_t len; char *auth; len = encode_base64 (arg.proxy_authorization, strlen (arg.proxy_authorization), &auth); if (len == -1) { log_error ("encode_base64 error: %s", strerror (errno)); } else { char *str = malloc (len + 7); if (str == NULL) { log_error ("out of memory when encoding " "authorization string"); log_exit (1); } strcpy (str, "Basic "); strcat (str, auth); free (auth); if (tunnel_setopt (tunnel, "proxy_authorization", str) == -1) log_error ("tunnel_setopt proxy_authorization error: %s", strerror (errno)); free (str); } } if (arg.user_agent != NULL) { if (tunnel_setopt (tunnel, "user_agent", arg.user_agent) == -1) log_error ("tunnel_setopt user_agent error: %s", strerror (errno)); } if (tunnel_connect (tunnel) == -1) { log_error ("couldn't open tunnel: %s", strerror (errno)); log_exit (1); } if (arg.proxy_name) log_notice ("connected to %s:%d via %s:%d", arg.host_name, arg.host_port, arg.proxy_name, arg.proxy_port); else log_notice ("connected to %s:%d", arg.host_name, arg.host_port); closed = FALSE; time (&last_tunnel_write); while (!closed) { struct pollfd pollfd[2]; int keep_alive_timeout; int timeout; time_t t; int n; pollfd[0].fd = fd; pollfd[0].events = POLLIN; pollfd[1].fd = tunnel_pollin_fd (tunnel); pollfd[1].events = POLLIN; time (&t); timeout = 1000 * (arg.keep_alive - (t - last_tunnel_write)); keep_alive_timeout = TRUE; if (timeout < 0) timeout = 0; if (arg.proxy_buffer_timeout != -1 && arg.proxy_buffer_timeout < timeout) { timeout = arg.proxy_buffer_timeout; keep_alive_timeout = FALSE; } log_annoying ("poll () ..."); n = poll (pollfd, 2, timeout); log_annoying ("... = %d", n); if (n == -1) { log_error ("poll error: %s", strerror (errno)); log_exit (1); } else if (n == 0) { log_verbose ("poll() timed out"); if (keep_alive_timeout) { tunnel_padding (tunnel, 1); time (&last_tunnel_write); } else { if (tunnel_maybe_pad (tunnel, arg.proxy_buffer_size) > 0) time (&last_tunnel_write); } continue; } handle_input ("device or port", tunnel, fd, pollfd[0].revents, handle_device_input, &closed); handle_input ("tunnel", tunnel, fd, pollfd[1].revents, handle_tunnel_input, &closed); if (pollfd[0].revents & POLLIN) time (&last_tunnel_write); } log_debug ("destroying tunnel"); if (fd != 0) { close (fd); } tunnel_destroy (tunnel); if (arg.proxy_name) log_notice ("disconnected from %s:%d via %s:%d", arg.host_name, arg.host_port, arg.proxy_name, arg.proxy_port); else log_notice ("disconnected from %s%d", arg.host_name, arg.host_port); } log_debug ("closing server socket"); close (s); log_exit (0); }
void dump_IBScatter(SDDS_TABLE *SDDS_table, IBSCATTER *IBS, long pass) { long i, islice; double gamma; log_entry("dump_IBScatter"); if (!IBS->elements) return; gamma = sqrt(ipow(IBS->pCentral[IBS->elements-1], 2)+1.); for (islice=0; islice<IBS->nslice; islice++) { if (!SDDS_StartTable(SDDS_table, IBS->elements)) { SDDS_SetError("Problem starting SDDS table (dump_IBScatter)"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } for (i=0; i<IBS->elements; i++) { if (!SDDS_SetRowValues(SDDS_table, SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE, i, 0, IBS->name[i], 1, IBS->s[i], 2, IBS->xRateVsS[islice][i], 3, IBS->yRateVsS[islice][i], 4, IBS->zRateVsS[islice][i], 5, IBS->betax[islice][i], 6, IBS->alphax[islice][i], 7, IBS->etax[i], 8, IBS->etaxp[i], 9, IBS->betay[islice][i], 10, IBS->alphay[islice][i], 11, IBS->etay[i], 12, IBS->etayp[i], -1)) { SDDS_SetError("Problem setting SDDS row values (dump_IBScatter)"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if ((!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "Particles", fabs(IBS->icharge[islice]/particleCharge), NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "Charge", IBS->icharge[islice]*1e9, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "TotalSlice", IBS->nslice, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "NSlice", islice+1, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "s", IBS->s[IBS->elements-1], NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "Pass", pass, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "StartPoint", IBS->offset, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "Elements", IBS->elements, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "ds", IBS->revolutionLength, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "dt", IBS->dT, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "xGrowthRate", IBS->xGrowthRate[islice], NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "yGrowthRate", IBS->yGrowthRate[islice], NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "zGrowthRate", IBS->zGrowthRate[islice], NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "enx0", IBS->emitx0[islice]*gamma, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "eny0", IBS->emity0[islice]*gamma, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "emitx0", IBS->emitx0[islice], NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "emity0", IBS->emity0[islice], NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "sigmaDelta0", IBS->sigmaDelta0[islice], NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "sigmaz0", IBS->sigmaz0[islice], NULL))){ SDDS_SetError("Problem setting SDDS parameters (dump_IBScatter)"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!IBS->isRing) { if ((!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "emitx", IBS->emitx[islice], NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "emity", IBS->emity[islice], NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "enx", IBS->emitx[islice]*gamma, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "eny", IBS->emity[islice]*gamma, NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "sigmaDelta", IBS->sigmaDelta[islice], NULL))|| (!SDDS_SetParameters(SDDS_table, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "sigmaz", IBS->sigmaz[islice], NULL))){ SDDS_SetError("Problem setting SDDS parameters (dump_IBScatter)"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (!SDDS_WriteTable(SDDS_table)) { SDDS_SetError("Problem writing SDDS table (dump_IBScatter)"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } SDDS_UpdatePage(SDDS_table, 0); if (!inhibitFileSync) SDDS_DoFSync(SDDS_table); } log_exit("dump_IBScatter"); }
void parse_vm_arguments2(Global_Env *p_env) { bool version_printed = false; #ifdef _DEBUG TRACE("p_env->vm_arguments.nOptions = " << p_env->vm_arguments.nOptions); for (int _i = 0; _i < p_env->vm_arguments.nOptions; _i++) TRACE("p_env->vm_arguments.options[ " << _i << "] = " << p_env->vm_arguments.options[_i].optionString); #endif //_DEBUG apr_pool_t *pool; apr_pool_create(&pool, 0); for (int i = 0; i < p_env->vm_arguments.nOptions; i++) { const char* option = p_env->vm_arguments.options[i].optionString; if (begins_with(option, XBOOTCLASSPATH)) { /* * Override for bootclasspath - * set in the environment- the boot classloader will be responsible for * processing and setting up "vm.boot.class.path" and "sun.boot.class.path" * Note that in the case of multiple arguments, the last one will be used */ p_env->VmProperties()->set(XBOOTCLASSPATH, option + strlen(XBOOTCLASSPATH)); } else if (begins_with(option, XBOOTCLASSPATH_A)) { /* * addition to append to boot classpath * set in environment - responsibility of boot classloader to process * Note that we accumulate if multiple, appending each time */ char *bcp_old = p_env->VmProperties()->get(XBOOTCLASSPATH_A); const char *value = option + strlen(XBOOTCLASSPATH_A); char *bcp_new = NULL; if (bcp_old) { char *tmp = (char *) STD_MALLOC(strlen(bcp_old) + strlen(PORT_PATH_SEPARATOR_STR) + strlen(value) + 1); strcpy(tmp, bcp_old); strcat(tmp, PORT_PATH_SEPARATOR_STR); strcat(tmp, value); bcp_new = tmp; } p_env->VmProperties()->set(XBOOTCLASSPATH_A, bcp_old ? bcp_new : value); p_env->VmProperties()->destroy(bcp_old); STD_FREE((void*)bcp_new); } else if (begins_with(option, XBOOTCLASSPATH_P)) { /* * addition to prepend to boot classpath * set in environment - responsibility of boot classloader to process * Note that we accumulate if multiple, prepending each time */ char *bcp_old = p_env->VmProperties()->get(XBOOTCLASSPATH_P); const char *value = option + strlen(XBOOTCLASSPATH_P); char *bcp_new = NULL; if (bcp_old) { char *tmp = (char *) STD_MALLOC(strlen(bcp_old) + strlen(PORT_PATH_SEPARATOR_STR) + strlen(value) + 1); strcpy(tmp, value); strcat(tmp, PORT_PATH_SEPARATOR_STR); strcat(tmp, bcp_old); bcp_new = tmp; } p_env->VmProperties()->set(XBOOTCLASSPATH_P, bcp_old ? bcp_new : value); p_env->VmProperties()->destroy(bcp_old); STD_FREE((void*)bcp_new); } else if (begins_with(option, "-Xjit:")) { // Do nothing here, just skip this option for later parsing } else if (strcmp(option, "-Xint") == 0) { p_env->VmProperties()->set("vm.use_interpreter", "true"); #ifdef VM_STATS } else if (begins_with(option, "-Xstats:")) { vm_print_total_stats = true; const char* arg = option + strlen("-Xstats:"); vm_print_total_stats_level = atoi(arg); #endif } else if (strcmp(option, "-version") == 0) { // Print the version number and exit LECHO_VERSION; log_exit(0); } else if (strcmp(option, "-showversion") == 0) { if (!version_printed) { // Print the version number and continue LECHO_VERSION; version_printed = true; } } else if (strcmp(option, "-fullversion") == 0) { // Print the version number and exit LECHO_VM_VERSION; log_exit(0); } else if (begins_with(option, "-Xgc:")) { // make prop_key to be "gc.<something>" char* prop_key = strdup(option + strlen("-X")); prop_key[2] = '.'; TRACE(prop_key << " = 1"); p_env->VmProperties()->set(prop_key, "1"); free(prop_key); } else if (begins_with(option, "-Xem:")) { const char* arg = option + strlen("-Xem:"); p_env->VmProperties()->set("em.properties", arg); } else if (strcmp(option, "-client") == 0 || strcmp(option, "-server") == 0) { p_env->VmProperties()->set("em.properties", option + 1); } else if (begins_with(option, "-Xms") || begins_with(option, "-ms")) { // cut -Xms || -ms const char* arg = option + (begins_with(option, "-ms") ? 3 : 4); TRACE("gc.ms = " << arg); if (atoi(arg) <= 0) { LECHO(34, "Negative or invalid heap size. Default value will be used!"); } p_env->VmProperties()->set("gc.ms", arg); } else if (begins_with(option, "-Xmx") || begins_with(option, "-mx")) { // cut -Xmx const char* arg = option + (begins_with(option, "-mx") ? 3 : 4); TRACE("gc.mx = " << arg); if (atoi(arg) <= 0) { LECHO(34, "Negative or invalid heap size. Default value will be used!"); } p_env->VmProperties()->set("gc.mx", arg); } else if (begins_with(option, "-Xss")) { const char* arg = option + 4; TRACE("thread.stacksize = " << arg); if (atoi(arg) <= 0) { LECHO(34, "Negative or invalid stack size. Default value will be used!"); } p_env->VmProperties()->set("thread.stacksize", arg); } else if (begins_with(option, STRING_POOL_SIZE_OPTION)) { // the pool is already created } else if (begins_with(option, "-agentlib:")) { p_env->TI->addAgent(option); } else if (begins_with(option, "-agentpath:")) { p_env->TI->addAgent(option); } else if (begins_with(option, "-javaagent:")) { char* dest = (char*) STD_MALLOC(strlen("-agentlib:hyinstrument=") + strlen(option + 11) + 1); strcpy(dest, "-agentlib:hyinstrument="); strcat(dest, option + 11); p_env->TI->addAgent(dest); STD_FREE((void*) dest); } else if (begins_with(option, "-Xrun")) { // Compatibility with JNDI p_env->TI->addAgent(option); } else if (strcmp(option, "-Xnoagent") == 0) { // Do nothing, this option is only for compatibility with old JREs } else if (strcmp(option, "-Xdebug") == 0) { // Do nothing, this option is only for compatibility with old JREs } else if (strcmp(option, "-Xfuture") == 0) { // Do nothing, this option is only for compatibility with old JREs } else if (strcmp(option, "-Xinvisible") == 0) { p_env->retain_invisible_annotations = true; } else if (strcmp(option, "-Xverify") == 0) { p_env->verify_all = true; } else if (strcmp(option, "-Xverify:none") == 0 || strcmp(option, "-noverify") == 0) { p_env->VmProperties()->set("vm.use_verifier", "false"); } else if (strcmp(option, "-Xverify:all") == 0) { p_env->verify_all = true; p_env->verify_strict = true; } else if (strcmp(option, "-Xverify:strict") == 0) { p_env->verify_all = true; p_env->verify_strict = true; } else if (strcmp(option, "-verify") == 0) { p_env->verify_all = true; } else if (begins_with(option, "-verbose")) { // Moved to set_log_levels_from_cmd } else if (begins_with(option, "-Xfileline")) { // Moved to set_log_levels_from_cmd } else if (begins_with(option, "-Xthread")) { // Moved to set_log_levels_from_cmd } else if (begins_with(option, "-Xcategory")) { // Moved to set_log_levels_from_cmd } else if (begins_with(option, "-Xtimestamp")) { // Moved to set_log_levels_from_cmd } else if (begins_with(option, "-Xverbose")) { // Moved to set_log_levels_from_cmd } else if (begins_with(option, "-Xwarn")) { // Moved to set_log_levels_from_cmd } else if (begins_with(option, "-Xfunction")) { // Moved to set_log_levels_from_cmd #ifdef _DEBUG } else if (begins_with(option, "-Xlog")) { // Moved to set_log_levels_from_cmd } else if (begins_with(option, "-Xtrace")) { // Moved to set_log_levels_from_cmd #endif //_DEBUG } else if (strncmp(option, "-D", 2) == 0) { } else if (strncmp(option, "-XD", 3) == 0 || strncmp(option, "-XX:", 4) == 0) { } else if (strcmp(option, "-Xdumpstubs") == 0) { dump_stubs = true; } else if (strcmp(option, "-Xparallel_jit") == 0) { parallel_jit = true; } else if (strcmp(option, "-Xno_parallel_jit") == 0) { parallel_jit = false; } else if (begins_with(option, "-Xdumpfile:")) { const char* arg = option + strlen("-Xdumpfile:"); dump_file_name = arg; } else if (strcmp(option, "_org.apache.harmony.vmi.portlib") == 0) { // Store a pointer to the portlib p_env->portLib = p_env->vm_arguments.options[i].extraInfo; } else if (strcmp(option, "-help") == 0 || strcmp(option, "-h") == 0 || strcmp(option, "-?") == 0) { print_generic_help(); log_exit(0); } else if (strcmp(option,"-X") == 0) { print_help_on_nonstandard_options(); log_exit(0); } else if (begins_with(option, "-enableassertions")) { add_assert_rec(p_env, option, "-enableassertions", true); } else if (begins_with(option, "-ea")) { add_assert_rec(p_env, option, "-ea", true); } else if (begins_with(option, "-disableassertions")) { add_assert_rec(p_env, option, "-disableassertions", false); } else if (begins_with(option, "-da")) { add_assert_rec(p_env, option, "-da", false); } else if (strcmp(option, "-esa") == 0 || strcmp(option, "-enablesystemassertions") == 0) { get_assert_reg(p_env)->enable_system = true; } else if (strcmp(option, "-dsa") == 0 || strcmp(option, "-disablesystemassertions") == 0) { if (p_env->assert_reg) { p_env->assert_reg->enable_system = false; } } else { LECHO(30, "Unknown option {0}" << option); USE_JAVA_HELP; log_exit(1); } } // for apr_pool_destroy(pool); } //parse_vm_arguments2
/* * Check if input is clean from SQL injection and return plaintext */ void get_plaintext_from_string(char *input, int is_mysql) { if (!input) return; /* Copy input in another string and work with that */ char *hashed_str = (char *) calloc(1, (strlen(input) + 1) * sizeof(char)); if (hashed_str == NULL) { perror("calloc str failed!"); exit(EXIT_FAILURE); } strcpy(hashed_str, input); /* make room for plaintext */ char *plain = calloc(1, (strlen(input) + 1) * sizeof(char)); if (plain == NULL) { perror("plain str failed!"); exit(EXIT_FAILURE); } /* read all strings in input */ int str_start = 0; char *str = NULL; unsigned int i = 0; unsigned int j; while(i < strlen(input)) { if (isalnum(input[i])) { /* If we found the beginning of a string, go all the way * and sanitize it if it is a keyword. Ignore anything not * alphanumeric and append to plain */ str_start = i; while (isalnum(input[i]) || input[i] == '_') i++; if (str) free(str); /* proper as now i is the \0 character */ str = calloc(1, (i - str_start) * sizeof(char)); for (j = str_start; j < i; j++) { str[j - str_start] = input[j]; } /* If we found a keyword abort */ if (isKeyword(str, is_mysql)) { /* log */ log_exit(input); exit(EXIT_FAILURE); } convert_to_plaintext(str, is_mysql); strncat(plain, str, i - str_start); } plain[i] = input[i++]; } strncpy(input, plain, strlen(input)); if (str) free(str); free(plain); free(hashed_str); }
static void connect_close(unsigned int socketfd) { socket_close(socketfd); log_exit(); }
void dump_cormon_stats(long verbose, long plane, double **kick, long n_kicks, double **position, long n_positions, double *Cdp, long n_iterations, long cycle, long final_cycle, long sim_step, long textOnly) { long i, j; static double **data = NULL; static char **stage = NULL; static long n_iter = 0; if (!verbose && !SDDS_cormon_initialized) return; log_entry("dump_cormon_stats"); if (!data || n_iterations>n_iter) { if (data) free_czarray_2d((void**)data, n_iter+1, N_COLUMNS); if (stage) free(stage); data = (double**)czarray_2d(sizeof(**data), n_iterations+1, N_COLUMNS); stage = tmalloc(sizeof(*stage)*(n_iterations+1)); n_iter = n_iterations; } for (j=0; j<=n_iterations; j++) { if (j==0) stage[j] = "uncorrected"; else if (j==n_iterations) stage[j] = "corrected"; else stage[j] = "intermediate"; data[j][IC_ITERATION] = j; data[j][IC_CYCLE] = cycle; data[j][IC_KRMS] = data[j][IC_KMAX] = data[j][IC_PRMS] = data[j][IC_PMAX] = 0; for (i=0; i<n_kicks; i++) { if (FABS(kick[j][i]) > data[j][IC_KMAX]) data[j][IC_KMAX] = FABS(kick[j][i]); data[j][IC_KRMS] += sqr(kick[j][i]); } if (n_kicks) data[j][IC_KRMS] = sqrt(data[j][IC_KRMS]/n_kicks); for (i=0; i<n_positions; i++) { if (FABS(position[j][i]) > data[j][IC_PMAX]) data[j][IC_PMAX] = FABS(position[j][i]); data[j][IC_PRMS] += sqr(position[j][i]); } if (n_positions) data[j][IC_PRMS] = sqrt(data[j][IC_PRMS]/n_positions); data[j][IC_CDP] = (Cdp?Cdp[j]:0); } if (SDDS_cormon_initialized && !textOnly) { if (!SDDS_StartTable(&SDDS_cormon, n_iterations+1)) { SDDS_SetError("Unable to start SDDS table (dump_cormon_stats)"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_SetParameters(&SDDS_cormon, SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE, IP_STEP, sim_step, IP_PLANE, plane?"vertical":"horizontal", IP_FINAL, final_cycle?'y':'n', -1)) { SDDS_SetError("Unable to set SDDS parameter values (dump_cormon_stats)"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } for (j=0; j<=n_iterations; j++) { if (!SDDS_SetRowValues(&SDDS_cormon, SDDS_SET_BY_INDEX|SDDS_PASS_BY_VALUE, j, IC_ITERATION, j, IC_CYCLE, cycle, IC_KRMS, data[j][IC_KRMS], IC_PRMS, data[j][IC_PRMS], IC_KMAX, data[j][IC_KMAX], IC_PMAX, data[j][IC_PMAX], IC_CDP, data[j][IC_CDP], IC_STAGE, stage[j], -1)) { fprintf(stdout, "Unable to set row %ld values (dump_cormon_stats)\n", j); fflush(stdout); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exitElegant(1); } } if (!SDDS_WriteTable(&SDDS_cormon)) { SDDS_SetError("Unable to write corrector data (dump_cormon_stats)"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!inhibitFileSync) SDDS_DoFSync(&SDDS_cormon); if (!SDDS_EraseData(&SDDS_cormon)) { SDDS_SetError("Unable to erase corrector data (dump_cormon_stats)"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (verbose) { for (j=0; j<=n_iterations; j++) if (Cdp) { fprintf(stdout, " %c %4ld %8.3f %8.3f %8.3f %8.3f %8.3f %s\n", 'x'+(plane?1:0), j, 1e3*data[j][IC_KRMS], 1e3*data[j][IC_PRMS], 1e3*data[j][IC_KMAX], 1e3*data[j][IC_PMAX], 1e2*data[j][IC_CDP], (j==n_iterations?"**":"") ); fflush(stdout); } else { fprintf(stdout, " %c %4ld %8.3f %8.3f %8.3f %8.3f %s\n", 'x'+(plane?1:0), j, 1e3*data[j][IC_KRMS], 1e3*data[j][IC_PRMS], 1e3*data[j][IC_KMAX], 1e3*data[j][IC_PMAX], (j==n_iterations?"**":"") ); fflush(stdout); } } log_exit("dump_cormon_stats"); }
/* This function gets called from all 3 processes. We want the client side * to actually output the text, but the sender is the only process that has * all the stats we need. So, if we're a client sender, we do the report. * If we're a server sender, we write the stats on the supplied fd. If * we're the client receiver we read the stats from the supplied fd and do * the report. All processes might also generate a set of debug stats, if * the verbose level is high enough (this is the only thing that the * generator process and the server receiver ever do here). */ static void handle_stats(int f) { endtime = time(NULL); /* Cache two stats because the read/write code can change it. */ total_read = stats.total_read; total_written = stats.total_written; if (do_stats && verbose > 1) { /* These come out from every process */ show_malloc_stats(); show_flist_stats(); } if (am_generator) return; if (am_daemon) { log_exit(0, __FILE__, __LINE__); if (f == -1 || !am_sender) return; } if (am_server) { if (am_sender) { write_longint(f, total_read); write_longint(f, total_written); write_longint(f, stats.total_size); if (protocol_version >= 29) { write_longint(f, stats.flist_buildtime); write_longint(f, stats.flist_xfertime); } } return; } /* this is the client */ if (f < 0 && !am_sender) /* e.g. when we got an empty file list. */ ; else if (!am_sender) { /* Read the first two in opposite order because the meaning of * read/write swaps when switching from sender to receiver. */ total_written = read_longint(f); total_read = read_longint(f); stats.total_size = read_longint(f); if (protocol_version >= 29) { stats.flist_buildtime = read_longint(f); stats.flist_xfertime = read_longint(f); } } else if (write_batch) { /* The --read-batch process is going to be a client * receiver, so we need to give it the stats. */ write_longint(batch_fd, total_read); write_longint(batch_fd, total_written); write_longint(batch_fd, stats.total_size); if (protocol_version >= 29) { write_longint(batch_fd, stats.flist_buildtime); write_longint(batch_fd, stats.flist_xfertime); } } }
/** * Eventually calls exit(), passing @p code, therefore does not return. * * @param code one of the RERR_* codes from errcode.h. **/ void _exit_cleanup(int code, const char *file, int line) { int ocode = code; static int inside_cleanup = 0; if (inside_cleanup > 10) { /* prevent the occasional infinite recursion */ return; } inside_cleanup++; signal(SIGUSR1, SIG_IGN); signal(SIGUSR2, SIG_IGN); if (verbose > 3) { rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n", code, safe_fname(file), line); } if (cleanup_child_pid != -1) { int status; if (waitpid(cleanup_child_pid, &status, WNOHANG) == cleanup_child_pid) { status = WEXITSTATUS(status); if (status > code) code = status; } } if (cleanup_got_literal && cleanup_fname && keep_partial && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) { char *fname = cleanup_fname; cleanup_fname = NULL; if (cleanup_fd_r != -1) close(cleanup_fd_r); if (cleanup_fd_w != -1) { flush_write_file(cleanup_fd_w); close(cleanup_fd_w); } finish_transfer(cleanup_new_fname, fname, cleanup_file, 0, !partial_dir); } io_flush(FULL_FLUSH); if (cleanup_fname) do_unlink(cleanup_fname); if (code) kill_all(SIGUSR1); if (cleanup_pid && cleanup_pid == getpid()) { char *pidf = lp_pid_file(); if (pidf && *pidf) unlink(lp_pid_file()); } if (code == 0) { if (io_error & IOERR_DEL_LIMIT) code = RERR_DEL_LIMIT; if (io_error & IOERR_VANISHED) code = RERR_VANISHED; if (io_error & IOERR_GENERAL || log_got_error) code = RERR_PARTIAL; } if (code) log_exit(code, file, line); if (verbose > 2) { rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n", ocode, safe_fname(file), line, code); } close_all(); exit(code); }