1 static char rcsid[] = "$Id: chg-scsi.c,v 1.52 2006/07/25 18:18:46 martinea Exp $";
5 * chg-scsi.c -- generic SCSI changer driver
7 * This program provides the framework to control
8 * SCSI changers. It is based on the original chg-scsi
9 * from Eric Schnoebelen <eric@cirr.com> (Original copyright below)
10 * The device dependent part is handled by scsi-changer-driver.c
11 * The SCSI OS interface is handled by scsi-ostype.c
13 * Original copyrights:
15 * This program provides a driver to control generic
16 * SCSI changers, no matter what platform. The host/OS
17 * specific portions of the interface are implemented
18 * in libscsi.a, which contains a module for each host/OS.
19 * The actual interface for HP/UX is in scsi-hpux.c;
20 * chio is in scsi-chio.c, etc.. A prototype system
21 * dependent scsi interface file is in scsi-proto.c.
23 * Copyright 1997, 1998 Eric Schnoebelen <eric@cirr.com>
25 * This module based upon seagate-changer, by Larry Pyeatt
26 * <pyeatt@cs.colostate.edu>
28 * The original introductory comments follow:
30 * This program was written to control the Seagate/Conner/Archive
31 * autoloading DAT drive. This drive normally has 4 tape capacity
32 * but can be expanded to 12 tapes with an optional tape cartridge.
33 * This program may also work on onther drives. Try it and let me
34 * know of successes/failures.
36 * I have attempted to conform to the requirements for Amanda tape
37 * changer interface. There could be some bugs.
39 * This program works for me under Linux with Gerd Knorr's
40 * <kraxel@cs.tu-berlin.de> SCSI media changer driver installed
41 * as a kernel module. The kernel module is available at
42 * http://sunsite.unc.edu/pub/Linux/kernel/patches/scsi/scsi-changer*
43 * Since the Linux media changer is based on NetBSD, this program
44 * should also work for NetBSD, although I have not tried it.
45 * It may be necessary to change the IOCTL calls to work on other
48 * (c) 1897 Larry Pyeatt, pyeatt@cs.colostate.edu
49 * All Rights Reserved.
51 * Permission to use, copy, modify, distribute, and sell this software and its
52 * documentation for any purpose is hereby granted without fee, provided that
53 * the above copyright notice appear in all copies and that both that
54 * copyright notice and this permission notice appear in supporting
55 * documentation. The author makes no representations about the
56 * suitability of this software for any purpose. It is provided "as is"
57 * without express or implied warranty.
59 * Michael C. Povel 03.06.98 added ejetct_tape and sleep for external tape
60 * devices, and changed some code to allow multiple drives to use their
61 * own slots. Also added support for reserverd slots.
62 * At the moment these parameters are hard coded and only tested under Linux
71 #include "scsi-defs.h"
74 char *tapestatfile = NULL;
75 FILE *debug_file = NULL;
78 * So we have 3 devices, here will all the infos be stored after an
82 OpenFiles_T *pDev = NULL;
84 /* Defined in scsi-changer-driver.c
86 extern int ElementStatusValid;
87 extern ElementInfo_T *pMTE; /*Medium Transport Element */
88 extern ElementInfo_T *pSTE; /*Storage Element */
89 extern ElementInfo_T *pIEE; /*Import Export Element */
90 extern ElementInfo_T *pDTE; /*Data Transfer Element */
91 extern size_t MTE; /*Counter for the above element types */
96 int do_inventory = 0; /* Set if load/unload functions thinks an inventory should be done */
100 NUMDRIVE,EJECT,SLEEP,CLEANMAX,DRIVE,START,END,CLEAN,DEVICE,STATFILE,CLEANFILE,DRIVENUM,
101 CHANGERDEV,USAGECOUNT,SCSITAPEDEV, TAPESTATFILE, LABELFILE, CHANGERIDENT,
102 TAPEIDENT, EMUBARCODE, HAVEBARCODE, DEBUGLEVEL, AUTOINV
110 tokentable_t t_table[] = {
111 { "number_configs", NUMDRIVE},
112 { "autoinv", AUTOINV},
115 { "cleanmax", CLEANMAX},
117 { "startuse", START},
119 { "cleancart", CLEAN},
121 { "statfile", STATFILE},
122 { "cleanfile", CLEANFILE},
123 { "drivenum", DRIVENUM},
124 { "changerdev", CHANGERDEV},
125 { "usagecount", USAGECOUNT},
126 { "scsitapedev", SCSITAPEDEV},
127 { "tapestatus", TAPESTATFILE},
128 { "labelfile", LABELFILE},
129 { "changerident", CHANGERIDENT},
130 { "tapeident", TAPEIDENT},
131 { "emubarcode", EMUBARCODE},
132 { "havebarcode", HAVEBARCODE},
133 { "debuglevel", DEBUGLEVEL},
137 int ask_clean(char *tapedev);
138 int get_current_slot(char *count_file);
139 int get_relative_target(int fd, int nslots, char *parameter,
140 int param_index, int loaded, char *slot_file,
141 int slot_offset, int maxslot);
142 int is_positive_number(char *tmp);
143 int MapBarCode(char *labelfile, MBC_T *result);
144 int read_config(char *configfile, changer_t *chg);
145 void clean_tape(int fd, char *tapedev, char *cnt_file, int drivenum,
146 int cleancart, int maxclean, char *usagetime);
147 void dump_changer_struct(changer_t *chg);
148 void free_changer_struct(changer_t **chg);
149 void init_changer_struct(changer_t *chg, int number_of_config);
150 void parse_line(char *linebuffer, int *token,char **value);
151 void put_current_slot(char *count_file, int slot);
152 void usage(char *argv[]);
154 int main(int argc, char *argv[]);
157 /* Initialize data structures with default values */
161 int number_of_config)
165 memset(chg, 0, SIZEOF(*chg));
166 chg->number_of_configs = number_of_config;
168 chg->conf = alloc(SIZEOF(config_t) * (size_t)number_of_config);
169 for (i=0; i < number_of_config; i++){
170 chg->conf[i].drivenum = 0;
171 chg->conf[i].start = -1;
172 chg->conf[i].end = -1;
173 chg->conf[i].cleanslot = -1;
174 chg->conf[i].device = NULL;
175 chg->conf[i].slotfile = NULL;
176 chg->conf[i].cleanfile = NULL;
177 chg->conf[i].timefile = NULL;
178 chg->conf[i].scsitapedev = NULL;
179 chg->conf[i].tapestatfile = NULL;
180 chg->conf[i].changerident = NULL;
181 chg->conf[i].tapeident = NULL;
185 /* Dump of information for debug */
192 dbprintf(_("Number of configurations: %d\n"), chg->number_of_configs);
193 dbprintf(_("Tapes need eject: %s\n"), (chg->eject>0 ? _("Yes") : _("No")));
194 dbprintf (_("\traw: %d\n"),chg->eject);
195 dbprintf(_("Inv. auto update: %s\n"), (chg->autoinv>0 ? _("Yes") : _("No")));
196 dbprintf (_("\traw: %d\n"),chg->autoinv);
197 dbprintf(_("barcode reader : %s\n"), (chg->havebarcode>0 ? _("Yes") : _("No")));
198 dbprintf (_("\traw: %d\n"),chg->havebarcode);
199 dbprintf(_("Emulate Barcode : %s\n"), (chg->emubarcode>0 ? _("Yes") : _("No")));
200 dbprintf (_("\traw: %d\n"),chg->emubarcode);
201 if (chg->debuglevel != NULL)
202 dbprintf(_("debug level : %s\n"), chg->debuglevel);
203 dbprintf(_("Tapes need sleep: %ld seconds\n"), (long int)chg->sleep);
204 dbprintf(_("Clean cycles : %d\n"), chg->cleanmax);
205 dbprintf(_("Changer device : %s\n"), chg->device);
206 if (chg->labelfile != NULL)
207 dbprintf(_("Label file : %s\n"), chg->labelfile);
208 for (i=0; i<chg->number_of_configs; i++){
209 dbprintf(_("Tape config Nr: %d\n"), i);
210 dbprintf(_(" Drive number : %d\n"), chg->conf[i].drivenum);
211 dbprintf(_(" Start slot : %d\n"), chg->conf[i].start);
212 dbprintf(_(" End slot : %d\n"), chg->conf[i].end);
213 dbprintf(_(" Clean slot : %d\n"), chg->conf[i].cleanslot);
215 if (chg->conf[i].device != NULL)
216 dbprintf(_(" Device name : %s\n"), chg->conf[i].device);
218 dbprintf(_(" Device name : none\n"));
220 if (chg->conf[i].changerident != NULL)
221 dbprintf(_(" changer ident : %s\n"), chg->conf[i].changerident);
223 dbprintf(_(" changer ident : none\n"));
225 if (chg->conf[i].scsitapedev != NULL)
226 dbprintf(_(" SCSI Tape dev : %s\n"), chg->conf[i].scsitapedev);
228 dbprintf(_(" SCSI Tape dev : none\n"));
230 if (chg->conf[i].tapeident != NULL)
231 dbprintf(_(" tape ident : %s\n"), chg->conf[i].tapeident);
233 dbprintf(_(" tape ident : none\n"));
235 if (chg->conf[i].tapestatfile != NULL)
236 dbprintf(_(" stat file : %s\n"), chg->conf[i].tapestatfile);
238 dbprintf(_(" stat file : none\n"));
240 if (chg->conf[i].slotfile != NULL)
241 dbprintf(_(" Slot file : %s\n"), chg->conf[i].slotfile);
243 dbprintf(_(" Slot file : none\n"));
245 if (chg->conf[i].cleanfile != NULL)
246 dbprintf(_(" Clean file : %s\n"), chg->conf[i].cleanfile);
248 dbprintf(_(" Clean file : none\n"));
250 if (chg->conf[i].timefile != NULL)
251 dbprintf(_(" Usage count : %s\n"), chg->conf[i].timefile);
253 dbprintf(_(" Usage count : none\n"));
257 /* Free all allocated memory */
266 if (chg->device != NULL)
268 for (i = 0; i < chg->number_of_configs; i++){
269 if (chg->conf[i].device != NULL)
270 amfree(chg->conf[i].device);
271 if (chg->conf[i].slotfile != NULL)
272 amfree(chg->conf[i].slotfile);
273 if (chg->conf[i].cleanfile != NULL)
274 amfree(chg->conf[i].cleanfile);
275 if (chg->conf[i].timefile != NULL)
276 amfree(chg->conf[i].timefile);
278 if (chg->conf != NULL)
285 /* This function parses a line, and returns a token and value */
295 *token = -1; /* No Token found */
296 tok=strtok(linebuffer," \t\n");
298 while ((tok != NULL) && (tok[0]!='#')&&(ready==0)){
304 while ((t_table[i].word != NULL)&&(*token==-1)){
305 if (0==strcasecmp(t_table[i].word,tok)){
306 *token=t_table[i].token;
311 tok=strtok(NULL," \t\n");
315 /* This function reads the specified configfile and fills the structure */
330 numconf = 1; /* At least one configuration is assumed */
331 /* If there are more, it should be the first entry in the configurationfile */
334 if ((file=fopen(configfile,"r")) == NULL) {
338 while ((NULL != (linebuffer = agets(file)))) {
339 if (linebuffer[0] == '\0') {
343 parse_line(linebuffer, &token, &value);
348 if (init_flag == 0) {
349 if (token != NUMDRIVE){
350 init_changer_struct(chg, numconf);
352 numconf = atoi(value);
353 if (numconf < 1 || numconf > 100) {
354 g_fprintf(stderr,_("numconf %d is bad\n"), numconf);
357 init_changer_struct(chg, numconf);
362 case NUMDRIVE: if (atoi(value) != numconf)
363 g_fprintf(stderr,_("Error: number_drives at wrong place, should be "
373 chg->debuglevel = stralloc(value);
376 chg->eject = atoi(value);
379 chg->havebarcode = atoi(value);
382 chg->sleep = (unsigned)atoi(value);
385 chg->labelfile = stralloc(value);
388 chg->device = stralloc(value);
391 chg->conf[drivenum].scsitapedev = stralloc(value);
394 chg->conf[drivenum].tapestatfile = stralloc(value);
397 chg->conf[drivenum].changerident = stralloc(value);
398 if (drivenum < 0 || drivenum > 100) {
399 g_fprintf(stderr,_("drivenum %d is bad\n"), drivenum);
402 if (strcmp(chg->conf[drivenum].changerident,"generic_changer") != 0) {
403 p = chg->conf[drivenum].changerident;
415 chg->conf[drivenum].tapeident = stralloc(value);
418 chg->cleanmax = atoi(value);
421 drivenum = atoi(value);
423 g_fprintf(stderr,_("Error: drive must be >= 0\n"));
425 } else if (drivenum >= numconf) {
426 g_fprintf(stderr,_("Error: drive must be less than number_drives\n"));
431 if (drivenum < numconf){
432 chg->conf[drivenum].drivenum = atoi(value);
434 g_fprintf(stderr,_("Error: drive is not less than number_drives"
435 " drivenum ignored\n"));
439 if (drivenum < numconf){
440 chg->conf[drivenum].start = atoi(value);
442 g_fprintf(stderr,_("Error: drive is not less than number_drives"
443 " startuse ignored\n"));
447 if (drivenum < numconf){
448 chg->conf[drivenum].end = atoi(value);
450 g_fprintf(stderr,_("Error: drive is not less than number_drives"
451 " enduse ignored\n"));
455 if (drivenum < numconf){
456 chg->conf[drivenum].cleanslot = atoi(value);
458 g_fprintf(stderr,_("Error: drive is not less than number_drives"
459 " cleanslot ignored\n"));
463 if (drivenum < numconf){
464 chg->conf[drivenum].device = stralloc(value);
466 g_fprintf(stderr,_("Error: drive is not less than number_drives"
467 " device ignored\n"));
471 if (drivenum < numconf){
472 chg->conf[drivenum].slotfile = stralloc(value);
474 g_fprintf(stderr,_("Error: drive is not less than number_drives"
475 " slotfile ignored\n"));
479 if (drivenum < numconf){
480 chg->conf[drivenum].cleanfile = stralloc(value);
482 g_fprintf(stderr,_("Error: drive is not less than number_drives"
483 " cleanfile ignored\n"));
487 if (drivenum < numconf){
488 chg->conf[drivenum].timefile = stralloc(value);
490 g_fprintf(stderr,_("Error: drive is not less than number_drives"
491 " usagecount ignored\n"));
495 g_fprintf(stderr,_("Error: Unknown token\n"));
507 /*----------------------------------------------------------------------------*/
509 /* The tape drive does not have an idea of current slot so
510 * we use a file to store the current slot. It is not ideal
511 * but it gets the job done
519 int ret; /* return value for the fscanf function */
520 if ((inf=fopen(count_file,"r")) == NULL) {
521 g_fprintf(stderr, _("%s: unable to open (%s)\n"),
522 get_pname(), count_file);
526 ret = fscanf(inf,"%d",&retval);
530 * check if we got an result
531 * if no set retval to -1
533 if (ret == 0 || ret == EOF)
538 if (retval < 0 || retval > 10000) {
554 if ((inf=fopen(count_file,"w")) == NULL) {
555 g_fprintf(stderr, _("%s: unable to open current slot file (%s)\n"),
556 get_pname(), count_file);
559 g_fprintf(inf, "%d\n", slot);
564 * Here we handle the inventory DB
565 * With this it should be possible to do an mapping
566 * Barcode -> Volume label
567 * Volume Label -> Barcode
568 * Volume label -> Slot number
573 * The passed struct MBC_T will hold the found entry in the DB
592 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : Parameter\n"));
593 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("labelfile -> %s, vol -> %s, barcode -> %s, action -> %c, slot -> %d, from -> %d\n"),
596 result->data.barcode,
601 if (labelfile == NULL)
603 DebugPrint(DEBUG_ERROR,SECTION_MAP_BARCODE,_("Got empty labelfile (NULL)\n"));
604 ChgExit("MapBarCode", _("MapBarCode name of labelfile is not set\n"),FATAL);
607 if (access(labelfile, F_OK) == -1)
609 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE, _("MapBarCode : creating %s"), labelfile);
610 if ((fp = fopen(labelfile, "w+")) == NULL)
612 DebugPrint(DEBUG_ERROR,SECTION_MAP_BARCODE,_(" failed\n"));
613 ChgExit("MapBarCode", _("MapBarCode, creating labelfile failed\n"), FATAL);
616 g_fprintf(fp,":%d:", LABEL_DB_VERSION);
620 if ((fp = fopen(labelfile, "r+")) == NULL)
622 DebugPrint(DEBUG_ERROR,SECTION_MAP_BARCODE,_("MapBarCode : failed to open %s\n"), labelfile);
623 ChgExit("MapBarCode", _("MapBarCode, opening labelfile for read/write failed\n"), FATAL);
627 if (fscanf(fp,":%d:", &version) != 1) {
628 ChgExit("MapBarCode", _("MapBarCode, DB Version unreadable.\n"), FATAL);
631 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : DB version %d\n"), version);
634 if (version != LABEL_DB_VERSION)
636 ChgExit("MapBarCode", _("MapBarCode, DB Version does not match\n"), FATAL);
640 if (( plabelv2 = (LabelV2_T *)alloc(SIZEOF(LabelV2_T))) == NULL)
642 DebugPrint(DEBUG_ERROR,SECTION_MAP_BARCODE,_("MapBarCode : alloc failed\n"));
643 ChgExit("MapBarCode", _("MapBarCode alloc failed\n"), FATAL);
647 memset(plabelv2, 0, SIZEOF(LabelV2_T));
649 while(feof(fp) == 0 && loop == 1)
651 rsize = fread(plabelv2, 1, SIZEOF(LabelV2_T), fp);
652 if (rsize == SIZEOF(LabelV2_T))
655 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : (%d) VolTag \"%s\", BarCode %s, inuse %d, slot %d, from %d, loadcount %d\n"),record,
661 plabelv2->LoadCount);
662 switch (result->action)
668 g_printf(_("Slot -> %d, from -> %d, valid -> %d, Tag -> %s, Barcode -> %s, Loadcount %u\n"),
678 * Set all the record to invalid, used by the Inventory function
682 if (fseek(fp, pos, SEEK_SET) == -1) {
687 if (fwrite(plabelv2, 1, SIZEOF(LabelV2_T), fp) < SIZEOF(LabelV2_T)) {
698 * If it is an invalid record we can use it,
699 * so save the record number.
700 * This value is used at the end if no other
701 * record/action matches.
703 if (plabelv2->valid == 0)
710 * OK this record matches the barcode label
713 if (strcmp(plabelv2->barcode, result->data.barcode) == 0)
716 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : update entry\n"));
717 if (fseek(fp, pos, SEEK_SET) == -1) {
723 plabelv2->from = result->data.from;
724 plabelv2->slot = result->data.slot;
725 plabelv2->LoadCount = plabelv2->LoadCount + result->data.LoadCount;
726 strncpy(plabelv2->voltag, result->data.voltag,
727 SIZEOF(plabelv2->voltag));
728 strncpy(plabelv2->barcode, result->data.barcode,
729 SIZEOF(plabelv2->barcode));
730 rc = (fwrite(plabelv2, 1, SIZEOF(LabelV2_T), fp) < SIZEOF(LabelV2_T));
737 * Look for an entry an return the entry
738 * if the voltag (the tape name) matches
741 if (strcmp(plabelv2->voltag, result->data.voltag) == 0)
743 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode FIND_SLOT : \n"));
744 memcpy(&(result->data), plabelv2, SIZEOF(LabelV2_T));
751 * reason can be an load, incr the LoadCount
755 if (strcmp(plabelv2->voltag, result->data.voltag) == 0)
757 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode UPDATE_SLOT : update entry\n"));
758 if (fseek(fp, pos, SEEK_SET) == -1) {
763 strncpy(plabelv2->voltag, result->data.voltag,
764 SIZEOF(plabelv2->voltag));
765 strncpy(plabelv2->barcode, result->data.barcode,
766 SIZEOF(plabelv2->barcode));
768 plabelv2->slot = result->data.slot;
769 plabelv2->from = result->data.from;
770 plabelv2->LoadCount = plabelv2->LoadCount + result->data.LoadCount;
771 rc = (fwrite(plabelv2, 1, SIZEOF(LabelV2_T), fp) < SIZEOF(LabelV2_T));
778 * Look for the barcode label of an given volume label
779 * return the slot number and the barcode label.
780 * If the entry is not valid return -1 as slot number
784 * DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode: (%d) inside BARCODE_VOL\n"), record);
785 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("file value: %s, searched for value: %s\n"), plabelv2->voltag, result->data.voltag);
787 if (strcmp(plabelv2->voltag, result->data.voltag) == 0)
789 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : VOL %s match\n"), result->data.voltag);
792 memcpy(&(result->data), plabelv2, SIZEOF(LabelV2_T));
798 * Look for an entry which matches the passed
801 case BARCODE_BARCODE:
802 if (strcmp(plabelv2->barcode, result->data.barcode) == 0)
804 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : BARCODE %s match\n"), result->data.barcode);
807 memcpy(&(result->data), plabelv2, SIZEOF(LabelV2_T));
814 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : unknown action\n"));
819 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : feof (%d)\n"), feof(fp));
820 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : error in read record expect %d, got %d\n"),SIZEOF(LabelV2_T), rsize);
826 * OK, if we come here and the action is either
827 * PUT or update it seems that we have to create a new
828 * record, becuae none of the exsisting records matches
830 if (result->action == BARCODE_PUT || result->action == UPDATE_SLOT )
833 * If we have an entry where the valid flag was set to 0
834 * we can use this record, so seek to this position
835 * If we have no record for reuse the new record will be written to the end.
839 DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : reuse record %d\n"), unusedrec);
840 if (fseek(fp, unusedpos, SEEK_SET) == -1) {
847 * Set all values to zero
849 memset(plabelv2, 0, SIZEOF(LabelV2_T));
851 strncpy(plabelv2->voltag, result->data.voltag,
852 SIZEOF(plabelv2->voltag));
853 strncpy(plabelv2->barcode, result->data.barcode,
854 SIZEOF(plabelv2->barcode));
856 plabelv2->from = result->data.from;
857 plabelv2->slot = result->data.slot;
858 rc = (fwrite(plabelv2, 1, SIZEOF(LabelV2_T), fp) < SIZEOF(LabelV2_T));
865 * If we hit this point nothing was
866 * found, so return an 0
873 /* ----------------------------------------------------------------------
874 This stuff deals with parsing the command line */
876 typedef struct com_arg
884 typedef struct com_stru
890 void parse_args(int argc, char *argv[],command *rval);
892 /* major command line args */
903 #define COM_INVENTORY 9
904 #define COM_DUMPDB 10
906 #define COM_GEN_CONF 12
907 argument argdefs[]={{"-slot",COM_SLOT,1},
908 {"-info",COM_INFO,0},
909 {"-reset",COM_RESET,0},
910 {"-eject",COM_EJECT,0},
911 {"-clean",COM_CLEAN,0},
912 {"-label",COM_LABEL,1},
913 {"-search",COM_SEARCH,1},
914 {"-status",COM_STATUS,1},
915 {"-trace",COM_TRACE,1},
916 {"-inventory", COM_INVENTORY,0},
917 {"-dumpdb", COM_DUMPDB,0},
918 {"-scan", COM_SCAN,0},
919 {"-genconf", COM_GEN_CONF,0}
923 /* minor command line args */
929 #define SLOT_ADVANCE 5
930 argument slotdefs[]={{"current",SLOT_CUR,0},
931 {"next",SLOT_NEXT,0},
932 {"prev",SLOT_PREV,0},
933 {"first",SLOT_FIRST,0},
934 {"last",SLOT_LAST,0},
935 {"advance",SLOT_ADVANCE,0},
937 #define SLOTCOUNT (int)(sizeof(slotdefs) / sizeof(slotdefs[0]))
939 /* is the string a valid positive int? */
945 if ((tmp==NULL)||(tmp[0]==0))
947 while ((tmp[i]>='0')&&(tmp[i]<='9')&&(tmp[i]!=0))
960 g_printf(_("%s: Usage error.\n"), argv[0]);
961 for (cnt=0; cnt < COMCOUNT; cnt++){
962 g_printf(" %s %s",argv[0],argdefs[cnt].str);
963 if (argdefs[cnt].takesparam)
964 g_printf(" <param>\n");
980 for (i=0; i < argc; i++)
981 dbprintf(_("ARG [%d] : %s\n"), i, argv[i]);
983 if ((argc<2)||(argc>3))
985 while ((i<COMCOUNT)&&(strcmp(argdefs[i].str,argv[1])))
989 rval->command_code = argdefs[i].command_code;
990 if (argdefs[i].takesparam) {
993 rval->parameter=argv[2];
1002 /* used to find actual slot number from keywords next, prev, first, etc */
1004 get_relative_target(
1016 (void)loaded; /* Quiet unused parameter warning */
1018 current_slot = get_current_slot(slot_file);
1020 if (current_slot > maxslot) {
1021 current_slot = slot_offset;
1023 if (current_slot < slot_offset) {
1024 current_slot = slot_offset;
1027 switch(param_index) {
1029 return current_slot;
1033 if (++current_slot==nslots+slot_offset)
1035 return current_slot;
1038 if (--current_slot<slot_offset)
1040 return current_slot;
1051 g_printf(_("<none> no slot `%s'\n"),parameter);
1057 /* This function should ask the drive if it wants to be cleaned */
1064 ret = get_clean_state(tapedev);
1066 if (ret < 0) /* < 0 means query does not work ... */
1073 /* This function should move the cleaning cartridge into the drive */
1086 if (cleancart == -1 ){
1090 /* Now we should increment the counter */
1091 if (cnt_file != NULL){
1092 counter = get_current_slot(cnt_file);
1094 if (counter>=maxclean){
1095 /* Now we should inform the administrator */
1096 char *mail_cmd = NULL;
1098 int mail_pipe_opened = 1;
1100 if(getconf_seen(CNF_MAILTO) && strlen(getconf_str(CNF_MAILTO)) > 0 &&
1101 validate_mailto(getconf_str(CNF_MAILTO))) {
1102 mail_cmd = vstralloc(MAILER,
1103 " -s", " \"", _("AMANDA PROBLEM: PLEASE FIX"), "\"",
1104 " ", getconf_str(CNF_MAILTO),
1106 if((mailf = popen(mail_cmd, "w")) == NULL){
1107 g_printf(_("Mail failed\n"));
1108 error(_("could not open pipe to \"%s\": %s"),
1109 mail_cmd, strerror(errno));
1113 mail_pipe_opened = 0;
1115 g_fprintf(mailf, _("\nNo mail recipient specified, output redirected to stderr"));
1118 mail_pipe_opened = 0;
1120 g_fprintf(mailf, _("\nNo mailer specified; output redirected to stderr"));
1122 g_fprintf(mailf,_("\nThe usage count of your cleaning tape in slot %d"),
1124 g_fprintf(mailf,_("\nis more than %d. (cleanmax)"),maxclean);
1125 g_fprintf(mailf,_("\nTapedrive %s needs to be cleaned"),tapedev);
1126 g_fprintf(mailf,_("\nPlease insert a new cleaning tape and reset"));
1127 g_fprintf(mailf,_("\nthe countingfile %s"),cnt_file);
1129 if(mail_pipe_opened == 1 && pclose(mailf) != 0) {
1130 error(_("mail command failed: %s"), mail_cmd);
1135 put_current_slot(cnt_file, counter);
1137 load(fd,drivenum,cleancart);
1139 * Hack, sleep for some time
1144 if (drive_loaded(fd, drivenum))
1145 unload(fd, drivenum, cleancart);
1149 /* ----------------------------------------------------------------------*/
1157 int target, oldtarget;
1158 command com; /* a little DOS joke */
1162 * drive_num really should be something from the config file, but..
1163 * for now, it is set to zero, since most of the common changers
1164 * used by amanda only have one drive ( until someone wants to
1165 * use an EXB60/120, or a Breece Hill Q45.. )
1167 unsigned char emubarcode;
1169 int need_eject; /* Does the drive need an eject command ? */
1170 time_t need_sleep; /* How many seconds to wait for the drive to get ready */
1177 * For the emubarcode stuff
1190 char *chg_scsi_conf; /* The config file for us */
1191 char *slot_file; /* Where we will place the info which
1194 char *scsitapedevice;
1196 int param_index = 0;
1199 * Configure program for internationalization:
1200 * 1) Only set the message locale for now.
1201 * 2) Set textdomain for all amanda related programs to "amanda"
1202 * We don't want to be forced to support dozens of message catalogs.
1204 setlocale(LC_MESSAGES, "C");
1205 textdomain("amanda");
1207 changer = alloc(SIZEOF(changer_t));
1208 pbarcoderes = alloc(SIZEOF(MBC_T));
1210 memset(pbarcoderes, 0 , SIZEOF(MBC_T));
1211 changer->number_of_configs = 0;
1214 changer->cleanmax = 0;
1215 changer->device = NULL;
1216 changer->labelfile = NULL;
1217 changer->conf = NULL;
1218 #ifdef CHG_SCSI_STANDALONE
1219 g_printf(_("Ups standalone\n"));
1221 set_pname("chg-scsi");
1223 /* Don't die when child closes pipe */
1224 signal(SIGPIPE, SIG_IGN);
1226 dbopen(DBG_SUBDIR_SERVER);
1228 dbprintf("chg-scsi: %s\n", rcsid);
1229 ChangerDriverVersion();
1231 if (debug_file == NULL)
1233 debug_file = dbfp();
1236 parse_args(argc,argv,&com);
1238 pDev = (OpenFiles_T *)alloc(SIZEOF(OpenFiles_T) * CHG_MAXDEV);
1239 memset(pDev, 0, SIZEOF(OpenFiles_T) * CHG_MAXDEV );
1242 switch(com.command_code)
1257 config_init(CONFIG_INIT_USE_CWD | CONFIG_INIT_FATAL, NULL);
1259 chg_scsi_conf = getconf_str(CNF_CHANGERFILE);
1260 tape_device = getconf_str(CNF_TAPEDEV);
1262 /* Get the configuration parameters */
1263 /* Attention, this will not support more than 10 tape devices 0-9 */
1265 if (strlen(tape_device)==1){
1266 if (read_config(chg_scsi_conf, changer) == -1)
1268 g_fprintf(stderr, _("%s open: of %s failed\n"), get_pname(), chg_scsi_conf);
1271 confnum=atoi(tape_device);
1272 if (changer->number_of_configs == 0)
1274 g_fprintf(stderr,_("%s: changer->conf[%d] == NULL\n"),
1275 get_pname(), confnum);
1278 if (confnum >= changer->number_of_configs) {
1279 g_fprintf(stderr,_("%s: Configuration %s config # out of range (%d >= %d)\n"),
1280 get_pname(), chg_scsi_conf,
1282 changer->number_of_configs);
1286 use_slots = changer->conf[confnum].end-changer->conf[confnum].start+1;
1287 slot_offset = changer->conf[confnum].start;
1288 drive_num = changer->conf[confnum].drivenum;
1289 need_eject = changer->eject;
1290 need_sleep = changer->sleep;
1292 if ( NULL != changer->conf[confnum].cleanfile)
1293 clean_file = stralloc(changer->conf[confnum].cleanfile);
1297 clean_slot = changer->conf[confnum].cleanslot;
1298 maxclean = changer->cleanmax;
1299 emubarcode = changer->emubarcode;
1300 if (NULL != changer->conf[confnum].timefile)
1301 time_file = stralloc(changer->conf[confnum].timefile);
1305 if (NULL != changer->conf[confnum].slotfile)
1306 slot_file = stralloc(changer->conf[confnum].slotfile);
1310 if (NULL != changer->conf[confnum].device)
1311 tape_device = stralloc(changer->conf[confnum].device);
1315 if (NULL != changer->device)
1316 changer_dev = stralloc(changer->device);
1320 if (NULL != changer->conf[confnum].scsitapedev)
1321 scsitapedevice = stralloc(changer->conf[confnum].scsitapedev);
1323 scsitapedevice = NULL;
1325 if (NULL != changer->conf[confnum].tapestatfile)
1326 tapestatfile = stralloc(changer->conf[confnum].tapestatfile);
1328 tapestatfile = NULL;
1329 dump_changer_struct(changer);
1334 * The changer device.
1335 * If we can't open it fail with a message
1338 if (OpenDevice(INDEX_CHANGER , changer_dev, "changer_dev", changer->conf[confnum].changerident) == 0)
1340 int localerr = errno;
1341 g_fprintf(stderr, _("%s: open: %s: %s\n"), get_pname(),
1342 changer_dev, strerror(localerr));
1343 g_printf(_("%s open: %s: %s\n"), _("<none>"), changer_dev, strerror(localerr));
1344 dbprintf(_("open: %s: %s\n"), changer_dev, strerror(localerr));
1353 * eject if eject is set
1354 * inventory (reading of the labels) if emubarcode (not yet)
1356 if (tape_device != NULL)
1358 if (OpenDevice(INDEX_TAPE, tape_device, "tape_device", changer->conf[confnum].tapeident) == 0)
1360 dbprintf(_("warning open of %s: failed\n"), tape_device);
1365 * This is for the status pages of the SCSI tape, nice to have but no must....
1367 if (scsitapedevice != NULL)
1369 if (OpenDevice(INDEX_TAPECTL, scsitapedevice, "scsitapedevice", changer->conf[confnum].tapeident) == 0)
1371 dbprintf(_("warning open of %s: failed\n"), scsitapedevice);
1377 * So if we need eject we need either an raw device to eject with an ioctl,
1378 * or an SCSI device to send the SCSI eject.
1381 if (need_eject != 0 )
1383 if (pDev[INDEX_TAPE].avail == 0 && pDev[INDEX_TAPECTL].avail == 0)
1385 g_printf(_("No device found for tape eject"));
1391 if ((changer->conf[confnum].end == -1) || (changer->conf[confnum].start == -1)){
1392 slotcnt = get_slot_count(fd);
1393 use_slots = slotcnt;
1398 * Now check if we have all what we need
1399 * If either emubarcode is set or the changer support barcode
1400 * we need an label file
1403 if ( changer->emubarcode == 1 || BarCode(INDEX_CHANGER) == 1)
1405 if (changer->labelfile == NULL)
1407 g_printf(_("labelfile param not set in your config\n"));
1412 if (slot_file == NULL)
1414 g_printf(_("slotfile param. not set in your config\n"));
1418 if (access(slot_file,R_OK|W_OK) != 0)
1420 g_printf(_("slotfile %s does not exsist or is not read/write\n"), slot_file);
1424 } else { /* if (strlen(tape_device)==1) */
1425 g_printf(_("please check your config and use a config file for chg-scsi\n"));
1429 drivecnt = get_drive_count(fd);
1431 if (drive_num > drivecnt) {
1432 g_printf(_("%s drive number error (%d > %d)\n"), _("<none>"),
1433 drive_num, drivecnt);
1434 g_fprintf(stderr, _("%s: requested drive number (%d) greater than "
1435 "number of supported drives (%d)\n"), get_pname(),
1436 drive_num, drivecnt);
1437 dbprintf(_("requested drive number (%d) is greater than "
1438 "number of supported drives (%d)\n"), drive_num, drivecnt);
1442 loaded = (int)drive_loaded(fd, drive_num);
1445 switch(com.command_code) {
1446 /* This is only for the experts ;-) */
1448 ChangerReplay(com.parameter);
1453 pbarcoderes->action = BARCODE_DUMP;
1454 MapBarCode(changer->labelfile, pbarcoderes);
1457 ChangerStatus(com.parameter, changer->labelfile,
1458 BarCode(fd), slot_file, changer_dev, tape_device);
1460 case COM_LABEL: /* Update BarCode/Label mapping file */
1461 pbarcoderes->action = BARCODE_PUT;
1462 strncpy(pbarcoderes->data.voltag, com.parameter,
1463 SIZEOF(pbarcoderes->data.voltag));
1464 strncpy(pbarcoderes->data.barcode, pDTE[drive_num].VolTag,
1465 SIZEOF(pbarcoderes->data.barcode));
1466 MapBarCode(changer->labelfile, pbarcoderes);
1467 g_printf("0 0 0\n");
1471 * Inventory does an scan of the library and updates the mapping in the label DB
1474 do_inventory = 1; /* Tell the label check not to exit on label errors */
1477 oldtarget = get_current_slot(slot_file);
1480 dbprintf(_("COM_INVENTORY: get_current_slot %d\n"), oldtarget);
1481 oldtarget = find_empty(fd, slot_offset, use_slots);
1482 dbprintf(_("COM_INVENTORY: find_empty %d\n"), oldtarget);
1487 eject_tape(scsitapedevice, need_eject);
1489 if (pDev[INDEX_TAPECTL].avail == 1 && pDev[INDEX_TAPE].avail == 1)
1491 LogSense(INDEX_TAPE);
1495 (void)unload(fd, drive_num, oldtarget);
1496 if (ask_clean(scsitapedevice))
1497 clean_tape(fd,tape_device,clean_file,drive_num,
1498 clean_slot, maxclean, time_file);
1500 Inventory(changer->labelfile, drive_num, need_eject, 0, 0, clean_slot);
1501 do_inventory = 0; /* If set on exit the labeldb will be set to invalid ..... */
1505 * Search for the tape, the index is the volume label
1510 * If we have an barcode reader use
1513 if (BarCode(fd) == 1 && emubarcode != 1)
1515 dbprintf(_("search : look for %s\n"), com.parameter);
1516 pbarcoderes->action = BARCODE_VOL;
1517 pbarcoderes->data.slot = -1;
1518 strncpy(pbarcoderes->data.voltag, com.parameter,
1519 SIZEOF(pbarcoderes->data.voltag));
1520 if (MapBarCode(changer->labelfile, pbarcoderes) == 1)
1523 * If both values are unset we have an problem
1524 * so leave the program
1526 if (pbarcoderes->data.slot == -1 && pbarcoderes->data.barcode == NULL)
1528 g_printf(_("Label %s not found (1)\n"),com.parameter);
1536 * Let's see, if we got an barcode check if it is
1537 * in the current inventory
1539 if (pbarcoderes->data.barcode != NULL)
1542 for (x = 0; x < (int)STE; x++)
1544 if (strcmp(pSTE[x].VolTag, pbarcoderes->data.barcode) == 0)
1546 dbprintf(_("search : found slot %d\n"), x);
1551 * Not found in the STE slots
1552 * my be in the DTE (tape)
1553 * If we find it check if it is in the right drive
1554 * if we have more than one drive.
1556 for (x = 0; x < (int)DTE; x++)
1558 if (strcmp(pDTE[x].VolTag, pbarcoderes->data.barcode) == 0)
1560 dbprintf(_("search : found in tape %d\n"), x);
1563 if (x == drive_num) {
1564 oldtarget = get_current_slot(slot_file);
1565 g_printf("%d %s\n", oldtarget - slot_offset, tape_device);
1568 g_printf(_("LABEL in wrong tape Unit\n"));
1574 * not found, so do an exit...
1578 g_printf(_("Label %s not found (2) \n"),com.parameter);
1583 } /* if barcode[0] != 0 */
1586 * If we didn't find anything we will try the info
1587 * from the DB. A reason for not finding anything in the inventory
1588 * might be an unreadable barcode label
1590 if (target == -1 && pbarcoderes->data.slot != -1)
1592 target = pbarcoderes->data.slot;
1596 * OK, if target is still -1 do the exit
1600 g_printf(_("Label %s not found (3)\n"),com.parameter);
1610 * And now if we have emubarcode set and no barcode reader
1613 if (emubarcode == 1 && BarCode(fd) != 1)
1615 dbprintf(_("search : look for %s\n"), com.parameter);
1616 pbarcoderes->action = FIND_SLOT;
1617 pbarcoderes->data.slot = -1;
1618 strncpy(pbarcoderes->data.voltag, com.parameter,
1619 SIZEOF(pbarcoderes->data.voltag));
1621 if (MapBarCode(changer->labelfile, pbarcoderes) == 1)
1623 if (pbarcoderes->data.valid == 1)
1625 target = pbarcoderes->data.slot;
1627 g_printf(_("Barcode DB out of sync \n"));
1633 g_printf(_("Label %s not found \n"),com.parameter);
1641 * The slot changing command
1646 if (is_positive_number(com.parameter)) {
1647 if ((target = atoi(com.parameter))>=use_slots) {
1648 g_printf(_("<none> no slot `%d'\n"),target);
1653 target = target + slot_offset;
1657 while((param_index < SLOTCOUNT) &&
1658 (strcmp(slotdefs[param_index].str,com.parameter))) {
1661 target=get_relative_target(fd, use_slots,
1662 com.parameter,param_index,
1666 (slot_offset + use_slots - 1));
1671 oldtarget = get_current_slot(slot_file);
1674 dbprintf(_("COM_SLOT: get_current_slot %d\n"), oldtarget);
1675 oldtarget = find_empty(fd, slot_offset, use_slots);
1676 dbprintf(_("COM_SLOT: find_empty %d\n"), oldtarget);
1680 * TODO check if the request slot for the unload is empty
1684 if ((oldtarget)!=target) {
1687 eject_tape(scsitapedevice, need_eject);
1690 * If we have an SCSI path to the tape and an raw io path
1691 * try to read the Error Counter and the label
1693 if (pDev[INDEX_TAPECTL].avail == 1 && pDev[INDEX_TAPE].avail == 1)
1695 LogSense(INDEX_TAPE);
1699 (void)unload(fd, drive_num, oldtarget);
1700 if (ask_clean(scsitapedevice))
1701 clean_tape(fd, tape_device, clean_file, drive_num,
1702 clean_slot, maxclean, time_file);
1707 put_current_slot(slot_file, target);
1709 if (!loaded && isempty(fd, target)) {
1710 g_printf(_("%d slot %d is empty\n"),target - slot_offset,
1711 target - slot_offset);
1717 if (!loaded && param_index != SLOT_ADVANCE)
1719 if (ask_clean(scsitapedevice))
1720 clean_tape(fd, tape_device, clean_file, drive_num,
1721 clean_slot, maxclean, time_file);
1722 if (load(fd, drive_num, target) != 0) {
1723 g_printf(_("%d slot %d move failed\n"),target - slot_offset,
1724 target - slot_offset);
1733 if (pDev[INDEX_TAPECTL].inqdone == 1)
1735 if (Tape_Ready(INDEX_TAPECTL, need_sleep) == -1)
1737 g_printf(_("tape not ready\n"));
1742 if (Tape_Ready(INDEX_TAPECTL, need_sleep) == -1)
1744 g_printf(_("tape not ready\n"));
1751 g_printf("%d %s\n", target - slot_offset, tape_device);
1755 loaded = (int)get_current_slot(slot_file);
1759 loaded = find_empty(fd, slot_offset, use_slots);
1761 loaded = loaded - (int)slot_offset;
1763 g_printf("%d %d 1", loaded, use_slots);
1765 if (BarCode(fd) == 1 || emubarcode == 1)
1774 target=get_current_slot(slot_file);
1778 dbprintf(_("COM_RESET: get_current_slot %d\n"), target);
1779 target = find_empty(fd, slot_offset, use_slots);
1780 dbprintf(_("COM_RESET: find_empty %d\n"), target);
1785 if (!isempty(fd, target))
1786 target = find_empty(fd, slot_offset, use_slots);
1790 eject_tape(scsitapedevice, need_eject);
1793 * If we have an SCSI path to the tape and an raw io path
1794 * try to read the Error Counter and the label
1796 if (pDev[INDEX_TAPECTL].avail == 1 && pDev[INDEX_TAPE].avail == 1)
1798 LogSense(INDEX_TAPE);
1802 (void)unload(fd, drive_num, target);
1803 if (ask_clean(scsitapedevice))
1804 clean_tape(fd,tape_device, clean_file, drive_num, clean_slot,
1805 maxclean, time_file);
1808 if (isempty(fd, slot_offset)) {
1809 g_printf(_("0 slot 0 is empty\n"));
1815 if (load(fd, drive_num, slot_offset) != 0) {
1816 g_printf(_("%d slot %d move failed\n"),
1817 drive_num, slot_offset);
1819 put_current_slot(slot_file, slot_offset);
1824 put_current_slot(slot_file, slot_offset);
1828 if (pDev[INDEX_TAPECTL].inqdone == 1)
1830 if (Tape_Ready(INDEX_TAPECTL, need_sleep) == -1)
1832 g_printf(_("tape not ready\n"));
1837 if (Tape_Ready(INDEX_TAPECTL, need_sleep) == -1)
1839 g_printf(_("tape not ready\n"));
1846 g_printf(_("%d %s\n"), slot_offset, tape_device);
1851 target = get_current_slot(slot_file);
1854 dbprintf(_("COM_EJECT: get_current_slot %d\n"), target);
1855 target = find_empty(fd, slot_offset, use_slots);
1856 dbprintf(_("COM_EJECT: find_empty %d\n"), target);
1861 eject_tape(scsitapedevice, need_eject);
1863 if (pDev[INDEX_TAPECTL].avail == 1 && pDev[INDEX_TAPE].avail == 1)
1865 LogSense(INDEX_TAPE);
1870 (void)unload(fd, drive_num, target);
1871 if (ask_clean(scsitapedevice))
1872 clean_tape(fd, tape_device, clean_file, drive_num, clean_slot,
1873 maxclean, time_file);
1874 g_printf("%d %s\n", target, tape_device);
1876 g_printf(_("%d drive was not loaded\n"), target);
1882 target = get_current_slot(slot_file);
1885 dbprintf(_("COM_CLEAN: get_current_slot %d\n"), target);
1886 target = find_empty(fd, slot_offset, use_slots);
1887 dbprintf(_("COM_CLEAN: find_empty %d\n"),target);
1892 eject_tape(scsitapedevice, need_eject);
1894 if (pDev[INDEX_TAPECTL].avail == 1 && pDev[INDEX_TAPE].avail == 1)
1896 LogSense(INDEX_TAPE);
1900 (void)unload(fd, drive_num, target);
1903 clean_tape(fd, tape_device, clean_file, drive_num, clean_slot,
1904 maxclean, time_file);
1905 g_printf(_("%s cleaned\n"), tape_device);
1909 /* FIX ME, should be an function to close the device */
1910 /* if (pChangerDev != NULL) */
1911 /* close(pChangerDev->fd); */
1913 /* if (pTapeDev != NULL) */
1914 /* close(pTapeDev->fd); */
1916 /* if (pTapeDevCtl != NULL) */
1917 /* close(pTapeDevCtl->fd); */
1921 if (do_inventory == 1 && endstatus == 0 && changer->labelfile != NULL)
1923 if (changer->autoinv == 1)
1925 DebugPrint(DEBUG_INFO,SECTION_INFO, _("Do an inventory \n"));
1926 Inventory(changer->labelfile, drive_num, changer->eject,
1929 DebugPrint(DEBUG_INFO,SECTION_INFO, _("Set all entrys in DB to invalid\n"));
1930 memset(pbarcoderes, 0 , SIZEOF(MBC_T));
1931 pbarcoderes->action = RESET_VALID;
1932 MapBarCode(changer->labelfile,pbarcoderes);
1936 DebugPrint(DEBUG_INFO,SECTION_INFO,_("Exit status -> %d\n"), endstatus);
1942 * indent-tabs-mode: nil