2 * $Id: chg-scsi-chio.c,v 1.12 2006/07/25 18:18:46 martinea Exp $
4 * chg-scsi-chio.c -- generic SCSI changer driver
6 * This program provides a driver to control generic
7 * SCSI changers, no matter what platform. The host/OS
8 * specific portions of the interface are implemented
9 * in libscsi.a, which contains a module for each host/OS.
10 * The actual interface for HP/UX is in scsi-hpux.c;
11 * chio is in scsi-chio.c, etc.. A prototype system
12 * dependent scsi interface file is in scsi-proto.c.
14 * Copyright 1997, 1998 Eric Schnoebelen <eric@cirr.com>
16 * This module based upon seagate-changer, by Larry Pyeatt
17 * <pyeatt@cs.colostate.edu>
19 * The original introductory comments follow:
21 * This program was written to control the Seagate/Conner/Archive
22 * autoloading DAT drive. This drive normally has 4 tape capacity
23 * but can be expanded to 12 tapes with an optional tape cartridge.
24 * This program may also work on onther drives. Try it and let me
25 * know of successes/failures.
27 * I have attempted to conform to the requirements for Amanda tape
28 * changer interface. There could be some bugs.
30 * This program works for me under Linux with Gerd Knorr's
31 * <kraxel@cs.tu-berlin.de> SCSI media changer driver installed
32 * as a kernel module. The kernel module is available at
33 * http://sunsite.unc.edu/pub/Linux/kernel/patches/scsi/scsi-changer*
34 * Since the Linux media changer is based on NetBSD, this program
35 * should also work for NetBSD, although I have not tried it.
36 * It may be necessary to change the IOCTL calls to work on other
39 * (c) 1897 Larry Pyeatt, pyeatt@cs.colostate.edu
40 * All Rights Reserved.
42 * Permission to use, copy, modify, distribute, and sell this software and its
43 * documentation for any purpose is hereby granted without fee, provided that
44 * the above copyright notice appear in all copies and that both that
45 * copyright notice and this permission notice appear in supporting
46 * documentation. The author makes no representations about the
47 * suitability of this software for any purpose. It is provided "as is"
48 * without express or implied warranty.
50 * Michael C. Povel 03.06.98 added ejetct_tape and sleep for external tape
51 * devices, and changed some code to allow multiple drives to use their
52 * own slots. Also added support for reserverd slots.
53 * At the moment these parameters are hard coded and only tested under Linux
61 #include "scsi-defs.h"
62 int Tape_Ready1 ( char *tapedev , int wait);
64 char *tapestatfile = NULL;
66 /*----------------------------------------------------------------------------*/
69 NUMDRIVE,EJECT,SLEEP,CLEANMAX,DRIVE,START,END,CLEAN,DEVICE,STATFILE,CLEANFILE,DRIVENUM,
70 CHANGERDEV,USAGECOUNT,SCSITAPEDEV, TAPESTATFILE
78 tokentable_t t_table[]={
79 { "number_configs", NUMDRIVE},
82 { "cleanmax", CLEANMAX},
86 { "cleancart", CLEAN},
88 { "statfile", STATFILE},
89 { "cleanfile", CLEANFILE},
90 { "drivenum", DRIVENUM},
91 { "changerdev", CHANGERDEV},
92 { "usagecount", USAGECOUNT},
93 { "scsitapedev", SCSITAPEDEV},
94 { "tapestatus", TAPESTATFILE},
100 void init_changer_struct(changer_t *chg, size_t number_of_config);
101 void dump_changer_struct(changer_t *chg);
102 void free_changer_struct(changer_t **changer);
103 void parse_line(char *linebuffer,int *token,char **value);
104 int read_config(char *configfile, changer_t *chg);
105 int get_current_slot(char *count_file);
106 void put_current_slot(char *count_file,int slot);
107 void usage(char *argv[]);
108 int get_relative_target(int fd,int nslots,char *parameter,int loaded,
109 char *changer_file,int slot_offset,int maxslot);
110 int is_positive_number(char *tmp);
111 int ask_clean(char *tapedev);
112 void clean_tape(int fd,char *tapedev,char *cnt_file, int drivenum,
113 int cleancart, int maxclean,char *usagetime);
114 int main(int argc, char *argv[]);
118 * Initialize data structures with default values
123 size_t number_of_config)
127 memset(chg, 0, SIZEOF(*chg));
128 chg->number_of_configs = number_of_config;
133 chg->conf = alloc(SIZEOF(config_t) * number_of_config);
134 for (i=0; i < number_of_config; i++){
135 chg->conf[i].drivenum = 0;
136 chg->conf[i].start = -1;
137 chg->conf[i].end = -1;
138 chg->conf[i].cleanslot = -1;
139 chg->conf[i].device = NULL;
140 chg->conf[i].slotfile = NULL;
141 chg->conf[i].cleanfile = NULL;
142 chg->conf[i].timefile = NULL;
143 chg->conf[i].scsitapedev = NULL;
144 chg->conf[i].tapestatfile = NULL;
145 chg->conf[i].changerident = NULL;
146 chg->conf[i].tapeident = NULL;
151 * Dump of information for debug
159 dbprintf(_("Number of configurations: %d\n"), chg->number_of_configs);
160 dbprintf(_("Tapes need eject: %s\n"), (chg->eject>0 ? _("Yes") : _("No")));
161 dbprintf(_("Tapes need sleep: %lld seconds\n"), (long long)chg->sleep);
162 dbprintf(_("Clean cycles : %d\n"), chg->cleanmax);
163 dbprintf(_("Changer device : %s\n"), chg->device);
164 for (i = 0; i < chg->number_of_configs; i++){
165 dbprintf(_("Tape config Nr: %d\n"), i);
166 dbprintf(_(" Drive number : %d\n"), chg->conf[i].drivenum);
167 dbprintf(_(" Start slot : %d\n"), chg->conf[i].start);
168 dbprintf(_(" End slot : %d\n"), chg->conf[i].end);
169 dbprintf(_(" Clean slot : %d\n"), chg->conf[i].cleanslot);
170 if (chg->conf[i].device != NULL)
171 dbprintf(_(" Device name : %s\n"), chg->conf[i].device);
173 dbprintf(_(" Device name : none\n"));
174 if (chg->conf[i].scsitapedev != NULL)
175 dbprintf(_(" SCSI Tape dev : %s\n"), chg->conf[i].scsitapedev);
177 dbprintf(_(" SCSI Tape dev : none\n"));
178 if (chg->conf[i].tapestatfile != NULL)
179 dbprintf(_(" stat file : %s\n"), chg->conf[i].tapestatfile);
181 dbprintf(_(" stat file : none\n"));
182 if (chg->conf[i].slotfile != NULL)
183 dbprintf(_(" Slot file : %s\n"), chg->conf[i].slotfile);
185 dbprintf(_(" Slot file : none\n"));
186 if (chg->conf[i].cleanfile != NULL)
187 dbprintf(_(" Clean file : %s\n"), chg->conf[i].cleanfile);
189 dbprintf(_(" Clean file : none\n"));
190 if (chg->conf[i].timefile != NULL)
191 dbprintf(_(" Usage count : %s\n"), chg->conf[i].timefile);
193 dbprintf(_(" Usage count : none\n"));
198 * Free all allocated memory
207 assert(changer != NULL);
208 assert(*changer != NULL);
211 if (chg->device != NULL)
213 for (i=0; i<chg->number_of_configs; i++){
214 if (chg->conf[i].device != NULL)
215 amfree(chg->conf[i].device);
216 if (chg->conf[i].slotfile != NULL)
217 amfree(chg->conf[i].slotfile);
218 if (chg->conf[i].cleanfile != NULL)
219 amfree(chg->conf[i].cleanfile);
220 if (chg->conf[i].timefile != NULL)
221 amfree(chg->conf[i].timefile);
223 if (chg->conf != NULL)
231 * This function parses a line, and returns a token and value
242 *token = -1; /* No Token found */
243 tok=strtok(linebuffer," \t\n");
245 while ((tok != NULL) && (tok[0]!='#')&&(ready==0)){
251 while ((t_table[i].word != NULL)&&(*token==-1)){
252 if (0==strcasecmp(t_table[i].word,tok)){
253 *token=t_table[i].token;
258 tok=strtok(NULL," \t\n");
264 * This function reads the specified configfile and fills the structure
279 numconf = 1; /* At least one configuration is assumed */
280 /* If there are more, it should be the first entry in the configurationfile */
282 if (NULL==(file=fopen(configfile,"r"))){
286 while (NULL != (linebuffer = agets(file))) {
287 if (linebuffer[0] == '\0') {
291 parse_line(linebuffer,&token,&value);
294 if (token != NUMDRIVE){
295 init_changer_struct(chg, numconf);
297 numconf = atoi(value);
298 init_changer_struct(chg, numconf);
303 case NUMDRIVE: if ((size_t)atoi(value) != numconf)
304 g_fprintf(stderr,_("Error: number_drives at wrong place, should be "
308 chg->eject = atoi(value);
311 chg->sleep = atoi(value);
314 chg->device = stralloc(value);
317 chg->conf[drivenum].scsitapedev = stralloc(value);
320 chg->conf[drivenum].tapestatfile = stralloc(value);
323 chg->cleanmax = atoi(value);
326 drivenum = atoi(value);
327 if(drivenum >= numconf){
328 g_fprintf(stderr,_("Error: drive must be less than number_drives\n"));
332 if (drivenum < numconf){
333 chg->conf[drivenum].drivenum = atoi(value);
335 g_fprintf(stderr,_("Error: drive is not less than number_drives"
336 " drivenum ignored\n"));
340 if (drivenum < numconf){
341 chg->conf[drivenum].start = atoi(value);
343 g_fprintf(stderr,_("Error: drive is not less than number_drives"
344 " startuse ignored\n"));
348 if (drivenum < numconf){
349 chg->conf[drivenum].end = atoi(value);
351 g_fprintf(stderr,_("Error: drive is not less than number_drives"
352 " enduse ignored\n"));
356 if (drivenum < numconf){
357 chg->conf[drivenum].cleanslot = atoi(value);
359 g_fprintf(stderr,_("Error: drive is not less than number_drives"
360 " cleanslot ignored\n"));
364 if (drivenum < numconf){
365 chg->conf[drivenum].device = stralloc(value);
367 g_fprintf(stderr,_("Error: drive is not less than number_drives"
368 " device ignored\n"));
372 if (drivenum < numconf){
373 chg->conf[drivenum].slotfile = stralloc(value);
375 g_fprintf(stderr,_("Error: drive is not less than number_drives"
376 " slotfile ignored\n"));
380 if (drivenum < numconf){
381 chg->conf[drivenum].cleanfile = stralloc(value);
383 g_fprintf(stderr,_("Error: drive is not less than number_drives"
384 " cleanfile ignored\n"));
388 if (drivenum < numconf){
389 chg->conf[drivenum].timefile = stralloc(value);
391 g_fprintf(stderr,_("Error: drive is not less than number_drives"
392 " usagecount ignored\n"));
396 g_fprintf(stderr,_("Error: Unknown token\n"));
408 /*----------------------------------------------------------------------------*/
410 /* The tape drive does not have an idea of current slot so
411 * we use a file to store the current slot. It is not ideal
412 * but it gets the job done
414 int get_current_slot(char *count_file)
418 if ((inf=fopen(count_file,"r")) == NULL) {
419 g_fprintf(stderr, _("%s: unable to open current slot file (%s)\n"),
420 get_pname(), count_file);
424 if (fscanf(inf, "%d", &retval) != 1) {
425 g_fprintf(stderr, _("%s: unable to read current slot file (%s)\n"),
426 get_pname(), count_file);
434 void put_current_slot(char *count_file,int slot)
438 if ((inf=fopen(count_file,"w")) == NULL) {
439 g_fprintf(stderr, _("%s: unable to open current slot file (%s)\n"),
440 get_pname(), count_file);
443 g_fprintf(inf, "%d\n", slot);
447 /* ----------------------------------------------------------------------
448 This stuff deals with parsing the command line */
450 typedef struct com_arg
458 typedef struct com_stru
465 void parse_args(int argc, char *argv[], command *rval);
467 /* major command line args */
474 argument argdefs[]={{"-slot",COM_SLOT,1},
475 {"-info",COM_INFO,0},
476 {"-reset",COM_RESET,0},
477 {"-eject",COM_EJECT,0},
478 {"-clean",COM_CLEAN,0}};
481 /* minor command line args */
488 argument slotdefs[]={{"current",SLOT_CUR,0},
489 {"next",SLOT_NEXT,0},
490 {"prev",SLOT_PREV,0},
491 {"first",SLOT_FIRST,0},
492 {"last",SLOT_LAST,0}};
494 int is_positive_number(char *tmp) /* is the string a valid positive int? */
497 if ((tmp==NULL)||(tmp[0]==0))
499 while ((tmp[i]>='0')&&(tmp[i]<='9')&&(tmp[i]!=0))
507 void usage(char *argv[])
510 g_printf(_("%s: Usage error.\n"), argv[0]);
511 for (cnt=0; cnt < COMCOUNT; cnt++){
512 g_printf(" %s %s",argv[0],argdefs[cnt].str);
513 if (argdefs[cnt].takesparam)
514 g_printf(_(" <param>\n"));
522 void parse_args(int argc, char *argv[],command *rval)
525 if ((argc < 2) || (argc > 3)) {
530 while ((i<COMCOUNT)&&(strcmp(argdefs[i].str,argv[1])))
536 rval->command_code = argdefs[i].command_code;
537 if (argdefs[i].takesparam) {
542 rval->parameter=argv[2];
553 /* used to find actual slot number from keywords next, prev, first, etc */
566 (void)loaded; /* Quiet unused warning */
567 if (changer_file != NULL)
569 current_slot=get_current_slot(changer_file);
571 current_slot = GetCurrentSlot(fd, 0);
573 if (current_slot > maxslot){
574 current_slot = slot_offset;
576 if (current_slot < slot_offset){
577 current_slot = slot_offset;
581 while((i < SLOTCOUNT) && (strcmp(slotdefs[i].str,parameter)))
589 if (++current_slot==nslots+slot_offset)
594 if (--current_slot<slot_offset)
605 g_printf(_("<none> no slot `%s'\n"),parameter);
614 * This function should ask the drive if it wants to be cleaned
620 return get_clean_state(tapedev);
624 * This function should move the cleaning cartridge into the drive
638 if (cleancart == -1 ){
642 /* Now we should increment the counter */
643 if (cnt_file != NULL){
644 counter = get_current_slot(cnt_file);
646 if (counter>=maxclean){
647 /* Now we should inform the administrator */
650 int mail_pipe_opened = 1;
652 if(getconf_seen(CNF_MAILTO) && strlen(getconf_str(CNF_MAILTO)) > 0 &&
653 validate_mailto(getconf_str(CNF_MAILTO))) {
654 mail_cmd = vstralloc(MAILER,
655 " -s", " \"", _("AMANDA PROBLEM: PLEASE FIX"), "\"",
656 " ", getconf_str(CNF_MAILTO),
658 if((mailf = popen(mail_cmd, "w")) == NULL){
659 g_printf(_("Mail failed\n"));
660 error(_("could not open pipe to \"%s\": %s"),
661 mail_cmd, strerror(errno));
665 mail_pipe_opened = 0;
667 g_fprintf(mailf, _("\nNo mail recipient specified, output redirected to stderr"));
670 mail_pipe_opened = 0;
672 g_fprintf(mailf, _("\nNo mailer specified; output redirected to stderr"));
674 g_fprintf(mailf, _("\nThe usage count of your cleaning tape in slot %d"),
676 g_fprintf(mailf,_("\nis more than %d. (cleanmax)"),maxclean);
677 g_fprintf(mailf,_("\nTapedrive %s needs to be cleaned"),tapedev);
678 g_fprintf(mailf,_("\nPlease insert a new cleaning tape and reset"));
679 g_fprintf(mailf,_("\nthe countingfile %s"),cnt_file);
681 if(mail_pipe_opened == 1 && pclose(mailf) != 0) {
682 error(_("mail command failed: %s"), mail_cmd);
689 put_current_slot(cnt_file,counter);
691 load(fd,drivenum,cleancart);
693 if (drive_loaded(fd, drivenum))
694 unload(fd,drivenum,cleancart);
697 /* ----------------------------------------------------------------------*/
707 command com; /* a little DOS joke */
710 * drive_num really should be something from the config file, but..
711 * for now, it is set to zero, since most of the common changers
712 * used by amanda only have one drive ( until someone wants to
713 * use an EXB60/120, or a Breece Hill Q45.. )
716 int need_eject = 0; /* Does the drive need an eject command ? */
717 unsigned need_sleep = 0; /* How many seconds to wait for the drive to get ready */
720 char *clean_file=NULL;
721 char *time_file=NULL;
727 int fd, slotcnt, drivecnt;
729 char *changer_dev = NULL;
730 char *tape_device = NULL;
731 char *changer_file = NULL;
732 char *scsitapedevice = NULL;
735 * Configure program for internationalization:
736 * 1) Only set the message locale for now.
737 * 2) Set textdomain for all amanda related programs to "amanda"
738 * We don't want to be forced to support dozens of message catalogs.
740 setlocale(LC_MESSAGES, "C");
741 textdomain("amanda");
743 set_pname("chg-scsi");
745 /* Don't die when child closes pipe */
746 signal(SIGPIPE, SIG_IGN);
748 dbopen(DBG_SUBDIR_SERVER);
749 parse_args(argc,argv,&com);
751 changer = alloc(SIZEOF(changer_t));
752 config_init(CONFIG_INIT_USE_CWD | CONFIG_INIT_FATAL, NULL);
754 changer_dev = getconf_str(CNF_CHANGERDEV);
755 changer_file = getconf_str(CNF_CHANGERFILE);
756 tape_device = getconf_str(CNF_TAPEDEV);
758 /* Get the configuration parameters */
760 if (strlen(tape_device)==1){
761 read_config(changer_file, changer);
762 confnum=atoi(tape_device);
763 use_slots = changer->conf[confnum].end-changer->conf[confnum].start+1;
764 slot_offset = changer->conf[confnum].start;
765 drive_num = changer->conf[confnum].drivenum;
766 need_eject = changer->eject;
767 need_sleep = changer->sleep;
768 clean_file = stralloc(changer->conf[confnum].cleanfile);
769 clean_slot = changer->conf[confnum].cleanslot;
770 maxclean = changer->cleanmax;
771 if (NULL != changer->conf[confnum].timefile)
772 time_file = stralloc(changer->conf[confnum].timefile);
773 if (NULL != changer->conf[confnum].slotfile)
774 changer_file = stralloc(changer->conf[confnum].slotfile);
777 if (NULL != changer->conf[confnum].device)
778 tape_device = stralloc(changer->conf[confnum].device);
779 if (NULL != changer->device)
780 changer_dev = stralloc(changer->device);
781 if (NULL != changer->conf[confnum].scsitapedev)
782 scsitapedevice = stralloc(changer->conf[confnum].scsitapedev);
783 if (NULL != changer->conf[confnum].tapestatfile)
784 tapestatfile = stralloc(changer->conf[confnum].tapestatfile);
785 dump_changer_struct(changer);
786 /* get info about the changer */
787 fd = OpenDevice(INDEX_CHANGER , changer_dev,
788 "changer_dev", changer->conf[confnum].changerident);
790 int localerr = errno;
791 g_fprintf(stderr, _("%s: open: %s: %s\n"), get_pname(),
792 changer_dev, strerror(localerr));
793 g_printf(_("%s open: %s: %s\n"), "<none>", changer_dev, strerror(localerr));
794 dbprintf(_("open: %s: %s\n"), changer_dev, strerror(localerr));
798 if (tape_device == NULL)
800 tape_device = stralloc(changer_dev);
803 if (scsitapedevice == NULL)
805 scsitapedevice = stralloc(tape_device);
808 if ((changer->conf[confnum].end == -1) || (changer->conf[confnum].start == -1)){
809 slotcnt = get_slot_count(fd);
813 free_changer_struct(&changer);
815 /* get info about the changer */
817 fd = OpenDevice(INDEX_CHANGER , changer_dev,
818 "changer_dev", changer->conf[confnum].changerident);
820 int localerr = errno;
821 g_fprintf(stderr, _("%s: open: %s: %s\n"), get_pname(),
822 changer_dev, strerror(localerr));
823 g_printf(_("%s open: %s: %s\n"), _("<none>"), changer_dev, strerror(localerr));
824 dbprintf(_("open: %s: %s\n"), changer_dev, strerror(localerr));
827 slotcnt = get_slot_count(fd);
835 drivecnt = get_drive_count(fd);
837 if (drive_num > drivecnt) {
838 g_printf(_("%s drive number error (%d > %d)\n"), _("<none>"),
839 drive_num, drivecnt);
840 g_fprintf(stderr, _("%s: requested drive number (%d) greater than "
841 "number of supported drives (%d)\n"), get_pname(),
842 drive_num, drivecnt);
843 dbprintf(_("requested drive number (%d) greater than "
844 "number of supported drives (%d)\n"), drive_num, drivecnt);
849 loaded = drive_loaded(fd, drive_num);
851 switch(com.command_code) {
852 case COM_SLOT: /* slot changing command */
853 if (is_positive_number(com.parameter)) {
854 if ((target = atoi(com.parameter))>=use_slots) {
855 g_printf(_("<none> no slot `%d'\n"),target);
860 target = target+slot_offset;
863 target=get_relative_target(fd, use_slots,
866 changer_file,slot_offset,slot_offset+use_slots);
868 if (changer_file != NULL)
870 oldtarget=get_current_slot(changer_file);
872 oldtarget = GetCurrentSlot(fd, drive_num);
874 if ((oldtarget)!=target) {
876 eject_tape(scsitapedevice, need_eject);
877 (void)unload(fd, drive_num, oldtarget);
878 if (ask_clean(scsitapedevice))
879 clean_tape(fd,tape_device,clean_file,drive_num,
880 clean_slot,maxclean,time_file);
884 if (changer_file != NULL)
886 put_current_slot(changer_file, target);
888 if (!loaded && isempty(fd, target)) {
889 g_printf(_("%d slot %d is empty\n"),target-slot_offset,
896 if (load(fd, drive_num, target) != 0) {
897 g_printf(_("%d slot %d move failed\n"),target-slot_offset,
904 Tape_Ready1(scsitapedevice, need_sleep);
905 g_printf(_("%d %s\n"), target-slot_offset, tape_device);
909 if (changer_file != NULL)
911 g_printf("%d ", get_current_slot(changer_file)-slot_offset);
913 g_printf("%d ", GetCurrentSlot(fd, drive_num)-slot_offset);
915 g_printf("%d 1\n", use_slots);
919 if (changer_file != NULL)
921 target=get_current_slot(changer_file);
923 target = GetCurrentSlot(fd, drive_num);
926 if (!isempty(fd, target))
927 target=find_empty(fd,0 ,0);
929 eject_tape(scsitapedevice, need_eject);
930 (void)unload(fd, drive_num, target);
931 if (ask_clean(scsitapedevice))
932 clean_tape(fd,tape_device,clean_file,drive_num,clean_slot,
936 if (isempty(fd, slot_offset)) {
937 g_printf(_("0 slot 0 is empty\n"));
943 if (load(fd, drive_num, slot_offset) != 0) {
944 g_printf(_("%d slot %d move failed\n"),slot_offset,
950 if (changer_file != NULL)
952 put_current_slot(changer_file, slot_offset);
955 Tape_Ready1(scsitapedevice, need_sleep);
956 if (changer_file != NULL)
958 g_printf("%d %s\n", get_current_slot(changer_file), tape_device);
960 g_printf("%d %s\n", GetCurrentSlot(fd, drive_num), tape_device);
966 if (changer_file != NULL)
968 target=get_current_slot(changer_file);
970 target = GetCurrentSlot(fd, drive_num);
973 eject_tape(scsitapedevice, need_eject);
974 (void)unload(fd, drive_num, target);
975 if (ask_clean(scsitapedevice))
976 clean_tape(fd,tape_device,clean_file,drive_num,clean_slot,
978 g_printf("%d %s\n", target, tape_device);
980 g_printf(_("%d drive was not loaded\n"), target);
986 if (changer_file != NULL)
988 target=get_current_slot(changer_file);
990 target = GetCurrentSlot(fd, drive_num);
993 eject_tape(scsitapedevice, need_eject);
994 (void)unload(fd, drive_num, target);
996 clean_tape(fd,tape_device,clean_file,drive_num,clean_slot,
998 g_printf(_("%s cleaned\n"), tape_device);
1008 * indent-tabs-mode: nil