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