00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <SUM.h>
00011 #include <sys/socket.h>
00012 #include <sys/errno.h>
00013 #include <rpc/rpc.h>
00014 #include <rpc/pmap_clnt.h>
00015 #include <signal.h>
00016 #include <sum_rpc.h>
00017 #include <soi_error.h>
00018 #include <tape.h>
00019 #include <printk.h>
00020
00021 void logkey();
00022 extern int errno;
00023 struct timeval TIMEOUT = { 120, 0 };
00024 uint32_t rinfo;
00025
00026 FILE *logfp;
00027 CLIENT *current_client, *clnttape;
00028 SVCXPRT *glb_transp;
00029 char *dbname;
00030 char *action;
00031 char thishost[MAX_STR];
00032 char datestr[32];
00033 int soi_errno = NO_ERROR;
00034 int debugflg = 0;
00035 int sim = 0;
00036 int tapeoffline = 0;
00037
00038
00039 void open_log(char *filename)
00040 {
00041 if((logfp=fopen(filename, "w")) == NULL) {
00042 fprintf(stderr, "Can't open the log file %s\n", filename);
00043 }
00044 }
00045
00046
00047
00048 static char *datestring()
00049 {
00050 struct timeval tvalr;
00051 struct tm *t_ptr;
00052
00053 gettimeofday(&tvalr, NULL);
00054 t_ptr = localtime((const time_t *)&tvalr);
00055 sprintf(datestr, "%s", asctime(t_ptr));
00056 datestr[19] = (char)NULL;
00057 return(&datestr[4]);
00058 }
00059
00060
00061
00062
00063 int write_log(const char *fmt, ...)
00064 {
00065 va_list args;
00066 char string[4096];
00067
00068 va_start(args, fmt);
00069 vsprintf(string, fmt, args);
00070 if(logfp) {
00071 fprintf(logfp, string);
00072 fflush(logfp);
00073 }
00074 else
00075 fprintf(stderr, string);
00076 va_end(args);
00077 return(0);
00078 }
00079
00080
00081 void sighandler(sig)
00082 int sig;
00083 {
00084 if(sig == SIGTERM) {
00085 write_log("*** %s sum_svc got SIGTERM. Exiting.\n", datestring());
00086 exit(1);
00087 }
00088 if(sig == SIGINT) {
00089 write_log("*** %s sum_svc got SIGINT. Exiting.\n", datestring());
00090 DS_DisConnectDB();
00091 exit(1);
00092 }
00093 write_log("*** %s sum_svc got an illegal signal %d, ignoring...\n",
00094 datestring(), sig);
00095 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00096 signal(SIGINT, sighandler);
00097 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00098 signal(SIGALRM, sighandler);
00099 }
00100
00101
00102 void get_cmd(int argc, char *argv[])
00103 {
00104 int c;
00105 char *username;
00106
00107 if(!(username = (char *)getenv("USER"))) username = "nouser";
00108 if(strcmp(username, "production")) {
00109 printf("!!NOTE: You must be user production to run tapeonoff!\n");
00110 exit(1);
00111 }
00112
00113 while((--argc > 0) && ((*++argv)[0] == '-')) {
00114 while((c = *++argv[0])) {
00115 switch(c) {
00116 case 'd':
00117 debugflg=1;
00118 break;
00119 default:
00120 break;
00121 }
00122 }
00123 }
00124 if(argc != 1) {
00125 printf("Usage: tapeonoff on|off|status|inv\n");
00126 exit(1);
00127 }
00128 else {
00129 action = argv[0];
00130 if(!strcmp(action, "on")) return;
00131 if(!strcmp(action, "off")) return;
00132 if(!strcmp(action, "status")) return;
00133 if(!strcmp(action, "inv")) return;
00134 printf("Usage: tapeonoff on|off|status|inv\n");
00135 exit(1);
00136 }
00137 }
00138
00139
00140 void setup()
00141 {
00142 int pid;
00143 char *cptr;
00144 char logname[MAX_STR];
00145
00146 gethostname(thishost, MAX_STR);
00147 cptr = index(thishost, '.');
00148 if(cptr) *cptr = (char)NULL;
00149 pid = getpid();
00150 sprintf(logname, "/usr/local/logs/SUM/tapeonoff_%d.log", pid);
00151 open_log(logname);
00152 printk_set(write_log, write_log);
00153 write_log("\n## %s tapeonoff on %s for pid = %d ##\n",
00154 datestring(), thishost, pid);
00155 write_log("tapeonoff %s\n", action);
00156 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00157 signal(SIGINT, sighandler);
00158 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
00159 signal(SIGTERM, sighandler);
00160 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00161 signal(SIGALRM, sighandler);
00162 }
00163
00164
00165 int main(int argc, char *argv[])
00166 {
00167 char *call_err;
00168 uint32_t onoffback;
00169 enum clnt_stat status;
00170 KEY *list = newkeylist();
00171
00172 get_cmd(argc, argv);
00173 setup();
00174
00175
00176 if(strcmp(thishost, SUMSVCHOST))
00177 clnttape = clnt_create(thishost, TAPEPROG, TAPEVERS, "tcp");
00178 else
00179 clnttape = clnt_create(TAPEHOST, TAPEPROG, TAPEVERS, "tcp");
00180 if(!clnttape) {
00181 clnt_pcreateerror("Can't get client handle to tape_svc");
00182 write_log("tape_svc not there on %s\n", thishost);
00183 exit(1);
00184 }
00185 setkey_str(&list, "host", thishost);
00186 setkey_str(&list, "action", action);
00187 status = clnt_call(clnttape, ONOFFDO, (xdrproc_t)xdr_Rkey, (char *)list,
00188 (xdrproc_t)xdr_uint32_t, (char *)&onoffback, TIMEOUT);
00189 if(status != RPC_SUCCESS) {
00190 call_err = clnt_sperror(clnttape, "Err clnt_call for ONOFF");
00191 write_log("%s %s\n", datestring(), call_err);
00192 exit(1);
00193 }
00194 switch(onoffback) {
00195 case 0:
00196 write_log("Success tape_svc is online\n");
00197 printf("Success tape_svc is online\n");
00198 break;
00199 case 1:
00200 write_log("Success tape_svc is offline\n");
00201 printf("Success tape_svc is offline\n");
00202 break;
00203 case 2:
00204 write_log("Can't inventory while tape_svc is still online\n");
00205 printf("Can't inventory while tape_svc is still online\n");
00206 break;
00207 case 3:
00208 write_log("Tape inventory complete. You can turn tape back online\n");
00209 printf("Tape inventory complete. You can turn tape back online\n");
00210 break;
00211 case 4:
00212 write_log("Can't take offline while robot is busy\n");
00213 printf("Can't take offline while robot is busy\n");
00214 break;
00215 case -1:
00216 write_log("**Error in ONOFF call to tape_svc\n");
00217 printf("**Error in ONOFF call to tape_svc\n");
00218 exit(1);
00219 break;
00220 default:
00221 write_log("**Error in ONOFF call to tape_svc\n");
00222 printf("**Error in ONOFF call to tape_svc\n");
00223 exit(1);
00224 break;
00225 }
00226 }
00227