Skip to content
sls.cc 2.42 KiB
Newer Older
Seblu's avatar
Seblu committed
/*
  This file is part of SLS.
Seblu's avatar
Seblu committed
  Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
Seblu's avatar
Seblu committed

  SLS is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
Seblu's avatar
Seblu committed
  the Free Software Foundation; version 2 of the License.
Seblu's avatar
Seblu committed

  SLS 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 SLS; if not, write to the Free Software
Seblu's avatar
Seblu committed
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Seblu's avatar
Seblu committed
*/
Seblu's avatar
Seblu committed

#include "sls.hh"
#include "option.hh"
#include "database.hh"
#include "server.hh"
Seblu's avatar
Seblu committed
#include "error.hh"
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

Option O;
Database D;
Server S;

Seblu's avatar
Seblu committed
int main(int argc, char *argv[])
{
Seblu's avatar
Seblu committed
  try {
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
    // Load options
    O.load(argc, argv);
Seblu's avatar
Seblu committed
    O.loadenv();
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
    // Daemonize
    if (O.daemon) {
      // open log file
      int logfd = open(O.logfile.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0640);
      if (logfd < 0)
	throw Error(ERR_FILE, "Cannot open " + O.logfile + ": " + strerror(errno));
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
      // redirect stdout and stderr
      if (dup2(logfd, STDOUT_FILENO) == -1)
	throw Error(ERR_FILE, (string) "Unable to redirect STDOUT into " + O.logfile + ": " + strerror(errno));
      if (dup2(logfd, STDERR_FILENO) == -1)
	throw Error(ERR_FILE, (string) "Unable to redirect STDERR into " + O.logfile + ": " + strerror(errno));

      // close unused fd
      if (close(STDIN_FILENO) == -1)
	throw Error(ERR_FILE, (string) "Unable to close STDIN: " + strerror(errno));

      if (close(logfd) == -1)
	throw Error(ERR_FILE, (string) "Unable to close: " + O.logfile + ": " + strerror(errno));

      // goto hell
      daemon(0, 1);
    }

    // Open DB
    D.open(O.database);
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
    // start network
    S.start();
    S.listen(O.maxthread);
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
    // Close DB
Seblu's avatar
Seblu committed
    D.close();
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
    return ERR_NO;
  }
  catch (const Error &e) {
    // Close server if started
    if (S.started())
      S.stop();

    // Save data in database
    if (D.opened())
      D.close();

    // print error
Seblu's avatar
Seblu committed
    e.print();

    // Print exit code if in daemon and verbose mode
    if (O.daemon && O.verbose) std::cerr << "Exit with code: " << e.code() << std::endl;

Seblu's avatar
Seblu committed
    return e.code();
  }
Seblu's avatar
Seblu committed
}