RmcProxyTcpIp.cpp 8.39 KB
Newer Older
Steven Murray's avatar
Steven Murray committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/******************************************************************************
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * 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 2
 * of the License, or (at your option) any later version.
 * 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.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
19
 * @author Castor Dev team, castor-dev@cern.ch
Steven Murray's avatar
Steven Murray committed
20
21
 *****************************************************************************/

Steven Murray's avatar
Steven Murray committed
22
#include "castor/io/io.hpp"
23
24
#include "castor/legacymsg/CommonMarshal.hpp"
#include "castor/legacymsg/RmcMarshal.hpp"
25
#include "castor/legacymsg/RmcProxyTcpIp.hpp"
Steven Murray's avatar
Steven Murray committed
26
#include "castor/utils/SmartFd.hpp"
27
#include "castor/utils/utils.hpp"
28
29
30
#include "h/rmc_constants.h"

#include <shift/Castor_limits.h>
31
32
33
34

//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
35
36
37
38
castor::legacymsg::RmcProxyTcpIp::RmcProxyTcpIp(
  const unsigned short rmcPort,
  const int netTimeout,
  const unsigned int maxRqstAttempts) throw():
39
  m_rmcPort(rmcPort),
40
41
  m_netTimeout(netTimeout),
  m_maxRqstAttempts(maxRqstAttempts) {
42
} 
Steven Murray's avatar
Steven Murray committed
43
44
45
46

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
47
castor::legacymsg::RmcProxyTcpIp::~RmcProxyTcpIp() throw() {
Steven Murray's avatar
Steven Murray committed
48
}
49

50
51
52
53
54
55
56
57
58
//------------------------------------------------------------------------------
// mountTapeReadOnly
//------------------------------------------------------------------------------
void castor::legacymsg::RmcProxyTcpIp::mountTapeReadOnly(
  const std::string &vid, const mediachanger::ScsiLibrarySlot &librarySlot) {
  // SCSI libraries do not support read-only mounts
  mountTapeReadWrite(vid, librarySlot);
}

59
60
61
62
//------------------------------------------------------------------------------
// mountTapeReadWrite
//------------------------------------------------------------------------------
void castor::legacymsg::RmcProxyTcpIp::mountTapeReadWrite(
63
  const std::string &vid, const mediachanger::ScsiLibrarySlot &librarySlot) {
64
  try {
65
66
67
68
    RmcMountMsgBody rqstBody;
    rqstBody.uid = geteuid();
    rqstBody.gid = getegid();
    castor::utils::copyString(rqstBody.vid, vid);
69
    rqstBody.drvOrd = librarySlot.getDrvOrd();
70

71
    rmcSendRecvNbAttempts(m_maxRqstAttempts, librarySlot.getRmcHostName(),
72
      rqstBody);
73
  } catch(castor::exception::Exception &ne) {
74
    castor::exception::Exception ex;
75
76
77
    ex.getMessage() <<
      "Failed to mount tape in SCSI tape-library for read/write access"
      ": vid=" << vid << " librarySlot=" << librarySlot.str() << ": " <<
78
      ne.getMessage().str();
79
80
    throw ex;
  }
81
82
83
}

//------------------------------------------------------------------------------
84
// dismountTape
85
//------------------------------------------------------------------------------
86
void castor::legacymsg::RmcProxyTcpIp::dismountTape(const std::string &vid,
87
  const mediachanger::ScsiLibrarySlot &librarySlot) {
88
  try {
89
90
91
92
    RmcUnmountMsgBody rqstBody;
    rqstBody.uid = geteuid();
    rqstBody.gid = getegid();
    castor::utils::copyString(rqstBody.vid, vid);
93
    rqstBody.drvOrd = librarySlot.getDrvOrd();
94
95
    rqstBody.force = 0;

96
    rmcSendRecvNbAttempts(m_maxRqstAttempts, librarySlot.getRmcHostName(),
97
      rqstBody);
98
  } catch(castor::exception::Exception &ne) {
99
    castor::exception::Exception ex;
100
101
102
    ex.getMessage() <<
      "Failed to dismount tape in SCSI tape-library"
      ": vid=" << vid << " librarySlot=" << librarySlot.str() << ": " <<
103
      ne.getMessage().str();
104
105
    throw ex;
  }
106
}
Steven Murray's avatar
Steven Murray committed
107

108
109
110
111
112
113
114
115
116
//------------------------------------------------------------------------------
// forceDismountTape
//------------------------------------------------------------------------------
void castor::legacymsg::RmcProxyTcpIp::forceDismountTape(const std::string &vid,
  const mediachanger::ScsiLibrarySlot &librarySlot) {
  // SCSI libraries do not support forced dismounts
  dismountTape(vid, librarySlot);
}

