TapeDaemonMain.cpp 8.5 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
19
20
/******************************************************************************
 *
 * 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.
 *
 *
 *
21
 * @author Castor Dev team, castor-dev@cern.ch
Steven Murray's avatar
Steven Murray committed
22
23
 *****************************************************************************/

24
#include "castor/common/CastorConfiguration.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
25
#include "common/log/SyslogLogger.hpp"
26
#include "common/processCap/ProcessCap.hpp"
27
#include "castor/tape/reactor/ZMQReactor.hpp"
Daniele Kruse's avatar
Daniele Kruse committed
28
#include "castor/tape/tapeserver/daemon/Constants.hpp"
29
#include "castor/tape/tapeserver/daemon/DataTransferSession.hpp"
Steven Murray's avatar
Steven Murray committed
30
#include "castor/tape/tapeserver/daemon/TapeDaemon.hpp"
Daniele Kruse's avatar
Daniele Kruse committed
31
#include "castor/utils/utils.hpp"
32
33
34
35
36
37
#include "h/rmc_constants.h"

#define MAJORVERSION 2
#define MINORVERSION 1
#define MAJORRELEASE 15
#define MINORRELEASE 9999
38

39
40
#include <google/protobuf/stubs/common.h>
#include <memory>
41
#include <sstream>
42
#include <string>
43

Steven Murray's avatar
Steven Murray committed
44
45
46
47
48
//------------------------------------------------------------------------------
// exceptionThrowingMain
//
// The main() function delegates the bulk of its implementation to this
// exception throwing version.
49
50
51
52
//
// @param argc The number of command-line arguments.
// @param argv The command-line arguments.
// @param log The logging system.
Steven Murray's avatar
Steven Murray committed
53
//------------------------------------------------------------------------------
54
static int exceptionThrowingMain(const int argc, char **const argv,
Victor Kotlyar's avatar
Victor Kotlyar committed
55
  cta::log::Logger &log);
Steven Murray's avatar
Steven Murray committed
56

Steven Murray's avatar
Steven Murray committed
57
58
59
60
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(const int argc, char **const argv) {
61
  // Try to instantiate the logging system API
Victor Kotlyar's avatar
Victor Kotlyar committed
62
  std::unique_ptr<cta::log::SyslogLogger> logPtr;
Steven Murray's avatar
Steven Murray committed
63
  try {
64
    logPtr.reset(new cta::log::SyslogLogger(std::string("cta-tapeserverd"),cta::log::DEBUG));
65
  } catch(cta::exception::Exception &ex) {
66
67
68
    std::cerr <<
      "Failed to instantiate object representing CASTOR logging system: " <<
      ex.getMessage().str() << std::endl;
69
    return 1;
70
  }
Victor Kotlyar's avatar
Victor Kotlyar committed
71
  cta::log::Logger &log = *logPtr.get();
72
73
74

  int programRc = 1; // Be pessimistic
  try {
75
    programRc = exceptionThrowingMain(argc, argv, log);
76
  } catch(cta::exception::Exception &ex) {
Victor Kotlyar's avatar
Victor Kotlyar committed
77
78
79
    std::list<cta::log::Param> params = {
      cta::log::Param("message", ex.getMessage().str())};
    log(cta::log::ERR, "Caught an unexpected CASTOR exception", params);
80
  } catch(std::exception &se) {
Victor Kotlyar's avatar
Victor Kotlyar committed
81
82
    std::list<cta::log::Param> params = {cta::log::Param("what", se.what())};
    log(cta::log::ERR, "Caught an unexpected standard exception", params);
Steven Murray's avatar
Steven Murray committed
83
  } catch(...) {
Victor Kotlyar's avatar
Victor Kotlyar committed
84
    log(cta::log::ERR, "Caught an unexpected and unknown exception");
85
86
  }

87
  google::protobuf::ShutdownProtobufLibrary();
88
  return programRc;
Steven Murray's avatar
Steven Murray committed
89
90
}

91
92
93
//------------------------------------------------------------------------------
// Logs the start of the daemon.
//------------------------------------------------------------------------------
Victor Kotlyar's avatar
Victor Kotlyar committed
94
static void logStartOfDaemon(cta::log::Logger &log, const int argc,
95
96
97
98
99
100
101
102
103
104
105
  const char *const *const argv);

//------------------------------------------------------------------------------
// Creates a string that contains the specified command-line arguments
// separated by single spaces.
//
// @param argc The number of command-line arguments.
// @param argv The array of command-line arguments.
//------------------------------------------------------------------------------
static std::string argvToString(const int argc, const char *const *const argv);

106
107
108
109
110
111
//------------------------------------------------------------------------------
// Writes the specified TPCONFIG lines to the specified logging system.
//
// @param log The logging system.
// @param lines The lines parsed from /etc/castor/TPCONFIG.
//------------------------------------------------------------------------------
Victor Kotlyar's avatar
Victor Kotlyar committed
112
static void logTpconfigLines(cta::log::Logger &log,
113
  const castor::tape::tapeserver::daemon::TpconfigLines &lines) throw();
