Subversion Repositories HomeAutomation

Rev

Rev 976 | Rev 981 | Go to most recent revision | Details | Compare with Previous | 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
 
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
 
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
 
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
 
978 runge 94
    slog << "\n";
95
    slog << "Goodbye!\n";
969 runge 96
 
97
    CanNetManager::deleteInstance();
98
    VirtualMachine::deleteInstance();
99
    SyslogStream::deleteInstance();
100
 
101
    return EXIT_SUCCESS;
102
}
103
 
104
void handler(int status)
105
{
106
    //cout << "In the handler function. Status: " << status << endl;
107
 
108
/*
109
    cout << "SIGTERM: " << SIGTERM << endl;
110
    cout << "SIGINT: " << SIGINT << endl;
111
    cout << "SIGQUIT: " << SIGQUIT << endl;
112
    cout << "SIGABRT: " << SIGABRT << endl;
113
    cout << "SIGIO: " << SIGIO << endl;
114
    cout << "SIGPIPE: " << SIGPIPE << endl;
115
 
116
*/
117
    cleanUp();
118
 
119
    /*The SIGTERM signal tells a process to exit, so we exit. You can't make a
120
    process invincible by ignoreing SIGTERM as there are still the
121
    SIGKILL and SIGSTOP signals which you can't set a handler for (catch) or
122
    ignore*/
123
    exit(EXIT_SUCCESS);
124
}