Daemon.cpp 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*******************************************************************************
 *                      castor/server/Daemon.hpp
 *
 * 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.
 *
 * @author castor dev team
 ******************************************************************************/

#include "castor/dlf/Dlf.hpp"
24
25
#include "castor/exception/Errnum.hpp"
#include "castor/exception/Internal.hpp"
26
27
28
29
#include "castor/io/UDPSocket.hpp"
#include "castor/server/Daemon.hpp"
#include "castor/server/ThreadNotification.hpp"
#include "castor/System.hpp"
30
#include "h/Cgetopt.h"
31
32

#include <signal.h>
33
#include <stdio.h>
34
#include <unistd.h>
35
36
37
38

//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
39
40
41
42
castor::server::Daemon::Daemon(std::ostream &stdOut, std::ostream &stdErr,
  log::Logger &logger) throw():
  m_stdOut(stdOut),
  m_stdErr(stdErr),
43
44
  m_foreground(false),
  m_commandLineHasBeenParsed(false),
45
46
47
48
49
50
51
52
53
54
  m_runAsStagerSuperuser(false),
  m_logger(logger) {
}

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
castor::server::Daemon::~Daemon() throw() {
}

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//------------------------------------------------------------------------------
// parseCommandLine
//------------------------------------------------------------------------------
void castor::server::Daemon::parseCommandLine(int argc,
  char *argv[]) throw(castor::exception::Exception) {
  Coptions_t longopts[4];

  longopts[0].name = "foreground";
  longopts[0].has_arg = NO_ARGUMENT;
  longopts[0].flag = NULL;
  longopts[0].val = 'f';

  longopts[1].name = "config";
  longopts[1].has_arg = REQUIRED_ARGUMENT;
  longopts[1].flag = NULL;
  longopts[1].val = 'c';

  longopts[2].name = "help";
  longopts[2].has_arg = NO_ARGUMENT;
  longopts[2].flag = NULL;
  longopts[2].val = 'h';

  longopts[3].name = 0;

  Coptind = 1;
  Copterr = 0;
  Coptreset = 1;

  char c;
  while ((c = Cgetopt_long(argc, argv, "fc:h", longopts, NULL)) != -1) {
    switch (c) {
    case 'f':
      m_foreground = true;
      break;
    case 'c':
      setenv("PATH_CONFIG", Coptarg, 1);
91
      m_stdOut << "Using configuration file " << Coptarg << std::endl;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
      break;
    case 'h':
      help(argv[0]);
      exit(0);
      break;
    default:
      break;
    }
  }

  m_commandLineHasBeenParsed = true;
}

//------------------------------------------------------------------------------
// help
//------------------------------------------------------------------------------
void castor::server::Daemon::help(const std::string &programName)
  throw() {
110
  m_stdOut << "Usage: " << programName << " [options]\n"
111
112
113
114
115
116
117
118
119
120
121
    "\n"
    "where options can be:\n"
    "\n"
    "\t--foreground            or -f         \tRemain in the Foreground\n"
    "\t--config <config-file>  or -c         \tConfiguration file\n"
    "\t--metrics               or -m         \tEnable metrics collection\n"
    "\t--help                  or -h         \tPrint this help and exit\n"
    "\n"
    "Comments to: Castor.Support@cern.ch\n";
}

122
123
124
125
126
127
128
129
130
131
132
133
134
135
//------------------------------------------------------------------------------
// getServerName
//------------------------------------------------------------------------------
const std::string &castor::server::Daemon::getServerName() const throw() {
  return m_logger.getProgramName();
}

//------------------------------------------------------------------------------
// runAsStagerSuperuser
//------------------------------------------------------------------------------
void castor::server::Daemon::runAsStagerSuperuser() throw() {
  m_runAsStagerSuperuser = true;
}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//------------------------------------------------------------------------------
// getForeground
//------------------------------------------------------------------------------
bool castor::server::Daemon::getForeground() const
  throw(castor::exception::CommandLineNotParsed) {
  if(!m_commandLineHasBeenParsed) {
    castor::exception::CommandLineNotParsed ex;
    ex.getMessage() <<
      "Failed to determine whether or not the daemon should run in the"
      " foreground because the command-line has not yet been parsed";
    throw ex;
  }

  return m_foreground;
}

