RmcProxyTcpIp.cpp 8.3 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
#include "h/Castor_limits.h"
29
#include "h/rmc_constants.h"
30
31
32
33

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

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
43
castor::legacymsg::RmcProxyTcpIp::~RmcProxyTcpIp() throw() {
Steven Murray's avatar
Steven Murray committed
44
}
45

46
47
48
49
50
51
52
53
54
//------------------------------------------------------------------------------
// 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);
}

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

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

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

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

104
105
106
107
108
109
110
111
112
//------------------------------------------------------------------------------
// 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
113
114
115
//-----------------------------------------------------------------------------
// connectToRmc
//-----------------------------------------------------------------------------
116
int castor::legacymsg::RmcProxyTcpIp::connectToRmc(const std::string &rmcHost)
117
  const {
Steven Murray's avatar
Steven Murray committed
118
119
  castor::utils::SmartFd smartConnectSock;
  try {
120
    smartConnectSock.reset(io::connectWithTimeout(rmcHost, m_rmcPort,
121
      m_netTimeout));
Steven Murray's avatar
Steven Murray committed
122
  } catch(castor::exception::Exception &ne) {
123
    castor::exception::Exception ex;
124
125
    ex.getMessage() << "Failed to connect to rmcd: rmcHost=" << rmcHost
      << " rmcPort=" << RMC_PORT << ": " << ne.getMessage().str();
Steven Murray's avatar
Steven Murray committed
126
127
128
129
130
    throw ex;
  }

  return smartConnectSock.release();
}
131
132
133
134

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

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

//-----------------------------------------------------------------------------
// readRmcMsgHeader
//-----------------------------------------------------------------------------
153
154
castor::legacymsg::MessageHeader
  castor::legacymsg::RmcProxyTcpIp::readRmcMsgHeader(const int fd) {
155
  char buf[12]; // Magic + type + len
156
  MessageHeader header;
157
158
159
160

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

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

  if(RMC_MAGIC != header.magic) {
172
    castor::exception::Exception ex;
173
174
175
176
177
178
179
180
181
182
183
184
    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
//-----------------------------------------------------------------------------
185
186
void castor::legacymsg::RmcProxyTcpIp::writeRmcUnmountMsg(const int fd,
  const RmcUnmountMsgBody &body) {
187
  char buf[RMC_MSGBUFSIZ];
188
  const size_t len = marshal(buf, body);
189
190
191
192

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

//-----------------------------------------------------------------------------
// rmcReplyTypeToStr
//-----------------------------------------------------------------------------
std::string castor::legacymsg::RmcProxyTcpIp::rmcReplyTypeToStr(
  const int replyType) {
  std::ostringstream oss;
  switch(replyType) {
207
208
209
210
211
212
213
214
  case RMC_RC:
    oss << "RMC_RC";
    break;
  case MSG_ERR:
    oss << "MSG_ERR";
    break;
  default:
    oss << "UNKNOWN(0x" << std::hex << replyType << ")";
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  }
  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;
}