00001 #include "jsoc.h"
00002 #include "exputil.h"
00003 #include "drms_keyword.h"
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 ExpUtlStat_t exputl_mk_expfilename(DRMS_Segment_t *srcseg,
00047 DRMS_Segment_t *tgtseg,
00048
00049 const char *filenamefmt,
00050 char *filename)
00051 {
00052 static int namesMade = 0;
00053 ExpUtlStat_t ret = kExpUtlStat_Success;
00054 char filenameWorking[128] = {0};
00055 char *fn = filenameWorking;
00056 char format[1024];
00057 char *fmt;
00058
00059 if (filenamefmt)
00060 snprintf(format, sizeof(format), "%s", filenamefmt);
00061 else
00062 snprintf(format, sizeof(format), "{seriesname}.{recnum:%%lld}.{segment}");
00063
00064 fmt = format;
00065 *fn = '\0';
00066 while (*fmt)
00067 {
00068 char *last;
00069 if (*fmt == '{')
00070 {
00071 char *val = NULL;
00072 char *p;
00073 char *keyname;
00074 char *layout;
00075 last = index(fmt, '}');
00076 if (!last)
00077 {
00078 ret = kExpUtlStat_InvalidFmt;
00079 break;
00080 }
00081
00082 keyname = ++fmt;
00083 layout = NULL;
00084 *last = '\0';
00085 for (p=keyname; p<last; p++)
00086 {
00087 if (*p == ':')
00088 {
00089 *p++ = '\0';
00090 layout = p;
00091 }
00092 }
00093 if (*keyname)
00094 {
00095 char valstr[128];
00096 char *tmpstr = NULL;
00097 char tmpstr2[128];
00098
00099 if (strcmp(keyname, "#") == 0)
00100 {
00101 snprintf(valstr, sizeof(valstr), (layout ? layout : "%05d"), namesMade++);
00102 val = valstr;
00103 }
00104 else if (strcmp(keyname,"seriesname")==0)
00105 val = srcseg->record->seriesinfo->seriesname;
00106 else if (strcmp(keyname,"recnum")==0)
00107 {
00108 snprintf(valstr, sizeof(valstr), (layout ? layout : "%lld"),
00109 srcseg->record->recnum);
00110 val = valstr;
00111 }
00112 else if (strcmp(keyname,"segment")==0)
00113 {
00114 if (srcseg->info->islink)
00115 {
00116 val = tgtseg->filename;
00117 }
00118 else
00119 {
00120 val = srcseg->filename;
00121 }
00122
00123 if (!val || !*val)
00124 {
00125
00126 val = srcseg->info->name;
00127 }
00128 }
00129
00130 else if (layout)
00131 {
00132
00133
00134 DRMS_Keyword_t *key = drms_keyword_lookup(srcseg->record,keyname,1);
00135
00136 if (!key)
00137 {
00138 ret = kExpUtlStat_UnknownKey;
00139 val = "ERROR";
00140 }
00141 else if (key->info->type == DRMS_TYPE_TIME)
00142 {
00143 char formatwas[DRMS_MAXFORMATLEN], unitwas[DRMS_MAXUNITLEN];
00144 int precision = 0;
00145 char Mod = ' ';
00146 strncpy(formatwas, key->info->format, DRMS_MAXFORMATLEN);
00147 strncpy(unitwas, key->info->unit, DRMS_MAXUNITLEN);
00148 if (isalpha(*layout)) Mod = *layout++;
00149 if (isdigit(*layout) || *layout == '-')
00150 precision = strtol(layout, &layout, 10);
00151 snprintf(key->info->format, DRMS_MAXFORMATLEN, "%d", precision);
00152 if (*layout == ',' && *(layout+1))
00153 strncpy(key->info->unit,layout+1,DRMS_MAXUNITLEN);
00154
00155
00156 if (srcseg->info->islink)
00157 {
00158 tmpstr = drms_getkey_string(tgtseg->record, keyname, NULL);
00159 }
00160 else
00161 {
00162 tmpstr = drms_getkey_string(srcseg->record, keyname, NULL);
00163 }
00164
00165 snprintf(tmpstr2, sizeof(tmpstr2), "%s", tmpstr);
00166 free(tmpstr);
00167 val = tmpstr2;
00168
00169 strncpy(key->info->format, formatwas, DRMS_MAXFORMATLEN);
00170 strncpy(key->info->unit, unitwas, DRMS_MAXUNITLEN);
00171 if (Mod != ' ')
00172 {
00173 if (Mod == 'A')
00174 {
00175 int i;
00176 char *cp;
00177 for (i=0, cp=val; *cp; cp++)
00178 if (*cp != '.' && *cp != ':')
00179 valstr[i++] = *cp;
00180 valstr[i] = '\0';
00181 val = valstr;
00182 }
00183 else if (Mod == 'D')
00184 {
00185 int i;
00186 char *cp;
00187 valstr[0] = '@';
00188 for (i=1, cp=val; *cp; cp++)
00189 {
00190 if (*cp == ':') continue;
00191 if (*cp == '.' || i == 11)
00192 valstr[i++] = '@';
00193 else
00194 valstr[i++] = *cp;
00195 }
00196 valstr[i] = '\0';
00197 val = valstr;
00198 }
00199
00200 }
00201 else
00202 {
00203 strncpy(valstr, val, 128);
00204
00205 val = valstr;
00206 }
00207 }
00208 else
00209 {
00210
00211 char formatwas[DRMS_MAXFORMATLEN];
00212 DRMS_Keyword_t *followedKey = NULL;
00213 char *pCh = NULL;
00214 char *pVal = NULL;
00215
00216 strncpy(formatwas, key->info->format, DRMS_MAXFORMATLEN);
00217 strncpy(key->info->format,layout,DRMS_MAXFORMATLEN);
00218 followedKey = drms_keyword_lookup(srcseg->record, keyname, 1);
00219
00220 if (followedKey)
00221 {
00222 *tmpstr2 = '\0';
00223 drms_keyword_snprintfval(followedKey, tmpstr2, sizeof(tmpstr2));
00224
00225
00226 for (pCh = tmpstr2, pVal = valstr; *pCh && pVal < valstr + sizeof(valstr) - 1; pCh++)
00227 {
00228 if (!isspace(*pCh))
00229 {
00230 *pVal = *pCh;
00231 pVal++;
00232 }
00233 }
00234
00235 *pVal = '\0';
00236
00237 val = valstr;
00238 }
00239
00240 strncpy(key->info->format, formatwas, DRMS_MAXFORMATLEN);
00241 }
00242 }
00243 else
00244 {
00245
00246 DRMS_Keyword_t *key = drms_keyword_lookup(srcseg->record, keyname, 1);
00247
00248 if (key)
00249 {
00250 drms_keyword_snprintfval(key, tmpstr2, sizeof(tmpstr2));
00251 val = tmpstr2;
00252 }
00253 else
00254 {
00255 ret = kExpUtlStat_UnknownKey;
00256 val = "ERROR";
00257 }
00258 }
00259
00260
00261
00262
00263 if (!val)
00264 {
00265 ret = kExpUtlStat_InvalidFmt;
00266 val = "ERROR";
00267 }
00268 #if 0
00269
00270
00271
00272 else
00273 {
00274 strncpy(valstr, val, 128);
00275
00276 val = valstr;
00277 }
00278 #endif
00279 if (strlen(filenameWorking) + strlen(val) < sizeof(filenameWorking))
00280 {
00281 for (p=val; *p; )
00282 {
00283 *fn++ = *p++;
00284 }
00285 *fn = '\0';
00286 }
00287 else
00288 {
00289
00290 ret = kExpUtlStat_InvalidFmt;
00291 }
00292 }
00293 fmt = last+1;
00294
00295 val = NULL;
00296 }
00297 else
00298 {
00299 if (strlen(filenameWorking) + strlen(fmt) < sizeof(filenameWorking))
00300 {
00301 *fn++ = *fmt++;
00302 }
00303 else
00304 {
00305
00306 ret = kExpUtlStat_InvalidFmt;
00307 }
00308 }
00309 }
00310 *fn = '\0';
00311
00312 if (filename)
00313 {
00314
00315
00316 char *pIn = NULL;
00317 char *pOut = NULL;
00318
00319 for (pIn = filenameWorking, pOut = filename; *pIn ; pIn++)
00320 {
00321 if (!isspace(*pIn))
00322 {
00323 *pOut = *pIn;
00324 pOut++;
00325 }
00326 }
00327
00328 *pOut = '\0';
00329
00330
00331 snprintf(filename, sizeof(filenameWorking), "%s", filenameWorking);
00332 }
00333
00334 return ret;
00335 }
00336
00337
00338
00339
00340 #define HANDLE_FILE "/home/jsoc/exports/Web_request_handles"
00341
00342 static FILE *lock_open(const char *filename)
00343 {
00344 int sleeps;
00345 char lockfile[1024];
00346
00347 snprintf(lockfile, sizeof(lockfile), "%s.lck", filename);
00348
00349 FILE *fp = fopen(lockfile, "w");
00350 if (!fp)
00351 {
00352 fprintf(stderr, "Failed to open file for locking %s.\n", lockfile);
00353 return NULL;
00354 }
00355
00356 for (sleeps=0; lockf(fileno(fp), F_TLOCK, 0); sleeps++)
00357 {
00358 if (sleeps >= 300)
00359 {
00360 fprintf(stderr,"Lock on %s failed.\n", lockfile);
00361 return NULL;
00362 }
00363 sleep(1);
00364 }
00365
00366 return(fp);
00367 }
00368
00369 static lock_close(FILE *fp)
00370 {
00371 lockf(fileno(fp), F_ULOCK, 0);
00372 fclose(fp);
00373 }
00374
00375 ExpUtlStat_t exputl_manage_cgibin_handles_add_proc_handle(const char *file, const char *handle, pid_t pid)
00376 {
00377 FILE *fp = NULL;
00378 FILE *fp_lock = NULL;
00379
00380 fp_lock = lock_open(file);
00381 if (!fp_lock)
00382 {
00383 return kExpUtlStat_ManageHandles;
00384 }
00385
00386 fp = fopen(file, "a");
00387 if (!fp)
00388 {
00389 fprintf(stderr, "Failed to open %s file.\n", file);
00390 return kExpUtlStat_ManageHandles;
00391 }
00392
00393 fprintf(fp, "%s\t%d\n", handle, pid);
00394 fclose(fp);
00395 lock_close(fp_lock);
00396 return kExpUtlStat_Success;
00397 }
00398
00399 char *exputl_manage_cgibin_handles_lookup_proc_handle(const char *file, const char *handle)
00400 {
00401 FILE *fp = NULL;
00402 FILE *fp_lock = NULL;
00403 char PID[128];
00404 char HANDLE[128];
00405 char *pid = NULL;
00406
00407 fp_lock = lock_open(file);
00408 if (!fp_lock)
00409 {
00410 return NULL;
00411 }
00412
00413 fp = fopen(file, "r");
00414 if (!fp)
00415 {
00416 fprintf(stderr, "Failed to open %s file.\n", file);
00417 return NULL;
00418 }
00419
00420 while (fscanf(fp, "%s\t%s\n", HANDLE, PID) == 2)
00421 {
00422 if (strcmp(handle, HANDLE) == 0)
00423 {
00424 pid = strdup(PID);
00425 break;
00426 }
00427 }
00428
00429 fclose(fp);
00430 lock_close(fp_lock);
00431 return pid;
00432 }
00433
00434 ExpUtlStat_t exputl_manage_cgibin_handles_delete_proc_handle(const char *file, const char *handle)
00435 {
00436 FILE *fp = NULL;
00437 FILE *fp_lock = NULL;
00438 char cmd[1024];
00439
00440 fp_lock = lock_open(file);
00441 if (!fp_lock)
00442 {
00443 return kExpUtlStat_ManageHandles;
00444 }
00445
00446 sprintf(cmd, "ed -s %s <<END\ng/^%s\t/d\nwq\nEND\n", file, handle);
00447 system(cmd);
00448 lock_close(fp_lock);
00449 return kExpUtlStat_Success;
00450 }
00451
00452 ExpUtlStat_t exputl_manage_cgibin_handles_edit_proc_handles(const char *file)
00453 {
00454 FILE *fp = NULL;
00455 FILE *fp_lock = NULL;
00456 char cmd[1024];
00457
00458 fp_lock = lock_open(file);
00459 if (!fp_lock)
00460 {
00461 return kExpUtlStat_ManageHandles;
00462 }
00463
00464 sprintf(cmd, "vi %s\n", file);
00465 system(cmd);
00466 lock_close(fp_lock);
00467 return kExpUtlStat_Success;
00468 }
00469
00470 ExpUtlStat_t exputl_manage_cgibin_handles(const char *op, const char *handle, pid_t pid, const char *file)
00471 {
00472 int op_add = 0;
00473 int op_delete = 0;
00474 int op_edit = 0;
00475 int op_lookup = 0;
00476
00477 if (!op)
00478 {
00479 fprintf(stderr, "No operation provided.\n");
00480 return kExpUtlStat_ManageHandles;
00481 }
00482 else if (strlen(op) != 1)
00483 {
00484 fprintf(stderr, "Invalid operation: %s\n", op);
00485 return kExpUtlStat_ManageHandles;
00486 }
00487 else
00488 {
00489 if (!file || *file == '\0')
00490 {
00491 file = HANDLE_FILE;
00492 }
00493
00494 op_add = (*op == 'a');
00495 op_delete = (*op == 'd');
00496 op_edit = (*op == 'e');
00497 op_lookup = (*op == 'l');
00498
00499 if (!op_edit && !handle)
00500 {
00501 fprintf(stderr, "handle must be provided.\n");
00502 return kExpUtlStat_ManageHandles;
00503 }
00504
00505 if (op_add)
00506 {
00507 return exputl_manage_cgibin_handles_add_proc_handle(file, handle, pid);
00508 }
00509
00510 if (op_lookup)
00511 {
00512 char *pid = exputl_manage_cgibin_handles_lookup_proc_handle(file, handle);
00513
00514 if (pid)
00515 {
00516 printf("%s\n", pid);
00517 }
00518
00519 fflush(stdout);
00520 return kExpUtlStat_Success;
00521 }
00522
00523 if (op_delete)
00524 {
00525 return exputl_manage_cgibin_handles_delete_proc_handle(file, handle);
00526 }
00527
00528 if (op_edit)
00529 {
00530 return exputl_manage_cgibin_handles_edit_proc_handles(file);
00531 }
00532 }
00533
00534 return kExpUtlStat_Success;
00535 }