From dc0b8504faa3a755021e74964336f6f8003dfbf9 Mon Sep 17 00:00:00 2001
From: Ezekiel Dohmen <ezekiel.dohmen@ligo.org>
Date: Mon, 21 Mar 2022 10:54:50 -0700
Subject: [PATCH] Adding error message and catching some errors instead of
 letting the program segfault

---
 install              |  6 +++++-
 src/epics/seq/main.c | 24 ++++++++++++++++++++++--
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/install b/install
index e9ecca34c..7bb6e6389 100755
--- a/install
+++ b/install
@@ -40,6 +40,8 @@ mkdir -p $TARGET
 echo "Installing channel file..."
 
 mkdir -p $RTCDS/chans/filter_archive/$system
+chmod 775 $RTCDS/chans/filter_archive/$system
+
 
 if test -e $RTCDS/chans/${upper_system}.txt; then
     cp  $RTCDS/chans/${upper_system}.txt $RTCDS/chans/filter_archive/$system/${upper_system}_${cur_date}_install.txt || exit 1
@@ -60,9 +62,11 @@ if test -e $TARGET; then
 fi
 
 mkdir -p $TARGET/{bin,scripts,logs}
-mkdir -p $RTCDS/chans/tmp
 chmod 775 $TARGET/logs
+mkdir -p $RTCDS/chans/tmp
+chmod 775 $RTCDS/chans/tmp
 mkdir -p $TARGET/${system}epics/burt
+chmod 775 $TARGET/${system}epics/burt
 if [ -e $RTCDS/target_archive/${system}/${system}_$cur_date/${system}epics/burt ]; then
     cp -pr $RTCDS/target_archive/${system}/${system}_$cur_date/${system}epics/burt $TARGET/${system}epics
 fi
diff --git a/src/epics/seq/main.c b/src/epics/seq/main.c
index b0d43b487..6a836569f 100644
--- a/src/epics/seq/main.c
+++ b/src/epics/seq/main.c
@@ -1972,12 +1972,23 @@ int readConfig( char *pref,		///< EPICS channel prefix from EPICS environment.
 		printf("PARTIAL %s\n",sdfile);
 		cdf = fopen(sdfile,"r");
 		if(cdf == NULL) {
-			sprintf(errMsg,"New SDF request ERROR: FILE %s DOES NOT EXIST\n",sdfile);
+			snprintf(errMsg, 64, "New SDF request ERROR: FILE %s DOES NOT EXIST", sdfile);
 			logFileEntry(errMsg);
 			lderror = 4;
+			perror(errMsg);
+			fprintf(stderr, "sdfile : %s\n", sdfile);
 			return(lderror);
 		}
 		adf = fopen(alarmfile,"w");
+		if(adf == NULL)
+		{
+			snprintf(errMsg, 64, "New SDF request ERROR: failed open alarm file");
+			logFileEntry(errMsg);
+			lderror = 4;
+			perror(errMsg);
+			fprintf(stderr, "alarmfile : %s\n", alarmfile);
+			return(lderror);
+		}
 		chNumP = 0;
 		alarmCnt = 0;
 		// Put dummy in s4 as this column may or may not exist.
@@ -3225,7 +3236,7 @@ int main(int argc,char *argv[])
 	printf("SDF FILE = %s\n",sdfile);
 	printf("CURRENt FILE = %s\n",bufile);
 	printf("LOG FILE = %s\n",logfilename);
-sleep(5);
+	sleep(5);
 	int majorversion = RCG_VERSION_MAJOR;
 	int subversion1 = RCG_VERSION_MINOR;
 	int subversion2 = RCG_VERSION_SUB;
@@ -3238,6 +3249,15 @@ sleep(5);
 	if(!myreleased) myversion *= -1.0;
 	char rcgversionname[256]; sprintf(rcgversionname, "%s_%s", pref, "RCG_VERSION");	// Set RCG Version EPICS
 	status = dbNameToAddr(rcgversionname,&rcgversion_addr);
+    if( status )
+    {
+        fprintf(stderr, "*\nFATAL: The address for the %s variable could not be "
+                        "located in the EPICS database. \n", rcgversionname);
+        fprintf(stderr, "Failure with first lookup in the EPICS database, "
+                        "this usually signifies an error with the .cmd passed to iocsh(), "
+                        "or a misconfigured EPCS environment.\n*\n");
+        return -1;
+    }
 	status = dbPutField(&rcgversion_addr,DBR_DOUBLE,&myversion,1);
 
 	// Create BURT/SDF EPICS channel names
-- 
GitLab