void nes_free(NES *nes) { // disable ndb debugging will make cpu keep running ndb_set_debug(&(nes->ndb), NDB_DEBUG_MODE_DISABLE); // destroy nes thread nes->thread_exit = TRUE; pthread_join(nes->thread_id, NULL); // free replay replay_free(&(nes->replay)); // free joypad joypad_setkey(&(nes->pad), 0, NES_PAD_CONNECT, 0); joypad_setkey(&(nes->pad), 1, NES_PAD_CONNECT, 0); joypad_free (&(nes->pad )); // free cpu & ppu & apu & mmc cpu_free(&(nes->cpu)); ppu_free(&(nes->ppu)); apu_free(&(nes->apu)); mmc_free(&(nes->mmc)); ndb_free(&(nes->ndb)); // free cartridge cartridge_free(&(nes->cart)); log_done(); // log done }
void server_done(sxc_client_t **sx) { ERR_free_strings(); sxc_shutdown(*sx, 0); *sx = NULL; log_done(); sxprocdone(); }
void real_error(char *file, int line) { #ifndef _JOHN_MISC_NO_LOG log_event("Terminating on error, %s:%d", file, line); log_done(); #endif exit(1); }
void error(void) { #ifndef _JOHN_MISC_NO_LOG log_event("Terminating on error"); log_done(); #endif exit(1); }
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lpszCmdLine, int nCmdShow) { void *fpdec = NULL; void *fpenc = NULL; int width, height; BYTE bkcolor; BYTE globalpaldata[256*4]; int globalpalsize = 0; int localpalflag = 0; int xpos, ypos; int type; log_init("gif.log"); if (*lpszCmdLine == 0) { lpszCmdLine = "res/cs.gif"; } fpdec = DEF_FIO_DRV.open(lpszCmdLine , "rb"); fpenc = DEF_FIO_DRV.open("output.gif", "wb"); g_gdctxt = gifdecodeinit(fpdec , &DEF_FIO_DRV); gifgetimageinfo(g_gdctxt, &width, &height, &bkcolor); gifgetglobalpal(g_gdctxt, globalpaldata, &globalpalsize); g_gectxt = gifencodeinit(fpenc, &DEF_FIO_DRV, width, height, bkcolor, globalpaldata, globalpalsize); while (1) { gifdecodeframe(g_gdctxt, &type, &g_mybmp, &xpos, &ypos, &g_ctrl, &g_text); gifgetlocalpal(g_gdctxt, &localpalflag); gifsetlocalpal(g_gectxt, localpalflag); gifencodeframe(g_gectxt, type, &g_mybmp, xpos, ypos, &g_ctrl, &g_text); if (type == GIF_FRAME_TYPE_END) break; } RGE_WIN_INIT(hInst); SCREEN.width = 640; SCREEN.height = 480; SCREEN.cdepth = 32; createbmp(&SCREEN); SetWindowLong(GET_SCREEN_HWND(), GWL_WNDPROC, (long)MyWndProc); SetTimer(GET_SCREEN_HWND(), 1, 100, NULL); RGE_MSG_LOOP(); KillTimer(GET_SCREEN_HWND(), 1); destroybmp(&SCREEN); gifdecodefree(g_gdctxt); gifdecodefree(g_gectxt); DEF_FIO_DRV.close(fpdec ); DEF_FIO_DRV.close(fpenc); log_done(); return 0; }
void stopit(int rc) { vidle();qqreset(); IFPerl(perl_done(0)); write_log("exiting with rc=%d",rc); log_done(); cls_close(ssock); cls_shutd(lins_sock); cls_shutd(uis_sock); exit(rc); }
void error(void) { #ifndef _JOHN_MISC_NO_LOG log_event("Terminating on error"); log_done(); #if defined(HAVE_MPI) && defined(JOHN_MPI_ABORT) if (mpi_p > 1) MPI_Abort(MPI_COMM_WORLD,1); #endif #endif exit(1); }
static void john_done(void) { path_done(); if ((options.flags & FLG_CRACKING_CHK) && !(options.flags & FLG_STDOUT)) { if (event_abort) log_event("Session aborted"); else log_event("Session completed"); } log_done(); check_abort(0); }
static void john_done(void) { path_done(); if ((options.flags & FLG_CRACKING_CHK) && !(options.flags & FLG_STDOUT)) { if (event_abort) log_event(timer_abort ? "Session aborted" : "Session stopped (max run-time reached)"); else log_event("Session completed"); } log_done(); check_abort(0); cleanup_tiny_memory(); }
void nes_free(NES *nes) { // destroy nes event & thread nes->bExitThread = TRUE; SetEvent(nes->hNesEvent); WaitForSingleObject(nes->hNesThread, -1); CloseHandle(nes->hNesEvent ); CloseHandle(nes->hNesThread); cpu_free (&(nes->cpu)); ppu_free (&(nes->ppu)); apu_free (&(nes->apu)); mmc_free (&(nes->mmc)); joypad_setkey (&(nes->pad), 0, NES_PAD_CONNECT, 0); joypad_setkey (&(nes->pad), 1, NES_PAD_CONNECT, 0); joypad_free (&(nes->pad )); // free joypad cartridge_free(&(nes->cart)); // free cartridge log_done(); }
RETSIGTYPE sigerr(int sig) { signal(sig,SIG_DFL); aso_done(); write_log("got SIG%s signal",sigs[sig]); if(cfgs(CFG_PIDFILE))if(getpid()==islocked(ccs))lunlink(ccs); IFPerl(perl_done(1)); log_done(); tty_close(); qqreset();sline("");title(""); cls_close(ssock); cls_shutd(lins_sock); cls_shutd(uis_sock); switch(sig) { case SIGSEGV: case SIGFPE: case SIGBUS: case SIGABRT: abort(); default: exit(1); } }
static void answer_mode(int type) { int rc, spd;char *cs; struct sockaddr_in sa; socklen_t ss=sizeof(sa); sts_t sts; if(cfgs(CFG_ROOTDIR)&&ccs[0])chdir(ccs); rnode=xcalloc(1,sizeof(ninfo_t)); is_ip=!isatty(0); xstrcpy(ip_id,"ipline",10); rnode->tty=xstrdup(is_ip?(bink?"binkp":"tcpip"):basename(ttyname(0))); rnode->options|=O_INB; if(!log_init(cfgs(CFG_LOG),rnode->tty)) { printf("can't open log %s!\n",ccs); exit(S_FAILURE); } signal(SIGINT,SIG_IGN); signal(SIGTERM,sigerr); signal(SIGSEGV,sigerr); signal(SIGFPE,sigerr); signal(SIGPIPE,SIG_IGN); IFPerl(perl_init(cfgs(CFG_PERLFILE),0)); log_callback=NULL;xsend_cb=NULL; ssock=cls_conn(CLS_LINE,cfgs(CFG_SERVER),NULL); if(ssock<0)write_log("can't connect to server: %s",strerror(errno)); else log_callback=vlogs; rc=aso_init(cfgs(CFG_ASOOUTBOUND),cfgs(CFG_BSOOUTBOUND),cfgs(CFG_QSTOUTBOUND),cfgal(CFG_ADDRESS)->addr.z); if(!rc) { write_log("No outbound defined"); stopit(S_FAILURE); } write_log("answering incoming call");vidle(); if(is_ip&&!getpeername(0,(struct sockaddr*)&sa,&ss)) { write_log("remote is %s",inet_ntoa(sa.sin_addr)); spd=TCP_SPEED; } else { cs=getenv("CONNECT");spd=cs?atoi(cs):0; xfree(connstr);connstr=xstrdup(cs); if(cs&&spd)write_log("*** CONNECT %s",cs); else { write_log("*** CONNECT Unknown"); spd=DEFAULT_SPEED; } } if((cs=getenv("CALLER_ID"))&&strcasecmp(cs,"none")&&strlen(cs)>3)write_log("caller-id: %s",cs); tty_setattr(0); tty_local(0); rc=session(0,type,NULL,spd); tty_local(1); if(!is_ip) { hangup(); stat_collect(); } tty_cooked(); if((S_OK==(rc&S_MASK))&&cfgi(CFG_HOLDONSUCCESS)) { log_done(); log_init(cfgs(CFG_MASTERLOG),NULL); aso_getstatus(&rnode->addrs->addr, &sts); sts.flags|=(Q_WAITA|Q_WAITR|Q_WAITX); sts.htime=MAX(t_set(cci*60),sts.htime); write_log("calls to %s delayed for %d min after successful incoming session", ftnaddrtoa(&rnode->addrs->addr),cci); aso_setstatus(&rnode->addrs->addr,&sts); log_done(); log_init(cfgs(CFG_LOG),rnode->tty); } title("Waiting..."); vidle();sline(""); aso_done(); stopit(rc); }
int os_main(int argc, char* argv[]) { int keyboard_id; adv_conf* context; const char* s; char* section_map[1]; const char** file_map; unsigned file_mac; int i; double latency_time; double buffer_time; double volume; unsigned rate; int attenuation; adv_bool opt_log; adv_bool opt_logsync; opt_log = 0; opt_logsync = 0; file_map = 0; file_mac = 0; context = conf_init(); if (os_init(context) != 0) goto err_conf; mixer_reg(context); conf_int_register_limit_default(context, "sound_volume", -32, 0, 0); conf_int_register_limit_default(context, "sound_samplerate", 5000, 96000, 44100); conf_float_register_limit_default(context, "sound_latency", 0.01, 2.0, 0.1); conf_float_register_limit_default(context, "sound_buffer", 0.05, 2.0, 0.1); if (conf_input_args_load(context, 0, "", &argc, argv, error_callback, 0) != 0) goto err_os; file_map = malloc(argc * sizeof(const char*)); for(i=1;i<argc;++i) { if (target_option_compare(argv[i], "log")) { opt_log = 1; } else if (target_option_compare(argv[i], "logsync")) { opt_logsync = 1; } else if (target_option_extract(argv[i]) == 0) { file_map[file_mac++] = argv[i]; } else { target_err("Unknown command line option '%s'.\n", argv[i]); goto err_os; } } if (argc <= 1 || file_mac == 0) { target_err("Syntax: advs FILES...\n"); goto err_os; } if (opt_log || opt_logsync) { const char* log = "advs.log"; remove(log); log_init(log, opt_logsync); } section_map[0] = ""; conf_section_set(context, section_map, 1); if (mixer_load(context) != 0) { goto err_os; } attenuation = conf_int_get_default(context, "sound_volume"); latency_time = conf_float_get_default(context, "sound_latency"); buffer_time = conf_float_get_default(context, "sound_buffer"); rate = conf_int_get_default(context, "sound_samplerate"); volume = 1.0; while (attenuation++ < 0) volume /= 1.122018454; /* = (10 ^ (1/20)) = 1dB */ if (os_inner_init("AdvanceSOUND") != 0) goto err_os; if (file_mac > MIXER_CHANNEL_MAX) { target_err("Too many files\n"); goto err_os_inner; } if (mixer_init(rate, file_mac, 1, buffer_time + latency_time, latency_time) != 0) { target_err("%s\n", error_get()); goto err_os_inner; } mixer_volume(volume); for(i=0;i<file_mac;++i) run(i, file_map[i]); free(file_map); signal(SIGINT, sigint); while (!done) { for(i=0;i<file_mac;++i) if (mixer_is_playing(i)) break; if (i==file_mac) break; mixer_poll(); target_idle(); } log_std(("s: shutdown\n")); mixer_done(); os_inner_done(); log_std(("s: the end\n")); if (opt_log || opt_logsync) { log_done(); } os_done(); conf_done(context); return EXIT_SUCCESS; err_os_inner: os_inner_done(); log_done(); err_os: os_done(); err_conf: conf_done(context); err: return EXIT_FAILURE; }
int os_main(int argc, char* argv[]) { adv_crtc_container selected; adv_crtc_container_iterator i; const char* opt_rc; adv_bool opt_log; adv_bool opt_logsync; int j; adv_error res; char* section_map[1]; char buffer[1024]; opt_rc = 0; opt_log = 0; opt_logsync = 0; the_advance = advance_mame; the_sound_flag = 1; the_config = conf_init(); if (os_init(the_config)!=0) { target_err("Error initializing the OS support.\n"); goto err_conf; } video_reg(the_config, 1); monitor_register(the_config); crtc_container_register(the_config); generate_interpolate_register(the_config); gtf_register(the_config); inputb_reg(the_config, 1); inputb_reg_driver_all(the_config); /* MSDOS requires a special driver sub set */ #ifndef __MSDOS__ video_reg_driver_all(the_config); #endif if (conf_input_args_load(the_config, 1, "", &argc, argv, error_callback, 0) != 0) goto err_os; for(j=1;j<argc;++j) { if (target_option_compare(argv[j], "rc") && j+1<argc) { opt_rc = argv[++j]; } else if (target_option_compare(argv[j], "log")) { opt_log = 1; } else if (target_option_compare(argv[j], "logsync")) { opt_logsync = 1; } else if (target_option_compare(argv[j], "nosound")) { the_sound_flag = 0; } else if (target_option_compare(argv[j], "advmamev")) { the_advance = advance_mame; } else if (target_option_compare(argv[j], "advmessv")) { the_advance = advance_mess; } else if (target_option_compare(argv[j], "advpacv")) { the_advance = advance_pac; } else if (target_option_compare(argv[j], "advmenuv")) { the_advance = advance_menu; #ifdef __MSDOS__ } else if (target_option_compare(argv[j], "vgav")) { the_advance = advance_vga; } else if (target_option_compare(argv[j], "vbev")) { the_advance = advance_vbe; #endif #ifdef __WIN32__ } else if (target_option_compare(argv[j], "videowv")) { the_advance = advance_videow; #endif } else { target_err("Unknown option %s\n", argv[j]); goto err; } } #ifdef __MSDOS__ /* WARNING the MSDOS drivers are registered after the command line management. */ /* It implyes that you cannot specify any driver options on the command line */ msdos_rut(); if (the_advance == advance_vga) { if (the_advance_vga_active) { target_err("The AdvanceVGA utility is active. Disable it before running vgav.\n"); goto err; } video_reg_driver(the_config, &video_vgaline_driver); } else if (the_advance == advance_vbe) { if (the_advance_vbe_active) { target_err("The AdvanceVBE utility is active. Disable it before running vbev.\n"); goto err; } video_reg_driver(the_config, &video_vbeline_driver); video_reg_driver(the_config, &video_vgaline_driver); /* for the text modes */ } else { video_reg_driver_all(the_config); } #endif if (!opt_rc) { switch (the_advance) { case advance_vbe : opt_rc = "vbe.rc"; break; case advance_vga : opt_rc = "vga.rc"; break; case advance_menu : opt_rc = file_config_file_home("advmenu.rc"); break; case advance_mame : opt_rc = file_config_file_home("advmame.rc"); break; case advance_mess : opt_rc = file_config_file_home("advmess.rc"); break; case advance_pac : opt_rc = file_config_file_home("advpac.rc"); break; case advance_videow : opt_rc = file_config_file_home("videow.rc"); break; default : opt_rc = "advv.rc"; break; } } if (access(opt_rc, R_OK)!=0) { target_err("Configuration file %s not found.\n", opt_rc); goto err_os; } if (conf_input_file_load_adv(the_config, 0, opt_rc, opt_rc, 1, 1, STANDARD, sizeof(STANDARD)/sizeof(STANDARD[0]), error_callback, 0) != 0) goto err_os; if (opt_log || opt_logsync) { const char* log = "advv.log"; remove(log); log_init(log, opt_logsync); } log_std(("v: %s %s\n", __DATE__, __TIME__)); section_map[0] = ""; conf_section_set(the_config, section_map, 1); if (video_load(the_config, "") != 0) { target_err("Error loading the video options from the configuration file %s.\n", opt_rc); target_err("%s\n", error_get()); goto err_os; } if (inputb_load(the_config) != 0) { target_err("%s\n", error_get()); goto err_os; } /* NOTE: After this command all the target_err() string must */ /* have \n\r at the end to ensure correct newline in graphics mode. */ if (os_inner_init("AdvanceVIDEO") != 0) { goto err_os; } if (adv_video_init() != 0) { target_err("%s\n\r", error_get()); troubleshotting(); goto err_os_inner; } if (video_blit_init() != 0) { target_err("%s\n\r", error_get()); goto err_video; } if (the_advance != advance_vbe && the_advance != advance_vga) { if ((video_mode_generate_driver_flags(VIDEO_DRIVER_FLAGS_MODE_GRAPH_MASK, 0) & VIDEO_DRIVER_FLAGS_PROGRAMMABLE_CLOCK) == 0) { target_err("No active video driver is able to program your video board.\n\r"); troubleshotting(); goto err_blit; } } if (inputb_init() != 0) { target_err("%s\n\r", error_get()); goto err_blit; } if (monitor_load(the_config, &the_monitor) != 0) { target_err("Error loading the clock options from the configuration file %s.\n\r", opt_rc); target_err("%s\n\r", error_get()); goto err_input; } monitor_print(buffer, sizeof(buffer), &the_monitor); log_std(("v: clock %s\n", buffer)); /* load generate_linear config */ res = generate_interpolate_load(the_config, &the_interpolate); if (res<0) { target_err("Error loading the format options from the configuration file %s.\n\r", opt_rc); target_err("%s\n\r", error_get()); goto err_input; } if (res>0) { generate_default_vga(&the_interpolate.map[0].gen); the_interpolate.map[0].hclock = 31500; the_interpolate.mac = 1; } /* load generate_linear config */ res = gtf_load(the_config, &the_gtf); if (res<0) { target_err("Error loading the gtf options from the configuration file %s.\n\r", opt_rc); target_err("%s\n\r", error_get()); goto err_input; } if (res>0) { gtf_default_vga(&the_gtf); } /* all mode */ crtc_container_init(&selected); /* insert modes */ crtc_container_insert_default_all(&selected); /* sort */ crtc_container_init(&the_modes); for(crtc_container_iterator_begin(&i, &selected);!crtc_container_iterator_is_end(&i);crtc_container_iterator_next(&i)) { adv_crtc* crtc = crtc_container_iterator_get(&i); crtc_container_insert_sort(&the_modes, crtc, crtc_compare); } crtc_container_done(&selected); /* load selected */ crtc_container_init(&selected); if (crtc_container_load(the_config, &selected) != 0) { target_err("%s\n\r", error_get()); goto err_input; } /* union set */ for(crtc_container_iterator_begin(&i, &selected);!crtc_container_iterator_is_end(&i);crtc_container_iterator_next(&i)) { adv_crtc* crtc = crtc_container_iterator_get(&i); adv_bool has = crtc_container_has(&the_modes, crtc, crtc_compare) != 0; if (has) crtc_container_remove(&the_modes, crtc_select_by_compare, crtc); crtc->user_flags |= MODE_FLAGS_USER_BIT0; crtc_container_insert_sort(&the_modes, crtc, crtc_compare); } crtc_container_done(&selected); the_modes_modified = 0; if (text_init(&the_modes, &the_monitor) != 0) { goto err_input; } if (inputb_enable(0) != 0) { goto err_text; } sound_signal(); menu_run(); log_std(("v: shutdown\n")); inputb_disable(); text_done(); crtc_container_done(&the_modes); inputb_done(); video_blit_done(); adv_video_done(); os_inner_done(); log_std(("v: the end\n")); if (opt_log || opt_logsync) { log_done(); } os_done(); conf_save(the_config, 0, 0, error_callback, 0); conf_done(the_config); return EXIT_SUCCESS; err_text: text_done(); err_input: inputb_done(); err_blit: video_blit_done(); err_video: adv_video_done(); err_os_inner: os_inner_done(); err_os: if (opt_log || opt_logsync) { log_done(); } os_done(); err_conf: conf_done(the_config); err: return EXIT_FAILURE; }
int os_main(int argc, char* argv[]) { int i; adv_conf* context; const char* section_map[1]; adv_bool opt_log; adv_bool opt_logsync; opt_log = 0; opt_logsync = 0; context = conf_init(); if (os_init(context) != 0) goto err_conf; joystickb_reg(context, 1); joystickb_reg_driver_all(context); if (conf_input_args_load(context, 0, "", &argc, argv, error_callback, 0) != 0) goto err_os; for (i = 1; i < argc; ++i) { if (target_option_compare(argv[i], "log")) { opt_log = 1; } else if (target_option_compare(argv[i], "logsync")) { opt_logsync = 1; } else { fprintf(stderr, "Unknown argument '%s'\n", argv[1]); goto err_os; } } if (opt_log || opt_logsync) { const char* log = "advj.log"; remove(log); log_init(log, opt_logsync); } log_std(("j: %s %s %s %s\n", "AdvanceJOYSTICK", ADV_VERSION, __DATE__, __TIME__)); section_map[0] = ""; conf_section_set(context, section_map, 1); if (joystickb_load(context) != 0) goto err_os; if (os_inner_init("AdvanceJOYSTICK") != 0) goto err_os; if (joystickb_init() != 0) { target_err("%s\n", error_get()); goto err_os_inner; } probe(); calibrate(); run(); joystickb_done(); os_inner_done(); log_std(("j: the end\n")); if (opt_log || opt_logsync) { log_done(); } os_done(); conf_done(context); return EXIT_SUCCESS; err_os_inner: os_inner_done(); log_done(); err_os: os_done(); err_conf: conf_done(context); return EXIT_FAILURE; }
void rec_restore_args(int lock) { char line[LINE_BUFFER_SIZE]; int index, argc; char **argv; char *save_rec_name; rec_name_complete(); if (!(rec_file = fopen(path_expand(rec_name), "r+"))) { #ifndef HAVE_MPI if (options.fork && !john_main_process && errno == ENOENT) { #else if (options.node_min > 1 && errno == ENOENT) { #endif fprintf(stderr, "%u Session completed\n", options.node_min); if (options.flags & FLG_STATUS_CHK) return; log_event("No crash recovery file, terminating"); log_done(); #ifdef HAVE_MPI mpi_teardown(); #endif exit(0); } #ifdef HAVE_MPI if (mpi_p > 1) { fprintf(stderr, "%u@%s: fopen: %s: %s\n", mpi_id + 1, mpi_name, path_expand(rec_name), strerror(errno)); error(); } #endif pexit("fopen: %s", path_expand(rec_name)); } rec_fd = fileno(rec_file); if (lock) rec_lock(lock); if (!fgetl(line, sizeof(line), rec_file)) rec_format_error("fgets"); rec_version = 0; if (!strcmp(line, RECOVERY_V4)) rec_version = 4; else if (!strcmp(line, RECOVERY_V3)) rec_version = 3; else if (!strcmp(line, RECOVERY_V2)) rec_version = 2; else if (!strcmp(line, RECOVERY_V1)) rec_version = 1; else if (strcmp(line, RECOVERY_V0)) rec_format_error(NULL); if (fscanf(rec_file, "%d\n", &argc) != 1) rec_format_error("fscanf"); if (argc < 2) rec_format_error(NULL); argv = mem_alloc_tiny(sizeof(char *) * (argc + 1), MEM_ALIGN_WORD); argv[0] = "john"; for (index = 1; index < argc; index++) if (fgetl(line, sizeof(line), rec_file)) argv[index] = str_alloc_copy(line); else rec_format_error("fgets"); argv[argc] = NULL; save_rec_name = rec_name; opt_init(argv[0], argc, argv, 0); rec_name = save_rec_name; rec_name_completed = 1; if (fscanf(rec_file, "%u\n%u\n%x\n%x\n", &status_restored_time, &status.guess_count, &status.combs.lo, &status.combs.hi) != 4) rec_format_error("fscanf"); if (!status_restored_time) status_restored_time = 1; if (rec_version >= 4) { if (fscanf(rec_file, "%x\n%x\n%x\n%x\n%x\n%d\n", &status.combs_ehi, &status.crypts.lo, &status.crypts.hi, &status.cands.lo, &status.cands.hi, &status.compat) != 6) rec_format_error("fscanf"); } else { /* Historically, we were reusing what became the combs field for candidates * count when in --stdout mode */ status.cands = status.combs; status.compat = 1; } if (rec_version == 0) { status.pass = 0; status.progress = -1; } else if (fscanf(rec_file, "%d\n%d\n", &status.pass, &status.progress) != 2) rec_format_error("fscanf"); if (status.pass < 0 || status.pass > 3) rec_format_error(NULL); if (rec_version < 3) rec_check = 0; else if (fscanf(rec_file, "%x\n", &rec_check) != 1) rec_format_error("fscanf"); rec_restoring_now = 1; } void rec_restore_mode(int (*restore_mode)(FILE *file)) { rec_name_complete(); if (!rec_file) return; if (restore_mode) if (restore_mode(rec_file)) rec_format_error("fscanf"); if (options.flags & FLG_MASK_STACKED) if (mask_restore_state(rec_file)) rec_format_error("fscanf"); /* * Unlocking the file explicitly is normally not necessary since we're about to * close it anyway (which would normally release the lock). However, when * we're the main process running with --fork, our newborn children may hold a * copy of the fd for a moment (until they close the fd themselves). Thus, if * we don't explicitly remove the lock, there may be a race condition between * our children closing the fd and us proceeding to re-open and re-lock it. */ rec_unlock(); if (fclose(rec_file)) pexit("fclose"); rec_file = NULL; rec_restoring_now = 0; }
void rec_restore_args(int lock) { char line[LINE_BUFFER_SIZE]; int index, argc; char **argv; char *save_rec_name; rec_name_complete(); if (!(rec_file = fopen(path_expand(rec_name), "r+"))) { if (options.fork && !john_main_process && errno == ENOENT) { fprintf(stderr, "%u Session completed\n", options.node_min); if (options.flags & FLG_STATUS_CHK) return; log_event("No crash recovery file, terminating"); log_done(); exit(0); } pexit("fopen: %s", path_expand(rec_name)); } rec_fd = fileno(rec_file); if (lock) rec_lock(); if (!fgetl(line, sizeof(line), rec_file)) rec_format_error("fgets"); rec_version = 0; if (!strcmp(line, RECOVERY_V4)) rec_version = 4; else if (!strcmp(line, RECOVERY_V3)) rec_version = 3; else if (!strcmp(line, RECOVERY_V2)) rec_version = 2; else if (!strcmp(line, RECOVERY_V1)) rec_version = 1; else if (strcmp(line, RECOVERY_V0)) rec_format_error(NULL); if (fscanf(rec_file, "%d\n", &argc) != 1) rec_format_error("fscanf"); if (argc < 2) rec_format_error(NULL); argv = mem_alloc_tiny(sizeof(char *) * (argc + 1), MEM_ALIGN_WORD); argv[0] = "john"; for (index = 1; index < argc; index++) if (fgetl(line, sizeof(line), rec_file)) argv[index] = str_alloc_copy(line); else rec_format_error("fgets"); argv[argc] = NULL; save_rec_name = rec_name; opt_init(argv[0], argc, argv); rec_name = save_rec_name; rec_name_completed = 1; if (fscanf(rec_file, "%u\n%u\n%x\n%x\n", &status_restored_time, &status.guess_count, &status.combs.lo, &status.combs.hi) != 4) rec_format_error("fscanf"); if (!status_restored_time) status_restored_time = 1; if (rec_version >= 4) { if (fscanf(rec_file, "%x\n%x\n%x\n%x\n%x\n%d\n", &status.combs_ehi, &status.crypts.lo, &status.crypts.hi, &status.cands.lo, &status.cands.hi, &status.compat) != 6) rec_format_error("fscanf"); } else { /* Historically, we were reusing what became the combs field for candidates * count when in --stdout mode */ status.cands = status.combs; status.compat = 1; } if (rec_version == 0) { status.pass = 0; status.progress = -1; } else if (fscanf(rec_file, "%d\n%d\n", &status.pass, &status.progress) != 2) rec_format_error("fscanf"); if (status.pass < 0 || status.pass > 3) rec_format_error(NULL); if (rec_version < 3) rec_check = 0; else if (fscanf(rec_file, "%x\n", &rec_check) != 1) rec_format_error("fscanf"); rec_restoring_now = 1; }
int main(int argc,char **argv,char **envp) { int c,daemon=-1,rc,sesstype=SESSION_EMSI,line=0,call_flags=0; char *hostname=NULL,*str=NULL; FTNADDR_T(fa); #ifndef HAVE_SETPROCTITLE setargspace(argc,argv,envp); #endif #ifdef HAVE_SETLOCALE setlocale(LC_ALL, ""); #endif while((c=getopt(argc, argv, "hI:da:ni:c:tbv"))!=EOF) { switch(c) { case 'c': daemon=12; str=optarg; while(str&&*str) { switch(toupper(*str)) { case 'N': call_flags=0; break; case 'I': call_flags|=1; break; case 'A': call_flags|=2; break; default: write_log("unknown call option: %c", *optarg); exit(S_FAILURE); } str++; } break; case 'i': hostname=optarg; break; case 'I': configname=optarg; break; case 'd': daemon=1; break; case 'a': daemon=0; sesstype=SESSION_AUTO; if(!strncasecmp(optarg,"**emsi",6)|| !strncasecmp(optarg,"auto",4))sesstype=SESSION_EMSI; #ifdef WITH_BINKP if(strncasecmp(optarg,"binkp",5)&& (*optarg!=0x80||!optarg[1]||optarg[2]))break; sesstype=SESSION_BINKP; case 'b': bink=1; #endif break; case 'n': daemon=2; break; case 't': daemon=3; break; case 'v': u_vers(progname); default: usage(argv[0]); } } if(!hostname&&daemon<0)usage(argv[0]); getsysinfo();ssock=lins_sock=uis_sock=-1; if(!readconfig(configname)) { write_log("there was some errors parsing '%s', aborting",configname); exit(S_FAILURE); } if(!log_init(cfgs(CFG_MASTERLOG),NULL)) { write_log("can't open master log '%s'",ccs); exit(S_FAILURE); } #ifdef NEED_DEBUG parse_log_levels(); if(facilities_levels['C']>=1)dumpconfig(); #endif psubsts=parsesubsts(cfgfasl(CFG_SUBST)); #ifdef NEED_DEBUG if(facilities_levels['C']>=1) { subst_t *s; dialine_t *l; for(s=psubsts;s;s=s->next) { write_log("subst for %s [%d]",ftnaddrtoa(&s->addr),s->nhids); for(l=s->hiddens;l;l=l->next) write_log(" * %s,%s,%s,%d,%d",l->phone,l->host,l->timegaps,l->flags,l->num); } } #endif log_done(); if(daemon==3)exit(S_OK); if(hostname||daemon==12) { if(!parseftnaddr(argv[optind],&fa,&DEFADDR,0)) { write_log("can't parse address '%s'",argv[optind]); exit(S_FAILURE); } optind++; } if(hostname) { is_ip=1; rnode=xcalloc(1,sizeof(ninfo_t)); xstrcpy(ip_id,"ipline",10); rnode->tty=bink?"binkp":"tcpip"; if(!log_init(cfgs(CFG_LOG),rnode->tty)) { write_log("can't open log %s",ccs); exit(S_FAILURE); } signal(SIGINT,sigerr); signal(SIGTERM,sigerr); signal(SIGSEGV,sigerr); signal(SIGPIPE,SIG_IGN); IFPerl(perl_init(cfgs(CFG_PERLFILE),0)); log_callback=NULL;xsend_cb=NULL; ssock=cls_conn(CLS_LINE,cfgs(CFG_SERVER),NULL); if(ssock<0)write_log("can't connect to server: %s",strerror(errno)); else log_callback=vlogs; rc=aso_init(cfgs(CFG_ASOOUTBOUND),cfgs(CFG_BSOOUTBOUND),cfgs(CFG_QSTOUTBOUND),cfgal(CFG_ADDRESS)->addr.z); if(!rc) { write_log("No outbound defined"); stopit(S_FAILURE); } rc=do_call(&fa,hostname,NULL); aso_done(); stopit(rc); } if(daemon==12) { if(optind<argc) { if(1!=sscanf(argv[optind],"%d",&line)) { write_log("can't parse line number '%s'!\n",argv[optind]); exit(S_FAILURE); } } else line = 0; log_callback=NULL;xsend_cb=NULL; ssock=cls_conn(CLS_LINE,cfgs(CFG_SERVER),NULL); if(ssock<0)write_log("can't connect to server: %s",strerror(errno)); else log_callback=vlogs; rc=aso_init(cfgs(CFG_ASOOUTBOUND),cfgs(CFG_BSOOUTBOUND),cfgs(CFG_QSTOUTBOUND),cfgal(CFG_ADDRESS)->addr.z); if(!rc) { write_log("No outbound defined"); cls_close(ssock); exit(S_FAILURE); } if(aso_locknode(&fa,LCK_c)) { signal(SIGINT,sigerr); signal(SIGTERM,sigerr); signal(SIGSEGV,sigerr); signal(SIGPIPE,SIG_IGN); IFPerl(perl_init(cfgs(CFG_PERLFILE),0)); rc=force_call(&fa,line,call_flags); aso_unlocknode(&fa,LCK_x); } else rc=S_FAILURE; if(rc&S_MASK)write_log("can't call to %s",ftnaddrtoa(&fa)); aso_done(); stopit(rc); } switch(daemon) { case 1: daemon_mode(); break; case 0: answer_mode(sesstype); break; case 2: compile_nodelists(); break; } return S_OK; }