RmcProxy.cpp 6.29 KB
Newer Older
1
2
3
/*
 * The CERN Tape Archive(CTA) project
 * Copyright(C) 2015  CERN
4
 *
5
6
7
8
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *(at your option) any later version.
9
10
11
12
13
14
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
15
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
18
19
20
21
22
23

#include "common/utils/utils.hpp"
#include "common/SmartFd.hpp"
#include "mediachanger/CommonMarshal.hpp"
#include "mediachanger/io.hpp"
#include "mediachanger/RmcMarshal.hpp"
Steven Murray's avatar
Steven Murray committed
24
#include "mediachanger/RmcProxy.hpp"
25
#include "mediachanger/ScsiLibrarySlot.hpp"
26
27
28
29
30
31
32

namespace cta {
namespace mediachanger {

//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
Steven Murray's avatar
Steven Murray committed
33
RmcProxy::RmcProxy(
34
35
  const unsigned short rmcPort,
  const int netTimeout,
36
  const unsigned int maxRqstAttempts):
37
38
39
40
41
42
43
44
  m_rmcPort(rmcPort),
  m_netTimeout(netTimeout),
  m_maxRqstAttempts(maxRqstAttempts) {
} 

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
45
RmcProxy::~RmcProxy() {
46
47
48
49
50
}

//------------------------------------------------------------------------------
// mountTapeReadOnly
//------------------------------------------------------------------------------
Steven Murray's avatar
Steven Murray committed
51
void RmcProxy::mountTapeReadOnly(const std::string &vid, const LibrarySlot &librarySlot) {
52
53
54
55
56
57
58
  // SCSI libraries do not support read-only mounts
  mountTapeReadWrite(vid, librarySlot);
}

//------------------------------------------------------------------------------
// mountTapeReadWrite
//------------------------------------------------------------------------------
Steven Murray's avatar
Steven Murray committed
59
void RmcProxy::mountTapeReadWrite(const std::string &vid, const LibrarySlot &librarySlot) {
60
61
62
63
64
  try {
    RmcMountMsgBody rqstBody;
    rqstBody.uid = geteuid();
    rqstBody.gid = getegid();
    utils::copyString(rqstBody.vid, vid);
65
66
    const ScsiLibrarySlot &scsiLibrarySlot = dynamic_cast<const ScsiLibrarySlot&>(librarySlot);
    rqstBody.drvOrd = scsiLibrarySlot.getDrvOrd();
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

    rmcSendRecvNbAttempts(m_maxRqstAttempts, rqstBody);
  } catch(cta::exception::Exception &ne) {
    cta::exception::Exception ex;
    ex.getMessage() <<
      "Failed to mount tape in SCSI tape-library for read/write access"
      ": vid=" << vid << " librarySlot=" << librarySlot.str() << ": " <<
      ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
// dismountTape
//------------------------------------------------------------------------------
Steven Murray's avatar
Steven Murray committed
82
void RmcProxy::dismountTape(const std::string &vid, const LibrarySlot &librarySlot) {
83
84
85
86
87
  try {
    RmcUnmountMsgBody rqstBody;
    rqstBody.uid = geteuid();
    rqstBody.gid = getegid();
    utils::copyString(rqstBody.vid, vid);
88
89
    const ScsiLibrarySlot &scsiLibrarySlot = dynamic_cast<const ScsiLibrarySlot&>(librarySlot);
    rqstBody.drvOrd = scsiLibrarySlot.getDrvOrd();
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    rqstBody.force = 0;

    rmcSendRecvNbAttempts(m_maxRqstAttempts, rqstBody);
  } catch(cta::exception::Exception &ne) {
    cta::exception::Exception ex;
    ex.getMessage() <<
      "Failed to dismount tape in SCSI tape-library"
      ": vid=" << vid << " librarySlot=" << librarySlot.str() << ": " <<
      ne.getMessage().str();
    throw ex;
  }
}

//-----------------------------------------------------------------------------
// connectToRmc
//-----------------------------------------------------------------------------
Steven Murray's avatar
Steven Murray committed
106
int RmcProxy::connectToRmc()
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  const {
  const std::string rmcHost = "localhost";
  cta::SmartFd smartConnectSock;
  try {
    smartConnectSock.reset(connectWithTimeout(rmcHost, m_rmcPort,
      m_netTimeout));
  } catch(cta::exception::Exception &ne) {
    cta::exception::Exception ex;
    ex.getMessage() << "Failed to connect to rmcd: rmcHost=" << rmcHost
      << " rmcPort=" << RMC_PORT << ": " << ne.getMessage().str();
    throw ex;
  }

  return smartConnectSock.release();
}

//-----------------------------------------------------------------------------
// readRmcMsgHeader
//-----------------------------------------------------------------------------
Steven Murray's avatar
Steven Murray committed
126
MessageHeader RmcProxy::readRmcMsgHeader(const int fd) {
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  char buf[12]; // Magic + type + len
  MessageHeader header;

  try {
    readBytes(fd, m_netTimeout, sizeof(buf), buf);
  } catch(cta::exception::Exception &ne) {
    cta::exception::Exception ex;
    ex.getMessage() << "Failed to read message header: "
      << ne.getMessage().str();
    throw ex;
  }

  const char *bufPtr = buf;
  size_t bufLen = sizeof(buf);
  unmarshal(bufPtr, bufLen, header);

  if(RMC_MAGIC != header.magic) {
    cta::exception::Exception ex;
    ex.getMessage() << "Failed to read message header: "
      " Header contains an invalid magic number: expected=0x" << std::hex <<
      RMC_MAGIC << " actual=0x" << header.magic;
    throw ex;
  }

  return header;
}

//-----------------------------------------------------------------------------
// rmcReplyTypeToStr
//-----------------------------------------------------------------------------
Steven Murray's avatar
Steven Murray committed
157
std::string RmcProxy::rmcReplyTypeToStr(const int replyType) {
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  std::ostringstream oss;
  switch(replyType) {
  case RMC_RC:
    oss << "RMC_RC";
    break;
  case MSG_ERR:
    oss << "MSG_ERR";
    break;
  default:
    oss << "UNKNOWN(0x" << std::hex << replyType << ")";
  }
  return oss.str();
}

//-----------------------------------------------------------------------------
// handleMSG_ERR
//-----------------------------------------------------------------------------
Steven Murray's avatar
Steven Murray committed
175
std::string RmcProxy::handleMSG_ERR(const MessageHeader &header, const int fd) {
176
177
178
179
180
181
182
183
184
185
  char errorBuf[1024];
  const int nbBytesToRead = header.lenOrStatus > sizeof(errorBuf) ?
    sizeof(errorBuf) : header.lenOrStatus;
  readBytes(fd, m_netTimeout, nbBytesToRead, errorBuf);
  errorBuf[sizeof(errorBuf) - 1] = '\0';
  return errorBuf;
}

} // namespace mediachanger
} // namespace cta