00001 #include <stdio.h>
00002 #include <time.h>
00003 #include <unistd.h>
00004 #include <stdlib.h>
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 int main()
00015 {
00016 int RequestIDsn;
00017 int old_date, new_date;
00018 int year, month, day;
00019 char RequestID[100];
00020 int sleeps;
00021 FILE *fp;
00022 struct tm *now;
00023 time_t nowtime;
00024
00025 fp = fopen("/home/jsoc/exports/WebRequestID", "r+");
00026 if (!fp)
00027 {
00028 fprintf(stderr, "GetWebRequestID failed to open sn file.\n");
00029 goto FAILEXIT;
00030 }
00031
00032 for(sleeps=0; lockf(fileno(fp),F_TLOCK,0); sleeps++)
00033 {
00034 if (sleeps >= 20)
00035 {
00036 fprintf(stderr,"Lock stuck on /home/jsoc/exports/WebRequestID, GetWebRequestID failed.\n");
00037 goto FAILEXIT;
00038 }
00039 sleep(1);
00040 }
00041
00042 fscanf(fp,"%d_%d",&old_date,&RequestIDsn);
00043
00044 nowtime = time(0);
00045 now = gmtime(&nowtime);
00046 new_date = 10000*(now->tm_year+1900) + 100*(now->tm_mon+1) + now->tm_mday;
00047 if (old_date != new_date)
00048 {
00049 FILE *history = fopen("/home/jsoc/exports/WebRequestID.history", "a");
00050 fprintf(history,"%d_%03d\n", old_date, RequestIDsn);
00051 fclose(history);
00052 RequestIDsn = 1;
00053 }
00054 else
00055 RequestIDsn += 1;
00056 rewind(fp);
00057 sprintf(RequestID,"%d_%05d", new_date, RequestIDsn);
00058
00059 fprintf(fp,"%s\n",RequestID);
00060 rewind(fp);
00061 lockf(fileno(fp),F_ULOCK,0);
00062 fclose(fp);
00063 printf("Content-Type: application/json\n\n{\"handle\":\"%s\"}\n",RequestID);
00064 fflush(stdout);
00065 exit(0);
00066
00067 FAILEXIT:
00068 printf("Content-Type: application/json\n\n{\"handle\":\"ERROR\"}\n");
00069 fflush(stdout);
00070 exit(1);
00071 }