152
153
154
155
156
157
158
159
160
//-----------------------------------------------------------------------------
// setCommandLineParsed
//-----------------------------------------------------------------------------
void castor::server::Daemon::setCommandLineHasBeenParsed(const bool foreground)
  throw() {
  m_foreground = foreground;
  m_commandLineHasBeenParsed = true;
}

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
190
191
192
193
194
195
//-----------------------------------------------------------------------------
// dlfInit
//-----------------------------------------------------------------------------
void castor::server::Daemon::dlfInit(castor::dlf::Message messages[])
  throw (castor::exception::Exception) {
  castor::dlf::dlf_init((char*)m_logger.getProgramName().c_str(), messages);
  // Add framework specific messages
  castor::dlf::Message frameworkMessages[] =
    {{  1, "Error while reading datagrams" },
     {  2, "Error while accepting connections" },
     {  3, "Thread pool started" },
     {  4, "Exception caught in the user thread" },
     {  5, "Thread run error" },
     {  6, "NotifierThread exception" },
     {  8, "Exception caught while initializing the child process" },
     {  9, "Error while processing an object from the pipe" },
     { 10, "Uncaught exception in a thread from pool" },
     { 11, "Uncaught GENERAL exception in a thread from pool" },
     { 12, "Caught signal - GRACEFUL STOP" },
     { 14, "Caught signal - CHILD STOPPED" },
     { 15, "Signal caught but not handled - IMMEDIATE STOP" },
     { 16, "Exception during wait for signal loop" },
     { 18, "No idle thread in pool to process request" },
     { 19, "Error while dispatching to a thread" },
     { 20, "Spawning a new thread in pool" },
     { 21, "Terminating a thread in pool" },
     { 22, "Task processed" },
     { -1, "" }};
  castor::dlf::dlf_addMessages(DLF_BASE_FRAMEWORK, frameworkMessages);
}

//------------------------------------------------------------------------------
// daemonize
//------------------------------------------------------------------------------
void castor::server::Daemon::daemonize() throw (castor::exception::Exception) {
196
197
198
199
200
  // Do nothing if already a daemon
  if (1 == getppid())  {
    return;
  }

201
202
203
204
  // If the daemon is to be run in the background
  if (!m_foreground) {
    m_logger.prepareForFork();

205
206
207
208
209
210
211
212
213
214
    {
      pid_t pid = 0;
      castor::exception::Errnum::throwOnNegative(pid = fork(),
        "Failed to daemonize: Failed to fork");
      // If we got a good PID, then we can exit the parent process
      if (0 < pid) {
        exit(EXIT_SUCCESS);
      }
    }

215
216
217
    // We could set our working directory to '/' here with a call to chdir(2).
    // For the time being we don't and leave it to the initd script to change
    // to a suitable directory for us!
218
219
220

    // Change the file mode mask
    umask(0);
221
222

    // Run the daemon in a new session
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    castor::exception::Errnum::throwOnNegative(setsid(),
      "Failed to daemonize: Failed to run daemon is a new session");

    // Redirect standard files to /dev/null
    castor::exception::Errnum::throwOnNull(
      freopen("/dev/null", "r", stdin),
      "Failed to daemonize: Falied to freopen stdin");
    castor::exception::Errnum::throwOnNull(
      freopen("/dev/null", "w", stdout),
      "Failed to daemonize: Failed to freopen stdout");
    castor::exception::Errnum::throwOnNull(
      freopen("/dev/null", "w", stderr),
      "Failed to daemonize: Failed to freopen stderr");
  } // if (!m_foreground)
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

  // Change the user of the daemon process to the Castor superuser if requested
  if (m_runAsStagerSuperuser) {
    castor::System::switchToCastorSuperuser();
  }

  // Ignore SIGPIPE (connection lost with client)
  // and SIGXFSZ (a file is too big)
  signal(SIGPIPE, SIG_IGN);
  signal(SIGXFSZ, SIG_IGN);
}


//------------------------------------------------------------------------------
// sendNotification
//------------------------------------------------------------------------------
void castor::server::Daemon::sendNotification(const std::string &host,
  const int port, const char tpName, const int nbThreads) throw() {
  try {
    // Create notification message
    castor::server::ThreadNotification notif;
    notif.setTpName(tpName);
    notif.setNbThreads(nbThreads);

    // Create UDP socket and send packet
    castor::io::UDPSocket sock(port, host);
    sock.sendObject(notif);
    sock.close();
  } catch (castor::exception::Exception& ignored) {
    // This is a best effort service, ignore any failure
  } catch(...) {
    // This is a best effort service, ignore any failure
  }
}


//-----------------------------------------------------------------------------
// logMsg
//-----------------------------------------------------------------------------
void castor::server::Daemon::logMsg(
  const int priority,
  const std::string &msg,
  const int numParams,
  const log::Param params[],
  const struct timeval &timeStamp) throw() {
  m_logger.logMsg(priority, msg, numParams, params, timeStamp);
}

//-----------------------------------------------------------------------------
// logMsg
//-----------------------------------------------------------------------------
void castor::server::Daemon::logMsg(
  const int priority,
  const std::string &msg,
  const int numParams,
  const log::Param params[]) throw() {
  m_logger.logMsg(priority, msg, numParams, params);
}

//-----------------------------------------------------------------------------
// logMsg
//-----------------------------------------------------------------------------
void castor::server::Daemon::logMsg(
  const int priority,
  const std::string &msg) throw() {
  m_logger.logMsg(priority, msg);
}