00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <SUM.h>
00012 #include <sys/errno.h>
00013 #include <sys/wait.h>
00014 #include <rpc/pmap_clnt.h>
00015 #include <sum_rpc.h>
00016 #include <soi_error.h>
00017 #include <tape.h>
00018 #include <string.h>
00019 #include <printk.h>
00020 #include <stdio.h>
00021 #include <unistd.h>
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024 #include <fcntl.h>
00025
00026 #include <sys/mtio.h>
00027 #include <dirent.h>
00028 #include <openssl/md5.h>
00029
00030 #define RETAINDAY 3
00031 #define MAX_WAIT 20
00032 #define BLOCK_SIZE GTARBLOCK*512
00033 #define CMDLENWRT 24576
00034 #define GTARLOGDIR "/var/logs/SUM/gtar"
00035 #define GTAR "/usr/local/bin/gtar"
00036 static char errstr[256];
00037 int write_wd_to_drive();
00038 int write_hdr_to_drive();
00039 int position_tape_eod();
00040 int position_tape_bot();
00041 int position_tape_file_asf();
00042 int position_tape_file_fsf();
00043 int get_tape_fnum_rdy();
00044 void drive_reset();
00045 uint64_t tell_blocks();
00046 int get_cksum();
00047 int read_drive_to_wd();
00048 char *get_time();
00049 void write_time();
00050 int send_mail(char *fmt, ...);
00051 int SUMLIB_Ds_Ix_File(uint64_t filedsix[], uint64_t filedsixoff[], int *dsmdiflg);
00052 int SUMLIB_Ds_Ix_Find(char *tapeid, int filenum, uint64_t filedsix[],
00053 double filebytes[]);
00054 int SUMLIB_Get_MD5(char *tapeid, int filenum, char *md5ret);
00055
00056 char logfile[MAX_STR];
00057 char md5file[64];
00058 char md5filter[128];
00059 char rcmd[CMDLENWRT];
00060
00061 void logkey();
00062 extern int errno;
00063 #ifdef DRIVE_0
00064 static void drive0prog_1();
00065 #endif
00066 #ifdef DRIVE_1
00067 static void drive1prog_1();
00068 #endif
00069 #ifdef DRIVE_2
00070 static void drive2prog_1();
00071 #endif
00072 #ifdef DRIVE_3
00073 static void drive3prog_1();
00074 #endif
00075 #ifdef DRIVE_4
00076 static void drive4prog_1();
00077 #endif
00078 #ifdef DRIVE_5
00079 static void drive5prog_1();
00080 #endif
00081 #ifdef DRIVE_6
00082 static void drive6prog_1();
00083 #endif
00084 #ifdef DRIVE_7
00085 static void drive7prog_1();
00086 #endif
00087 #ifdef DRIVE_8
00088 static void drive8prog_1();
00089 #endif
00090 #ifdef DRIVE_9
00091 static void drive9prog_1();
00092 #endif
00093 #ifdef DRIVE_10
00094 static void drive10prog_1();
00095 #endif
00096 #ifdef DRIVE_11
00097 static void drive11prog_1();
00098 #endif
00099 static struct timeval TIMEOUT = { 60, 0 };
00100 uint32_t rinfo;
00101 uint32_t procnum;
00102
00103 FILE *logfp;
00104 CLIENT *current_client, *clnttape;
00105 SVCXPRT *glb_transp;
00106 int debugflg = 0;
00107 int tapeoffline = 0;
00108 int drivenum;
00109 int efnum = 0;
00110 char *dbname;
00111 char *timetag;
00112 char md5str[33];
00113 char drvname[MAX_STR];
00114 char thishost[MAX_STR];
00115 char hostn[MAX_STR];
00116 char datestr[32];
00117
00118 int soi_errno = NO_ERROR;
00119
00120
00121 void open_log(char *filename)
00122 {
00123
00124 if((logfp=fopen(filename, "a+")) == NULL) {
00125 fprintf(stderr, "Can't open the log file %s\n", filename);
00126 }
00127 }
00128
00129
00130
00131
00132 static char *datestring()
00133 {
00134 struct timeval tvalr;
00135 struct tm *t_ptr;
00136
00137 gettimeofday(&tvalr, NULL);
00138 t_ptr = localtime((const time_t *)&tvalr);
00139 sprintf(datestr, "%s", asctime(t_ptr));
00140 datestr[19] = (char)NULL;
00141 return(&datestr[4]);
00142 }
00143
00144
00145
00146
00147 int write_log(const char *fmt, ...)
00148 {
00149 va_list args;
00150 char string[CMDLENWRT];
00151
00152 va_start(args, fmt);
00153 vsprintf(string, fmt, args);
00154 if(logfp) {
00155 fprintf(logfp, string);
00156 fflush(logfp);
00157 }
00158 else
00159 fprintf(stderr, string);
00160 va_end(args);
00161 return(0);
00162 }
00163
00164
00165 void sighandler(sig)
00166 int sig;
00167 {
00168 if(sig == SIGTERM) {
00169 write_log("*** %s %s got SIGTERM. Exiting.\n", datestring(), drvname);
00170 exit(1);
00171 }
00172 if(sig == SIGINT) {
00173 write_log("*** %s %s got SIGINT. Exiting.\n", datestring(), drvname);
00174 DS_DisConnectDB();
00175 exit(1);
00176 }
00177 write_log("*** %s %s got an illegal signal %d, ignoring...\n",
00178 datestring(), drvname, sig);
00179 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00180 signal(SIGINT, sighandler);
00181 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00182 signal(SIGALRM, sighandler);
00183 }
00184
00185
00186 void get_cmd(int argc, char *argv[])
00187 {
00188 int c;
00189
00190 while(--argc > 0 && (*++argv)[0] == '-') {
00191 while((c = *++argv[0]))
00192 switch(c) {
00193 case 'd':
00194 debugflg=1;
00195 break;
00196 case 'o':
00197 tapeoffline=1;
00198 break;
00199 default:
00200 break;
00201 }
00202 }
00203 if(argc != 2) {
00204 printf("\nERROR: driven_svc must be call with dbname and timestamp\n");
00205 exit(1);
00206 }
00207 else {
00208 dbname = argv[0];
00209 timetag = argv[1];
00210 }
00211 }
00212
00213
00214 void setup()
00215 {
00216 int pid;
00217 char *cptr;
00218
00219 gethostname(hostn, 80);
00220 cptr = index(hostn, '.');
00221 if(cptr) *cptr = (char)NULL;
00222
00223 sprintf(thishost, "localhost");
00224
00225 #ifdef DRIVE_0
00226 sprintf(drvname, "drive0_svc");
00227 drivenum = 0;
00228 #endif
00229 #ifdef DRIVE_1
00230 sprintf(drvname, "drive1_svc");
00231 drivenum = 1;
00232 #endif
00233 #ifdef DRIVE_2
00234 sprintf(drvname, "drive2_svc");
00235 drivenum = 2;
00236 #endif
00237 #ifdef DRIVE_3
00238 sprintf(drvname, "drive3_svc");
00239 drivenum = 3;
00240 #endif
00241 #ifdef DRIVE_4
00242 sprintf(drvname, "drive4_svc");
00243 drivenum = 4;
00244 #endif
00245 #ifdef DRIVE_5
00246 sprintf(drvname, "drive5_svc");
00247 drivenum = 5;
00248 #endif
00249 #ifdef DRIVE_6
00250 sprintf(drvname, "drive6_svc");
00251 drivenum = 6;
00252 #endif
00253 #ifdef DRIVE_7
00254 sprintf(drvname, "drive7_svc");
00255 drivenum = 7;
00256 #endif
00257 #ifdef DRIVE_8
00258 sprintf(drvname, "drive8_svc");
00259 drivenum = 8;
00260 #endif
00261 #ifdef DRIVE_9
00262 sprintf(drvname, "drive9_svc");
00263 drivenum = 9;
00264 #endif
00265 #ifdef DRIVE_10
00266 sprintf(drvname, "drive10_svc");
00267 drivenum = 10;
00268 #endif
00269 #ifdef DRIVE_11
00270 sprintf(drvname, "drive11_svc");
00271 drivenum = 11;
00272 #endif
00273
00274 pid = getppid();
00275 sprintf(logfile, "/usr/local/logs/SUM/tape_svc_%s.log", timetag);
00276 open_log(logfile);
00277 printk_set(write_log, write_log);
00278 sleep(1);
00279 write_log("\n## %s %s for pid = %d ##\n",
00280 datestring(), drvname, pid);
00281 write_log("Database to connect to is %s\n", dbname);
00282
00283
00284 sprintf(md5filter, "/usr/local/bin/md5filter");
00285
00286
00287 signal(SIGINT, sighandler);
00288 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
00289 signal(SIGTERM, sighandler);
00290 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00291 signal(SIGALRM, sighandler);
00292 }
00293
00294
00295 int main(int argc, char *argv[])
00296 {
00297 register SVCXPRT *transp;
00298 char compcmd[256];
00299
00300 get_cmd(argc, argv);
00301 setup();
00302
00303
00304 #ifdef DRIVE_0
00305 (void) pmap_unset(DRIVE0PROG, DRIVE0VERS);
00306 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00307 if (transp == NULL) {
00308 write_log("***cannot create tcp service\n");
00309 exit(1);
00310 }
00311 if(!svc_register(transp,DRIVE0PROG,DRIVE0VERS,drive0prog_1,IPPROTO_TCP)) {
00312 write_log("***unable to register (DRIVE0PROG, DRIVE0VERS, tcp)\n");
00313 exit(1);
00314 }
00315 #ifdef SUMT120
00316 if(tapeoffline == 0) {
00317
00318 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR0);
00319 write_log("%s\n", compcmd);
00320 if(system(compcmd)) {
00321 write_log("***Error on: %s\n", compcmd);
00322
00323 }
00324 }
00325 #endif
00326 #endif
00327 #ifdef DRIVE_1
00328 (void) pmap_unset(DRIVE1PROG, DRIVE1VERS);
00329 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00330 if (transp == NULL) {
00331 write_log("***cannot create tcp service\n");
00332 exit(1);
00333 }
00334 if(!svc_register(transp,DRIVE1PROG,DRIVE1VERS,drive1prog_1,IPPROTO_TCP)) {
00335 write_log("***unable to register (DRIVE1PROG, DRIVE1VERS, tcp)\n");
00336 exit(1);
00337 }
00338 #ifdef SUMT120
00339 if(tapeoffline == 0) {
00340
00341 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR1);
00342 write_log("%s\n", compcmd);
00343 if(system(compcmd)) {
00344 write_log("***Error on: %s\n", compcmd);
00345
00346 }
00347 }
00348 #endif
00349 #endif
00350 #ifdef DRIVE_2
00351 (void) pmap_unset(DRIVE2PROG, DRIVE2VERS);
00352 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00353 if (transp == NULL) {
00354 write_log("***cannot create tcp service\n");
00355 exit(1);
00356 }
00357 if(!svc_register(transp,DRIVE2PROG,DRIVE2VERS,drive2prog_1,IPPROTO_TCP)) {
00358 write_log("***unable to register (DRIVE2PROG, DRIVE2VERS, tcp)\n");
00359 exit(1);
00360 }
00361 #ifdef SUMT120
00362 if(tapeoffline == 0) {
00363
00364 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR2);
00365 write_log("%s\n", compcmd);
00366 if(system(compcmd)) {
00367 write_log("***Error on: %s\n", compcmd);
00368
00369 }
00370 }
00371 #endif
00372 #endif
00373 #ifdef DRIVE_3
00374 (void) pmap_unset(DRIVE3PROG, DRIVE3VERS);
00375 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00376 if (transp == NULL) {
00377 write_log("***cannot create tcp service\n");
00378 exit(1);
00379 }
00380 if(!svc_register(transp,DRIVE3PROG,DRIVE3VERS,drive3prog_1,IPPROTO_TCP)) {
00381 write_log("***unable to register (DRIVE3PROG, DRIVE3VERS, tcp)\n");
00382 exit(1);
00383 }
00384 #ifdef SUMT120
00385 if(tapeoffline == 0) {
00386
00387 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR3);
00388 write_log("%s\n", compcmd);
00389 if(system(compcmd)) {
00390 write_log("***Error on: %s\n", compcmd);
00391
00392 }
00393 }
00394 #endif
00395 #endif
00396 #ifdef DRIVE_4
00397 (void) pmap_unset(DRIVE4PROG, DRIVE4VERS);
00398 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00399 if (transp == NULL) {
00400 write_log("***cannot create tcp service\n");
00401 exit(1);
00402 }
00403 if(!svc_register(transp,DRIVE4PROG,DRIVE4VERS,drive4prog_1,IPPROTO_TCP)) {
00404 write_log("***unable to register (DRIVE4PROG, DRIVE4VERS, tcp)\n");
00405 exit(1);
00406 }
00407 #ifdef SUMT120
00408 if(tapeoffline == 0) {
00409
00410 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR4);
00411 write_log("%s\n", compcmd);
00412 if(system(compcmd)) {
00413 write_log("***Error on: %s\n", compcmd);
00414
00415 }
00416 }
00417 #endif
00418 #endif
00419 #ifdef DRIVE_5
00420 (void) pmap_unset(DRIVE5PROG, DRIVE5VERS);
00421 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00422 if (transp == NULL) {
00423 write_log("***cannot create tcp service\n");
00424 exit(1);
00425 }
00426 if(!svc_register(transp,DRIVE5PROG,DRIVE5VERS,drive5prog_1,IPPROTO_TCP)) {
00427 write_log("***unable to register (DRIVE5PROG, DRIVE5VERS, tcp)\n");
00428 exit(1);
00429 }
00430 #ifdef SUMT120
00431 if(tapeoffline == 0) {
00432
00433 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR5);
00434 write_log("%s\n", compcmd);
00435 if(system(compcmd)) {
00436 write_log("***Error on: %s\n", compcmd);
00437
00438 }
00439 }
00440 #endif
00441 #endif
00442 #ifdef DRIVE_6
00443 (void) pmap_unset(DRIVE6PROG, DRIVE6VERS);
00444 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00445 if (transp == NULL) {
00446 write_log("***cannot create tcp service\n");
00447 exit(1);
00448 }
00449 if(!svc_register(transp,DRIVE6PROG,DRIVE6VERS,drive6prog_1,IPPROTO_TCP)) {
00450 write_log("***unable to register (DRIVE6PROG, DRIVE6VERS, tcp)\n");
00451 exit(1);
00452 }
00453 #ifdef SUMT120
00454 if(tapeoffline == 0) {
00455
00456 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR6);
00457 write_log("%s\n", compcmd);
00458 if(system(compcmd)) {
00459 write_log("***Error on: %s\n", compcmd);
00460
00461 }
00462 }
00463 #endif
00464 #endif
00465 #ifdef DRIVE_7
00466 (void) pmap_unset(DRIVE7PROG, DRIVE7VERS);
00467 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00468 if (transp == NULL) {
00469 write_log("***cannot create tcp service\n");
00470 exit(1);
00471 }
00472 if(!svc_register(transp,DRIVE7PROG,DRIVE7VERS,drive7prog_1,IPPROTO_TCP)) {
00473 write_log("***unable to register (DRIVE7PROG, DRIVE7VERS, tcp)\n");
00474 exit(1);
00475 }
00476 #ifdef SUMT120
00477 if(tapeoffline == 0) {
00478
00479 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR7);
00480 write_log("%s\n", compcmd);
00481 if(system(compcmd)) {
00482 write_log("***Error on: %s\n", compcmd);
00483
00484 }
00485 }
00486 #endif
00487 #endif
00488 #ifdef DRIVE_8
00489 (void) pmap_unset(DRIVE8PROG, DRIVE8VERS);
00490 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00491 if (transp == NULL) {
00492 write_log("***cannot create tcp service\n");
00493 exit(1);
00494 }
00495 if(!svc_register(transp,DRIVE8PROG,DRIVE8VERS,drive8prog_1,IPPROTO_TCP)) {
00496 write_log("***unable to register (DRIVE8PROG, DRIVE8VERS, tcp)\n");
00497 exit(1);
00498 }
00499 #ifdef SUMT120
00500 if(tapeoffline == 0) {
00501
00502 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR8);
00503 write_log("%s\n", compcmd);
00504 if(system(compcmd)) {
00505 write_log("***Error on: %s\n", compcmd);
00506
00507 }
00508 }
00509 #endif
00510 #endif
00511 #ifdef DRIVE_9
00512 (void) pmap_unset(DRIVE9PROG, DRIVE9VERS);
00513 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00514 if (transp == NULL) {
00515 write_log("***cannot create tcp service\n");
00516 exit(1);
00517 }
00518 if(!svc_register(transp,DRIVE9PROG,DRIVE9VERS,drive9prog_1,IPPROTO_TCP)) {
00519 write_log("***unable to register (DRIVE9PROG, DRIVE9VERS, tcp)\n");
00520 exit(1);
00521 }
00522 #ifdef SUMT120
00523 if(tapeoffline == 0) {
00524
00525 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR9);
00526 write_log("%s\n", compcmd);
00527 if(system(compcmd)) {
00528 write_log("***Error on: %s\n", compcmd);
00529
00530 }
00531 }
00532 #endif
00533 #endif
00534 #ifdef DRIVE_10
00535 (void) pmap_unset(DRIVE10PROG, DRIVE10VERS);
00536 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00537 if (transp == NULL) {
00538 write_log("***cannot create tcp service\n");
00539 exit(1);
00540 }
00541 if(!svc_register(transp,DRIVE10PROG,DRIVE10VERS,drive10prog_1,IPPROTO_TCP)) {
00542 write_log("***unable to register (DRIVE10PROG, DRIVE10VERS, tcp)\n");
00543 exit(1);
00544 }
00545 #ifdef SUMT120
00546 if(tapeoffline == 0) {
00547
00548 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR10);
00549 write_log("%s\n", compcmd);
00550 if(system(compcmd)) {
00551 write_log("***Error on: %s\n", compcmd);
00552
00553 }
00554 }
00555 #endif
00556 #endif
00557 #ifdef DRIVE_11
00558 (void) pmap_unset(DRIVE11PROG, DRIVE11VERS);
00559 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
00560 if (transp == NULL) {
00561 write_log("***cannot create tcp service\n");
00562 exit(1);
00563 }
00564 if(!svc_register(transp,DRIVE11PROG,DRIVE11VERS,drive11prog_1,IPPROTO_TCP)) {
00565 write_log("***unable to register (DRIVE11PROG, DRIVE11VERS, tcp)\n");
00566 exit(1);
00567 }
00568 #ifdef SUMT120
00569 if(tapeoffline == 0) {
00570
00571 sprintf(compcmd, "sudo /usr/local/bin/mt -f %s defcompression 0", SUMDR11);
00572 write_log("%s\n", compcmd);
00573 if(system(compcmd)) {
00574 write_log("***Error on: %s\n", compcmd);
00575
00576 }
00577 }
00578 #endif
00579 #endif
00580
00581 sleep(2);
00582
00583 clnttape = clnt_create(thishost, TAPEPROG, TAPEVERS, "tcp");
00584 if(!clnttape) {
00585 clnt_pcreateerror("Can't get client handle to tape_svc");
00586 write_log("tape_svc not there on %s\n", thishost);
00587 exit(1);
00588 }
00589
00590 DS_ConnectDB(dbname);
00591
00592
00593
00594
00595
00596 svc_run();
00597 write_log("!!!Fatal Error: svc_run() returned in drive[n]_svc\n");
00598 exit(1);
00599 }
00600
00601
00602
00603
00604 static void
00605 #ifdef DRIVE_0
00606 drive0prog_1(rqstp, transp)
00607 #endif
00608 #ifdef DRIVE_1
00609 drive1prog_1(rqstp, transp)
00610 #endif
00611 #ifdef DRIVE_2
00612 drive2prog_1(rqstp, transp)
00613 #endif
00614 #ifdef DRIVE_3
00615 drive3prog_1(rqstp, transp)
00616 #endif
00617 #ifdef DRIVE_4
00618 drive4prog_1(rqstp, transp)
00619 #endif
00620 #ifdef DRIVE_5
00621 drive5prog_1(rqstp, transp)
00622 #endif
00623 #ifdef DRIVE_6
00624 drive6prog_1(rqstp, transp)
00625 #endif
00626 #ifdef DRIVE_7
00627 drive7prog_1(rqstp, transp)
00628 #endif
00629 #ifdef DRIVE_8
00630 drive8prog_1(rqstp, transp)
00631 #endif
00632 #ifdef DRIVE_9
00633 drive9prog_1(rqstp, transp)
00634 #endif
00635 #ifdef DRIVE_10
00636 drive10prog_1(rqstp, transp)
00637 #endif
00638 #ifdef DRIVE_11
00639 drive11prog_1(rqstp, transp)
00640 #endif
00641 struct svc_req *rqstp;
00642 SVCXPRT *transp;
00643 {
00644 union __svcargun {
00645 Rkey tapedo_1_arg;
00646 } argument;
00647 char *result, *call_err;
00648 enum clnt_stat clnt_stat;
00649
00650 bool_t (*xdr_argument)(), (*xdr_result)();
00651 char *(*local)();
00652
00653 switch (rqstp->rq_proc) {
00654 case NULLPROC:
00655 (void) svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL);
00656 return;
00657 case READDRVDO:
00658 xdr_argument = xdr_Rkey;
00659 xdr_result = xdr_Rkey;;
00660 local = (char *(*)()) readdrvdo_1;
00661 break;
00662 case WRITEDRVDO:
00663 xdr_argument = xdr_Rkey;
00664 xdr_result = xdr_Rkey;
00665 local = (char *(*)()) writedrvdo_1;
00666 break;
00667 default:
00668 write_log("**drive[0,1]prog_1() dispatch default procedure %d,ignore\n", rqstp->rq_proc);
00669 svcerr_noproc(transp);
00670 return;
00671 }
00672 bzero((char *)&argument, sizeof(argument));
00673 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
00674 write_log("***Error on svc_getargs()\n");
00675 svcerr_decode(transp);
00676 svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL);
00677 return;
00678
00679 }
00680 glb_transp = transp;
00681
00682 result = (*local)(&argument, rqstp);
00683
00684 if(result) {
00685 if(result == (char *)1) {
00686
00687 }
00688 else {
00689 if(debugflg) {
00690 write_log("\nKEYS in drive[0,1]_svc response to tape_svc are:\n");
00691 keyiterate(logkey, (KEY *)result);
00692 }
00693 if(current_client == 0) {
00694 write_log("***Error on clnt_call() back to orig sum_svc caller\n");
00695 write_log(" current_client was NULL\n");
00696 }
00697 else {
00698 clnt_stat=clnt_call(current_client, procnum, (xdrproc_t)xdr_result,
00699 (char *)result, (xdrproc_t)xdr_void, 0, TIMEOUT);
00700
00701 if(clnt_stat != RPC_SUCCESS) {
00702 if(clnt_stat != RPC_TIMEDOUT) {
00703 clnt_perrno(clnt_stat);
00704 write_log("***Error on clnt_call() back to %d procedure (in driven_svc)\n", procnum);
00705 call_err = clnt_sperror(current_client, "Err");
00706 write_log("%s\n", call_err);
00707 } else {
00708 write_log("timeout occured in drive%d_svc call to tape_svc\n", drivenum);
00709 }
00710 }
00711 }
00712 freekeylist((KEY **)&result);
00713 }
00714 }
00715 else {
00716
00717 }
00718 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
00719 write_log("**unable to free arguments\n");
00720
00721 }
00722 return;
00723 }
00724
00725
00726
00727
00728
00729
00730 void send_ack()
00731 {
00732
00733 if (!svc_sendreply(glb_transp, (xdrproc_t)xdr_uint32_t, (char *)&rinfo)) {
00734 write_log("***Error on immed ack back to client. FATAL???\n");
00735 svcerr_systemerr(glb_transp);
00736 }
00737 }
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752 int kludge_dirs(uint64_t file_dsix_off[], char *wd)
00753 {
00754 DIR *dfd;
00755 struct dirent *dp;
00756 char name[512], d_name[512], newname[512], nameD[512], cmd[512];
00757 char *savename[512];
00758 uint64_t ix;
00759 int i, k, found;
00760 int j=0;
00761
00762 if((dfd=opendir(wd)) == NULL) {
00763 printk("kludge_dirs() can't open dir %s\n", wd);
00764 return(1);
00765 }
00766 savename[0] = NULL;
00767 while((dp=readdir(dfd)) != NULL) {
00768 if(strcmp(dp->d_name, ".") == 0
00769 || strcmp(dp->d_name, "..") == 0)
00770 continue;
00771 sprintf(d_name, dp->d_name);
00772 for(i=0; ; i++) {
00773 ix = file_dsix_off[i];
00774 if(!ix) break;
00775 sprintf(name, "D%lu", ix);
00776 if(!strcmp(d_name, name)) {
00777 break;
00778 }
00779 }
00780 if(!ix) {
00781 savename[j++] = strdup(d_name);
00782 }
00783 }
00784 closedir(dfd);
00785 if(savename[0]) {
00786 for(i=0; i < j; i++) {
00787 sprintf(name, "%s/%s", wd, savename[i]);
00788 if((dfd=opendir(name)) == NULL) {
00789 printk("kludge_dirs() can't open dir %s\n", name);
00790 return(1);
00791 }
00792 while((dp=readdir(dfd)) != NULL) {
00793 if(strcmp(dp->d_name, ".") == 0
00794 || strcmp(dp->d_name, "..") == 0)
00795 continue;
00796
00797 found = 0;
00798 for(k=0; ; k++) {
00799 ix = file_dsix_off[k];
00800 if(!ix) break;
00801 sprintf(nameD, "D%lu", ix);
00802 if(!strcmp(nameD, dp->d_name)) {
00803 found = 1;
00804 break;
00805 }
00806 }
00807 if(found) {
00808 sprintf(d_name, "%s/%s", name, dp->d_name);
00809 sprintf(newname, "%s/%s", wd, dp->d_name);
00810 if(rename(d_name, newname)) {
00811 printk("kludge_dirs() error on rename() %s %s\n", d_name, newname);
00812 closedir(dfd);
00813 return(1);
00814 }
00815 }
00816 }
00817 closedir(dfd);
00818 }
00819
00820
00821 for(i=0; i < j; i++) {
00822 sprintf(cmd, "/bin/rm -rf %s/%s", wd, savename[i]);
00823 if(system(cmd)) {
00824 printk("Error: %s", cmd);
00825 }
00826 }
00827 }
00828 return(0);
00829 }
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874 KEY *readdrvdo_1(KEY *params)
00875 {
00876 CLIENT *client;
00877 static KEY *retlist;
00878 int sim, dsmdiflg;
00879 uint64_t file_dsix[MAXSUMREQCNT+1];
00880 uint64_t file_dsix_off[MAXSUMREQCNT+1];
00881 uint64_t uid, ds_index, dsixtmp;
00882 double file_bytes[MAXSUMREQCNT+1];
00883 double bytes;
00884 int dnum, tapefilenum, reqofflinenum, status, tapemode, filenum, touch;
00885 int reqcnt, xtapefilenum, i, j, filenumdelta, retainday;
00886 char tmpname[80], newdir[80], dname[64], rdlog[80];
00887 char *wd, *oldwd, *token, *lasttoken;
00888 char *tapeid, *xtapeid, *cptr, *delday;
00889 char *initdirs[MAXSUMREQCNT];
00890
00891 if(findkey(params, "DEBUGFLG")) {
00892 debugflg = getkey_int(params, "DEBUGFLG");
00893 if(debugflg) {
00894 write_log("!!Keylist in readdrvdo_1() is:\n");
00895 keyiterate(logkey, params);
00896 }
00897 }
00898 if(findkey(params, "tdays"))
00899 touch = abs(getkey_int(params, "tdays"));
00900 else
00901 touch = 2;
00902 retlist = newkeylist();
00903 add_keys(params, &retlist);
00904 client = (CLIENT *)getkey_fileptr(params, "current_client");
00905
00906 setkey_fileptr(&retlist, "current_client", (FILE *)client);
00907 dnum = getkey_int(params, "dnum");
00908 sprintf(dname, "%s%d", SUMDR, dnum);
00909 sim = 0;
00910 if(findkey(params, "sim")) {
00911 sim = getkey_int(params, "sim");
00912 }
00913 uid = getkey_uint64(params, "uid");
00914
00915 reqofflinenum = getkey_int(params, "reqofflinenum");
00916
00917
00918 sprintf(tmpname, "rootwd_%d", reqofflinenum);
00919 wd = getkey_str(params, tmpname);
00920 sprintf(tmpname, "tapefilenum_%d", reqofflinenum);
00921 tapefilenum = getkey_int(params, tmpname);
00922 rinfo = RESULT_PEND;
00923 send_ack();
00924
00925 if((filenum = get_tape_fnum_rdy(sim, dname)) == -1) {
00926 write_log("***Error: can't get file # on drive %d\n", dnum);
00927 setkey_int(&retlist, "STATUS", 1);
00928 sprintf(errstr, "Error on tape ready for read in drive #%d", dnum);
00929 setkey_str(&retlist, "ERRSTR", errstr);
00930 free(wd);
00931 return(retlist);
00932 }
00933
00934 current_client = clnttape;
00935 procnum = TAPERESPREADDO;
00936 tapemode = getkey_int(params, "tapemode");
00937 if(tapemode == TAPE_RD_INIT) {
00938 if(position_tape_file_asf(sim, dnum, tapefilenum) == -1) {
00939 setkey_int(&retlist, "STATUS", 1);
00940 sprintf(errstr, "Error on position tape to file #%d in drive #%d",
00941 tapefilenum, dnum);
00942 setkey_str(&retlist, "ERRSTR", errstr);
00943 free(wd);
00944 return(retlist);
00945 }
00946 get_tape_fnum_rdy(sim, dname);
00947 }
00948 else if(tapemode == TAPE_RD_CONT) {
00949
00950
00951
00952
00953 filenumdelta = tapefilenum - filenum;
00954 if(position_tape_file_fsf(sim, dnum, filenumdelta) == -1) {
00955 setkey_int(&retlist, "STATUS", 1);
00956 sprintf(errstr, "Error on position tape to file #%d in drive #%d",
00957 tapefilenum, dnum);
00958 setkey_str(&retlist, "ERRSTR", errstr);
00959 free(wd);
00960 return(retlist);
00961 }
00962 if((filenum = get_tape_fnum_rdy(sim, dname)) == -1) {
00963 write_log("***Error: can't get file # on drive %d\n", dnum);
00964 setkey_int(&retlist, "STATUS", 1);
00965 sprintf(errstr, "Error on position tape to file #%d in drive #%d",
00966 tapefilenum, dnum);
00967 setkey_str(&retlist, "ERRSTR", errstr);
00968 free(wd);
00969 return(retlist);
00970 }
00971 write_log("Dr%d:rd:Next file on tape=%d\n", dnum, filenum);
00972 }
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991 reqcnt = getkey_int(params, "reqcnt");
00992 sprintf(tmpname, "tapeid_%d", reqofflinenum);
00993 tapeid = getkey_str(params, tmpname);
00994
00995 if(SUMLIB_Ds_Ix_Find(tapeid, tapefilenum, file_dsix, file_bytes)) {
00996 write_log("***Error: SUMLIB_Ds_Ix_Find(%s, %d)\n", tapeid, tapefilenum);
00997 setkey_int(&retlist, "STATUS", 1);
00998 sprintf(errstr, "Error: SUMLIB_Ds_Ix_Find(%s, %d)", tapeid, tapefilenum);
00999 setkey_str(&retlist, "ERRSTR", errstr);
01000 free(wd);
01001 free(tapeid);
01002 return(retlist);
01003 }
01004
01005
01006
01007
01008 status = SUMLIB_Ds_Ix_File(file_dsix, file_dsix_off, &dsmdiflg);
01009 if(status == -1) {
01010 setkey_int(&retlist, "STATUS", 1);
01011 sprintf(errstr, "Error on SUMLIB_Ds_Ix_File() in driven_svc");
01012 setkey_str(&retlist, "ERRSTR", errstr);
01013 free(wd);
01014 free(tapeid);
01015 return(retlist);
01016 }
01017 if(status == 1) {
01018
01019 sprintf(rdlog, "%s/gtar_rd_%d_%s_%d.log",
01020 GTARLOGDIR, dnum, tapeid, tapefilenum);
01021 if((status = read_drive_to_wd(sim, wd, dnum, tapeid, tapefilenum, rdlog, file_dsix_off, dsmdiflg))== -1) {
01022 setkey_int(&retlist, "STATUS", 1);
01023 sprintf(errstr, "Error on read drive #%d into %s", dnum, wd);
01024 setkey_str(&retlist, "ERRSTR", errstr);
01025 free(wd);
01026 free(tapeid);
01027 return(retlist);
01028 }
01029 sprintf(tmpname, "/bin/rm -f %s", rdlog);
01030 system(tmpname);
01031
01032 if(dsmdiflg) {
01033 if(kludge_dirs(file_dsix_off, wd)) {
01034 setkey_int(&retlist, "STATUS", 1);
01035 sprintf(errstr, "Error on kludge_dirs() for %s", wd);
01036 setkey_str(&retlist, "ERRSTR", errstr);
01037 free(wd);
01038 free(tapeid);
01039 return(retlist);
01040 }
01041 }
01042
01043
01044 for(i=0; ; i++) {
01045 ds_index = file_dsix_off[i];
01046 if(!ds_index) break;
01047 retainday = RETAINDAY;
01048 for(j=0; j < reqcnt ; j++) {
01049 sprintf(tmpname, "dsix_%d", j);
01050 dsixtmp = getkey_uint64(params, tmpname);
01051 if(ds_index == dsixtmp) {
01052 retainday = touch;
01053 break;
01054 }
01055 }
01056 sprintf(newdir, "%s/D%lu", wd, ds_index);
01057 bytes = file_bytes[i];
01058 if(SUM_StatOnline(ds_index, newdir)) {
01059 setkey_int(&retlist, "STATUS", 1);
01060 sprintf(errstr,"Error: can't put SU ix=%lu online in SUM_MAIN table",
01061 ds_index);
01062 setkey_str(&retlist, "ERRSTR", errstr);
01063 free(wd);
01064 free(tapeid);
01065 return(retlist);
01066 }
01067 delday = (char *)get_effdate(retainday);
01068 write_log("Tape rd of sunum=%lu has touch days = %d\n",
01069 ds_index, retainday);
01070
01071 if(NC_PaUpdate(newdir, uid, bytes, DADP, 0, delday,
01072 0, 0, ds_index, 1, 1)) {
01073 setkey_int(&retlist, "STATUS", 1);
01074 sprintf(errstr,"Error: can't put SU %s on del pend list", newdir);
01075 setkey_str(&retlist, "ERRSTR", errstr);
01076 free(wd);
01077 free(tapeid);
01078 return(retlist);
01079 }
01080 free(delday);
01081
01082
01083
01084
01085
01086 }
01087 }
01088 setkey_int(&retlist, "STATUS", 0);
01089 uint32_t sprog;
01090 sprog = getkey_uint32(retlist, "SPROG");
01091 write_log("!!TEMP: SPROG for tape rd completion = %d, uid = %lu\n",sprog,uid);
01092 free(wd);
01093 free(tapeid);
01094 return(retlist);
01095 }
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148 KEY *writedrvdo_1(KEY *params)
01149 {
01150 CLIENT *client;
01151 static KEY *retlist;
01152 uint64_t tell;
01153 int dnum, sim, tape_closed, group, filenumwrt, tapenxtfn, rdyfn;
01154 char *tapeid;
01155 char gtarlog[80], md5sum[64], dname[64], cmd[80];
01156
01157 write_log("Called writedrvdo_1() in driven_svc\n");
01158 if(findkey(params, "DEBUGFLG")) {
01159 debugflg = getkey_int(params, "DEBUGFLG");
01160 if(debugflg) {
01161 write_log("!!Keylist in writedrvdo_1() is:\n");
01162 keyiterate(logkey, params);
01163 }
01164 }
01165 retlist = newkeylist();
01166 dnum = getkey_int(params, "dnum");
01167 sprintf(dname, "%s%d", SUMDR, dnum);
01168 filenumwrt = getkey_int(params, "nxtwrtfn");
01169 sim = 0;
01170 if(findkey(params, "sim")) {
01171 sim = getkey_int(params, "sim");
01172 }
01173 group = getkey_int(params, "group_id");
01174 tapeid = GETKEY_str(params, "tapeid");
01175 tape_closed = getkey_int(params, "tape_closed");
01176 rinfo = RESULT_PEND;
01177 send_ack();
01178 if((rdyfn=get_tape_fnum_rdy(sim, dname)) == -1) {
01179 write_log("***Error: can't get ready for write on drive %d\n", dnum);
01180 setkey_int(&retlist, "STATUS", 1);
01181 sprintf(errstr, "Error on tape ready for read in drive #%d", dnum);
01182 setkey_str(&retlist, "ERRSTR", errstr);
01183 return(retlist);
01184 }
01185
01186 add_keys(params, &retlist);
01187 client = (CLIENT *)getkey_fileptr(params, "current_client");
01188
01189 setkey_fileptr(&retlist, "current_client", (FILE *)client);
01190 current_client = clnttape;
01191 procnum = TAPERESPWRITEDO;
01192 if(tape_closed == TAPECLOSED) {
01193 setkey_int(&retlist, "STATUS", TAPECLOSEDREJECT);
01194 sprintf(errstr,"Error: Can't write to closed tape %s (driven_svc)", tapeid);
01195 setkey_str(&retlist, "ERRSTR", errstr);
01196 return(retlist);
01197 }
01198 if(tape_closed == TAPEUNINIT) {
01199 if(position_tape_bot(sim, dnum) == -1) {
01200 setkey_int(&retlist, "STATUS", 1);
01201 sprintf(errstr,"Error: Can't position tape %s to BOT in drive #%d",
01202 tapeid, dnum);
01203 setkey_str(&retlist, "ERRSTR", errstr);
01204 return(retlist);
01205 }
01206 sprintf(gtarlog, "%s/gtar_wt_%d_%s_%d.hdr.log",
01207 GTARLOGDIR, dnum, tapeid, filenumwrt);
01208 if(write_hdr_to_drive(sim, tapeid, group, dnum, gtarlog) == -1) {
01209 setkey_int(&retlist, "STATUS", 1);
01210 sprintf(errstr,"Error: Can't write TAPELABEL to tape %s in drive #%d",
01211 tapeid, dnum);
01212 setkey_str(&retlist, "ERRSTR", errstr);
01213 return(retlist);
01214 }
01215 sprintf(cmd, "/bin/rm -f %s", gtarlog);
01216 system(cmd);
01217 get_tape_fnum_rdy(sim, dname);
01218 }
01219 if(filenumwrt != rdyfn) {
01220 if(position_tape_eod(sim, dnum) == -1) {
01221 setkey_int(&retlist, "STATUS", 1);
01222 sprintf(errstr,"Error: Can't position to EOD tape %s in drive #%d",
01223 tapeid, dnum);
01224 setkey_str(&retlist, "ERRSTR", errstr);
01225 return(retlist);
01226 }
01227 }
01228 sprintf(gtarlog, "%s/gtar_wt_%d_%s_%d.log",
01229 GTARLOGDIR, dnum, tapeid, filenumwrt);
01230 if((tapenxtfn =write_wd_to_drive(sim, params, dnum, filenumwrt, gtarlog)) == -1) {
01231 setkey_int(&retlist, "STATUS", 1);
01232 sprintf(errstr,"Error on write to drive #%d", dnum);
01233 setkey_str(&retlist, "ERRSTR", errstr);
01234 return(retlist);
01235 }
01236 sprintf(cmd, "/bin/rm -f %s", gtarlog);
01237 system(cmd);
01238 setkey_int(&retlist, "TAPENXTFN", tapenxtfn);
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250 setkey_str(&retlist, "md5cksum", md5str);
01251 #ifdef SUMT120
01252 if((tell = tell_blocks(sim, dnum)) == -1) {
01253 write_log("**Can't get current block# on drive %d. Proceed w/o it.\n",
01254 dnum);
01255 tell = 0;
01256 }
01257 #else
01258 tell = 0;
01259 #endif
01260 setkey_uint64(&retlist, "tellblock", tell);
01261 setkey_int(&retlist, "gtarblock", GTARBLOCK);
01262 setkey_int(&retlist, "STATUS", 0);
01263 return(retlist);
01264 }
01265
01266
01267
01268
01269 int position_tape_eod(int sim, int dnum)
01270 {
01271 char cmd[256], dname[80];
01272 int lastfilenum;
01273
01274 sprintf(dname, "%s%d", SUMDR, dnum);
01275 sprintf(cmd, "/usr/local/bin/mt -f %s eod 1>> %s 2>&1", dname, logfile);
01276 write_log("*Dr%d:wt: %s\n", dnum, cmd);
01277 if(sim) {
01278 sleep(2);
01279 lastfilenum = 999999;
01280 }
01281 else {
01282 if(system(cmd)) {
01283 write_log("***Dr%d:wt:error\n", dnum);
01284 return(-1);
01285 }
01286 if((lastfilenum = get_tape_fnum_rdy(sim, dname)) == -1) {
01287 write_log("***Error: can't get file # on drive %d\n", dnum);
01288 return(-1);
01289 }
01290 }
01291 write_log("**Dr%d:wt:success eod=%d\n", dnum,lastfilenum);
01292 return(0);
01293 }
01294
01295
01296
01297
01298 int position_tape_bot(int sim, int dnum)
01299 {
01300 char cmd[256], dname[80];
01301
01302 sprintf(dname, "%s%d", SUMDR, dnum);
01303 sprintf(cmd, "/usr/local/bin/mt -f %s rewind 1>> %s 2>&1", dname, logfile);
01304 write_log("*Dr%d:wt: %s\n", drivenum, cmd);
01305 if(sim) {
01306 sleep(2);
01307 }
01308 else {
01309 if(system(cmd)) {
01310 write_log("***Dr%d:wt:error\n", drivenum);
01311 return(-1);
01312 }
01313 }
01314
01315 if(get_tape_fnum_rdy(0, dname) == -1) {
01316 write_log("***Dr%d:wt:error\n", drivenum);
01317 return(-1);
01318 }
01319 write_log("**Dr%d:wt:success\n", drivenum);
01320 return(0);
01321 }
01322
01323
01324
01325
01326 int position_tape_file_asf(int sim, int dnum, int fnum)
01327 {
01328 char cmd[256], dname[80];
01329
01330 sprintf(dname, "%s%d", SUMDR, dnum);
01331 sprintf(cmd, "/usr/local/bin/mt -f %s asf %d 1>> %s 2>&1", dname, fnum, logfile);
01332 write_log("*Dr%d:rd: %s\n", drivenum, cmd);
01333 if(sim) {
01334 sleep(2);
01335 }
01336 else {
01337 if(system(cmd)) {
01338 write_log("***Dr%d:rd:error\n", drivenum);
01339 send_mail("Dr%d:rd:error position", drivenum);
01340 drive_reset(dname);
01341 return(-1);
01342 }
01343 }
01344 write_log("**Dr%d:rd:success\n", drivenum);
01345 return(0);
01346 }
01347
01348
01349
01350
01351 int position_tape_file_fsf(int sim, int dnum, int fdelta)
01352 {
01353 char cmd[256], dname[80];
01354
01355 if(fdelta == 0) { return(0); }
01356 sprintf(dname, "%s%d", SUMDR, dnum);
01357 if(fdelta < 0) {
01358 fdelta = -fdelta;
01359
01360 fdelta += 1;
01361 sprintf(cmd, "/usr/local/bin/mt -f %s bsfm %d 1>> %s 2>&1",
01362 dname, fdelta, logfile);
01363
01364 }
01365 else {
01366
01367 sprintf(cmd, "/usr/local/bin/mt -f %s fsf %d 1>> %s 2>&1",
01368 dname, fdelta, logfile);
01369 }
01370 write_log("*Dr%d:rd: %s\n", drivenum, cmd);
01371 if(sim) {
01372 sleep(2);
01373 }
01374 else {
01375 if(system(cmd)) {
01376 write_log("***Dr%d:rd:error\n", drivenum);
01377 send_mail("Dr%d:rd:error position", drivenum);
01378 drive_reset(dname);
01379 return(-1);
01380 }
01381 }
01382 write_log("**Dr%d:rd:success\n", drivenum);
01383 return(0);
01384 }
01385
01386
01387
01388
01389 int get_cksum(char *cfile, char *cksum)
01390 {
01391 FILE *cfp;
01392 char row[MAXSTR];
01393 struct stat *statbuf = (struct stat *)malloc(sizeof(struct stat));
01394
01395 stat(cfile, statbuf);
01396 if(statbuf->st_size == 0) {
01397 write_log("***Error: No md5 cksum produced.\n");
01398 return(1);
01399 }
01400 if (!(cfp = fopen(cfile, "r"))) {
01401 write_log("***Error: can't open %s\n", cfile);
01402 return(1);
01403 }
01404 while (fgets (row,MAXSTR,cfp)) {
01405 sscanf(row, "%s", cksum);
01406 }
01407 fclose(cfp);
01408 return(0);
01409 }
01410
01411 uint64_t tell_blocks(int sim, int dnum)
01412 {
01413 char cmd[256], dname[64], outname[64], scr[64], row[MAXSTR];
01414 uint64_t tell;
01415 FILE *tfp;
01416
01417
01418 sprintf(outname, "/usr/local/logs/SUM/%d/tell_blocks.txt", dnum);
01419 sprintf(dname, "%s%d", SUMDR, dnum);
01420 sprintf(cmd, "/usr/local/bin/mt -f %s tell 1> %s 2>&1", dname, outname);
01421 write_log("cmd = %s\n", cmd);
01422 if(sim) {
01423 sleep(2);
01424 }
01425 else {
01426 if(system(cmd)) {
01427 write_log("***cmd failed\n");
01428 return(-1);
01429 }
01430 }
01431 write_log("**cmd success\n");
01432 if (!(tfp = fopen(outname, "r"))) {
01433 write_log("***Error: can't open %s\n", outname);
01434 return(-1);
01435 }
01436 while (fgets (row,MAXSTR,tfp)) {
01437 if(strstr(row, "At block ")) {
01438 sscanf(row, "%s %s %lu", scr, scr, &tell);
01439 }
01440 }
01441 fclose(tfp);
01442 return(tell);
01443 }
01444
01445
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483
01484 int write_wd_to_drive(int sim, KEY *params, int drive, int fnum, char *logname)
01485 {
01486 FILE *fptar;
01487 MD5_CTX md5ctx;
01488 size_t bytesread;
01489 ssize_t byteswrite;
01490 unsigned char md5val[16];
01491 char buf[BLOCK_SIZE];
01492 char cmd[CMDLENWRT], dname[64], tmpname[64], wdroot[64], wdD[64];
01493 char *cptr, *wd;
01494 int status, cnt, i, len, tapefilenum, fd;
01495
01496 sprintf(dname, "%s%d", SUMDR, drive);
01497 sprintf(md5file, "/usr/local/logs/SUM/md5/cksum%d", drive);
01498 sprintf(cmd, "touch %s", logname);
01499 if(status = system(cmd)) {
01500 write_log("***Dr%d:wt:Err: %s\n", drive, cmd);
01501 return(-1);
01502 }
01503
01504 sprintf(cmd, "%s -cf - -b %d ", GTAR, GTARBLOCK);
01505 cnt = getkey_int(params, "reqcnt");
01506 for(i = 0; i < cnt; i++) {
01507 sprintf(tmpname, "wd_%i", i);
01508 wd = GETKEY_str(params, tmpname);
01509 cptr = strstr(wd, "/D");
01510 strcpy(wdD, cptr+1);
01511 strcpy(wdroot, wd);
01512 cptr = strstr(wdroot, "/D");
01513 *cptr = (char)NULL;
01514 len = strlen(cmd) + strlen(wdroot) + strlen(wdD) + 8;
01515 if(len > CMDLENWRT) {
01516 write_log("***Error: cmd too long: %s\n", cmd);
01517 return(-1);
01518 }
01519 sprintf(cmd, "%s -C %s %s", cmd, wdroot, wdD);
01520 }
01521 sprintf(cmd, "%s 2>%s", cmd, logname);
01522
01523
01524
01525
01526 write_time();
01527 write_log("*Dr%d:wt: %s\n", drive, cmd);
01528 if(sim) {
01529 sleep(2);
01530 }
01531 else {
01532
01533
01534
01535
01536
01537
01538
01539 if(!(fptar = popen(cmd, "r"))) {
01540 write_log("***Dr%d:wt:Error. Can't popen() cmd\n", drive);
01541 drive_reset(dname);
01542 return(-1);
01543 }
01544 if((fd = open(dname, O_WRONLY)) == -1) {
01545 pclose(fptar);
01546 write_log("***Dr%d:wt:Error. Can't open(%s) cmd\n", drive, dname);
01547 drive_reset(dname);
01548 return(-1);
01549 }
01550 MD5_Init(&md5ctx);
01551 errno = 0;
01552 while(bytesread = fread(buf, 1, BLOCK_SIZE, fptar)) {
01553 MD5_Update(&md5ctx, buf, bytesread);
01554 while(bytesread > 0) {
01555 if((byteswrite = write(fd, buf, bytesread)) == -1) {
01556 pclose(fptar);
01557 close(fd);
01558 write_log("***Dr%d:wt:Error. Can't write(%s). errno=%d\n",
01559 drive, dname, errno);
01560 drive_reset(dname);
01561 return(-1);
01562 }
01563 bytesread -= byteswrite;
01564 }
01565 }
01566 close(fd);
01567 if(!feof(fptar)) {
01568 pclose(fptar);
01569 write_log("***Dr%d:wt:Error. Not at eof on popen cmd. errno=%d\n",
01570 drive, errno);
01571 drive_reset(dname);
01572 return(-1);
01573 }
01574 MD5_Final(md5val, &md5ctx);
01575 if(status = pclose(fptar)) {
01576 write_log("***Dr%d:wt:Error. popen cmd status=%d. errno=%d\n",
01577 drive, status, errno);
01578 drive_reset(dname);
01579 return(-1);
01580 }
01581 md5str[32] = 0;
01582 for(i=0; i < 16; i++)
01583 sprintf(md5str+2*i, "%02x", md5val[i]);
01584 }
01585 if((tapefilenum = get_tape_fnum_rdy(sim, dname)) == -1) {
01586 write_log("***Error: can't get file # on drive %d\n", drive);
01587 return(-1);
01588 }
01589 write_log("Dr%d:wt:Next file on tape=%d\n", drive, tapefilenum);
01590 if(!sim) {
01591 if(tapefilenum != (fnum + 1)) {
01592 write_log("***Dr%d:wt:Advisory: Tape file expected=%d, found=%d\n",
01593 drive, fnum+1, tapefilenum);
01594 if((tapefilenum - fnum) > 2) {
01595 return(-1);
01596 }
01597
01598
01599
01600
01601
01602
01603
01604 }
01605 }
01606 write_log("***Dr%d:wt:success\n", drive);
01607 return(tapefilenum);
01608 }
01609
01610
01611
01612
01613 int write_hdr_to_drive(int sim, char *tapeid, int group, int drive, char *log)
01614 {
01615 FILE *lfp;
01616 char cmd[1024], dname[64], tmpname[64], dirname[64];
01617 int status;
01618
01619
01620 sprintf(dirname, "/usr/local/logs/SUM/%d", drive);
01621 sprintf(cmd, "mkdir -p %s", dirname);
01622 system(cmd);
01623 sprintf(tmpname, "%s/TAPELABEL", dirname);
01624 if((lfp=fopen(tmpname, "w")) == NULL) {
01625 write_log("***Error: Can't open %s errno=%d\n", tmpname, errno);
01626 return(-1);
01627 }
01628 fprintf(lfp, "Created %s", get_time());
01629 fprintf(lfp, "tapeid %s\n", tapeid);
01630 fprintf(lfp, "group_id %d\n", group);
01631 if(fclose(lfp)) {
01632 write_log("***Error: Can't close %s errno=%d\n", tmpname, errno);
01633 return(-1);
01634 }
01635 sprintf(dname, "%s%d", SUMDR, drive);
01636 sprintf(cmd, "%s -cvf %s -b %d -C %s TAPELABEL 1> %s 2>&1",
01637 GTAR, dname, GTARBLOCK, dirname, log);
01638 write_time();
01639 write_log("*Dr%d:wt: %s\n", drivenum, cmd);
01640 if(sim) {
01641 sleep(2);
01642 }
01643 else {
01644 if(status = system(cmd)) {
01645 write_log("***Dr%d:wt:Error. exit status=%d\n",drivenum,WEXITSTATUS(status));
01646 drive_reset(dname);
01647 return(-1);
01648 }
01649 }
01650 write_log("***Dr%d:wt:success\n", drivenum);
01651 return(drive);
01652 }
01653
01654
01655
01656
01657
01658
01659 int get_tape_fnum_rdy(int sim, char *dname)
01660 {
01661 int fd;
01662 int ret;
01663 int waitcnt = 0;
01664 struct mtget mt_stat;
01665
01666 if(sim) { return(999999); }
01667 while(1) {
01668 fd = open(dname, O_RDONLY | O_NONBLOCK);
01669 if(fd == -1) {
01670 write_log("Failed to open %s. errno=%d\n", dname, errno);
01671 return(-1);
01672 }
01673 ioctl(fd, MTIOCGET, &mt_stat);
01674 write_log("mt_gstat = 0x%0x\n", mt_stat.mt_gstat);
01675 if(!GMT_ONLINE(mt_stat.mt_gstat)) {
01676 if(++waitcnt == MAX_WAIT) {
01677 write_log("%s does not go ready\n", dname);
01678 ret = -1;
01679 break;
01680 }
01681 write_log("Wait for ready %s %d of %d\n", dname, waitcnt, MAX_WAIT);
01682 sleep(2);
01683 }
01684 else {
01685
01686
01687 close(fd);
01688 fd = open(dname, O_RDONLY | O_NONBLOCK);
01689 if(fd == -1) {
01690 write_log("Failed to open %s. errno=%d\n", dname, errno);
01691 return(-1);
01692 }
01693 ioctl(fd, MTIOCGET, &mt_stat);
01694 write_log("After ready mt_fileno=%d\n", mt_stat.mt_fileno);
01695 ret = mt_stat.mt_fileno;
01696 break;
01697 }
01698 close(fd);
01699 }
01700 close(fd);
01701 return(ret);
01702 }
01703
01704 void drive_reset(char *dname)
01705 {
01706 int fd;
01707 int waitcnt = 0;
01708 struct mtop mt_op;
01709 struct mtget mt_stat;
01710
01711 return;
01712
01713 write_log("***PENDING RESET: drive %s\n", dname);
01714 fd = open(dname, O_RDONLY | O_NONBLOCK);
01715 mt_op.mt_op = MTRESET;
01716 mt_op.mt_count = 0;
01717 ioctl(fd, MTIOCTOP, &mt_op);
01718 while(1) {
01719 ioctl(fd, MTIOCGET, &mt_stat);
01720 if(mt_stat.mt_gstat == 0) {
01721 if(++waitcnt == MAX_WAIT) {
01722 write_log("***RESET ERROR: drive %s doesn't go ready\n", dname);
01723 return;
01724 }
01725 sleep(1);
01726 }
01727 }
01728 close(fd);
01729 write_log("***RESET: drive %s\n", dname);
01730 }
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740
01741
01742
01743
01744
01745
01746
01747
01748
01749 int read_drive_to_wd(int sim, char *wd, int drive, char *tapeid,
01750 int tapefilenum, char *logname, uint64_t filedsixoff[], int dsmdiflg)
01751 {
01752 char dname[80], md5file[80], md5sum[36], md5db[36], Ddir[128];
01753 uint64_t dsix;
01754 int i, status;
01755
01756 sprintf(dname, "%s%d", SUMDR, drive);
01757 sprintf(md5file, "/usr/local/logs/SUM/md5/rdsum_%d", drive);
01758 sprintf(rcmd, "dd if=%s bs=%db 2> %s | %s %d %s 2>> %s | %s xvf - -b%d -C %s",
01759 dname, GTARBLOCK, logname, md5filter, GTARBLOCK, md5file, logname, GTAR, GTARBLOCK, wd);
01760
01761 if(!dsmdiflg) {
01762 for(i=0; ; i++) {
01763 dsix = filedsixoff[i];
01764 if(!dsix) break;
01765 sprintf(Ddir, "D%lu", dsix);
01766 sprintf(rcmd, "%s %s", rcmd, Ddir);
01767 }
01768 }
01769 sprintf(rcmd, "%s 1>>%s 2>> %s", rcmd, logfile, logname);
01770 write_time();
01771 write_log("*Dr%d:rd: %s\n", drivenum, rcmd);
01772 if(sim) {
01773 sleep(2);
01774
01775
01776
01777 }
01778 else {
01779 if(status = system(rcmd)) {
01780 write_log("***Dr%d:rd:Error. exit status=%d\n", drivenum, WEXITSTATUS(status));
01781 send_mail("Dr%d:rd:error %s", drivenum, tapeid);
01782 return(-1);
01783 }
01784 }
01785 if(!sim) {
01786 if(get_cksum(md5file, md5sum)) { return(-1); }
01787 if(SUMLIB_Get_MD5(tapeid, tapefilenum, md5db)) {
01788 return(-1);
01789 }
01790 if(strcmp(md5sum, md5db)) {
01791 write_log("***Dr%d:rd:Error md5 compare:\n", drivenum);
01792 write_log(" exp=%s read=%s\n", md5db, md5sum);
01793 send_mail("Dr%d:rd:error md5", drivenum);
01794 return(-1);
01795 }
01796 }
01797 write_log("***Dr%d:rd:success %s\n", drivenum, md5sum);
01798 write_time();
01799 return(drive);
01800 }
01801
01802 char *get_time()
01803 {
01804 struct timeval tvalr;
01805 struct tm *t_ptr;
01806 static char datestr[32];
01807
01808 gettimeofday(&tvalr, NULL);
01809 t_ptr = localtime((const time_t *)&tvalr.tv_sec);
01810 sprintf(datestr, "%s", asctime(t_ptr));
01811 return(datestr);
01812 }
01813
01814 int send_mail(char *fmt, ...)
01815 {
01816 va_list args;
01817 char string[1024], cmd[1024];
01818
01819 va_start(args, fmt);
01820 vsprintf(string, fmt, args);
01821
01822 sprintf(cmd, "echo \"%s\" | Mail -s \"drive_svc error\" dcs_admin@sun.stanford.edu", string);
01823 system(cmd);
01824 va_end(args);
01825 return(0);
01826 }
01827
01828 void write_time()
01829 {
01830 write_log("%s", get_time());
01831 }
01832