From de12ebe8e3c0e95b83a23a022b84ee0211d253ae Mon Sep 17 00:00:00 2001
From: Michael Davis <michael.davis@cern.ch>
Date: Thu, 13 Apr 2023 15:17:47 +0200
Subject: [PATCH] Resolve "cta-rmcd issues if `/dev/sg0` is missing"

---
 ReleaseNotes.md                               |  6 ++++++
 .../castorrmc/rmc/rmc_send_scsi_cmd.c         | 21 +++++++------------
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/ReleaseNotes.md b/ReleaseNotes.md
index 346437a58f..b821295493 100644
--- a/ReleaseNotes.md
+++ b/ReleaseNotes.md
@@ -1,3 +1,9 @@
+# v4.NEXT
+
+### Features
+### Bug Fixes
+- cta/CTA#259 - cta-rmcd should not exit if /dev/sg0 is missing
+
 # v4.8.7-1
 
 ### Features
diff --git a/mediachanger/castorrmc/rmc/rmc_send_scsi_cmd.c b/mediachanger/castorrmc/rmc/rmc_send_scsi_cmd.c
index ba1106fd39..d453c7085a 100644
--- a/mediachanger/castorrmc/rmc/rmc_send_scsi_cmd.c
+++ b/mediachanger/castorrmc/rmc/rmc_send_scsi_cmd.c
@@ -164,7 +164,6 @@ int rmc_send_scsi_cmd (
 	int n;
 	int resid = 0;
 	struct stat sbuf;
-	struct stat sbufa;
 	static char *sg_buffer;
 	static int sg_bufsiz = 0;
 	struct sg_header *sg_hd;
@@ -220,19 +219,15 @@ int rmc_send_scsi_cmd (
 			return (-1);
 		}
 
-                /* get the major device ID of the sg devices ... */
-		if (stat ("/dev/sg0", &sbufa) < 0) {
-			serrno = errno;
-			snprintf (rmc_err_msgbuf, sizeof(rmc_err_msgbuf), "/dev/sg0 : stat error : %s\n", strerror(errno));
-			rmc_err_msgbuf[sizeof(rmc_err_msgbuf) - 1] = '\0';
-			*msgaddr = rmc_err_msgbuf;
-			return (-1);
-		}
-                /* ... to detect links and use the path directly! */
-		if (major(sbuf.st_rdev) == major(sbufa.st_rdev)) {
-			strcpy (sgpath, path);
+		struct stat sbufa;
+		if (stat("/dev/sg0", &sbufa) == 0 && major(sbuf.st_rdev) == major(sbufa.st_rdev)) {
+		  /* If the major device ID of the specified device is the same as the major device ID of sg0,
+                   * we can use the path directly */
+		  strcpy(sgpath, path);
 		} else {
-                        find_sgpath(sgpath, major(sbuf.st_rdev), minor(sbuf.st_rdev));
+		  /* Otherwise, look up the path using the (major,minor) device ID. If no match is found,
+                   * sgpath is set to an empty string */
+		  find_sgpath(sgpath, major(sbuf.st_rdev), minor(sbuf.st_rdev));
 		}
 
 		if ((fd = open (sgpath, O_RDWR)) < 0) {
-- 
GitLab