00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdlib.h>
00012 #include <SUM.h>
00013 #include <soi_error.h>
00014 #include <sys/errno.h>
00015 #include <rpc/rpc.h>
00016 #include <rpc/pmap_clnt.h>
00017 #include <signal.h>
00018 #include <sum_rpc.h>
00019 #include <printk.h>
00020 #include "serverdefs.h"
00021 #include <sys/wait.h>
00022
00023 static void sumexprog_1(struct svc_req *rqstp, SVCXPRT *transp);
00024 void logkey(KEY *key);
00025 void logkeyk(KEY *key);
00026 struct timeval TIMEOUT = { 20, 0 };
00027
00028 static KEY *retlist;
00029 char thishost[MAX_STR];
00030 char datestr[32];
00031 char logname[MAX_STR];
00032 uint32_t rinfo;
00033 FILE *logfp, *wlogfp;
00034 SVCXPRT *glb_transp;
00035 CLIENT *current_client;
00036
00037 int open_log(char *filename)
00038 {
00039 if((logfp=fopen(filename, "w")) == NULL) {
00040 fprintf(stderr, "Can't open the log file %s\n", filename);
00041 return(1);
00042 }
00043 return(0);
00044 }
00045
00046
00047
00048 int msg(const char *fmt, ...)
00049 {
00050 va_list args;
00051 char string[4096];
00052
00053 va_start(args, fmt);
00054 vsprintf(string, fmt, args);
00055 if(logfp) {
00056 fprintf(logfp, string);
00057 fflush(logfp);
00058 }
00059 else
00060 fprintf(stderr, string);
00061 va_end(args);
00062 return(0);
00063 }
00064
00065
00066
00067
00068 int write_log(const char *fmt, ...)
00069 {
00070 va_list args;
00071 char string[4096];
00072
00073 va_start(args, fmt);
00074 vsprintf(string, fmt, args);
00075 if(wlogfp) {
00076 fprintf(wlogfp, string);
00077 fflush(wlogfp);
00078 }
00079 else
00080 fprintf(stderr, string);
00081 va_end(args);
00082 return(0);
00083 }
00084
00085
00086 char *datestring()
00087 {
00088 struct timeval tvalr;
00089 struct tm *t_ptr;
00090
00091 gettimeofday(&tvalr, NULL);
00092 t_ptr = localtime((const time_t *)&tvalr);
00093 sprintf(datestr, "%s", asctime(t_ptr));
00094 datestr[19] = (char)NULL;
00095 return(&datestr[4]);
00096 }
00097
00098 void sighandler(int sig)
00099 {
00100 if(sig == SIGTERM) {
00101 printk("*** %s sum_export_svc got SIGTERM. Exiting.\n", datestring());
00102 exit(1);
00103 }
00104 if(sig == SIGINT) {
00105 printk("*** %s sum_export_svc got SIGINT. Exiting.\n", datestring());
00106 exit(1);
00107 }
00108 printk("*** %s sum_export_svc got an illegal signal %d, ignoring...\n",
00109 datestring(), sig);
00110 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00111 signal(SIGINT, sighandler);
00112 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00113 signal(SIGALRM, sighandler);
00114 }
00115
00116
00117 int setup () {
00118 int pid, i;
00119 char *username, *cptr;
00120 char lfile[MAX_STR], acmd[MAX_STR], line[MAX_STR];
00121 char gfile[MAX_STR];
00122 FILE *fplog;
00123
00124 if(!(username = (char *)getenv("USER"))) username = "nouser";
00125 if(strcmp(username, "production")) {
00126
00127
00128 }
00129 gethostname(thishost, MAX_STR);
00130 cptr = index(thishost, '.');
00131 if(cptr) *cptr = (char)NULL;
00132 pid = getpid();
00133
00134 sprintf(gfile, "/tmp/grep_sum_export_svc.%d.log", pid);
00135 sprintf(lfile, "/tmp/wc_sum_export_svc.%d.log", pid);
00136 sprintf(acmd, "ps -ef | grep %s 1> %s 2>&1", "\" sum_export_svc\"", gfile);
00137 if(system(acmd)) {
00138 printf("**Can't execute %s.\n", acmd);
00139 return(1);
00140 }
00141 sprintf(acmd, "cat %s | wc -l 1> %s", gfile, lfile);
00142 if(system(acmd)) {
00143 printk("**Can't execute %s.\n", acmd);
00144 return(1);
00145 }
00146 if((fplog=fopen(lfile, "r")) == NULL) {
00147 printk("**Can't open cmd log file %s\n", lfile);
00148 return(1);
00149 }
00150 while(fgets(line, 128, fplog)) {
00151 i = atoi(line);
00152 if(i > 3) {
00153 printk("Can't run more than 1 sum_export_svc\n");
00154 return(1);
00155 }
00156 }
00157 fclose(fplog);
00158
00159 sprintf(logname, "%s/sum_export_svc_%d.log", SUMLOG_BASEDIR, pid);
00160 if(open_log(logname)) return(1);
00161 printk_set(msg, msg);
00162 printf("Starting sum_export_svc logfile = %s\n\n", logname);
00163 printk("%s\nStarting sum_export_svc logfile = %s\n",
00164 datestring(), logname);
00165 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00166 signal(SIGINT, sighandler);
00167 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
00168 signal(SIGTERM, sighandler);
00169 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00170 signal(SIGALRM, sighandler);
00171 return(0);
00172 }
00173
00174
00175
00176 int main(int argc, char **argv)
00177 {
00178 int status = 0;
00179 register SVCXPRT *transp;
00180
00181 if(setup()) return(1);
00182
00183
00184 (void) pmap_unset(SUMEXPROG, SUMEXVERS);
00185 transp = (SVCXPRT *)svctcp_create(RPC_ANYSOCK, 0, 0);
00186 if (transp == NULL) {
00187 printf("***cannot create tcp service\n");
00188 return(1);
00189 }
00190 if (!svc_register(transp, SUMEXPROG, SUMEXVERS, sumexprog_1, IPPROTO_TCP)) {
00191 printf("***unable to register (SUMEXPROG, SUMEXVERS, tcp)\n");
00192 return(1);
00193 }
00194
00195
00196
00197
00198 svc_run();
00199 printk("!!Fatal Error: svc_run() returned in sum_export_svc\n");
00200 return(1);
00201 }
00202
00203
00204
00205
00206
00207
00208 static void
00209 sumexprog_1(struct svc_req *rqstp, SVCXPRT *transp)
00210 {
00211 union __svcargun {
00212 Rkey sumdo_1_arg;
00213 } argument;
00214 char *result, *call_err;
00215 enum clnt_stat clnt_stat;
00216
00217 bool_t (*xdr_argument)(), (*xdr_result)();
00218 char *(*local)();
00219
00220 switch (rqstp->rq_proc) {
00221 case NULLPROC:
00222 (void) svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL);
00223 return;
00224 break;
00225 case SUMEXDO:
00226 xdr_argument = xdr_Rkey;
00227 xdr_result = xdr_Rkey;;
00228 local = (char *(*)()) sumexdo_1;
00229 break;
00230 case SUMEXACK:
00231 xdr_argument = xdr_Rkey;
00232 xdr_result = xdr_Rkey;;
00233 local = (char *(*)()) sumexack_1;
00234 break;
00235 default:
00236 printk("**sumexprog_1() dispatch default procedure %d,ignore\n",
00237 rqstp->rq_proc);
00238 svcerr_noproc(transp);
00239 return;
00240 }
00241 bzero((char *)&argument, sizeof(argument));
00242 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
00243 msg("***Error on svc_getargs()\n");
00244 svcerr_decode(transp);
00245
00246
00247
00248 svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL);
00249 return;
00250
00251 }
00252 glb_transp = transp;
00253 result = (*local)(&argument, rqstp);
00254
00255
00256
00257 if(result) {
00258 if(result == (char *)1) {
00259
00260 }
00261 else {
00262 clnt_stat=clnt_call(current_client,REMSUMRESPDO,(xdrproc_t)xdr_result,
00263 result, (xdrproc_t)xdr_void, 0, TIMEOUT);
00264 if(clnt_stat != 0) {
00265 clnt_perrno(clnt_stat);
00266 msg("***Error on clnt_call() back to REMSUMRESPDO procedure\n");
00267 msg("***The original client caller has probably exited\n");
00268 call_err = clnt_sperror(current_client, "Err");
00269 msg("%s\n", call_err);
00270 }
00271 clnt_destroy(current_client);
00272 freekeylist((KEY **)&result);
00273 }
00274 }
00275 else {
00276 }
00277 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
00278 msg("**unable to free arguments\n");
00279
00280 }
00281 return;
00282 }
00283
00284
00285
00286 CLIENT *set_client_handle(uint32_t prognum, uint32_t versnum)
00287 {
00288 static CLIENT *client;
00289 struct sockaddr_in *sock_in;
00290 int sock = RPC_ANYSOCK;
00291
00292
00293
00294
00295
00296 sock_in = svc_getcaller(glb_transp);
00297 sock_in->sin_port = 0;
00298 client = clnttcp_create(sock_in,prognum,versnum,&sock,0,0);
00299 if(!client) {
00300 clnt_pcreateerror("Can't do a clnttcp_create to send a response");
00301 printk("**** Can't do a clnttcp_create to send a response ****\n");
00302 printk("**** Did someone remove us from the portmapper? ****\n");
00303 return(0);
00304 }
00305
00306 current_client = client;
00307 return(client);
00308 }
00309
00310
00311
00312
00313
00314
00315
00316 void send_ack()
00317 {
00318
00319 if (!svc_sendreply(glb_transp, (xdrproc_t)xdr_uint32_t, (char *)&rinfo)) {
00320 printk("***Error on immed ack back to client. FATAL???\n");
00321 svcerr_systemerr(glb_transp);
00322 }
00323 }
00324
00325 KEY *sumexack_1(KEY *params)
00326 {
00327 KEY *retlist;
00328 pid_t pid;
00329 int status;
00330
00331 rinfo = 0;
00332 send_ack();
00333 pid = wait(&status);
00334 printk("Complete: sum_export pid=%u\n", pid);
00335 retlist = newkeylist();
00336 add_keys(params, &retlist);
00337 current_client = (CLIENT *)getkey_fileptr(params, "current_client");
00338 return(retlist);
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 KEY *sumexdo_1(KEY *params)
00351 {
00352 static CLIENT *clresp;
00353 pid_t pid;
00354 char *args[5];
00355 uint32_t uid;
00356 char name[128], argkey1[80], argkey2[80];
00357
00358 uid = getkey_uint32(params, "uid");
00359 sprintf(name, "/tmp/keylist_%u.log", uid);
00360
00361 if((wlogfp=fopen(name, "w")) == NULL) {
00362 fprintf(stderr, "Can't open the log file %s\n", name);
00363 rinfo = 1;
00364 send_ack();
00365 return((KEY *)1);
00366 }
00367 retlist = newkeylist();
00368 add_keys(params, &retlist);
00369 if(!(clresp = set_client_handle(REMSUMPROG, (uint32_t)uid))) {
00370 freekeylist(&retlist);
00371 rinfo = 1;
00372 send_ack();
00373 return((KEY *)1);
00374 }
00375
00376 setkey_fileptr(&retlist, "current_client", (FILE *)clresp);
00377 keyiterate(logkey, retlist);
00378 fclose(wlogfp);
00379
00380 printk("\nFork sum_export\n");
00381 printk("execvp of: sum_export server=%s keyfile=%s\n", thishost, name);
00382 keyiterate(logkeyk, retlist);
00383 if((pid = fork()) < 0) {
00384 printk("***Can't fork() a sum_export. errno=%d\n", errno);
00385 exit(1);
00386 }
00387 else if(pid == 0) {
00388 sprintf(argkey1, "%s", thishost);
00389 sprintf(argkey2, "%s", name);
00390 args[0] = "sum_export";
00391 args[1] = argkey1;
00392 args[2] = argkey2;
00393 args[3] = NULL;
00394 if(execvp(args[0], args) < 0) {
00395 write_log("***Can't execvp() sum_export keyfile=%s. errno=%d\n",
00396 name, errno);
00397 exit(1);
00398 }
00399 }
00400 rinfo = 0;
00401 send_ack();
00402 freekeylist(&retlist);
00403 return((KEY *)1);
00404 }
00405
00406 void logkeyk(KEY *key)
00407 {
00408 switch(key->type) {
00409 case KEYTYP_STRING:
00410 printk ("%s:\tKEYTYP_STRING\t%s\n", key->name, (char*)key->val);
00411 break;
00412 case KEYTYP_BYTE:
00413 printk ("%s:\tKEYTYP_BYTE\t%d\n", key->name, *(char*)key->val);
00414 break;
00415 case KEYTYP_INT:
00416 printk ("%s:\tKEYTYP_INT\t%d\n", key->name, *(int*)key->val);
00417 break;
00418 case KEYTYP_FLOAT:
00419 printk ("%s:\tKEYTYP_FLOAT\t%13.6e\n", key->name, *(float*)key->val);
00420 break;
00421 case KEYTYP_DOUBLE:
00422 printk ("%s:\tKEYTYP_DOUBLE\t%23.6e\n", key->name, *(double*)key->val);
00423 break;
00424 case KEYTYP_TIME:
00425 printk ("%s:\tKEYTYP_TIME\t%23.6e\n", key->name, *(TIME*)key->val);
00426 break;
00427 case KEYTYP_SHORT:
00428 printk ("%s:\tKEYTYP_SHORT\t%d\n", key->name, *(short*)key->val);
00429 break;
00430 case KEYTYP_LONG:
00431 printk ("%s:\tKEYTYP_LONG\t%d\n", key->name, *(long*)key->val);
00432 break;
00433 case KEYTYP_UBYTE:
00434 printk ("%s:\tKEYTYP_UBYTE\t%d\n", key->name, *(unsigned char*)key->val);
00435 break;
00436 case KEYTYP_USHORT:
00437 printk ("%s:\tKEYTYP_USHORT\t%d\n", key->name, *(unsigned short*)key->val);
00438 break;
00439 case KEYTYP_UINT:
00440 printk ("%s:\tKEYTYP_UINT\t%d\n", key->name, *(unsigned int*)key->val);
00441 break;
00442 case KEYTYP_ULONG:
00443 printk ("%s:\tKEYTYP_ULONG\t%d\n", key->name, *(unsigned long*)key->val);
00444 break;
00445 case KEYTYP_UINT64:
00446 printk ("%s:\tKEYTYP_UINT64\t%ld\n", key->name, *(uint64_t*)key->val);
00447 break;
00448 case KEYTYP_UINT32:
00449 printk ("%s:\tKEYTYP_UINT32\t%d\n", key->name, *(uint32_t*)key->val);
00450 break;
00451 case KEYTYP_FILEP:
00452 printk ("%s:\tKEYTYP_FILEP\t%ld\n", key->name, *(FILE **)key->val);
00453 break;
00454 default:
00455 printk ("(void)\n");
00456 }
00457 }
00458