00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "jsoc.h"
00011 #include "cmdparams.h"
00012 #include <stdlib.h>
00013 #include <SUM.h>
00014 #include <soi_error.h>
00015 #include <sys/errno.h>
00016 #include <rpc/rpc.h>
00017 #include <rpc/pmap_clnt.h>
00018 #include <signal.h>
00019 #include <sum_rpc.h>
00020 #include <printk.h>
00021 #include <sys/wait.h>
00022
00023 static char PGPORTDRMS[] = "5432";
00024
00025 ModuleArgs_t module_args[] =
00026 {
00027 {ARG_STRING, "logdir", "/usr/local/logs/SUM", "dir for log file"},
00028 {ARG_FLAG, "v", "0", "verbose flag"},
00029 {ARG_FLAG, "h", "0", "help - print usage info"},
00030 {ARG_END}
00031 };
00032
00033 ModuleArgs_t *gModArgs = module_args;
00034
00035 CmdParams_t cmdparams;
00036
00037 static void sumpeprog_1(struct svc_req *rqstp, SVCXPRT *transp);
00038 void logkey(KEY *key);
00039 struct timeval TIMEOUT = { 20, 0 };
00040
00041 static KEY *retlist;
00042 char *module_name = "sum_pe_svc";
00043 char *logdir, *db;
00044 char thishost[MAX_STR];
00045 char datestr[32];
00046 uint32_t rinfo;
00047 FILE *logfp, *wlogfp;
00048 SVCXPRT *glb_transp;
00049 CLIENT *current_client;
00050
00051 int open_log(char *filename)
00052 {
00053 if((logfp=fopen(filename, "w")) == NULL) {
00054 fprintf(stderr, "Can't open the log file %s\n", filename);
00055 return(1);
00056 }
00057 return(0);
00058 }
00059
00060
00061
00062 int msg(const char *fmt, ...)
00063 {
00064 va_list args;
00065 char string[4096];
00066
00067 va_start(args, fmt);
00068 vsprintf(string, fmt, args);
00069 if(logfp) {
00070 fprintf(logfp, string);
00071 fflush(logfp);
00072 }
00073 else
00074 fprintf(stderr, string);
00075 va_end(args);
00076 return(0);
00077 }
00078
00079
00080
00081
00082 int write_log(const char *fmt, ...)
00083 {
00084 va_list args;
00085 char string[4096];
00086
00087 va_start(args, fmt);
00088 vsprintf(string, fmt, args);
00089 if(wlogfp) {
00090 fprintf(wlogfp, string);
00091 fflush(wlogfp);
00092 }
00093 else
00094 fprintf(stderr, string);
00095 va_end(args);
00096 return(0);
00097 }
00098
00099
00100 char *datestring()
00101 {
00102 struct timeval tvalr;
00103 struct tm *t_ptr;
00104
00105 gettimeofday(&tvalr, NULL);
00106 t_ptr = localtime((const time_t *)&tvalr);
00107 sprintf(datestr, "%s", asctime(t_ptr));
00108 datestr[19] = (char)NULL;
00109 return(&datestr[4]);
00110 }
00111
00112 void sighandler(int sig)
00113 {
00114 if(sig == SIGTERM) {
00115 printk("*** %s sum_pe_svc got SIGTERM. Exiting.\n", datestring());
00116 exit(1);
00117 }
00118 if(sig == SIGINT) {
00119 printk("*** %s sum_pe_svc got SIGINT. Exiting.\n", datestring());
00120 exit(1);
00121 }
00122 printk("*** %s sum_pe_svc got an illegal signal %d, ignoring...\n",
00123 datestring(), sig);
00124 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00125 signal(SIGINT, sighandler);
00126 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00127 signal(SIGALRM, sighandler);
00128 }
00129
00130
00131 int setup () {
00132 int pid, i;
00133 char *username, *cptr;
00134 char logname[MAX_STR], lfile[MAX_STR], acmd[MAX_STR], line[MAX_STR];
00135 char gfile[MAX_STR];
00136 FILE *fplog;
00137
00138 if(!(username = (char *)getenv("USER"))) username = "nouser";
00139 if(strcmp(username, "production")) {
00140
00141
00142
00143
00144 }
00145 gethostname(thishost, MAX_STR);
00146 cptr = index(thishost, '.');
00147 if(cptr) *cptr = (char)NULL;
00148 pid = getpid();
00149
00150 sprintf(gfile, "/data1/tmp/grep_sum_pe_svc.%d.log", pid);
00151 sprintf(lfile, "/data1/tmp/wc_sum_pe_svc.%d.log", pid);
00152 sprintf(acmd, "ps -ef | grep %s 1> %s 2>&1", "\" sum_pe_svc\"", gfile);
00153 if(system(acmd)) {
00154 printf("**Can't execute %s.\n", acmd);
00155 return(1);
00156 }
00157 sprintf(acmd, "cat %s | wc -l 1> %s", gfile, lfile);
00158 if(system(acmd)) {
00159 printk("**Can't execute %s.\n", acmd);
00160 return(1);
00161 }
00162 if((fplog=fopen(lfile, "r")) == NULL) {
00163 printk("**Can't open cmd log file %s\n", lfile);
00164 return(1);
00165 }
00166 while(fgets(line, 128, fplog)) {
00167 i = atoi(line);
00168 if(i > 3) {
00169 printk("Can't run more than 1 sum_pe_svc\n");
00170 return(1);
00171 }
00172 }
00173 fclose(fplog);
00174
00175 sprintf(logname, "%s/sum_pe_svc_%d.log", logdir, pid);
00176 if(open_log(logname)) return(1);
00177 printk_set(msg, msg);
00178 printf("Starting sum_pe_svc for database = %s\nlogfile = %s\n\n",db, logname);
00179 printk("%s\nStarting sum_pe_svc for database = %s\nlogfile = %s\n\n",
00180 datestring(), db, logname);
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 return(0);
00188 }
00189
00190 int nice_intro(int usage) {
00191 if (usage)
00192 {
00193 printf ("Usage:\nsum_pe_svc [-hv] "
00194 "[logdir=<dir for log file>] db_name\n"
00195 " details are:\n"
00196 " -h: help - show this message then exit\n"
00197 " -v: verbose mode\n"
00198 "logdir=dir to put the log file in\n"
00199 "dbname is the DRMS data base to connect to, e.g. jsoc\n");
00200 return(1);
00201 }
00202 return (0);
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 int main(int argc, char **argv)
00240 {
00241 int status = 0;
00242
00243
00244 register SVCXPRT *transp;
00245
00246 setenv("PGPORT", PGPORTDRMS, 1);
00247
00248 status = cmdparams_parse (&cmdparams, argc, argv);
00249 if (status == CMDPARAMS_QUERYMODE) {
00250 cmdparams_usage (argv[0]);
00251 return 0;
00252 }
00253
00254
00255 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00256 logdir = cmdparams_get_str (&cmdparams, "logdir", NULL);
00257
00258 if(nice_intro(usage)) return (0);
00259 if(cmdparams_numargs(&cmdparams) >= 1 && (db = cmdparams_getarg (&cmdparams, 1))) {
00260
00261 }
00262 else {
00263 nice_intro(1);
00264 return(0);
00265 }
00266 if(setup()) return(1);
00267
00268
00269 (void) pmap_unset(SUMPEPROG, SUMPEVERS);
00270 transp = (SVCXPRT *)svctcp_create(RPC_ANYSOCK, 0, 0);
00271 if (transp == NULL) {
00272 printf("***cannot create tcp service\n");
00273 return(1);
00274 }
00275 if (!svc_register(transp, SUMPEPROG, SUMPEVERS, sumpeprog_1, IPPROTO_TCP)) {
00276 printf("***unable to register (SUMPEPROG, SUMPEVERS, tcp)\n");
00277 return(1);
00278 }
00279
00280
00281
00282
00283 svc_run();
00284 printk("!!Fatal Error: svc_run() returned in sum_pe_svc\n");
00285 return(1);
00286 }
00287
00288
00289
00290
00291
00292 static void
00293 sumpeprog_1(struct svc_req *rqstp, SVCXPRT *transp)
00294 {
00295 union __svcargun {
00296 Rkey sumdo_1_arg;
00297 } argument;
00298 char *result, *call_err;
00299 enum clnt_stat clnt_stat;
00300
00301 bool_t (*xdr_argument)(), (*xdr_result)();
00302 char *(*local)();
00303
00304 switch (rqstp->rq_proc) {
00305 case NULLPROC:
00306 (void) svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL);
00307 return;
00308 break;
00309 case SUMPEDO:
00310 xdr_argument = xdr_Rkey;
00311 xdr_result = xdr_Rkey;;
00312 local = (char *(*)()) sumpedo_1;
00313 break;
00314 case SUMPEACK:
00315 xdr_argument = xdr_Rkey;
00316 xdr_result = xdr_Rkey;;
00317 local = (char *(*)()) sumpeack_1;
00318 break;
00319 default:
00320 printk("**sumpeprog_1() dispatch default procedure %d,ignore\n",
00321 rqstp->rq_proc);
00322 svcerr_noproc(transp);
00323 return;
00324 }
00325 bzero((char *)&argument, sizeof(argument));
00326 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
00327 msg("***Error on svc_getargs()\n");
00328 svcerr_decode(transp);
00329
00330
00331
00332
00333 svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL);
00334 return;
00335
00336 }
00337 glb_transp = transp;
00338 result = (*local)(&argument, rqstp);
00339
00340
00341
00342 if(result) {
00343 if(result == (char *)1) {
00344
00345 }
00346 else {
00347 clnt_stat=clnt_call(current_client, PEPEQRESPDO,(xdrproc_t)xdr_result,
00348 result, (xdrproc_t)xdr_void, 0, TIMEOUT);
00349 if(clnt_stat != 0) {
00350 clnt_perrno(clnt_stat);
00351 msg("***Error on clnt_call() back to PEPEQRESPDO procedure\n");
00352 msg("***The original client caller has probably exited\n");
00353 call_err = clnt_sperror(current_client, "Err");
00354 msg("%s\n", call_err);
00355 }
00356 clnt_destroy(current_client);
00357 freekeylist((KEY **)&result);
00358 }
00359 }
00360 else {
00361 }
00362 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
00363 msg("**unable to free arguments\n");
00364
00365 }
00366 return;
00367 }
00368
00369
00370
00371 CLIENT *set_client_handle(uint32_t prognum, uint32_t versnum)
00372 {
00373 static CLIENT *client;
00374 struct sockaddr_in *sock_in;
00375 int sock = RPC_ANYSOCK;
00376
00377
00378
00379
00380
00381 sock_in = svc_getcaller(glb_transp);
00382 sock_in->sin_port = 0;
00383 client = clnttcp_create(sock_in,prognum,versnum,&sock,0,0);
00384 if(!client) {
00385 clnt_pcreateerror("Can't do a clnttcp_create to send a response");
00386 printk("**** Can't do a clnttcp_create to send a response ****\n");
00387 printk("**** Did someone remove us from the portmapper? ****\n");
00388 return(0);
00389 }
00390
00391 current_client = client;
00392 return(client);
00393 }
00394
00395
00396
00397
00398
00399
00400
00401 void send_ack()
00402 {
00403
00404 if (!svc_sendreply(glb_transp, (xdrproc_t)xdr_uint32_t, (char *)&rinfo)) {
00405 printk("***Error on immed ack back to client. FATAL???\n");
00406 svcerr_systemerr(glb_transp);
00407 }
00408 }
00409
00410 KEY *sumpeack_1(KEY *params)
00411 {
00412 KEY *retlist;
00413 pid_t pid;
00414 int status;
00415
00416 rinfo = 0;
00417 send_ack();
00418 pid = wait(&status);
00419 printk("Complete: sum_pe pid=%u\n", pid);
00420 retlist = newkeylist();
00421 add_keys(params, &retlist);
00422 current_client = (CLIENT *)getkey_fileptr(params, "current_client");
00423 return(retlist);
00424 }
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 KEY *sumpedo_1(KEY *params)
00463 {
00464 static CLIENT *clresp;
00465 pid_t pid;
00466 char *args[5];
00467
00468 uint64_t uid;
00469 char name[128], argkey1[80], argkey2[80];
00470
00471
00472 uid = (uint64_t)getkey_long(params, "dsds_uid");
00473 sprintf(name, "/tmp/keylist_%lu.log", uid);
00474
00475 if((wlogfp=fopen(name, "w")) == NULL) {
00476 fprintf(stderr, "Can't open the log file %s\n", name);
00477 rinfo = 1;
00478 send_ack();
00479 return((KEY *)1);
00480 }
00481 retlist = newkeylist();
00482 add_keys(params, &retlist);
00483 if(!(clresp = set_client_handle(PEPEQPROG, (uint32_t)uid))) {
00484 freekeylist(&retlist);
00485 rinfo = 1;
00486 send_ack();
00487 return((KEY *)1);
00488 }
00489
00490 setkey_fileptr(&retlist, "current_client", (FILE *)clresp);
00491 keyiterate(logkey, retlist);
00492 fclose(wlogfp);
00493 if((pid = fork()) < 0) {
00494 printk("***Can't fork() a sum_pe. errno=%d\n", errno);
00495 exit(1);
00496 }
00497 else if(pid == 0) {
00498 printk("execvp of sum_pe server=%s keyfile=%s %s\n", thishost, name, db);
00499 sprintf(argkey1, "server=%s", thishost);
00500 sprintf(argkey2, "keyfile=%s", name);
00501 args[0] = "sum_pe";
00502 args[1] = argkey1;
00503 args[2] = argkey2;
00504 args[3] = db;
00505 args[4] = NULL;
00506 if(execvp(args[0], args) < 0) {
00507 write_log("***Can't execvp() sum_pe keyfile=%s. errno=%d\n", name, errno);
00508 exit(1);
00509 }
00510 }
00511 printk("Fork sum_pe pid=%u\n", pid);
00512 rinfo = 0;
00513 send_ack();
00514 return((KEY *)1);
00515 }