tapeserverd.cpp 4.42 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
/******************************************************************************
 *                      tapeserverd.cpp
 *
 * 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, castor-dev@cern.ch
 *****************************************************************************/
24
25
26
27
28
29

#include <iostream>
#include <exception>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
Eric Cano's avatar
Eric Cano committed
30
#include <stdio.h>
31
32
33
#include <sys/stat.h>
#include <sstream>

34
#include "tapeserverd.hpp"
35
36
37

int main(int argc, char ** argv) {
  try {
38
    castor::tape::Server::Daemon daemon(argc, argv);
39
40
41
42
43
44
45
46
47
48
  } catch (std::exception & e) {
    std::cerr << "Uncaught standard exception in tapeserverd:" << std::endl
        << e.what() << std::endl;
    exit(EXIT_FAILURE);
  } catch (...) {
    std::cerr << "Uncaught non-standard exception in tapeserverd:" << std::endl;
    exit(EXIT_FAILURE);
  }
}

49
castor::tape::Server::Daemon::Daemon(int argc, char** argv) throw (castor::tape::Exception) {
50
51
52
53
  m_options = getCommandLineOptions(argc, argv);
  if (m_options.daemonize) daemonize();
}

54
55
castor::tape::Server::Daemon::options castor::tape::Server::Daemon::getCommandLineOptions(int argc, char** argv)
throw (castor::tape::Exception)
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{
  options ret;
  /* Expect -f or --foreground */
  struct ::option opts[] = {
    { "foreground", no_argument, 0, 'f'},
    { 0, 0, 0, 0}
  };
  int c;
  while (-1 != (c = getopt_long(argc, argv, ":f", opts, NULL))) {
    switch (c) {
      case 'f':
        ret.daemonize = false;
        break;
      case ':':
      {
71
        castor::tape::Exceptions::InvalidArgument ex(std::string("The -") + (char) optopt + " option requires a parameter");
72
73
74
75
76
77
        throw ex;
      }
      case '?':
      {
        std::stringstream err("Unknown command-line option");
        if (optopt) err << std::string(": -") << optopt;
78
        castor::tape::Exceptions::InvalidArgument ex(err.str().c_str());
79
80
81
82
83
84
85
        throw ex;
      }
      default:
      {
        std::stringstream err;
        err << "getopt_long returned the following unknown value: 0x" <<
            std::hex << (int) c;
86
        castor::tape::Exceptions::InvalidArgument ex(err.str().c_str());
87
88
89
90
91
92
93
        throw ex;
      }
    }
  }
  return ret;
}

94
void castor::tape::Server::Daemon::daemonize()
95
96
97
98
99
100
101
102
103
{
  pid_t pid, sid;

  /* already a daemon */
  if (getppid() == 1) return;

  /* Fork off the parent process */
  pid = fork();
  if (pid < 0) {
104
    castor::tape::Exceptions::Errnum e("Failed to fork in castor::tape::Server::Daemon::daemonize");
105
106
107
108
109
110
111
112
113
114
115
116
117
    throw e;
  }
  /* If we got a good PID, then we can exit the parent process. */
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }

  /* Change the file mode mask */
  umask(0);

  /* Create a new session for the child process */
  sid = setsid();
  if (sid < 0) {
118
    castor::tape::Exceptions::Errnum e("Failed to create new session in castor::tape::Server::Daemon::daemonize");
119
120
121
122
    throw e;
  }

  /* At this point we are executing as the child process, and parent process should be init */
Eric Cano's avatar
Eric Cano committed
123
  if (getppid() != 1) { 
124
    castor::tape::Exception e("Failed to detach from parent process in castor::tape::Server::Daemon::daemonize");
125
126
127
128
129
130
    throw e;
  }

  /* Change the current working directory.  This prevents the current
     directory from being locked; hence not being able to remove it. */
  if ((chdir(m_options.runDirectory.c_str())) < 0) {
131
    std::stringstream err("Failed to chdir in castor::tape::Server::Daemon::daemonize");
132
    err << " ( destination directory: " << m_options.runDirectory << ")";
133
    castor::tape::Exceptions::Errnum e(err.str());
134
135
136
137
138
139
140
141
    throw e;
  }

  /* Redirect standard files to /dev/null */
  freopen("/dev/null", "r", stdin);
  freopen("/dev/null", "w", stdout);
  freopen("/dev/null", "w", stderr);
}