00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00022
00023
00024
00025
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <ctype.h>
00029 #include <signal.h>
00030 #include <strings.h>
00031 #include <dirent.h>
00032 #include <sum_rpc.h>
00033 #include <sys/types.h>
00034 #include <sys/stat.h>
00035 #include <sys/time.h>
00036 #include <unistd.h>
00037 #include <printk.h>
00038
00039 #define MAXFILES 512
00040
00041 FILE *h0logfp;
00042 char datestr[32];
00043 int msg(char *fmt, ...);
00044 void open_h0log(char *filename, char *type);
00045 int h0log(const char *fmt, ...);
00046 void abortit(int stat);
00047 void sighandler(int sig);
00048 void usage(void);
00049 void get_cmd(int argc, char *argv[]);
00050 void setup(int argc, char *argv[]);
00051
00052 int verbose;
00053 int cadence;
00054 int abort_active;
00055 int sigalrmflg = 0;
00056 int sigtermflg = 0;
00057 int ALRMSEC = 60;
00058 int debugflg;
00059 char *username;
00060 char *sourcedir;
00061 char *targetdir;
00062 char *hostname;
00063
00064
00065
00066 int msg(char *fmt, ...)
00067 {
00068 va_list args;
00069 char string[32768];
00070
00071 va_start(args, fmt);
00072 vsprintf(string, fmt, args);
00073 printf(string);
00074 fflush(stdout);
00075 va_end(args);
00076 return(0);
00077 }
00078
00079
00080
00081
00082 void open_h0log(char *filename, char *type)
00083 {
00084 if((h0logfp=fopen(filename, type)) == NULL)
00085 fprintf(stderr, "**Can't open the log file %s\n", filename);
00086 }
00087
00088
00089
00090 int h0log(const char *fmt, ...)
00091 {
00092 va_list args;
00093 char string[32768];
00094
00095 va_start(args, fmt);
00096 vsprintf(string, fmt, args);
00097 if(h0logfp) {
00098 fprintf(h0logfp, string);
00099 fflush(h0logfp);
00100 } else {
00101 printf(string);
00102 fflush(stdout);
00103 }
00104 va_end(args);
00105 return(0);
00106 }
00107
00108
00109
00110
00111 void abortit(int stat)
00112 {
00113 printk("**Exit soc_pipe_scp w/ status = %d\n", stat);
00114 msg("**Exit soc_pipe_scp w/ status = %d\n\n", stat);
00115 if (h0logfp) fclose(h0logfp);
00116 exit(stat);
00117 }
00118
00119 void sighandler(int sig)
00120 {
00121 sigtermflg = 1;
00122 return;
00123 }
00124
00125
00126
00127 void usage()
00128 {
00129 msg("Usage:\nsoc_pipe_scp [-v] /source_dir /target_dir pipeline_host cadence\n");
00130 msg("where: -v = verbose\n");
00131 msg(" source_dir = $DIRSOC2PIPE that dc system puts files for the pipeline\n");
00132 msg(" target_dir = dir in the pipeline host to copy the files to\n");
00133 msg(" pipeline_host = host name of the pipeline backend\n");
00134 msg(" cadence = cadence in seconds of the dds\n");
00135 abortit(1);
00136 }
00137
00138
00139
00140
00141 void get_cmd(int argc, char *argv[])
00142 {
00143 int c;
00144
00145 while(--argc > 0 && ((*++argv)[0] == '-')) {
00146 while((c = *++argv[0]))
00147 switch(c) {
00148 case 'd':
00149 debugflg=1;
00150 break;
00151 case 'v':
00152 verbose=1;
00153 break;
00154 default:
00155 usage();
00156 break;
00157 }
00158 }
00159 if(argc != 4) usage();
00160 sourcedir = argv[0];
00161 targetdir = argv[1];
00162 hostname = argv[2];
00163 cadence = atoi(argv[3]);
00164 }
00165
00166
00167
00168 void setup(int argc, char *argv[])
00169 {
00170 int i;
00171 time_t tval;
00172 struct tm *t_ptr;
00173 char logname[128], string[128], cwdbuf[128], idstr[256];
00174
00175 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
00176 signal(SIGINT, sighandler);
00177 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
00178 signal(SIGTERM, sighandler);
00179
00180 tval = time(NULL);
00181 t_ptr = localtime(&tval);
00182 sprintf(datestr, "%d.%02d.%02d_%02d:%02d:%02d",
00183 (t_ptr->tm_year+1900), (t_ptr->tm_mon+1),
00184 t_ptr->tm_mday, t_ptr->tm_hour, t_ptr->tm_min, t_ptr->tm_sec);
00185 if(!(username = (char *)getenv("USER"))) username = "nouser";
00186 sprintf(logname, "/usr/local/logs/soc/soc_pipe_scp_%s_%d",hostname,getpid());
00187 open_h0log(logname, "w");
00188 printk_set(h0log, h0log);
00189 printk("%s\n", datestr);
00190 getcwd(cwdbuf, 126);
00191 sprintf(idstr, "Cwd: %s\nCall: ", cwdbuf);
00192 for(i=0; i < argc; i++) {
00193 sprintf(string, "%s%s", argv[i], (i < argc-1) ? " " : "");
00194 strcat(idstr, string);
00195 }
00196 strcat(idstr, "\n");
00197 sprintf(string, "soc_pipe_scp started as pid=%d user=%s\n", getpid(), username);
00198 strcat(idstr, string);
00199 printk("*%s", idstr);
00200
00201 }
00202
00203 int main(int argc, char *argv[])
00204 {
00205 DIR *dfd;
00206 struct dirent *dp;
00207 struct stat statbuf;
00208 int i, j;
00209 char cmd[256], qacfile[128], svfile[128];
00210 char *cptr;
00211
00212 get_cmd(argc, argv);
00213 setup(argc, argv);
00214 while(1) {
00215 if((dfd=opendir(sourcedir)) == NULL) {
00216 printk("**Can't opendir(%s) to find files\n", sourcedir);
00217 abortit(3);
00218 }
00219 i = 0;
00220 while((dp=readdir(dfd)) != NULL) {
00221
00222 if(cptr=strstr(dp->d_name, ".tlm")) {
00223 sprintf(cmd, "/usr/bin/scp %s/%s %s:%s 1> /dev/null 2>&1",
00224 sourcedir, dp->d_name, hostname, targetdir);
00225 printk("%s\n", cmd);
00226 if(system(cmd)) {
00227 printk("***Error on: %s\n", cmd);
00228 sprintf(svfile, "/tmp/scp_stdout_%d.log", i++);
00229 sprintf(cmd, "/usr/bin/scp %s/%s %s:%s 1> %s 2>&1",
00230 sourcedir, dp->d_name, hostname, targetdir, svfile);
00231 if(system(cmd)) {
00232 printk("***Error on Retry: %s\n", cmd);
00233 continue;
00234 }
00235 else {
00236 printk("Retry OK: %s\n", cmd);
00237 sprintf(cmd, "/bin/rm -f %s/%s", sourcedir, dp->d_name);
00238 if(system(cmd)) {
00239 printk("***Error on: %s\n", cmd);
00240 }
00241 }
00242 }
00243 else {
00244 sprintf(cmd, "/bin/rm -f %s/%s", sourcedir, dp->d_name);
00245 if(system(cmd)) {
00246 printk("***Error on: %s\n", cmd);
00247 }
00248 }
00249
00250 strcpy(cptr, ".qac");
00251 sprintf(qacfile, "%s/%s", sourcedir, dp->d_name);
00252 if(stat(qacfile, &statbuf)) {
00253 sleep(1);
00254 if(stat(qacfile, &statbuf)) {
00255
00256 strcpy(cptr, ".qacx");
00257 sprintf(qacfile, "%s/%s", sourcedir, dp->d_name);
00258 if(stat(qacfile, &statbuf)) {
00259 printk("***Error: Can't find qac[x] %s\n", qacfile);
00260 continue;
00261 }
00262 }
00263 }
00264 sprintf(cmd, "/usr/bin/scp %s/%s %s:%s 1> /dev/null 2>&1",
00265 sourcedir, dp->d_name, hostname, targetdir);
00266 printk("%s\n", cmd);
00267 if(system(cmd)) {
00268 printk("***Error on: %s\n", cmd);
00269 sprintf(svfile, "/tmp/scp_stdout_%d.log", i++);
00270 sprintf(cmd, "/usr/bin/scp %s/%s %s:%s 1> %s 2>&1",
00271 sourcedir, dp->d_name, hostname, targetdir, svfile);
00272 if(system(cmd)) {
00273 printk("***Error on Retry: %s\n", cmd);
00274 continue;
00275 }
00276 else {
00277 printk("Retry OK: %s\n", cmd);
00278 sprintf(cmd, "/bin/rm -f %s/%s", sourcedir, dp->d_name);
00279 if(system(cmd)) {
00280 printk("***Error on: %s\n", cmd);
00281 }
00282 }
00283 }
00284 else {
00285 sprintf(cmd, "/bin/rm -f %s/%s", sourcedir, dp->d_name);
00286 if(system(cmd)) {
00287 printk("***Error on: %s\n", cmd);
00288 }
00289 }
00290 }
00291 else {
00292 if(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")
00293 || strstr(dp->d_name, ".qac"))
00294 continue;
00295 sprintf(cmd, "/usr/bin/scp %s/%s %s:%s 1> /dev/null 2>&1",
00296 sourcedir, dp->d_name, hostname, targetdir);
00297 printk("%s\n", cmd);
00298 if(system(cmd)) {
00299 printk("***Error on: %s\n", cmd);
00300 }
00301 else {
00302 sprintf(cmd, "/bin/rm -f %s/%s", sourcedir, dp->d_name);
00303 if(system(cmd)) {
00304 printk("***Error on: %s\n", cmd);
00305 }
00306 }
00307 }
00308 }
00309 closedir(dfd);
00310 if(sigtermflg) abortit(2);
00311 sleep(cadence/2);
00312 }
00313 }
00314