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