Subversion Repositories HomeAutomation

Rev

Rev 969 | Rev 978 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /***************************************************************************
  2.  *   Copyright (C) November 26, 2008, 3:00 PM by Mattias Runge             *
  3.  *   mattias@runge.se                                                      *
  4.  *   main.cpp                                                              *
  5.  *                                                                         *
  6.  *   This program is free software; you can redistribute it and/or modify  *
  7.  *   it under the terms of the GNU General Public License as published by  *
  8.  *   the Free Software Foundation; either version 2 of the License, or     *
  9.  *   (at your option) any later version.                                   *
  10.  *                                                                         *
  11.  *   This program is distributed in the hope that it will be useful,       *
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  13.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  14.  *   GNU General Public License for more details.                          *
  15.  *                                                                         *
  16.  *   You should have received a copy of the GNU General Public License     *
  17.  *   along with this program; if not, write to the                         *
  18.  *   Free Software Foundation, Inc.,                                       *
  19.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  20.  ***************************************************************************/
  21.  
  22. using namespace std;
  23.  
  24.  
  25. #include <stdlib.h>
  26. #include <iostream>
  27. #include <signal.h>
  28.  
  29. #include "version.h"
  30. #include "Tools/tools.h"
  31. #include "SyslogStream/syslogstream.h"
  32. #include "Settings/settings.h"
  33. #include "CanNet/cannetmanager.h"
  34. #include "Socket/asyncsocket.h"
  35. #include "VM/virtualmachine.h"
  36.  
  37. int cleanUp();
  38. void handler(int status);
  39.  
  40. int main(int argc, char** argv)
  41. {
  42.     signal(SIGTERM, handler);
  43.     signal(SIGINT, handler);
  44.     signal(SIGQUIT, handler);
  45.     signal(SIGABRT, handler);
  46.     signal(SIGPIPE, handler);
  47.  
  48.     SyslogStream &slog = SyslogStream::getInstance();
  49.  
  50.     slog << "Atom " + ftos(VERSION) + " starting...\n";
  51.     slog << "Written by Mattias Runge 2008\n\n";
  52.  
  53.     if (file_exists("/etc/atom.conf"))
  54.     {
  55.         if (!Settings::read("/etc/atom.conf"))
  56.         {
  57.             return cleanUp();
  58.         }
  59.     }
  60.     else
  61.     {
  62.         if (!Settings::read("atom.conf"))
  63.         {
  64.             return cleanUp();
  65.         }
  66.     }
  67.  
  68.  
  69.     VirtualMachine &vm = VirtualMachine::getInstance();
  70.  
  71.     vm.start();
  72.  
  73.     if (Settings::get("DaemonMode") == "yes")
  74.     {
  75.         slog << "Entering daemon mode...\n";
  76.         if (daemon(0, 0) == -1)
  77.         {
  78.             slog << "Could not enter daemon mode. Exiting...\n";
  79.             return cleanUp();
  80.         }
  81.     }
  82.  
  83.     CanNetManager &canMan = CanNetManager::getInstance();
  84.  
  85.     canMan.openChannel();
  86.  
  87.     return cleanUp();
  88. }
  89.  
  90. int cleanUp()
  91. {
  92.     SyslogStream &slog = SyslogStream::getInstance();
  93.  
  94.     slog << "\nGoodbye!\n";
  95.    
  96.     CanNetManager::deleteInstance();
  97.     VirtualMachine::deleteInstance();
  98.     SyslogStream::deleteInstance();
  99.  
  100.     return EXIT_SUCCESS;
  101. }
  102.  
  103. void handler(int status)
  104. {
  105.     //cout << "In the handler function. Status: " << status << endl;
  106.  
  107. /*
  108.     cout << "SIGTERM: " << SIGTERM << endl;
  109.     cout << "SIGINT: " << SIGINT << endl;
  110.     cout << "SIGQUIT: " << SIGQUIT << endl;
  111.     cout << "SIGABRT: " << SIGABRT << endl;
  112.     cout << "SIGIO: " << SIGIO << endl;
  113.     cout << "SIGPIPE: " << SIGPIPE << endl;
  114.  
  115. */
  116.     cleanUp();
  117.  
  118.     /*The SIGTERM signal tells a process to exit, so we exit. You can't make a
  119.     process invincible by ignoreing SIGTERM as there are still the
  120.     SIGKILL and SIGSTOP signals which you can't set a handler for (catch) or
  121.     ignore*/
  122.     exit(EXIT_SUCCESS);
  123. }
  124.