AdminConnectionHandler.cpp 15.3 KB
Newer Older
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
20
21
22
23
24
25
26
27
28
29
30
31
32
 *****************************************************************************/

#include "castor/exception/BadAlloc.hpp"
#include "castor/exception/Errnum.hpp"
#include "castor/io/io.hpp"
#include "castor/tape/tapeserver/daemon/AdminConnectionHandler.hpp"
#include "castor/utils/SmartFd.hpp"
#include "h/common.h"
#include "h/serrno.h"
#include "h/Ctape.h"
#include "h/vdqm_api.h"
#include "castor/legacymsg/CommonMarshal.hpp"
#include "castor/legacymsg/TapeMarshal.hpp"
Daniele Kruse's avatar
Daniele Kruse committed
33
#include "castor/legacymsg/legacymsg.hpp"
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "castor/tape/utils/utils.hpp"
#include "castor/utils/utils.hpp"

#include <errno.h>
#include <memory>
#include <string.h>
#include <list>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::tape::tapeserver::daemon::AdminConnectionHandler::AdminConnectionHandler(
  const int fd,
50
  reactor::ZMQReactor &reactor,
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  log::Logger &log,
  legacymsg::VdqmProxy &vdqm,
  DriveCatalogue &driveCatalogue,
  const std::string &hostName)
  throw():
    m_fd(fd),
    m_reactor(reactor),
    m_log(log),
    m_vdqm(vdqm),
    m_driveCatalogue(driveCatalogue),
    m_hostName(hostName),
    m_netTimeout(1) { // Timeout in seconds
}

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
68
69
castor::tape::tapeserver::daemon::AdminConnectionHandler::
  ~AdminConnectionHandler() throw() {
70
71
72
73
74
  log::Param params[] = {log::Param("fd", m_fd)};
  m_log(LOG_DEBUG, "Closing admin connection", params);
  close(m_fd);
}

Steven Murray's avatar
Steven Murray committed
75
76
77
78
79
80
81
//------------------------------------------------------------------------------
// getName
//------------------------------------------------------------------------------
std::string castor::tape::tapeserver::daemon::AdminConnectionHandler::getName() 
  const throw() {
  return "AdminConnectionHandler";
}
82
83
84
85
86

//------------------------------------------------------------------------------
// fillPollFd
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::AdminConnectionHandler::fillPollFd(
Steven Murray's avatar
Steven Murray committed
87
  zmq_pollitem_t &fd) throw() {
88
  fd.fd = m_fd;
Steven Murray's avatar
Steven Murray committed
89
  fd.events = ZMQ_POLLIN;
90
  fd.revents = 0;
David COME's avatar
David COME committed
91
  fd.socket = NULL;
92
93
94
95
96
97
}

//------------------------------------------------------------------------------
// handleEvent
//------------------------------------------------------------------------------
bool castor::tape::tapeserver::daemon::AdminConnectionHandler::handleEvent(
Steven Murray's avatar
Steven Murray committed
98
  const zmq_pollitem_t &fd) {
99
100
101
102
  logAdminConnectionEvent(fd);

  checkHandleEventFd(fd.fd);

103
104
105
  std::list<log::Param> params;
  params.push_back(log::Param("fd", m_fd));

106
107

  try {
108
    const legacymsg::MessageHeader header = readMsgHeader();
109
    handleRequest(header);
Daniele Kruse's avatar
Daniele Kruse committed
110
111
112
113
114
115
116
117
118
119
120
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to handle IO event on admin connection. Reason: " << ne.getMessage().str() << std::endl;    
    m_log(LOG_ERR, ex.getMessage().str());
    try {
      castor::legacymsg::writeTapeReplyErrorMsg(m_netTimeout, m_fd, ex.getMessage().str());
    } catch(castor::exception::Exception &ne2) {
      castor::exception::Exception ex2;
      ex2.getMessage() << "Failed to notify the client of the failure of the handling of the IO event on admin connection. Reason: " << ne2.getMessage().str();
      m_log(LOG_ERR, ex2.getMessage().str());
    }
121
122
  }

123
124
  m_log(LOG_DEBUG, "Asking reactor to remove and delete"
    " AdminConnectionHandler", params);
125
126
127
128
129
130
131
  return true; // Ask reactor to remove and delete this handler
}

//------------------------------------------------------------------------------
// logAdminConnectionEvent
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::AdminConnectionHandler::
Steven Murray's avatar
Steven Murray committed
132
133
134
135
136
137
  logAdminConnectionEvent(const zmq_pollitem_t &fd)  {
  log::Param params[] = {
  log::Param("fd", fd.fd),
  log::Param("ZMQ_POLLIN", fd.revents & ZMQ_POLLIN ? "true" : "false"),
  log::Param("ZMQ_POLLOUT", fd.revents & ZMQ_POLLOUT ? "true" : "false"),
  log::Param("ZMQ_POLLERR", fd.revents & ZMQ_POLLERR ? "true" : "false")};
138
139
140
141
142
143
144
145
146
147
  m_log(LOG_DEBUG, "I/O event on admin connection", params);
}

