00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <SUM.h>
00013 #include <sys/socket.h>
00014 #include <sys/errno.h>
00015 #include <signal.h>
00016 #include <rpc/rpc.h>
00017 #include <sum_rpc.h>
00018 #include <soi_error.h>
00019 #if defined(SUMS_TAPE_AVAILABLE) && SUMS_TAPE_AVAILABLE
00020 #include <tape.h>
00021 #endif
00022 #include <printk.h>
00023
00024 void logkey();
00025 extern int errno;
00026 static struct timeval TIMEOUT = { 600, 0 };
00027
00028 #define FIRST_EXP_SLOT 2201
00029
00030 FILE *logfp;
00031 CLIENT *current_client, *clnttape;
00032 uint32_t tapeback;
00033 enum clnt_stat status;
00034 char *call_err;
00035 char *dbname;
00036 char thishost[MAX_STR];
00037 char datestr[32];
00038 int soi_errno = NO_ERROR;
00039
00040
00041 void open_log(char *filename)
00042 {
00043 if((logfp=fopen(filename, "w")) == NULL) {
00044 fprintf(stderr, "Can't open the log file %s\n", filename);
00045 }
00046 }
00047
00048
00049
00050 static char *datestring()
00051 {
00052 struct timeval tvalr;
00053 struct tm *t_ptr;
00054
00055 gettimeofday(&tvalr, NULL);
00056 t_ptr = localtime((const time_t *)&tvalr);
00057 sprintf(datestr, "%s", asctime(t_ptr));
00058 datestr[19] = (char)NULL;
00059 return(&datestr[4]);
00060 }
00061
00062
00063
00064
00065 int write_log(const char *fmt, ...)
00066 {
00067 va_list args;
00068 char string[4096];
00069
00070 va_start(args, fmt);
00071 vsprintf(string, fmt, args);
00072 if(logfp) {
00073 fprintf(logfp, string);
00074 fflush(logfp);
00075 }
00076 else
00077 fprintf(stderr, string);
00078 va_end(args);
00079 return(0);
00080 }
00081
00082
00083 void sighandler(sig)
00084 int sig;
00085 {
00086 if(sig == SIGTERM) {
00087 write_log("*** %s exportclosed got SIGTERM. Exiting.\n", datestring());
00088 exit(1);
00089 }
00090 if(sig == SIGINT) {
00091 write_log("*** %s exportclosed got SIGINT. Exiting.\n", datestring());
00092 exit(1);
00093 }
00094 write_log("*** %s exportclosed got an illegal signal %d, ignoring...\n",
00095 datestring(), sig);
00096 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00097 signal(SIGINT, sighandler);
00098 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00099 signal(SIGALRM, sighandler);
00100 }
00101
00102
00103 void setup()
00104 {
00105 int pid;
00106 char *cptr;
00107 char logname[MAX_STR];
00108
00109 gethostname(thishost, MAX_STR);
00110 cptr = index(thishost, '.');
00111 if(cptr) *cptr = (char)NULL;
00112 pid = getpid();
00113 sprintf(logname, "/usr/local/logs/SUM/exportclosed_%d.log", pid);
00114 open_log(logname);
00115 printk_set(write_log, write_log);
00116 write_log("\n## %s exportclosed on %s for pid = %d ##\n",
00117 datestring(), thishost, pid);
00118
00119
00120 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00121 signal(SIGINT, sighandler);
00122 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
00123 signal(SIGTERM, sighandler);
00124 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00125 signal(SIGALRM, sighandler);
00126 }
00127
00128
00129
00130
00131 int send2tapesvc(char *cmd)
00132 {
00133 static KEY *retlist;
00134 int statback = 0;
00135
00136 retlist = newkeylist();
00137 setkey_str(&retlist, "OP", cmd);
00138 status = clnt_call(clnttape, EXPCLOSEDO, (xdrproc_t)xdr_Rkey,
00139 (char *)retlist, (xdrproc_t)xdr_uint32_t,
00140 (char *)&tapeback, TIMEOUT);
00141 write_log("exportclosed: tapeback=%ld, status=%d\n", tapeback, status);
00142 if(status != RPC_SUCCESS) {
00143 call_err = clnt_sperror(clnttape, "Err clnt_call for EXPCLOSEDO");
00144 write_log("%s %s\n", datestring(), call_err);
00145 statback = 1;
00146 }
00147 if(tapeback == 1) {
00148 write_log("**Error in EXPCLOSEDO call to tape_svc in exportclosed\n");
00149 statback = 1;
00150 }
00151 freekeylist(&retlist);
00152 return(statback);
00153 }
00154
00155
00156 int main(int argc, char *argv[])
00157 {
00158 int i, slotnum;
00159 char cmd[256];
00160
00161 setup();
00162
00163
00164 slotnum = FIRST_EXP_SLOT;
00165
00166 clnttape = clnt_create(thishost, TAPEPROG, TAPEVERS, "tcp");
00167 if(!clnttape) {
00168 clnt_pcreateerror("Can't get client handle to tape_svc");
00169 write_log("tape_svc not there on %s\n", thishost);
00170 exit(1);
00171 }
00172 write_log("exportclosed called with op: %s\n", argv[1]);
00173 if(!strcmp(argv[1], "unload")) {
00174 if(send2tapesvc("unload")) {
00175 write_log("Error sending unload: exportclosed is aborting\n");
00176 exit(1);
00177 }
00178 argc = argc - 2;
00179 for(i=0; i < argc; i++) {
00180 sprintf(cmd, "jmtx -f /dev/t950 transtape %s %d", argv[i+2], slotnum++);
00181 write_log("%s\n", cmd);
00182 if(send2tapesvc(cmd)) {
00183 write_log("Error sending %s to tape_svc\n", cmd);
00184 continue;
00185 }
00186 if(system(cmd)) {
00187 write_log("Error: %s\n", cmd);
00188 }
00189 }
00190 if(send2tapesvc("stop unload")) {
00191 write_log("Error sending stop unload\n");
00192 }
00193 }
00194 else if(!strcmp(argv[1], "load")) {
00195 if(send2tapesvc("load")) {
00196 write_log("Error sending load: exportclosed is aborting\n");
00197 exit(1);
00198 }
00199 argc = argc - 2;
00200 for(i=0; i < argc; i++) {
00201 sprintf(cmd, "jmtx -f /dev/t950 transfer %d %s", slotnum++, argv[i+2]);
00202 write_log("%s\n", cmd);
00203 if(send2tapesvc(cmd)) {
00204 write_log("Error sending %s to tape_svc\n", cmd);
00205 continue;
00206 }
00207 if(system(cmd)) {
00208 write_log("Error: %s\n", cmd);
00209 }
00210 }
00211 if(send2tapesvc("stop load")) {
00212 write_log("Error sending stop load\n");
00213 }
00214 if(send2tapesvc("reinventory")) {
00215 write_log("Error sending reinventory: exportclosed is aborting\n");
00216 exit(1);
00217 }
00218 }
00219 else {
00220 write_log("Bad call. arg = %s\n", argv[1]);
00221 exit(1);
00222 }
00223
00224 clnt_destroy(clnttape);
00225 write_log("exportclosed done\n");
00226 fclose(logfp);
00227
00228 }