00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00021 #ifdef __LOCALIZED_DEFS__
00022 #define LOGDIR SUMLOG_BASEDIR
00023 #else
00024 #define LOGDIR "/usr/local/logs/SUM"
00025 #endif
00026
00027 void logkey();
00028 KEY *getsumexport(KEY *params);
00029 int file2keylist(char *filename, KEY **list);
00030
00031 struct timeval TIMEOUT = { 20, 0 };
00032
00033 static KEY *retlist;
00034 char *logdir, *db, *keyfile, *server;
00035 char datestr[32];
00036 char logname[MAX_STR];
00037 int pid, debugflg;
00038 uint32_t rinfo;
00039 FILE *logfp;
00040 SVCXPRT *glb_transp;
00041 CLIENT *current_client;
00042
00043 int 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 return(1);
00048 }
00049 return(0);
00050 }
00051
00052
00053
00054 int write_log(const char *fmt, ...)
00055 {
00056 va_list args;
00057 char string[4096];
00058
00059 va_start(args, fmt);
00060 vsprintf(string, fmt, args);
00061 if(logfp) {
00062 fprintf(logfp, string);
00063 fflush(logfp);
00064 }
00065 else
00066 fprintf(stderr, string);
00067 va_end(args);
00068 return(0);
00069 }
00070
00071
00072 static char *datestring()
00073 {
00074 struct timeval tvalr;
00075 struct tm *t_ptr;
00076
00077 gettimeofday(&tvalr, NULL);
00078 t_ptr = localtime((const time_t *)&tvalr);
00079 sprintf(datestr, "%s", asctime(t_ptr));
00080 datestr[19] = (char)NULL;
00081 return(&datestr[4]);
00082 }
00083
00084 void sighandler(int sig)
00085 {
00086 if(sig == SIGTERM) {
00087 printk("*** %s sum_export got SIGTERM. Exiting.\n", datestring());
00088 exit(1);
00089 }
00090 if(sig == SIGINT) {
00091 printk("*** %s sum_export got SIGINT. Exiting.\n", datestring());
00092 exit(1);
00093 }
00094 printk("*** %s sum_export got an illegal signal %d, ignoring...\n",
00095 datestring(), sig);
00096 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00097 signal(SIGINT, sighandler);
00098 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00099 signal(SIGALRM, sighandler);
00100 }
00101
00102
00103
00104
00105 void get_cmd(int argc, char *argv[])
00106 {
00107 int c;
00108
00109 while((--argc > 0) && ((*++argv)[0] == '-')) {
00110 while((c = *++argv[0])) {
00111 switch(c) {
00112 case 'd':
00113 debugflg=1;
00114 break;
00115 default:
00116 break;
00117 }
00118 }
00119 }
00120 server = argv[0];
00121 keyfile = argv[1];
00122 }
00123
00124 int setup () {
00125 pid = getpid();
00126 sprintf(logname, "%s/sum_export_%d.log", LOGDIR, pid);
00127 if(open_log(logname)) return(1);
00128 printk_set(write_log, write_log);
00129 printk("%s\nStarting sum_export \nserver=%s\nkeyfile = %s\nlogfile = %s\n\n", datestring(), server, keyfile, logname);
00130 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00131 signal(SIGINT, sighandler);
00132 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
00133 signal(SIGTERM, sighandler);
00134 if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
00135 signal(SIGALRM, sighandler);
00136 return(0);
00137 }
00138
00139
00140
00141 int main(int argc, char **argv)
00142 {
00143 int status = 0;
00144 uint32_t sumpeback;
00145 char cmd[128];
00146 char *call_err;
00147 KEY *list=newkeylist();
00148 CLIENT *clntsumpesvc;
00149
00150
00151 get_cmd(argc, argv);
00152 if(setup()) exit(0);
00153 if(file2keylist(keyfile, &list)) {
00154 printk("Error in file2keylist() for %s\n", keyfile);
00155 return(1);
00156 }
00157 sprintf(cmd, "/bin/rm -f %s", keyfile);
00158 system(cmd);
00159 keyiterate(logkey, list);
00160
00161 clntsumpesvc = clnt_create(server, SUMEXPROG, SUMEXVERS, "tcp");
00162 if(!clntsumpesvc) {
00163 clnt_pcreateerror("Can't get client handle to sum_export_svc");
00164 printk("sum_export_svc not there on %s\n", server);
00165 return(1);
00166 }
00167
00168 retlist = getsumexport(list);
00169
00170 status = clnt_call(clntsumpesvc,SUMEXACK, (xdrproc_t)xdr_Rkey,
00171 (char *)retlist, (xdrproc_t)xdr_uint32_t, (char *)&sumpeback, TIMEOUT);
00172 if(status != RPC_SUCCESS) {
00173 call_err = clnt_sperror(clntsumpesvc, "Err clnt_call for SUMPEACK");
00174 printk("%s %s\n", datestring(), call_err);
00175 if(status != RPC_TIMEDOUT) {
00176 return(status);
00177 }
00178 }
00179 if(sumpeback != 0) {
00180 printk("Error status= %d from clnt_call to SUMEXACK = %d\n", sumpeback);
00181 status = 1;
00182 }
00183 sprintf(cmd, "/bin/rm -f %s", logname);
00184 system(cmd);
00185 return(0);
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 KEY *getsumexport(KEY *params)
00198 {
00199 uint port;
00200 int reqcnt;
00201 int status = 0;
00202 int xdirflg = 0;
00203 int first_rec, last_rec, nrecs, irec, retrieve_flg, i;
00204 char srcext[128], destext[128], cmd[256], errmsg[128];
00205 char *prog, *host, *src, *dest;
00206
00207
00208
00209 retlist = newkeylist();
00210 add_keys(params, &retlist);
00211 setkey_fileptr(&retlist, "current_client", getkey_fileptr(params, "current_client"));
00212 setkey_int(&retlist, "REQCODE", REMSUMRESPDO);
00213 reqcnt = getkey_int(params, "reqcnt");
00214 prog = getkey_str(params, "cmd");
00215 host = getkey_str(params, "host");
00216 for(i=0; i < reqcnt; i++) {
00217 sprintf(srcext, "src_%d", i);
00218 sprintf(destext, "dest_%d", i);
00219 src = getkey_str(params, srcext);
00220 dest = getkey_str(params, destext);
00221 port = getkey_uint(params, "port");
00222 if(port == 0)
00223 sprintf(cmd, "%s -r %s:%s/* %s", prog, host, src, dest);
00224 else
00225 sprintf(cmd, "%s -r -P %d %s:%s/* %s", prog, port, host, src, dest);
00226 printk("%s\n", cmd);
00227 if(system(cmd)) {
00228 printk("Error on: %s\n", cmd);
00229 status = 1;
00230 }
00231 }
00232 setkey_int(&retlist, "STATUS", status);
00233 return(retlist);
00234 }