//------------------------------------------------------------------------------
// checkHandleEventFd
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::AdminConnectionHandler::
  checkHandleEventFd(const int fd)  {
  if(m_fd != fd) {
    castor::exception::Exception ex;
148
149
    ex.getMessage() <<
      "AdminConnectionHandler passed wrong file descriptor"
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
      ": expected=" << m_fd << " actual=" << fd;
    throw ex;
  }
}

//------------------------------------------------------------------------------
// handleRequest
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::AdminConnectionHandler::handleRequest(
  const legacymsg::MessageHeader &header) {
  switch(header.reqType) {
  case TPCONF:
    handleConfigRequest(header);
    break;
  case TPSTAT:
    handleStatRequest(header);
    break;
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to handle request: Unknown request type"
        ": reqType=" << header.reqType;
      throw ex;
    }
  }
}

//------------------------------------------------------------------------------
// handleConfigRequest
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::AdminConnectionHandler::
  handleConfigRequest(const legacymsg::MessageHeader &header) {
  const uint32_t totalLen = header.lenOrStatus;
  const uint32_t headerLen = 3 * sizeof(uint32_t); // magic, type and len
  const uint32_t bodyLen = totalLen - headerLen;
  const legacymsg::TapeConfigRequestMsgBody body =
    readTapeConfigMsgBody(bodyLen);
  logTapeConfigJobReception(body);

  const std::string unitName(body.drive);
Daniele Kruse's avatar
Daniele Kruse committed
190
  
191
192
  DriveCatalogueEntry &drive = m_driveCatalogue.findDrive(unitName);
  const utils::DriveConfig &driveConfig = drive.getConfig();
193
194
195
196
197
198
199
200

  log::Param params[] = {
    log::Param("unitName", unitName),
    log::Param("dgn", driveConfig.dgn)};

  switch(body.status) {
  case CONF_UP:
    m_vdqm.setDriveUp(m_hostName, unitName, driveConfig.dgn);
201
    drive.configureUp();
202
203
204
205
    m_log(LOG_INFO, "Drive configured up", params);
    break;
  case CONF_DOWN:
    m_vdqm.setDriveDown(m_hostName, unitName, driveConfig.dgn);
206
    drive.configureDown();
207
208
209
210
211
    m_log(LOG_INFO, "Drive configured down", params);
    break;
  default:
    {
      castor::exception::Exception ex;
212
213
      ex.getMessage() << "Failed to configure drive"
        ": Unexpected drive status requested: status=" << body.status;
214
215
216
      throw ex;
    }
  }
Daniele Kruse's avatar
Daniele Kruse committed
217
  
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  writeTapeRcReplyMsg(0); // Return code of 0 means success
}

//------------------------------------------------------------------------------
// readTapeConfigMsgBody
//------------------------------------------------------------------------------
castor::legacymsg::TapeConfigRequestMsgBody castor::tape::tapeserver::daemon::
  AdminConnectionHandler::readTapeConfigMsgBody(uint32_t bodyLen) {
  char buf[REQBUFSZ];

  if(sizeof(buf) < bodyLen) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to read body of config message"
       ": Maximum body length exceeded"
       ": max=" << sizeof(buf) << " actual=" << bodyLen;
    throw ex;
  }

  try {
    io::readBytes(m_fd, m_netTimeout, bodyLen, buf);
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to read body of config message"
      ": " << ne.getMessage().str();
    throw ex;
  }

  legacymsg::TapeConfigRequestMsgBody body;
  const char *bufPtr = buf;
  size_t bufLen = sizeof(buf);
  legacymsg::unmarshal(bufPtr, bufLen, body);
  return body;
}

//------------------------------------------------------------------------------
// logTapeConfigJobReception
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::AdminConnectionHandler::
  logTapeConfigJobReception(
  const legacymsg::TapeConfigRequestMsgBody &job) const throw() {
  log::Param params[] = {
    log::Param("drive", job.drive),
    log::Param("gid", job.gid),
    log::Param("uid", job.uid),
    log::Param("status", job.status)};
  m_log(LOG_INFO, "Received message from tpconfig command", params);
}

//------------------------------------------------------------------------------
// writeTapeConfigReplyMsg
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::AdminConnectionHandler::
  writeTapeRcReplyMsg(const int rc)  {
  char buf[REPBUFSZ];
  const size_t len = marshalTapeRcReplyMsg(buf, sizeof(buf), rc);
  try {
    io::writeBytes(m_fd, m_netTimeout, len, buf);
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to write job reply message: " <<
      ne.getMessage().str();
    throw ex;
  }
}

//-----------------------------------------------------------------------------
// marshalTapeConfigReplyMsg
//-----------------------------------------------------------------------------
size_t castor::tape::tapeserver::daemon::AdminConnectionHandler::
  marshalTapeRcReplyMsg(char *const dst, const size_t dstLen, const int rc) {
  legacymsg::MessageHeader src;
  src.magic = TPMAGIC;
  src.reqType = TAPERC;
  src.lenOrStatus = rc;
  return legacymsg::marshal(dst, dstLen, src);
}

