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 fixedpart[100];
00020 char RequestID[100];
00021 int sleeps;
00022 FILE *fp;
00023 struct tm *now;
00024 time_t nowtime;
00025
00026 fp = fopen("/home/jsoc/exports/RequestID", "r+");
00027 if (!fp)
00028 {
00029 fprintf(stderr, "GetJsocRequestID failed to open sn file.\n");
00030 exit(1);
00031 }
00032
00033 for(sleeps=0; lockf(fileno(fp),F_TLOCK,0); sleeps++)
00034 {
00035 if (sleeps >= 20)
00036 {
00037 fprintf(stderr,"Lock stuck on /home/jsoc/exports/RequestID, GetJsocRequestID failed.\n");
00038 exit(1);
00039 }
00040 sleep(1);
00041 }
00042
00043 fscanf(fp,"%[^_]_%d_%d",fixedpart,&old_date,&RequestIDsn);
00044
00045 nowtime = time(0);
00046 now = gmtime(&nowtime);
00047 new_date = 10000*(now->tm_year+1900) + 100*(now->tm_mon+1) + now->tm_mday;
00048 if (old_date != new_date)
00049 {
00050 FILE *history = fopen("/home/jsoc/exports/RequestID.history", "a");
00051 fprintf(history,"%s_%d_%03d\n", fixedpart, old_date, RequestIDsn);
00052 fclose(history);
00053 RequestIDsn = 1;
00054 }
00055 else
00056 RequestIDsn += 1;
00057 rewind(fp);
00058 sprintf(RequestID,"%s_%d_%03d", fixedpart, new_date, RequestIDsn);
00059
00060 fprintf(fp,"%s\n",RequestID);
00061 rewind(fp);
00062 lockf(fileno(fp),F_ULOCK,0);
00063 fclose(fp);
00064 printf("%s\n",RequestID);
00065 }