00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <SUM.h>
00011 #include <soi_key.h>
00012 #include <sys/time.h>
00013 #include <sys/errno.h>
00014 #include <sum_rpc.h>
00015 #include <soi_error.h>
00016 #include <printk.h>
00017
00018 void usage();
00019 void get_cmd(int argc, char *argv[]);
00020
00021 extern int errno;
00022 char *dbname, *timetag;
00023 int tapeoffline = 0;
00024 int sim = 0;
00025 rm_0_found = 0;
00026 rm_1_found = 0;
00027 rm_2_found = 0;
00028
00029 int send_mail(char *fmt, ...)
00030 {
00031 va_list args;
00032 char string[1024], cmd[1024];
00033
00034 va_start(args, fmt);
00035 vsprintf(string, fmt, args);
00036
00037 sprintf(cmd, "echo \"%s\" | Mail -s \"jmtx mail\" jim@sun.stanford.edu", string);
00038 system(cmd);
00039 va_end(args);
00040 return(0);
00041 }
00042
00043 void usage()
00044 {
00045 printf("This is a stand alone program that the sum_svc, running on j1,\n");
00046 printf("calls on d02 in order to start the tape_svc and drive_svc and \n");
00047 printf("robot_svc on the machine that has the T950 tape robot and drives.\n");
00048 printf("Usage: sum_forker -[o|s] dbname timetag\n");
00049 printf(" -o = offline mode\n");
00050 printf(" -s = simulation mode\n");
00051 printf(" dbname = name of database to connect to, e.g. jsoc_sums\n");
00052 printf(" timetag = time tag of form 2009.06.03.115824\n");
00053 exit(1);
00054 }
00055
00056
00057 void get_cmd(int argc, char *argv[])
00058 {
00059 int c;
00060
00061 while(--argc > 0 && (*++argv)[0] == '-') {
00062 while((c = *++argv[0]))
00063 switch(c) {
00064 case 'o':
00065 tapeoffline=1;
00066 break;
00067 case 's':
00068 sim=1;
00069 break;
00070 default:
00071 usage();
00072 break;
00073 }
00074 }
00075 if(argc != 2) {
00076 usage();
00077 }
00078 dbname = argv[0];
00079 timetag = argv[1];
00080
00081
00082 }
00083
00084
00085
00086
00087 int find_sum_rm()
00088 {
00089 FILE *fplog;
00090 char acmd[128], line[128], look[64];
00091 char log[] = "/usr/local/logs/SUM/find_sum_rm.log";
00092
00093 sprintf(acmd, "ps -ef | grep sum_rm_ 1> %s 2>&1", log);
00094 if(system(acmd)) {
00095 printf("Can't execute %s.\n", acmd);
00096 return(-1);
00097 }
00098 if((fplog=fopen(log, "r")) == NULL) {
00099 printf("Can't open %s to find sum_rm_\n", log);
00100 return(-1);
00101 }
00102 sprintf(look, " %s", dbname);
00103 while(fgets(line, 128, fplog)) {
00104 if (strstr(line, look) && strstr(line, "sum_rm_")) {
00105 if(!strstr(line, "sh ")) {
00106 if(strstr(line, "sum_rm_0")) rm_0_found = 1;
00107 else if(strstr(line, "sum_rm_1")) rm_1_found = 1;
00108 else if(strstr(line, "sum_rm_2")) rm_2_found = 1;
00109 }
00110 }
00111 }
00112 fclose(fplog);
00113 return (0);
00114 }
00115
00116 int main(int argc, char *argv[])
00117 {
00118 int i;
00119 pid_t pid;
00120 char *args[8];
00121 char dsvcname[80], pgport[32], cmd[128];
00122
00123 printf("This is sum_forker\n");
00124 get_cmd(argc, argv);
00125 if(find_sum_rm() == -1) {
00126 printf("Fatal Error, quit\n");
00127 exit(1);
00128 }
00129
00130
00131
00132
00133 sprintf(pgport, SUMPGPORT);
00134 setenv("PGPORT", pgport, 1);
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 printf("sum_forker about to start tape_svc\n");
00145 if((pid = fork()) < 0) {
00146 printf("***Can't fork(). errno=%d\n", errno);
00147 exit(1);
00148 }
00149 else if(pid == 0) {
00150 printf("execvp of tape_svc\n");
00151
00152
00153 args[0] = "tape_svc";
00154
00155 if(tapeoffline) {
00156 args[1] = "-o";
00157 args[2] = dbname;
00158 args[3] = timetag;
00159 args[4] = NULL;
00160 }
00161 else if(sim) {
00162 args[1] = "-s";
00163 args[2] = dbname;
00164 args[3] = timetag;
00165 args[4] = NULL;
00166 }
00167 else {
00168 args[1] = dbname;
00169 args[2] = timetag;
00170 args[3] = NULL;
00171 }
00172 if(execvp(args[0], args) < 0) {
00173 printf("***Can't execvp() tape_svc. errno=%d\n", errno);
00174 exit(1);
00175 }
00176 }
00177 sleep(10);
00178 for(i=0; i < MAX_DRIVES; i++) {
00179 if((pid = fork()) < 0) {
00180 printf("***Can't fork(). errno=%d\n", errno);
00181 exit(1);
00182 }
00183 else if(pid == 0) {
00184 sprintf(dsvcname, "drive%d_svc", i);
00185 printf("execvp of %s\n", dsvcname);
00186 args[0] = dsvcname;
00187 if(tapeoffline) {
00188 args[1] = "-o";
00189 args[2] = dbname;
00190 args[3] = timetag;
00191 args[4] = NULL;
00192 }
00193 else if(sim) {
00194 args[1] = "-s";
00195 args[2] = dbname;
00196 args[3] = timetag;
00197 args[4] = NULL;
00198 }
00199 else {
00200 args[1] = dbname;
00201 args[2] = timetag;
00202 args[3] = NULL;
00203 }
00204 if(execvp(args[0], args) < 0) {
00205 printf("***Can't execvp() %s. errno=%d\n", dsvcname, errno);
00206 exit(1);
00207 }
00208 }
00209 }
00210 if((pid = fork()) < 0) {
00211 printf("***Can't fork(). errno=%d\n", errno);
00212 exit(1);
00213 }
00214 else if(pid == 0) {
00215 printf("execvp of robot0_svc\n");
00216 args[0] = "robot0_svc";
00217 args[1] = dbname;
00218 args[2] = timetag;
00219 args[3] = NULL;
00220 if(execvp(args[0], args) < 0) {
00221 printf("***Can't execvp() robot0_svc. errno=%d\n", errno);
00222 exit(1);
00223 }
00224 }
00225 if(!rm_0_found) {
00226 if((pid = fork()) < 0) {
00227 printf("***Can't fork(). errno=%d\n", errno);
00228 exit(1);
00229 }
00230 else if(pid == 0) {
00231 printf("execvp of sum_rm_0\n");
00232 args[0] = "sum_rm_0";
00233 args[1] = dbname;
00234 args[2] = timetag;
00235 args[3] = NULL;
00236 if(execvp(args[0], args) < 0) {
00237 printf("***Can't execvp() sum_rm_0. errno=%d\n", errno);
00238 exit(1);
00239 }
00240 }
00241 }
00242 sleep(2);
00243 if(!rm_1_found) {
00244 if((pid = fork()) < 0) {
00245 printf("***Can't fork(). errno=%d\n", errno);
00246 exit(1);
00247 }
00248 else if(pid == 0) {
00249 printf("execvp of sum_rm_1\n");
00250 args[0] = "sum_rm_1";
00251 args[1] = dbname;
00252 args[2] = timetag;
00253 args[3] = NULL;
00254 if(execvp(args[0], args) < 0) {
00255 printf("***Can't execvp() sum_rm_1. errno=%d\n", errno);
00256 exit(1);
00257 }
00258 }
00259 }
00260 sleep(2);
00261 if(!rm_2_found) {
00262 if((pid = fork()) < 0) {
00263 printf("***Can't fork(). errno=%d\n", errno);
00264 exit(1);
00265 }
00266 else if(pid == 0) {
00267 printf("execvp of sum_rm_2\n");
00268 args[0] = "sum_rm_2";
00269 args[1] = dbname;
00270 args[2] = timetag;
00271 args[3] = NULL;
00272 if(execvp(args[0], args) < 0) {
00273 printf("***Can't execvp() sum_rm_2. errno=%d\n", errno);
00274 exit(1);
00275 }
00276 }
00277 }
00278 sleep(2);
00279
00280
00281 }