Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
969 runge 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
#include <stdlib.h>
25
#include <iostream>
26
#include <signal.h>
27
 
28
#include "version.h"
29
#include "Tools/tools.h"
30
#include "SyslogStream/syslogstream.h"
31
#include "Settings/settings.h"
32
#include "CanNet/cannetmanager.h"
33
#include "Socket/asyncsocket.h"
34
#include "VM/virtualmachine.h"
981 runge 35
#include "CanNet/candebug.h"
969 runge 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
 
976 runge 50
    slog << "Atom " + ftos(VERSION) + " starting...\n";
969 runge 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
    VirtualMachine &vm = VirtualMachine::getInstance();
69
    vm.start();
70
 
981 runge 71
    CanDebug &canDebug = CanDebug::getInstance();
72
    canDebug.start();
73
 
969 runge 74
    if (Settings::get("DaemonMode") == "yes")
75
    {
76
        slog << "Entering daemon mode...\n";
77
        if (daemon(0, 0) == -1)
78
        {
79
            slog << "Could not enter daemon mode. Exiting...\n";
80
            return cleanUp();
81
        }
82
    }
83
 
981 runge 84
 
85
 
969 runge 86
    CanNetManager &canMan = CanNetManager::getInstance();
87
 
88
    canMan.openChannel();
89
 
90
    return cleanUp();
91
}
92
 
93
int cleanUp()
94
{
95
    SyslogStream &slog = SyslogStream::getInstance();
96
 
978 runge 97
    slog << "\n";
98
    slog << "Goodbye!\n";
981 runge 99
 
100
    CanDebug::deleteInstance();
969 runge 101
    CanNetManager::deleteInstance();
102
    VirtualMachine::deleteInstance();
103
    SyslogStream::deleteInstance();
104
 
105
    return EXIT_SUCCESS;
106
}
107
 
108
void handler(int status)
109
{
110
    //cout << "In the handler function. Status: " << status << endl;
111
 
112
/*
113
    cout << "SIGTERM: " << SIGTERM << endl;
114
    cout << "SIGINT: " << SIGINT << endl;
115
    cout << "SIGQUIT: " << SIGQUIT << endl;
116
    cout << "SIGABRT: " << SIGABRT << endl;
117
    cout << "SIGIO: " << SIGIO << endl;
118
    cout << "SIGPIPE: " << SIGPIPE << endl;
119
 
120
*/
121
    cleanUp();
122
 
123
    /*The SIGTERM signal tells a process to exit, so we exit. You can't make a
124
    process invincible by ignoreing SIGTERM as there are still the
125
    SIGKILL and SIGSTOP signals which you can't set a handler for (catch) or
126
    ignore*/
127
    exit(EXIT_SUCCESS);
128
}