Steven Murray's avatar
Steven Murray committed
117
118
119
//-----------------------------------------------------------------------------
// connectToRmc
//-----------------------------------------------------------------------------
120
int castor::legacymsg::RmcProxyTcpIp::connectToRmc(const std::string &rmcHost)
121
  const {
Steven Murray's avatar
Steven Murray committed
122
123
  castor::utils::SmartFd smartConnectSock;
  try {
124
    smartConnectSock.reset(io::connectWithTimeout(rmcHost, m_rmcPort,
125
      m_netTimeout));
Steven Murray's avatar
Steven Murray committed
126
  } catch(castor::exception::Exception &ne) {
127
    castor::exception::Exception ex;
128
129
    ex.getMessage() << "Failed to connect to rmcd: rmcHost=" << rmcHost
      << " rmcPort=" << RMC_PORT << ": " << ne.getMessage().str();
Steven Murray's avatar
Steven Murray committed
130
131
132
133
134
    throw ex;
  }

  return smartConnectSock.release();
}
135
136
137
138

//-----------------------------------------------------------------------------
// writeRmcMountMsg
//-----------------------------------------------------------------------------
139
140
void castor::legacymsg::RmcProxyTcpIp::writeRmcMountMsg(const int fd,
  const RmcMountMsgBody &body) {
141
  char buf[RMC_MSGBUFSIZ];
142
  const size_t len = marshal(buf, body);
143
144
145
146

  try {
    io::writeBytes(fd, m_netTimeout, len, buf);
  } catch(castor::exception::Exception &ne) {
147
    castor::exception::Exception ex;
148
    ex.getMessage() << "Failed to write RMC_SCSI_MOUNT message: "
149
150
151
152
      << ne.getMessage().str();
    throw ex;
  }
}
153
154
155
156

//-----------------------------------------------------------------------------
// readRmcMsgHeader
//-----------------------------------------------------------------------------
157
158
castor::legacymsg::MessageHeader
  castor::legacymsg::RmcProxyTcpIp::readRmcMsgHeader(const int fd) {
159
  char buf[12]; // Magic + type + len
160
  MessageHeader header;
161
162
163
164

  try {
    io::readBytes(fd, m_netTimeout, sizeof(buf), buf);
  } catch(castor::exception::Exception &ne) {
165
    castor::exception::Exception ex;
166
167
168
169
170
171
172
    ex.getMessage() << "Failed to read message header: "
      << ne.getMessage().str();
    throw ex;
  }

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

  if(RMC_MAGIC != header.magic) {
176
    castor::exception::Exception ex;
177
178
179
180
181
182
183
184
185
186
187
188
    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;
}

//-----------------------------------------------------------------------------
// writeRmcUnmountMsg
//-----------------------------------------------------------------------------
189
190
void castor::legacymsg::RmcProxyTcpIp::writeRmcUnmountMsg(const int fd,
  const RmcUnmountMsgBody &body) {
191
  char buf[RMC_MSGBUFSIZ];
192
  const size_t len = marshal(buf, body);
193
194
195
196

  try {
    io::writeBytes(fd, m_netTimeout, len, buf);
  } catch(castor::exception::Exception &ne) {
197
    castor::exception::Exception ex;
198
    ex.getMessage() << "Failed to write RMC_SCSI_UNMOUNT message: "
199
200
201
202
      << ne.getMessage().str();
    throw ex;
  }
}
203
204
205
206
207
208
209
210

//-----------------------------------------------------------------------------
// rmcReplyTypeToStr
//-----------------------------------------------------------------------------
std::string castor::legacymsg::RmcProxyTcpIp::rmcReplyTypeToStr(
  const int replyType) {
  std::ostringstream oss;
  switch(replyType) {
211
212
213
214
215
216
217
218
  case RMC_RC:
    oss << "RMC_RC";
    break;
  case MSG_ERR:
    oss << "MSG_ERR";
    break;
  default:
    oss << "UNKNOWN(0x" << std::hex << replyType << ")";
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  }
  return oss.str();
}

//-----------------------------------------------------------------------------
// handleMSG_ERR
//-----------------------------------------------------------------------------
std::string castor::legacymsg::RmcProxyTcpIp::handleMSG_ERR(
  const MessageHeader &header,
  const int fd) {
  char errorBuf[1024];
  const int nbBytesToRead = header.lenOrStatus > sizeof(errorBuf) ?
    sizeof(errorBuf) : header.lenOrStatus;
  io::readBytes(fd, m_netTimeout, nbBytesToRead, errorBuf);
  errorBuf[sizeof(errorBuf) - 1] = '\0';
  return errorBuf;
}