📄 chg-scsi-chio.c
字号:
void usage(char *argv[]){ int cnt; g_printf(_("%s: Usage error.\n"), argv[0]); for (cnt=0; cnt < COMCOUNT; cnt++){ g_printf(" %s %s",argv[0],argdefs[cnt].str); if (argdefs[cnt].takesparam) g_printf(_(" <param>\n")); else g_printf("\n"); } exit(2);}void parse_args(int argc, char *argv[],command *rval){ int i=0; if ((argc < 2) || (argc > 3)) { usage(argv); /*NOTREACHED*/ } while ((i<COMCOUNT)&&(strcmp(argdefs[i].str,argv[1]))) i++; if (i == COMCOUNT) { usage(argv); /*NOTREACHED*/ } rval->command_code = argdefs[i].command_code; if (argdefs[i].takesparam) { if (argc < 3) { usage(argv); /*NOTREACHED*/ } rval->parameter=argv[2]; } else { if (argc > 2) { usage(argv); /*NOTREACHED*/ } rval->parameter=0; }}/* used to find actual slot number from keywords next, prev, first, etc */intget_relative_target( int fd, int nslots, char * parameter, int loaded, char * changer_file, int slot_offset, int maxslot){ int current_slot,i; (void)loaded; /* Quiet unused warning */ if (changer_file != NULL) { current_slot=get_current_slot(changer_file); } else { current_slot = GetCurrentSlot(fd, 0); } if (current_slot > maxslot){ current_slot = slot_offset; } if (current_slot < slot_offset){ current_slot = slot_offset; } i=0; while((i < SLOTCOUNT) && (strcmp(slotdefs[i].str,parameter))) i++; switch(i) { case SLOT_CUR: break; case SLOT_NEXT: if (++current_slot==nslots+slot_offset) return slot_offset; break; case SLOT_PREV: if (--current_slot<slot_offset) return maxslot; break; case SLOT_FIRST: return slot_offset; case SLOT_LAST: return maxslot; default: g_printf(_("<none> no slot `%s'\n"),parameter); close(fd); exit(2); /*NOTREACHED*/ } return current_slot;}/* * This function should ask the drive if it wants to be cleaned */intask_clean( char * tapedev){ return get_clean_state(tapedev);}/* * This function should move the cleaning cartridge into the drive */voidclean_tape( int fd, char * tapedev, char * cnt_file, int drivenum, int cleancart, int maxclean, char * usagetime){ int counter; if (cleancart == -1 ){ return; } /* Now we should increment the counter */ if (cnt_file != NULL){ counter = get_current_slot(cnt_file); counter++; if (counter>=maxclean){ /* Now we should inform the administrator */ char *mail_cmd; FILE *mailf; int mail_pipe_opened = 1;#ifdef MAILER if(getconf_seen(CNF_MAILTO) && strlen(getconf_str(CNF_MAILTO)) > 0 && validate_mailto(getconf_str(CNF_MAILTO))) { mail_cmd = vstralloc(MAILER, " -s", " \"", _("AMANDA PROBLEM: PLEASE FIX"), "\"", " ", getconf_str(CNF_MAILTO), NULL); if((mailf = popen(mail_cmd, "w")) == NULL){ g_printf(_("Mail failed\n")); error(_("could not open pipe to \"%s\": %s"), mail_cmd, strerror(errno)); /*NOTREACHED*/ } } else { mail_pipe_opened = 0; mailf = stderr; g_fprintf(mailf, _("\nNo mail recipient specified, output redirected to stderr")); }#else mail_pipe_opened = 0; mailf = stderr; g_fprintf(mailf, _("\nNo mailer specified; output redirected to stderr"));#endif g_fprintf(mailf, _("\nThe usage count of your cleaning tape in slot %d"), cleancart); g_fprintf(mailf,_("\nis more than %d. (cleanmax)"),maxclean); g_fprintf(mailf,_("\nTapedrive %s needs to be cleaned"),tapedev); g_fprintf(mailf,_("\nPlease insert a new cleaning tape and reset")); g_fprintf(mailf,_("\nthe countingfile %s"),cnt_file); if(mail_pipe_opened == 1 && pclose(mailf) != 0) { error(_("mail command failed: %s"), mail_cmd); /*NOTREACHED*/ } return; } put_current_slot(cnt_file,counter); } load(fd,drivenum,cleancart); if (drive_loaded(fd, drivenum)) unload(fd,drivenum,cleancart); unlink(usagetime);}/* ----------------------------------------------------------------------*/intmain( int argc, char ** argv){ int loaded; int target = -1; int oldtarget; command com; /* a little DOS joke */ /* * drive_num really should be something from the config file, but.. * for now, it is set to zero, since most of the common changers * used by amanda only have one drive ( until someone wants to * use an EXB60/120, or a Breece Hill Q45.. ) */ int drive_num = 0; int need_eject = 0; /* Does the drive need an eject command ? */ unsigned need_sleep = 0; /* How many seconds to wait for the drive to get ready */ int clean_slot = -1; int maxclean = 0; char *clean_file=NULL; char *time_file=NULL; int use_slots; int slot_offset; int confnum; int fd, slotcnt, drivecnt; int endstatus = 0; char *changer_dev = NULL; char *tape_device = NULL; char *changer_file = NULL; char *scsitapedevice = NULL; /* * Configure program for internationalization: * 1) Only set the message locale for now. * 2) Set textdomain for all amanda related programs to "amanda" * We don't want to be forced to support dozens of message catalogs. */ setlocale(LC_MESSAGES, "C"); textdomain("amanda"); set_pname("chg-scsi"); /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); dbopen(DBG_SUBDIR_SERVER); parse_args(argc,argv,&com); changer = alloc(SIZEOF(changer_t)); config_init(CONFIG_INIT_USE_CWD | CONFIG_INIT_FATAL, NULL); changer_dev = getconf_str(CNF_CHANGERDEV); changer_file = getconf_str(CNF_CHANGERFILE); tape_device = getconf_str(CNF_TAPEDEV); /* Get the configuration parameters */ if (strlen(tape_device)==1){ read_config(changer_file, changer); confnum=atoi(tape_device); use_slots = changer->conf[confnum].end-changer->conf[confnum].start+1; slot_offset = changer->conf[confnum].start; drive_num = changer->conf[confnum].drivenum; need_eject = changer->eject; need_sleep = changer->sleep; clean_file = stralloc(changer->conf[confnum].cleanfile); clean_slot = changer->conf[confnum].cleanslot; maxclean = changer->cleanmax; if (NULL != changer->conf[confnum].timefile) time_file = stralloc(changer->conf[confnum].timefile); if (NULL != changer->conf[confnum].slotfile) changer_file = stralloc(changer->conf[confnum].slotfile); else changer_file = NULL; if (NULL != changer->conf[confnum].device) tape_device = stralloc(changer->conf[confnum].device); if (NULL != changer->device) changer_dev = stralloc(changer->device); if (NULL != changer->conf[confnum].scsitapedev) scsitapedevice = stralloc(changer->conf[confnum].scsitapedev); if (NULL != changer->conf[confnum].tapestatfile) tapestatfile = stralloc(changer->conf[confnum].tapestatfile); dump_changer_struct(changer); /* get info about the changer */ fd = OpenDevice(INDEX_CHANGER , changer_dev, "changer_dev", changer->conf[confnum].changerident); if (fd == -1) { int localerr = errno; g_fprintf(stderr, _("%s: open: %s: %s\n"), get_pname(), changer_dev, strerror(localerr)); g_printf(_("%s open: %s: %s\n"), "<none>", changer_dev, strerror(localerr)); dbprintf(_("open: %s: %s\n"), changer_dev, strerror(localerr)); return 2; } if (tape_device == NULL) { tape_device = stralloc(changer_dev); } if (scsitapedevice == NULL) { scsitapedevice = stralloc(tape_device); } if ((changer->conf[confnum].end == -1) || (changer->conf[confnum].start == -1)){ slotcnt = get_slot_count(fd); use_slots = slotcnt; slot_offset = 0; } free_changer_struct(&changer); } else { /* get info about the changer */ confnum = 0; fd = OpenDevice(INDEX_CHANGER , changer_dev, "changer_dev", changer->conf[confnum].changerident); if (fd == -1) { int localerr = errno; g_fprintf(stderr, _("%s: open: %s: %s\n"), get_pname(), changer_dev, strerror(localerr)); g_printf(_("%s open: %s: %s\n"), _("<none>"), changer_dev, strerror(localerr)); dbprintf(_("open: %s: %s\n"), changer_dev, strerror(localerr)); return 2; } slotcnt = get_slot_count(fd); use_slots = slotcnt; slot_offset = 0; drive_num = 0; need_eject = 0; need_sleep = 0; } drivecnt = get_drive_count(fd); if (drive_num > drivecnt) { g_printf(_("%s drive number error (%d > %d)\n"), _("<none>"), drive_num, drivecnt); g_fprintf(stderr, _("%s: requested drive number (%d) greater than " "number of supported drives (%d)\n"), get_pname(), drive_num, drivecnt); dbprintf(_("requested drive number (%d) greater than " "number of supported drives (%d)\n"), drive_num, drivecnt); CloseDevice("", fd); return 2; } loaded = drive_loaded(fd, drive_num); switch(com.command_code) { case COM_SLOT: /* slot changing command */ if (is_positive_number(com.parameter)) { if ((target = atoi(com.parameter))>=use_slots) { g_printf(_("<none> no slot `%d'\n"),target); close(fd); endstatus = 2; break; } else { target = target+slot_offset; } } else target=get_relative_target(fd, use_slots, com.parameter, loaded, changer_file,slot_offset,slot_offset+use_slots); if (loaded) { if (changer_file != NULL) { oldtarget=get_current_slot(changer_file); } else { oldtarget = GetCurrentSlot(fd, drive_num); } if ((oldtarget)!=target) { if (need_eject) eject_tape(scsitapedevice, need_eject); (void)unload(fd, drive_num, oldtarget); if (ask_clean(scsitapedevice)) clean_tape(fd,tape_device,clean_file,drive_num, clean_slot,maxclean,time_file); loaded=0; } } if (changer_file != NULL) { put_current_slot(changer_file, target); } if (!loaded && isempty(fd, target)) { g_printf(_("%d slot %d is empty\n"),target-slot_offset, target-slot_offset); close(fd); endstatus = 1; break; } if (!loaded) if (load(fd, drive_num, target) != 0) { g_printf(_("%d slot %d move failed\n"),target-slot_offset, target-slot_offset); close(fd); endstatus = 2; break; } if (need_sleep) Tape_Ready1(scsitapedevice, need_sleep); g_printf(_("%d %s\n"), target-slot_offset, tape_device); break; case COM_INFO: if (changer_file != NULL) { g_printf("%d ", get_current_slot(changer_file)-slot_offset); } else { g_printf("%d ", GetCurrentSlot(fd, drive_num)-slot_offset); } g_printf("%d 1\n", use_slots); break; case COM_RESET: if (changer_file != NULL) { target=get_current_slot(changer_file); } else { target = GetCurrentSlot(fd, drive_num); } if (loaded) { if (!isempty(fd, target)) target=find_empty(fd,0 ,0); if (need_eject) eject_tape(scsitapedevice, need_eject); (void)unload(fd, drive_num, target); if (ask_clean(scsitapedevice)) clean_tape(fd,tape_device,clean_file,drive_num,clean_slot, maxclean,time_file); } if (isempty(fd, slot_offset)) { g_printf(_("0 slot 0 is empty\n")); close(fd); endstatus = 1; break; } if (load(fd, drive_num, slot_offset) != 0) { g_printf(_("%d slot %d move failed\n"),slot_offset, slot_offset); close(fd); endstatus = 2; break; } if (changer_file != NULL) { put_current_slot(changer_file, slot_offset); } if (need_sleep) Tape_Ready1(scsitapedevice, need_sleep); if (changer_file != NULL) { g_printf("%d %s\n", get_current_slot(changer_file), tape_device); } else { g_printf("%d %s\n", GetCurrentSlot(fd, drive_num), tape_device); } break; case COM_EJECT: if (loaded) { if (changer_file != NULL) { target=get_current_slot(changer_file); } else { target = GetCurrentSlot(fd, drive_num); } if (need_eject) eject_tape(scsitapedevice, need_eject); (void)unload(fd, drive_num, target); if (ask_clean(scsitapedevice)) clean_tape(fd,tape_device,clean_file,drive_num,clean_slot, maxclean,time_file); g_printf("%d %s\n", target, tape_device); } else { g_printf(_("%d drive was not loaded\n"), target); endstatus = 1; } break; case COM_CLEAN: if (loaded) { if (changer_file != NULL) { target=get_current_slot(changer_file); } else { target = GetCurrentSlot(fd, drive_num); } if (need_eject) eject_tape(scsitapedevice, need_eject); (void)unload(fd, drive_num, target); } clean_tape(fd,tape_device,clean_file,drive_num,clean_slot, maxclean,time_file); g_printf(_("%s cleaned\n"), tape_device); break; }; CloseDevice("", 0); dbclose(); return endstatus;}/* * Local variables: * indent-tabs-mode: nil * tab-width: 4 * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -