00001 #define DEBUG 0
00002
00003
00004
00005
00006
00007
00045 #include "jsoc_main.h"
00046 #include "drms.h"
00047 #include "drms_names.h"
00048 #include "json.h"
00049 #include <time.h>
00050 #include <sys/types.h>
00051 #include <unistd.h>
00052
00053 #define LOGFILE "/home/jsoc/exports/logs/fetch_log"
00054 #define kLockFile "/home/jsoc/exports/tmp/lock.txt"
00055
00056 static char x2c (char *what)
00057 {
00058 char digit;
00059 digit = (char)(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
00060 digit *= 16;
00061 digit = (char)(digit + (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0')));
00062 return (digit);
00063 }
00064
00065 static void CGI_unescape_url (char *url)
00066 {
00067 int x, y;
00068 for (x = 0, y = 0; url[y]; ++x, ++y)
00069 {
00070 if ((url[x] = url[y]) == '%')
00071 {
00072 url[x] = x2c (&url[y+1]);
00073 y += 2;
00074 }
00075 }
00076 url[x] = '\0';
00077 }
00078
00079 static void getlock(int fd, char *fname, int mustchmodlck)
00080 {
00081 int sleeps;
00082 for(sleeps=0; lockf(fd,F_TLOCK,0); sleeps++)
00083 {
00084 if (sleeps >= 20)
00085 {
00086 fprintf(stderr,"Lock stuck on %s, GetNextID failed.\n", fname);
00087 if (mustchmodlck)
00088 {
00089 fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
00090 }
00091 lockf(fd,F_ULOCK,0);
00092 close(fd);
00093 exit(1);
00094 }
00095 sleep(1);
00096 }
00097 return;
00098 }
00099
00100 ModuleArgs_t module_args[] =
00101 {
00102 {ARG_STRING, "op", "Not Specified", "<Operation>, value sent to jsoc_fetch"},
00103 {ARG_STRING, "ds", "Not Specified", "<record_set query>"},
00104 {ARG_INT, "n", "0", "RecordSet Limit"},
00105 {ARG_DOUBLE, "lag", "0", "Processing time, secs"},
00106 {ARG_INT, "status", "0", "jsoc_fetch status"},
00107 {ARG_STRING, "QUERY_STRING", "Not Specified", "AJAX query from the web"},
00108 {ARG_STRING, "REMOTE_ADDR", "0.0.0.0", "Remote IP address"},
00109 {ARG_STRING, "host", "0.0.0.0", "Server"},
00110 {ARG_END}
00111 };
00112
00113 char *module_name = "jsoc_stats1";
00114
00115
00116 int DoIt(void)
00117 {
00118 char *op;
00119 char *ds;
00120 char *web_query;
00121 char *IP;
00122 char *host;
00123 int from_web;
00124 int rstatus;
00125 int n;
00126 FILE *log;
00127 double lag;
00128 int lockfd;
00129 struct stat stbuf;
00130 int mustchmodlck = (stat(kLockFile, &stbuf) != 0);
00131 int mustchmodlog = (stat(LOGFILE, &stbuf) != 0);;
00132
00133 web_query = strdup (cmdparams_get_str (&cmdparams, "QUERY_STRING", NULL));
00134 from_web = strcmp (web_query, "Not Specified") != 0;
00135
00136 if (from_web)
00137 {
00138 char *getstring, *p;
00139 CGI_unescape_url(web_query);
00140 getstring = strdup (web_query);
00141 for (p=strtok(getstring,"&"); p; p=strtok(NULL, "&"))
00142 {
00143 char *key=p, *val=index(p,'=');
00144 if (!val)
00145 {
00146 fprintf(stderr,"Bad QUERY_STRING %s",web_query);
00147 return(1);
00148 }
00149 *val++ = '\0';
00150 cmdparams_set(&cmdparams, key, val);
00151 }
00152 free(getstring);
00153 }
00154
00155 op = (char *)cmdparams_get_str (&cmdparams, "op", NULL);
00156 ds = (char *)cmdparams_get_str (&cmdparams, "ds", NULL);
00157 IP = (char *)cmdparams_get_str (&cmdparams, "REMOTE_ADDR", NULL);
00158 host = (char *)cmdparams_get_str (&cmdparams, "host", NULL);
00159 n = cmdparams_get_int (&cmdparams, "n", NULL);
00160 rstatus = cmdparams_get_int (&cmdparams, "status", NULL);
00161 lag = cmdparams_get_double (&cmdparams, "lag", NULL);
00162
00163 lockfd = open(kLockFile, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG);
00164 if (lockfd >= 0)
00165 {
00166 getlock(lockfd, kLockFile, mustchmodlck);
00167 log = fopen(LOGFILE,"a");
00168
00169 if (log)
00170 {
00171 fprintf(log, "host='%s'\t",host);
00172 fprintf(log, "lag=%0.3f\t",lag);
00173 fprintf(log, "IP='%s'\t",IP);
00174 fprintf(log, "op='%s'\t",op);
00175 fprintf(log, "ds='%s'\t",ds);
00176 fprintf(log, "n=%d\t",n);
00177 fprintf(log, "status=%d\n",rstatus);
00178 fflush(log);
00179 if (mustchmodlog)
00180 {
00181 fchmod(fileno(log), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
00182 }
00183 fclose(log);
00184 }
00185 else
00186 {
00187 fprintf(stderr, "Unable to open log file for writing: %s.\n", LOGFILE);
00188 }
00189
00190 lockf(lockfd,F_ULOCK,0);
00191 if (mustchmodlck)
00192 {
00193 fchmod(lockfd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
00194 }
00195 close(lockfd);
00196 }
00197 else
00198 {
00199 fprintf(stderr, "Unable to open lock file for writing: %s.\n", kLockFile);
00200 }
00201
00202
00203 printf("Content-Type: text/plain\n\nOK\n");
00204 fflush(stdout);
00205 return(0);
00206 }