//------------------------------------------------------------------------------
// writeTapeStatReplyMsg
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::AdminConnectionHandler::
  writeTapeStatReplyMsg()  {
  legacymsg::TapeStatReplyMsgBody body;
  
  const std::list<std::string> unitNames = m_driveCatalogue.getUnitNames();
  if(unitNames.size() > CA_MAXNBDRIVES) {
    castor::exception::Exception ex;
    ex.getMessage() << "Too many drives in drive catalogue: " <<
      unitNames.size() << ": max=" << CA_MAXNBDRIVES << " actual=" <<
      unitNames.size();
    throw ex;
  }
  body.number_of_drives = unitNames.size();
  int i=0;
  for(std::list<std::string>::const_iterator itor = unitNames.begin();
    itor!=unitNames.end() and i<CA_MAXNBDRIVES; itor++) {
    const std::string &unitName = *itor;
315
316
    const DriveCatalogueEntry &drive = m_driveCatalogue.findDrive(unitName);
    body.drives[i] = drive.getTapeStatDriveEntry();
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
    i++;
  }
  
  char buf[REPBUFSZ];
  const size_t len = legacymsg::marshal(buf, sizeof(buf), body);
  try {
    io::writeBytes(m_fd, m_netTimeout, len, buf);
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to write job reply message: " <<
      ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
// handleStatRequest
//------------------------------------------------------------------------------
void
  castor::tape::tapeserver::daemon::AdminConnectionHandler::handleStatRequest(
  const legacymsg::MessageHeader &header) {
  const uint32_t totalLen = header.lenOrStatus;
  const uint32_t headerLen = 3 * sizeof(uint32_t); // magic, type and len
  const uint32_t bodyLen = totalLen - headerLen;
  const legacymsg::TapeStatRequestMsgBody body = readTapeStatMsgBody(bodyLen);
  logTapeStatJobReception(body);
  writeTapeStatReplyMsg();
  writeTapeRcReplyMsg(0); // Return value of 0 means success
}

//------------------------------------------------------------------------------
// logTapeStatJobReception
//------------------------------------------------------------------------------
void
  castor::tape::tapeserver::daemon::AdminConnectionHandler::logTapeStatJobReception(
  const legacymsg::TapeStatRequestMsgBody &job) const throw() {
  log::Param params[] = {
    log::Param("gid", job.gid),
    log::Param("uid", job.uid)};
  m_log(LOG_INFO, "Received message from tpstat command", params);
}

//------------------------------------------------------------------------------
// readJobMsgHeader
//------------------------------------------------------------------------------
castor::legacymsg::MessageHeader
363
  castor::tape::tapeserver::daemon::AdminConnectionHandler::readMsgHeader() {
364
365
  // Read in the message header
  char buf[3 * sizeof(uint32_t)]; // magic + request type + len
366
  io::readBytes(m_fd, m_netTimeout, sizeof(buf), buf);
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425

  const char *bufPtr = buf;
  size_t bufLen = sizeof(buf);
  legacymsg::MessageHeader header;
  memset(&header, '\0', sizeof(header));
  legacymsg::unmarshal(bufPtr, bufLen, header);

  if(TPMAGIC != header.magic) {
    castor::exception::Exception ex;
    ex.getMessage() << "Invalid admin job message: Invalid magic"
      ": expected=0x" << std::hex << TPMAGIC << " actual=0x" <<
      header.magic;
    throw ex;
  }

  if(TPCONF != header.reqType and TPSTAT != header.reqType) {
    castor::exception::Exception ex;
    ex.getMessage() << "Invalid admin job message: Invalid request type"
       ": expected= 0x" << std::hex << TPCONF << " or 0x" << TPSTAT << " actual=0x" <<
       header.reqType;
    throw ex;
  }

  // The length of the message body is checked later, just before it is read in
  // to memory

  return header;
}

//------------------------------------------------------------------------------
// readTapeStatMsgBody
//------------------------------------------------------------------------------
castor::legacymsg::TapeStatRequestMsgBody castor::tape::tapeserver::daemon::
  AdminConnectionHandler::readTapeStatMsgBody(const uint32_t bodyLen) {
  char buf[REQBUFSZ];

  if(sizeof(buf) < bodyLen) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to read body of stat message"
       ": Maximum body length exceeded"
       ": max=" << sizeof(buf) << " actual=" << bodyLen;
    throw ex;
  }

  try {
    io::readBytes(m_fd, m_netTimeout, bodyLen, buf);
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to read body of stat message"
      ": " << ne.getMessage().str();
    throw ex;
  }

  legacymsg::TapeStatRequestMsgBody body;
  const char *bufPtr = buf;
  size_t bufLen = sizeof(buf);
  legacymsg::unmarshal(bufPtr, bufLen, body);
  return body;
}