Debian Codegraph @

RailControl.cpp

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #include <cstdio> //printf
22 #include <cstdlib> //exit(0);
23 #include <cstring> //memset
24 #include <iostream>
25 #include <signal.h>
26 #include <sstream>
•27 #include <unistd.h> //close; isatty
28 #include <vector>
29
30 #include "ArgumentHandler.h"
31 #include "Hardware/HardwareHandler.h"
32 #include "Languages.h"
33 #include "Logger/Logger.h"
34 #include "Manager.h"
35 #include "Network/Select.h"
36 #include "RailControl.h"
37 #include "Utils/Utils.h"
38
39 using std::vector;
40 using std::string;
41
42 void killRailControlIfNeeded(Logger::Logger* logger)
43 {
44 if (++stopSignalCounter < MaxStopSignalCounter)
45 {
46 return;
47 }
48 logger->Info(Languages::TextReceivedSignalKill, MaxStopSignalCounter);
49 exit(1);
50 }
51
52 void shutdownRailControlSignal(int signo)
53 {
54 Logger::Logger* logger = Logger::Logger::GetLogger("Main");
55 logger->Info(Languages::TextShutdownRequestedBySignal, signo);
56 killRailControlIfNeeded(logger);
57 }
58
59 void shutdownRailControlWebserver()
60 {
61 Logger::Logger* logger = Logger::Logger::GetLogger("Main");
62 logger->Info(Languages::TextShutdownRequestedByWebClient);
63 killRailControlIfNeeded(logger);
64 }
65
66 int main (int argc, char* argv[])
67 {
68 std::map<std::string,char> argumentMap;
69 argumentMap["config"] = 'c';
70 argumentMap["daemonize"] = 'd';
71 argumentMap["logfile"] = 'l';
72 argumentMap["help"] = 'h';
73 argumentMap["silent"] = 's';
74 ArgumentHandler argumentHandler(argc, argv, argumentMap, 'c');
75
76 const bool help = argumentHandler.GetArgumentBool('h');
77 if (help)
78 {
79 std::cout << "Usage: " << argv[0] << " <options>" << std::endl;
80 std::cout << "Options:" << std::endl;
81 std::cout << " --config=ConfigFile Read config file with file name ConfigFile (default ConfigFile: railcontrol.conf)" << std::endl;
82 std::cout << "-d --daemonize Daemonize RailControl. Implies -s" << std::endl;
83 std::cout << " --logfile=LogFile Write a logfile to file LogFile (default LogFile: railcontrol.log)" << std::endl;
84 std::cout << "-h --help Show this help" << std::endl;
85 std::cout << "-s --silent Omit writing to console" << std::endl;
86 return EXIT_SUCCESS;
87 }
88
89 const bool daemonize = argumentHandler.GetArgumentBool('d');
90 if (daemonize)
91 {
92 pid_t pid = fork();
93 if (pid > 0)
94 {
95 return EXIT_SUCCESS;
96 }
97 close(STDERR_FILENO);
98 close(STDOUT_FILENO);
99 close(STDIN_FILENO);
100 }
101
102 stopSignalCounter = 0;
103 signal(SIGINT, shutdownRailControlSignal);
104 signal(SIGTERM, shutdownRailControlSignal);
105
106 const string RailControl = "RailControl";
107 Utils::Utils::SetThreadName(RailControl);
108
109 Logger::Logger* logger = Logger::Logger::GetLogger("Main");
110
111 const bool silent = daemonize || argumentHandler.GetArgumentBool('s');
112 if (!silent)
113 {
114 logger->AddConsoleLogger();
115 }
116
117 const string logFileName = argumentHandler.GetArgumentString('l', "railcontrol.log");
118 if (logFileName.length() > 0)
119 {
120 logger->AddFileLogger(logFileName);
121 }
122
123 logger->Info(Languages::TextStarting, RailControl);
124
125 logger->Info(Languages::TextStartArgument, argv[0]);
126
127 const string configFileDefaultName("railcontrol.conf");
128 string configFileName = argumentHandler.GetArgumentString('c', configFileDefaultName);
129 if (configFileName.compare("") == 0)
130 {
131 configFileName = configFileDefaultName;
132 }
133
134 if (!Utils::Utils::FileExists(configFileName))
135 {
136 logger->Warning(Languages::TextConfigFileNotFound, configFileName, configFileDefaultName);
137 configFileName = configFileDefaultName;
138 }
139
140 if (configFileName.compare(configFileDefaultName) == 0 && !Utils::Utils::FileExists(configFileDefaultName))
141 {
142 Utils::Utils::CopyFile(logger, "railcontrol.conf.dist", configFileDefaultName);
143 }
144
145 Config config(configFileName);
146
147 unsigned int logKeepBackups = config.getIntValue("logkeepbackups", 10);
148 Utils::Utils::RemoveOldBackupFiles(logger, logFileName, logKeepBackups);
149
150 char input = 0;
151 {
152 // the main program is running in the manager.
153 Manager m(config);
154
155 // wait for q or r followed by \n or SIGINT or SIGTERM
156 do
157 {
•158 if (!isatty(STDIN_FILENO) || silent)
159 {
160 Utils::Utils::SleepForSeconds(1);
161 }
162 else
163 {
164 struct timeval tv;
165 tv.tv_sec = 1;
166 tv.tv_usec = 0;
167 fd_set set;
168 FD_ZERO(&set);
169 FD_SET(STDIN_FILENO, &set);
170 int ret = TEMP_FAILURE_RETRY(select(FD_SETSIZE, &set, NULL, NULL, &tv));
171 if (ret > 0 && FD_ISSET(STDIN_FILENO, &set))
172 {
173 __attribute__((unused)) size_t unused = read(STDIN_FILENO, &input, sizeof(input));
174 }
175 }
176 } while ((input != 'q') && (input != 'r') && !isShutdownRunning());
177
178 logger->Info(Languages::TextShutdownRailControl);
179
180 } // here the destructor of manager is called and RailControl is shut down
181
182 if (input == 'r')
183 {
184 // restart RailControl
185 return execv(argv[0], argv);
186 }
187 else
188 {
189 // exit RailControl
190 return EXIT_SUCCESS;
191 }
192 }