From ba403e302263e9ff486da328b508658ac59ab63b Mon Sep 17 00:00:00 2001
From: Steven Murray <Steven.Murray@cern.ch>
Date: Wed, 27 Sep 2017 14:38:44 +0200
Subject: [PATCH] setProcessCapabilities() now called at start of
 DataTransferSession::execute()

---
 .../tapeserver/daemon/DataTransferSession.cpp | 23 +++++++++++++++++++
 .../tapeserver/daemon/DataTransferSession.hpp |  7 ++++++
 .../daemon/TapeReadSingleThread.cpp           |  7 +-----
 .../daemon/TapeSingleThreadInterface.hpp      | 16 -------------
 .../daemon/TapeWriteSingleThread.cpp          |  7 +-----
 5 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/tapeserver/castor/tape/tapeserver/daemon/DataTransferSession.cpp b/tapeserver/castor/tape/tapeserver/daemon/DataTransferSession.cpp
index 0392653e67..a912740c7a 100644
--- a/tapeserver/castor/tape/tapeserver/daemon/DataTransferSession.cpp
+++ b/tapeserver/castor/tape/tapeserver/daemon/DataTransferSession.cpp
@@ -69,6 +69,27 @@ castor::tape::tapeserver::daemon::DataTransferSession::DataTransferSession(
     {
 }
 
+//------------------------------------------------------------------------------
+// setProcessCapabilities
+//------------------------------------------------------------------------------
+/**
+ * This function will try to set the cap_sys_rawio capability that is needed
+ * for by tape thread to access /dev/nst
+ */
+void castor::tape::tapeserver::daemon::DataTransferSession::setProcessCapabilities(
+  const std::string &capabilities){
+  cta::log::LogContext lc(m_log);
+  try {
+    m_capUtils.setProcText(capabilities);
+    cta::log::LogContext::ScopedParam sp(lc,
+      cta::log::Param("capabilities", m_capUtils.getProcText()));
+    lc.log(cta::log::INFO, "Set process capabilities for using tape");
+  } catch(const cta::exception::Exception &ne) {
+    lc.log(cta::log::ERR,
+      "Failed to set process capabilities for using the tape ");
+  }
+}
+
 //------------------------------------------------------------------------------
 //DataTransferSession::execute
 //------------------------------------------------------------------------------
@@ -87,6 +108,8 @@ castor::tape::tapeserver::daemon::Session::EndOfSessionAction
   // Create a sticky thread name, which will be overridden by the other threads
   lc.pushOrReplace(cta::log::Param("thread", "MainThread"));
   lc.pushOrReplace(cta::log::Param("unitName", m_driveConfig.unitName));
+
+  setProcessCapabilities("cap_sys_rawio+ep");
   
   // 2a) Determine if we want to mount at all (for now)
   // This variable will allow us to see if we switched from down to up and start a
diff --git a/tapeserver/castor/tape/tapeserver/daemon/DataTransferSession.hpp b/tapeserver/castor/tape/tapeserver/daemon/DataTransferSession.hpp
index 0a3b99a190..f8f4956472 100644
--- a/tapeserver/castor/tape/tapeserver/daemon/DataTransferSession.hpp
+++ b/tapeserver/castor/tape/tapeserver/daemon/DataTransferSession.hpp
@@ -88,6 +88,13 @@ namespace daemon {
      */
     EndOfSessionAction execute();
 
+    /**
+     * Sets the capabilities of the process and logs the result.
+     *
+     * @param capabilities The string representation of the capabilities.
+     */
+    void setProcessCapabilities(const std::string &capabilities);
+
     /** Temporary method used for debugging while building the session class */
     std::string getVid() { return m_volInfo.vid; }
     
diff --git a/tapeserver/castor/tape/tapeserver/daemon/TapeReadSingleThread.cpp b/tapeserver/castor/tape/tapeserver/daemon/TapeReadSingleThread.cpp
index 7365ca34f7..7c5f91e6eb 100644
--- a/tapeserver/castor/tape/tapeserver/daemon/TapeReadSingleThread.cpp
+++ b/tapeserver/castor/tape/tapeserver/daemon/TapeReadSingleThread.cpp
@@ -205,12 +205,8 @@ const char *castor::tape::tapeserver::daemon::TapeReadSingleThread::
 void castor::tape::tapeserver::daemon::TapeReadSingleThread::run() {
   m_logContext.pushOrReplace(cta::log::Param("thread", "TapeRead"));
   cta::utils::Timer timer, totalTimer;
-  std::string currentErrorToCount = "Error_setCapabilities";
+  std::string currentErrorToCount = "Error_tapeMountForRead";
   try{
-    // Set capabilities allowing rawio (and hence arbitrary SCSI commands)
-    // through the st driver file descriptor.
-    setCapabilities();
-    
     // Report the parameters of the session to the main thread
     typedef cta::log::Param Param;
     m_watchdog.addParameter(Param("TPVID", m_volInfo.vid));
@@ -227,7 +223,6 @@ void castor::tape::tapeserver::daemon::TapeReadSingleThread::run() {
       // will also take care of the TapeServerReporter and of RecallTaskInjector
       TapeCleaning tapeCleaner(*this, timer);
       // Before anything, the tape should be mounted
-      currentErrorToCount = "Error_tapeMountForRead";
       m_rrp.reportDriveStatus(cta::common::dataStructures::DriveStatus::Mounting);
       mountTapeReadOnly();
       currentErrorToCount = "Error_tapeLoad";
diff --git a/tapeserver/castor/tape/tapeserver/daemon/TapeSingleThreadInterface.hpp b/tapeserver/castor/tape/tapeserver/daemon/TapeSingleThreadInterface.hpp
index 276bcb2128..7ccc635b1f 100644
--- a/tapeserver/castor/tape/tapeserver/daemon/TapeSingleThreadInterface.hpp
+++ b/tapeserver/castor/tape/tapeserver/daemon/TapeSingleThreadInterface.hpp
@@ -95,22 +95,6 @@ protected:
   /** Encryption helper object */
   EncryptionControl m_encryptionControl;
  
-  /**
-   * This function will try to set the cap_sys_rawio capability that is needed
-   * for by tape thread to access /dev/nst
-   */
-  void setCapabilities(){
-    try {
-      m_capUtils.setProcText("cap_sys_rawio+ep");
-      cta::log::LogContext::ScopedParam sp(m_logContext,
-        cta::log::Param("capabilities", m_capUtils.getProcText()));
-      m_logContext.log(cta::log::INFO, "Set process capabilities for using tape");
-    } catch(const cta::exception::Exception &ne) {
-      m_logContext.log(cta::log::ERR,
-        "Failed to set process capabilities for using the tape ");
-    }
-  }
-  
   /**
    * Try to mount the tape for read-only access, get an exception if it fails 
    */
diff --git a/tapeserver/castor/tape/tapeserver/daemon/TapeWriteSingleThread.cpp b/tapeserver/castor/tape/tapeserver/daemon/TapeWriteSingleThread.cpp
index 00a949be0f..edffd1605f 100644
--- a/tapeserver/castor/tape/tapeserver/daemon/TapeWriteSingleThread.cpp
+++ b/tapeserver/castor/tape/tapeserver/daemon/TapeWriteSingleThread.cpp
@@ -272,13 +272,9 @@ void castor::tape::tapeserver::daemon::TapeWriteSingleThread::run() {
   // process we're in, and to count the error if it occurs.
   // We will not record errors for an empty string. This will allow us to
   // prevent counting where error happened upstream.
-  std::string currentErrorToCount = "Error_setCapabilities";
+  std::string currentErrorToCount = "Error_tapeMountForWrite";
   try
   {
-    // Set capabilities allowing rawio (and hence arbitrary SCSI commands)
-    // through the st driver file descriptor.
-    setCapabilities();
-    
     // Report the parameters of the session to the main thread
     typedef cta::log::Param Param;
     m_watchdog.addParameter(Param("TPVID", m_volInfo.vid));
@@ -298,7 +294,6 @@ void castor::tape::tapeserver::daemon::TapeWriteSingleThread::run() {
       // will also take care of the TapeServerReporter
       // 
       TapeCleaning cleaner(*this, timer);
-      currentErrorToCount = "Error_tapeMountForWrite";
       m_reportPacker.reportDriveStatus(cta::common::dataStructures::DriveStatus::Mounting, m_logContext);
       // Before anything, the tape should be mounted
       // This call does the logging of the mount
-- 
GitLab