00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <ctype.h>
00024 #include <dirent.h>
00025 #include <errno.h>
00026 #include <signal.h>
00027 #include <string.h>
00028 #include <unistd.h>
00029 #include <sys/resource.h>
00030
00031 #include "ingest_lib/ingest_util.h"
00032 #include "ingest_lib/ingest_stats.h"
00033 #include "ingest_lib/sds_util.h"
00034 #include "ingest_lib/sds_log.h"
00035 #include "ingest_lib/sds_msg.h"
00036 #include "ingest_lib/sds_status.h"
00037 #include "ingest_lib/sds_tdb.h"
00038
00039 #include "message.h"
00040 #include "timer.h"
00041
00042
00043
00044
00045
00046 static char *gRCSId;
00047 static char *gRCSState;
00048
00049 static char gTDBInstBase[MAXPLATNAME];
00050 static char gTDBInstName[MAXPLATNAME];
00051 static int gTDBInstNum;
00052
00053 static ProcLoc *gIngestLocation;
00054 static char gIngestInDir[MAXPATHNAME];
00055 static DSClass **gIngestOutputs;
00056
00057 static int gIngestTimeLimit;
00058 static char *gFileExtensionString;
00059
00060 static char *gIngestCustodian;
00061 static char *gMentorCustodian;
00062
00063
00064
00065 const char *get_ingest_rcsid(void) { return((const char *)gRCSId); }
00066 const char *get_ingest_rcsstate(void) { return((const char *)gRCSState); }
00067
00068 const char *get_tdb_inst_name(void) { return((const char *)gTDBInstName); }
00069 const char *get_tdb_inst_base(void) { return((const char *)gTDBInstBase); }
00070 int get_tdb_inst_num(void) { return(gTDBInstNum); }
00071
00072 ProcLoc *get_ingest_location(void) { return(gIngestLocation); }
00073 const char *get_ingest_in_dir(void) { return(gIngestInDir); }
00074 DSClass **get_ingest_outputs(void) { return(gIngestOutputs); }
00075
00076 int get_time_limit(void) { return(gIngestTimeLimit); }
00077 char *get_extension_string(void) { return(gFileExtensionString); }
00078
00079 char *get_ingest_custodian(void) { return(gIngestCustodian); }
00080 char *get_mentor_custodian(void) { return(gMentorCustodian); }
00081
00082
00083
00084
00085
00086 static int get_raw_data_in_dir()
00087 {
00088 char *collection_home;
00089 DSClass **ds_classes;
00090 const char *site;
00091 const char *facility;
00092
00093 collection_home = get_collection_home();
00094 if (!collection_home) {
00095 return(0);
00096 }
00097
00098 ds_classes = get_process_inputs();
00099 if (!ds_classes) {
00100 return(0);
00101 }
00102
00103 site = get_process_site();
00104 facility = get_process_facility();
00105
00106 sprintf(gIngestInDir, "%s/%s/%s%s%s.%s",
00107 collection_home, site,
00108 site,
00109 ds_classes[0]->name,
00110 facility,
00111 ds_classes[0]->level);
00112
00113 print_debug(__FILE__, __LINE__,
00114 "Raw Data Input Directory: %s\n", gIngestInDir);
00115
00116 dsdb_free_ds_classes(ds_classes);
00117
00118 return(1);
00119 }
00120
00121
00122
00123 int validate_ingest_output_dsc(char *dsc_name, char *dsc_level)
00124 {
00125 int row;
00126
00127 for (row = 0; gIngestOutputs[row]; row++) {
00128 if ((strcmp(gIngestOutputs[row]->name, dsc_name) == 0) &&
00129 (strcmp(gIngestOutputs[row]->level, dsc_level) == 0) ) {
00130 return(1);
00131 }
00132 }
00133
00134 current_status(STATUS_BADOUTDSC);
00135 append_log_msg(__FILE__, __LINE__,
00136 "Invalid output datastream class: ['%s' '%s']\n", dsc_name, dsc_level);
00137
00138 return(0);
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 static int incoming(struct message *msg)
00157 {
00158 switch (msg->m_proto) {
00159 case MT_TIMER:
00160 tl_DispatchEvent((struct tm_time *) msg->m_data);
00161 break;
00162 }
00163 return(0);
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 int init_DS(char *site, char *facility, char *proc_name)
00183 {
00184 char id_name[SHORTSTRLEN];
00185
00186 sprintf(id_name, "%s.%s.%s.ingest.%d", site, facility, proc_name, (int)getpid());
00187
00188 usy_init();
00189
00190 if (!msg_connect(incoming, id_name)) {
00191 print_debug(__FILE__, __LINE__, "msg_connect failed.\n");
00192 return(0);
00193 }
00194
00195 if (!ds_Initialize()) {
00196 print_debug(__FILE__, __LINE__, "ds_Initialize failed.\n");
00197 return(0);
00198 }
00199
00200 return(1);
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 void init_ingest(
00237 char *rcsid,
00238 char *rcsstate,
00239 char *site,
00240 char *facility,
00241 char *proc_name)
00242 {
00243 int db_attempts;
00244 char mail_from[64];
00245 char mail_subject[64];
00246 char si;
00247
00248 print_debug(__FILE__, __LINE__,
00249 "Initializing ingest for: ['%s' '%s' '%s']\n"
00250 "RCS Id: %s\n"
00251 "RCS State: %s\n",
00252 site, facility, proc_name, rcsid, rcsstate);
00253
00254 gRCSId = rcsid;
00255 gRCSState = rcsstate;
00256
00257
00258
00259
00260 db_attempts = db_connect("ingest");
00261 if (!db_attempts) {
00262 fprintf(stderr,
00263 "Ingest ['%s' '%s' '%s'] could not connect to DSDB using db_alias 'ingest'.\n",
00264 __FILE__, __LINE__, site, facility, proc_name);
00265 exit(1);
00266 }
00267
00268
00269
00270
00271 init_process_methods(site, facility, "Ingest", proc_name);
00272 init_tdb_methods(site);
00273
00274
00275
00276
00277 sprintf(mail_from, "%s.%s.%s", site, facility, proc_name);
00278 for (si = 0; mail_from[si] != '\0'; si++) {
00279 mail_from[si] = toupper(mail_from[si]);
00280 }
00281 mail_from[si] = '\0';
00282
00283 sprintf(mail_subject, "Ingest");
00284
00285 gIngestCustodian = get_tdb_ingest_custodian();
00286 if (!gIngestCustodian) {
00287 if (is_db_error()) {
00288 db_finish();
00289 exit(1);
00290 }
00291 else {
00292 print_debug(__FILE__, __LINE__,
00293 "Ingest Mail Disabled: No ingest_custodian found in TDB Tables.\n");
00294 }
00295 }
00296
00297 if (gIngestCustodian) {
00298 if (!init_mail_message(INGEST_MSG, mail_from, mail_subject)) {
00299 db_finish();
00300 exit(1);
00301 }
00302 }
00303
00304
00305
00306
00307
00308
00309 gMentorCustodian = get_tdb_mentor_custodian(proc_name);
00310 if (!gMentorCustodian) {
00311 if (is_db_error()) {
00312 db_disconnect();
00313 exit_ingest(FAILURE);
00314 }
00315 else {
00316 print_debug(__FILE__, __LINE__,
00317 "Mentor Mail Disabled: No %s_mentor_custodian was found in the TDB Tables.\n",
00318 proc_name);
00319 }
00320 }
00321
00322 if (gMentorCustodian) {
00323 if (!init_mail_message(MENTOR_MSG, mail_from, mail_subject)) {
00324 db_disconnect();
00325 exit_ingest(FAILURE);
00326 }
00327 }
00328
00329
00330
00331
00332 if (!open_instrlog(proc_name, INGESTLOG_EXTENSION)) {
00333 current_status(STATUS_NOLOGOPEN);
00334 db_disconnect();
00335 exit_ingest(FAILURE);
00336 }
00337
00338
00339
00340
00341 if (!init_signal_handlers()) {
00342 db_disconnect();
00343 exit_ingest(FAILURE);
00344 }
00345
00346
00347
00348
00349
00350 if (db_attempts > 1) {
00351 append_instrlog(
00352 "DB_ATTEPTS: It took %d attempts to connect to the database.",
00353 db_attempts);
00354 }
00355
00356
00357
00358
00359 if (!update_process_started()) {
00360 db_disconnect();
00361 exit_ingest(FAILURE);
00362 }
00363
00364
00365
00366
00367
00368 strcpy(gTDBInstBase, proc_name);
00369
00370 gTDBInstNum = get_tdb_instrument_number(gTDBInstBase, facility);
00371
00372 if (!gTDBInstNum) {
00373 if (!is_db_error()) {
00374 append_log_msg(__FILE__, __LINE__,
00375 "Could not find instrument number in TDB Tables\n",
00376 gTDBInstBase, facility);
00377 current_status(STATUS_NOINSTNUM);
00378 }
00379 exit_ingest(FAILURE);
00380 }
00381
00382 sprintf(gTDBInstName, "%s%d", gTDBInstBase, gTDBInstNum);
00383
00384
00385
00386
00387 gIngestLocation = get_process_location();
00388 if (!gIngestLocation) {
00389 db_disconnect();
00390 exit_ingest(FAILURE);
00391 }
00392
00393 if (!get_raw_data_in_dir()) {
00394 db_disconnect();
00395 exit_ingest(FAILURE);
00396 }
00397
00398 gIngestOutputs = get_process_outputs();
00399 if (!gIngestOutputs) {
00400 db_disconnect();
00401 exit_ingest(FAILURE);
00402 }
00403
00404 gIngestTimeLimit = get_tdb_ingest_time_limit(gTDBInstBase, gTDBInstNum);
00405 if (!gIngestTimeLimit) {
00406 if (!is_db_error()) {
00407 append_log_msg(__FILE__, __LINE__,
00408 "Could not find Ingest time limit in TDB Tables\n");
00409 current_status(STATUS_NOTIMELIMIT);
00410 }
00411 db_disconnect();
00412 exit_ingest(FAILURE);
00413 }
00414
00415 gFileExtensionString = get_tdb_ingest_file_extension(gTDBInstBase, gTDBInstNum);
00416 if (!gFileExtensionString) {
00417 if (!is_db_error()) {
00418 append_log_msg(__FILE__, __LINE__,
00419 "Could not find ingest file extension in TDB Tables\n");
00420 current_status(STATUS_NOFILEEXT);
00421 }
00422 db_disconnect();
00423 exit_ingest(FAILURE);
00424 }
00425
00426
00427
00428
00429 db_disconnect();
00430
00431
00432
00433
00434 if (!init_DS(site, facility, proc_name)) {
00435 current_status(STATUS_INITDS);
00436 append_log_msg(__FILE__, __LINE__,
00437 "Unable to initialize the zebra data store\n");
00438 exit_ingest(FAILURE);
00439 }
00440
00441
00442
00443
00444 reset_dc_stats();
00445 reset_file_stats();
00446 }
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465 char **build_extlist(int *numext)
00466 {
00467 static char *ext_list[MAXEXTENSIONS];
00468 static char ext_str[SHORTSTRLEN];
00469
00470 int ext_strlen;
00471 char *chrp;
00472
00473 *numext = 0;
00474
00475 strcpy(ext_str, gFileExtensionString);
00476 ext_strlen = strlen(gFileExtensionString);
00477
00478 chrp = ext_str;
00479
00480 while(chrp < ext_str + ext_strlen) {
00481
00482 while((!isalnum(*chrp)) && (*chrp != '\0')) {
00483 chrp++;
00484 }
00485
00486 if (*chrp == '\0') {
00487 break;
00488 }
00489 else {
00490 ext_list[*numext] = chrp;
00491 while(isalnum(*++chrp));
00492 *chrp = '\0';
00493 print_debug(__FILE__, __LINE__,
00494 "Added %s to the file extensions list\n", ext_list[*numext]);
00495 chrp++;
00496 (*numext)++;
00497 }
00498 }
00499
00500 if (*numext == 0) {
00501 ext_str[0] = '\0';
00502 ext_list[*numext] = ext_str;
00503 (*numext)++;
00504 }
00505
00506 ext_list[*numext] = (char *)NULL;
00507 return(ext_list);
00508 }
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528 char **build_filelist(int *nfiles, char *dirname)
00529 {
00530 int increment_size = 128;
00531 char **file_list;
00532 char **ext_list;
00533 int numext;
00534 DIR *dirp;
00535 struct dirent *direntp;
00536 char **extpp;
00537 char *file_ext;
00538 int list_size;
00539
00540 print_debug(__FILE__, __LINE__,
00541 "Building the file list for: %s\n", gTDBInstName);
00542
00543
00544
00545
00546 list_size = increment_size;
00547 file_list = (char **)calloc((size_t)list_size, sizeof(char *));
00548
00549 if (!file_list) {
00550 append_log_msg(__FILE__, __LINE__,
00551 "Could not allocate memory for the file list!\n");
00552 current_status(STATUS_NOMEM);
00553 *nfiles = -1;
00554 return((char **)NULL);
00555 }
00556 file_list[0] = NULL;
00557
00558
00559
00560
00561 if (dirname) {
00562 strcpy(dirname, gIngestInDir);
00563 }
00564
00565
00566
00567
00568 ext_list = build_extlist(&numext);
00569
00570
00571
00572
00573 print_debug(__FILE__, __LINE__,
00574 "Searching for files in directory %s\n", gIngestInDir);
00575
00576 dirp = opendir(gIngestInDir);
00577 if (!dirp) {
00578 append_log_msg(__FILE__, __LINE__,
00579 "Error #%i opening directory %s\n -> %s\n",
00580 errno, gIngestInDir, strerror(errno));
00581 free(file_list);
00582 current_status(STATUS_NODIROPEN);
00583 *nfiles = -1;
00584 return((char **)NULL);
00585 }
00586
00587 *nfiles = 0;
00588 while ((direntp = readdir(dirp)) != NULL ) {
00589
00590
00591
00592
00593 for (extpp = ext_list; *extpp != NULL; extpp++) {
00594
00595 if (strlen(direntp->d_name) > strlen(*extpp)) {
00596
00597 file_ext = direntp->d_name + strlen(direntp->d_name) - strlen(*extpp);
00598
00599 if (strcmp(*extpp, file_ext) == 0) {
00600
00601
00602
00603
00604
00605 if (*nfiles > list_size - 1) {
00606
00607 list_size += increment_size;
00608
00609 file_list = (char **)realloc(file_list, list_size * sizeof(char *));
00610
00611 if (!file_list) {
00612 append_log_msg(__FILE__, __LINE__,
00613 "Could not reallocate memory for file list!\n");
00614 closedir(dirp);
00615 current_status(STATUS_NOMEM);
00616 *nfiles = -1;
00617 return((char **)NULL);
00618 }
00619 }
00620
00621
00622
00623
00624 file_list[*nfiles] = (char *)malloc((strlen(direntp->d_name) + 1) * sizeof(char));
00625
00626 if (!file_list[*nfiles]) {
00627 append_log_msg(__FILE__, __LINE__,
00628 "Could not allocate memory for file name: %s\n", direntp->d_name);
00629 closedir(dirp);
00630 current_status(STATUS_NOMEM);
00631 *nfiles = -1;
00632 return((char **)NULL);
00633 }
00634
00635 strcpy(file_list[*nfiles], direntp->d_name);
00636 (*nfiles)++;
00637
00638 print_debug(__FILE__, __LINE__,
00639 "Added %s to the file list\n", direntp->d_name);
00640
00641 break;
00642 }
00643 }
00644 }
00645 }
00646
00647 closedir(dirp);
00648
00649
00650
00651
00652 file_list[*nfiles] = (char *)NULL;
00653
00654
00655
00656
00657 qsort(file_list, *nfiles, sizeof(char *), namecompare);
00658
00659 return(file_list);
00660 }
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677 void free_filelist(char **file_list)
00678 {
00679 int i;
00680
00681 if (file_list) {
00682
00683 for (i = 0; file_list[i]; i++) {
00684 free(file_list[i]);
00685 }
00686 free(file_list);
00687 }
00688 }
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714 void exit_ingest(int status)
00715 {
00716 ProcStatus *proc_status;
00717 time_t last_started;
00718 time_t last_successful;
00719 time_t inst_exp_int;
00720 const char *status_text;
00721 const char *status_name;
00722 const char *site;
00723 const char *facility;
00724 const char *proc_name;
00725 char *hostname;
00726 char *tdbvalue;
00727 int disable_status_changed_mail;
00728 char current_status_msg[512];
00729 char mail_msg[512];
00730
00731 char uc_proc_name[32];
00732 char uc_site[8];
00733 int si;
00734
00735 int found_files = 1;
00736 int db_connected;
00737
00738
00739
00740
00741 db_connected = db_reconnect();
00742
00743 if (db_connected) {
00744
00745
00746
00747
00748 proc_status = get_process_status();
00749 if (!proc_status || !proc_status->text) {
00750
00751 print_debug(__FILE__, __LINE__,
00752 "No status for %s has ever been recorded\n", gTDBInstName);
00753
00754 proc_status = (ProcStatus *)NULL;
00755 last_started = 0;
00756 last_successful = 0;
00757 }
00758 else {
00759
00760 print_debug(__FILE__, __LINE__,
00761 "Previous Ingest Status: \"%s\"\n", proc_status->text);
00762
00763 if (proc_status->last_started) {
00764 last_started = mktime(proc_status->last_started);
00765 }
00766 else {
00767 last_started = 0;
00768 }
00769
00770 if (proc_status->last_successful) {
00771 last_successful = mktime(proc_status->last_successful);
00772 }
00773 else {
00774 last_successful = 0;
00775 }
00776 }
00777 }
00778
00779
00780
00781
00782 status_text = current_status(NULL);
00783
00784 if (status == SUCCESS) {
00785
00786
00787
00788
00789
00790 if ((strcmp(status_text, STATUS_NULLDC) != 0)
00791 && (strcmp(status_text, STATUS_NOMOVERAW) != 0)
00792 && (strcmp(status_text, STATUS_NOUTIME) != 0)) {
00793
00794 status_text = current_status(STATUS_GOOD);
00795 }
00796 }
00797 else if (db_connected) {
00798
00799
00800
00801
00802
00803
00804
00805 if (strcmp(status_text, STATUS_NOFILES) == 0) {
00806
00807 inst_exp_int = get_tdb_inst_exp_int(gTDBInstName);
00808
00809 if (inst_exp_int &&
00810 ((last_started - last_successful) <= inst_exp_int)) {
00811
00812 append_instrlog(
00813 "No new files found but we are within the instrument expectation interval: %d seconds\n",
00814 inst_exp_int);
00815
00816 update_process_completed();
00817
00818 found_files = 0;
00819 }
00820 }
00821 }
00822
00823
00824
00825
00826 site = get_process_site();
00827 for (si = 0; site[si] != '\0'; si++) {
00828 uc_site[si] = toupper(site[si]);
00829 }
00830 uc_site[si] = '\0';
00831
00832 facility = get_process_facility();
00833
00834 proc_name = get_process_name();
00835 for (si = 0; proc_name[si] != '\0'; si++) {
00836 uc_proc_name[si] = toupper(proc_name[si]);
00837 }
00838 uc_proc_name[si] = '\0';
00839
00840 hostname = get_hostname();
00841 if (!hostname) {
00842 hostname = "unknown";
00843 }
00844
00845 sprintf(current_status_msg,
00846 "Current Status:\n"
00847 "Proc: %s.%s.%s\n"
00848 "Host: %s\n"
00849 "Status: %s\n",
00850 uc_site, uc_proc_name, facility,
00851 hostname,
00852 status_text);
00853
00854 if (db_connected) {
00855
00856
00857
00858
00859 disable_status_changed_mail = 0;
00860
00861 tdbvalue = shared_tdb_fetch(DS_TDB, "disable_status_changed_mail");
00862 if (tdbvalue && (strcmp(tdbvalue, "off") != 0)) {
00863 disable_status_changed_mail = 1;
00864 }
00865
00866 if (!proc_status || (strcmp(status_text, proc_status->text) != 0)) {
00867
00868 if (found_files) {
00869 print_debug(__FILE__, __LINE__,
00870 "Status for %s has changed\n", gTDBInstName);
00871
00872 if (!disable_status_changed_mail) {
00873
00874 sprintf(mail_msg, "%s\nLast successful ingest completed at %s",
00875 current_status_msg, ctime(&last_successful));
00876
00877 append_to_msg(INGEST_MSG, mail_msg);
00878 }
00879 }
00880 }
00881 else {
00882 print_debug(__FILE__, __LINE__,
00883 "Status for %s has not changed\n", gTDBInstName);
00884 }
00885
00886
00887
00888
00889 if (found_files) {
00890
00891 if (strcmp(status_text, STATUS_GOOD) == 0) {
00892 status_name = "Success";
00893 }
00894 else {
00895 status_name = "Failure";
00896 }
00897
00898 update_process_status(status_name, status_text);
00899 }
00900 }
00901
00902
00903
00904
00905 write_dc_stats();
00906 write_file_stats();
00907
00908
00909
00910
00911 db_finish();
00912
00913 dsdb_free_process_location(gIngestLocation);
00914 dsdb_free_ds_classes(gIngestOutputs);
00915
00916 print_debug(__FILE__, __LINE__,
00917 "Closed the database\n");
00918
00919
00920
00921
00922 if (gIngestCustodian) {
00923 mail_message(INGEST_MSG, gIngestCustodian);
00924 }
00925
00926 if (gMentorCustodian) {
00927 mail_message(MENTOR_MSG, gMentorCustodian);
00928 }
00929
00930
00931
00932
00933 append_instrlog("\n%s", current_status_msg);
00934 close_instrlog();
00935
00936 if (status == FAILURE) {
00937 exit(1);
00938 }
00939
00940 exit(0);
00941 }
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968 void catch_sig(int sig, siginfo_t *si, void *uc)
00969 {
00970 char msg[128];
00971
00972 switch (sig) {
00973
00974 case SIGQUIT:
00975 strcpy(msg, "SIGQUIT: Quit (see termio(7I))\n");
00976 current_status(STATUS_COREDUMP);
00977 break;
00978 case SIGILL:
00979 strcpy(msg, "SIGILL: Illegal Instruction\n");
00980 current_status(STATUS_COREDUMP);
00981 break;
00982 case SIGTRAP:
00983 strcpy(msg, "SIGTRAP: Trace or Breakpoint Trap\n");
00984 current_status(STATUS_COREDUMP);
00985 break;
00986 case SIGABRT:
00987 strcpy(msg, "SIGABRT: Abort\n");
00988 current_status(STATUS_COREDUMP);
00989 break;
00990 case SIGEMT:
00991 strcpy(msg, "SIGEMT: Emulation Trap\n");
00992 current_status(STATUS_COREDUMP);
00993 break;
00994 case SIGFPE:
00995 strcpy(msg, "SIGFPE: Arithmetic Exception\n");
00996 current_status(STATUS_COREDUMP);
00997 break;
00998 case SIGBUS:
00999 strcpy(msg, "SIGBUS: Bus Error\n");
01000 current_status(STATUS_COREDUMP);
01001 break;
01002 case SIGSEGV:
01003 strcpy(msg, "SIGSEGV: Segmentation Fault\n");
01004 current_status(STATUS_COREDUMP);
01005 break;
01006 case SIGSYS:
01007 strcpy(msg, "SIGSYS: Bad System Call\n");
01008 current_status(STATUS_COREDUMP);
01009 break;
01010 case SIGHUP:
01011 strcpy(msg, "SIGHUP: Hangup (see termio(7I))\n");
01012 current_status(STATUS_SIGNAL);
01013 break;
01014 case SIGINT:
01015 strcpy(msg, "SIGINT: Interrupt (see termio(7I))\n");
01016 current_status(STATUS_SIGNAL);
01017 break;
01018 case SIGPIPE:
01019 strcpy(msg, "SIGPIPE: Broken Pipe\n");
01020 current_status(STATUS_SIGNAL);
01021 break;
01022 case SIGALRM:
01023 strcpy(msg, "SIGALRM: Alarm Clock\n");
01024 current_status(STATUS_SIGNAL);
01025 break;
01026 case SIGTERM:
01027 strcpy(msg, "SIGTERM: Terminated\n");
01028 current_status(STATUS_SIGNAL);
01029 break;
01030 default:
01031 strcpy(msg, "Unknown Signal Type\n");
01032 current_status(STATUS_SIGNAL);
01033 }
01034
01035 append_log_msg(__FILE__, __LINE__,
01036 "Received Signal %s\n", msg);
01037
01038 exit_ingest(0);
01039 }
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072 int init_signal_handlers(void)
01073 {
01074 static int initialized;
01075
01076 struct rlimit rl;
01077 struct sigaction act;
01078
01079 if (initialized) {
01080 return(1);
01081 }
01082 initialized = 1;
01083
01084 act.sa_handler = 0;
01085
01086
01087 act.sa_flags = (SA_SIGINFO);
01088 act.sa_sigaction = catch_sig;
01089
01090 if (sigaction(SIGHUP, &act, 0) != 0 ||
01091 sigaction(SIGINT, &act, 0) != 0 ||
01092 sigaction(SIGQUIT, &act, 0) != 0 ||
01093 sigaction(SIGILL, &act, 0) != 0 ||
01094 sigaction(SIGTRAP, &act, 0) != 0 ||
01095 sigaction(SIGABRT, &act, 0) != 0 ||
01096 sigaction(SIGEMT, &act, 0) != 0 ||
01097 sigaction(SIGFPE, &act, 0) != 0 ||
01098 sigaction(SIGBUS, &act, 0) != 0 ||
01099 sigaction(SIGSEGV, &act, 0) != 0 ||
01100 sigaction(SIGSYS, &act, 0) != 0 ||
01101 sigaction(SIGPIPE, &act, 0) != 0 ||
01102 sigaction(SIGALRM, &act, 0) != 0 ||
01103 sigaction(SIGTERM, &act, 0) != 0
01104 ) {
01105
01106 append_log_msg(__FILE__, __LINE__,
01107 "Calling sigaction failed, error #%i: %s\n", errno, strerror(errno));
01108 current_status(STATUS_SIGNAL);
01109 return(0);
01110 }
01111
01112
01113
01114 rl.rlim_cur = COREDUMPSIZE;
01115 rl.rlim_max = COREDUMPSIZE;
01116 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
01117
01118 append_log_msg(__FILE__, __LINE__,
01119 "Calling setrlimit failed, error #%i: %s\n", errno, strerror(errno));
01120 current_status(STATUS_SIGNAL);
01121 return(0);
01122 }
01123
01124 return(1);
01125 }
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149 void bad_line(int line, char *filename, int linetype)
01150 {
01151 static int badline[BADLINE_MAX];
01152 char errorstr[MAXSTRLEN];
01153 char badline_report[MAXSTRLEN*64];
01154 int badline_total = 0;
01155 int i;
01156
01157 if (linetype == BADLINE_REPORT) {
01158 for(i = 0; i < BADLINE_MAX; i++) {
01159
01160
01161
01162 if (i != BADLINE_COMMENT) {
01163 badline_total += badline[i];
01164 }
01165 }
01166
01167 if (badline_total) {
01168
01169 sprintf(badline_report, "Bad data lines in file: %s\n", filename);
01170 sprintf(errorstr, "Bad number of elements: %d\n", badline[BADLINE_ELE]);
01171 strcat(badline_report,errorstr);
01172 sprintf(errorstr, " Bad characters: %d\n", badline[BADLINE_ALPHA]);
01173 strcat(badline_report,errorstr);
01174 sprintf(errorstr, " Bad date: %d\n", badline[BADLINE_DATE]);
01175 strcat(badline_report,errorstr);
01176 sprintf(errorstr, " Bad Facility ID: %d\n", badline[BADLINE_FACID]);
01177 strcat(badline_report,errorstr);
01178
01179
01180
01181
01182
01183
01184
01185
01186 sprintf(errorstr, " TOTAL BAD LINES: %d\n", badline_total);
01187 strcat(badline_report, errorstr);
01188 append_log_msg(__FILE__, __LINE__, "%s", badline_report);
01189
01190
01191
01192 for(i = 0; i < BADLINE_MAX; i++) {
01193 badline[i] = 0;
01194 }
01195 }
01196 }
01197 else {
01198 sprintf(errorstr, "Improper DataLine (line %d) in %s", line, filename);
01199 append_to_msg(INGEST_MSG, errorstr);
01200 print_debug(__FILE__, __LINE__, "%s\n", errorstr);
01201 badline[linetype]++;
01202 }
01203 }
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223 int get_full_raw_data_file_path(char *filename, char *full_path)
01224 {
01225 sprintf(full_path, "%s/%s", gIngestInDir, filename);
01226 return(1);
01227 }
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244 int open_data_file(char *filename, FILE **fp)
01245 {
01246 if ((*fp = fopen(filename, "r")) == NULL ) {
01247 current_status(STATUS_FOPEN);
01248 append_log_msg(__FILE__, __LINE__,
01249 "Could not open file: %s\n", filename);
01250 return(0);
01251 }
01252
01253 return(1);
01254 }