114
115
116
117
118
119
120

//------------------------------------------------------------------------------
// Writes the specified TPCONFIG lines to the logging system.
//
// @param log The logging system.
// @param line The line parsed from /etc/castor/TPCONFIG.
//------------------------------------------------------------------------------
Victor Kotlyar's avatar
Victor Kotlyar committed
121
static void logTpconfigLine(cta::log::Logger &log,
122
  const castor::tape::tapeserver::daemon::TpconfigLine &line) throw();
123

Steven Murray's avatar
Steven Murray committed
124
125
126
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
127
static int exceptionThrowingMain(const int argc, char **const argv,
Victor Kotlyar's avatar
Victor Kotlyar committed
128
  cta::log::Logger &log) {
129
  using namespace castor;
130

131
132
  logStartOfDaemon(log, argc, argv);

133
134
135
  // Parse /etc/castor/castor.conf
  const tape::tapeserver::daemon::TapeDaemonConfig tapeDaemonConfig =
    tape::tapeserver::daemon::TapeDaemonConfig::createFromCastorConf(&log);
136

137
  // Parse /etc/castor/TPCONFIG
138
139
  const tape::tapeserver::daemon::TpconfigLines tpconfigLines =
    tape::tapeserver::daemon::TpconfigLines::parseFile("/etc/castor/TPCONFIG");
140
  logTpconfigLines(log, tpconfigLines);
141
  tape::tapeserver::daemon::DriveConfigMap driveConfigs;
142
  driveConfigs.enterTpconfigLines(tpconfigLines);
143

144
  const int netTimeout = 10; // Timeout in seconds
145

146
  tape::reactor::ZMQReactor reactor(log);
Steven Murray's avatar
Steven Murray committed
147

148
  // Create the object providing utilities for working with UNIX capabilities
149
  cta::server::ProcessCap capUtils;
150

151
  // Create the main tapeserverd object
152
  tape::tapeserver::daemon::TapeDaemon daemon(
153
154
155
156
157
    argc,
    argv,
    std::cout,
    std::cerr,
    log,
158
    netTimeout,
159
    driveConfigs,
160
    reactor,
161
162
    capUtils,
    tapeDaemonConfig);
163
164

  // Run the tapeserverd daemon
Steven Murray's avatar
Steven Murray committed
165
  return daemon.main();
Steven Murray's avatar
Steven Murray committed
166
}
167

168
169
170
//------------------------------------------------------------------------------
// logStartOfDaemon
//------------------------------------------------------------------------------
Victor Kotlyar's avatar
Victor Kotlyar committed
171
static void logStartOfDaemon(cta::log::Logger &log, const int argc,
172
173
174
175
176
177
178
  const char *const *const argv) {
  using namespace castor;

  const std::string concatenatedArgs = argvToString(argc, argv);
  std::ostringstream version;
  version << MAJORVERSION << "." << MINORVERSION << "." << MAJORRELEASE << "-"
    << MINORRELEASE;
Victor Kotlyar's avatar
Victor Kotlyar committed
179
180
181
  std::list<cta::log::Param> params = {
    cta::log::Param("version", version.str()),
    cta::log::Param("argv", concatenatedArgs)};
182
  log(cta::log::INFO, "cta-tapeserverd started", params);
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
}

//------------------------------------------------------------------------------
// argvToString
//------------------------------------------------------------------------------
static std::string argvToString(const int argc, const char *const *const argv) {
  std::string str;

  for(int i=0; i < argc; i++) {
    if(i != 0) {
      str += " ";
    }

    str += argv[i];
  }
  return str;
}

201
//------------------------------------------------------------------------------
202
203
// logTpconfigLines
//------------------------------------------------------------------------------
Victor Kotlyar's avatar
Victor Kotlyar committed
204
static void logTpconfigLines(cta::log::Logger &log,
205
206
207
208
  const castor::tape::tapeserver::daemon::TpconfigLines &lines) throw() {
  using namespace castor::tape::tapeserver::daemon;

  for(TpconfigLines::const_iterator itor = lines.begin();
209
210
211
212
213
214
215
216
    itor != lines.end(); itor++) {
    logTpconfigLine(log, *itor);
  }
}

//------------------------------------------------------------------------------
// logTpconfigLine
//------------------------------------------------------------------------------
Victor Kotlyar's avatar
Victor Kotlyar committed
217
static void logTpconfigLine(cta::log::Logger &log,
218
  const castor::tape::tapeserver::daemon::TpconfigLine &line) throw() {
Victor Kotlyar's avatar
Victor Kotlyar committed
219
220
221
222
223
224
  std::list<cta::log::Param> params = {
    cta::log::Param("unitName", line.unitName),
    cta::log::Param("logicalLibrary", line.logicalLibrary),
    cta::log::Param("devFilename", line.devFilename),
    cta::log::Param("librarySlot", line.librarySlot)};
  log(cta::log::INFO, "TPCONFIG line", params);
225
}