Debian Codegraph @

RailControl.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
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;
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 "Version.h"
38 #include "Utils/Utils.h"
39
40 using std::vector;
41 using std::string;
42
43 void killRailControlIfNeeded(Logger::Logger* logger)
44 {
45 if (++stopSignalCounter < MaxStopSignalCounter)
46 {
47 return;
48 }
49 logger->Info(Languages::TextReceivedSignalKill, MaxStopSignalCounter);
50 exit(1);
51 }
52
53 void shutdownRailControlSignal(int signo)
54 {
55 Logger::Logger* logger = Logger::Logger::GetLogger("Main");
56 logger->Info(Languages::TextShutdownRequestedBySignal, signo);
57 killRailControlIfNeeded(logger);
58 }
59
60 void shutdownRailControlWebserver()
61 {
62 Logger::Logger* logger = Logger::Logger::GetLogger("Main");
63 logger->Info(Languages::TextShutdownRequestedByWebClient);
64 killRailControlIfNeeded(logger);
65 }
66
67 int main (int argc, char* argv[])
68 {
69 std::map<std::string,char> argumentMap;
70 argumentMap["config"] = 'c';
71 argumentMap["daemonize"] = 'd';
72 argumentMap["logfile"] = 'l';
73 argumentMap["help"] = 'h';
74 argumentMap["silent"] = 's';
75 ArgumentHandler argumentHandler(argc, argv, argumentMap, 'c');
76
77 const bool help = argumentHandler.GetArgumentBool('h');
78 if (help)
79 {
80 std::cout << "Usage: " << argv[0] << " <options>" << std::endl;
81 std::cout << "Options:" << std::endl;
82 std::cout << " --config=ConfigFile Read config file with file name ConfigFile (default ConfigFile: railcontrol.conf)" << std::endl;
83 std::cout << "-d --daemonize Daemonize RailControl. Implies -s" << std::endl;
84 std::cout << " --logfile=LogFile Write a logfile to file LogFile (default LogFile: railcontrol.log)" << std::endl;
85 std::cout << "-h --help Show this help" << std::endl;
86 std::cout << "-s --silent Omit writing to console" << std::endl;
87 return EXIT_SUCCESS;
88 }
89
90 const bool daemonize = argumentHandler.GetArgumentBool('d');
91 if (daemonize)
92 {
93 pid_t pid = fork();
94 if (pid > 0)
95 {
96 return EXIT_SUCCESS;
97 }
98 close(STDERR_FILENO);
99 close(STDOUT_FILENO);
100 close(STDIN_FILENO);
101 }
102
103 stopSignalCounter = 0;
104 signal(SIGINT, shutdownRailControlSignal);
105 signal(SIGTERM, shutdownRailControlSignal);
106
107 const string RailControl = "RailControl";
108 Utils::Utils::SetThreadName(RailControl);
109
110 Logger::Logger* logger = Logger::Logger::GetLogger("Main");
111
112 const bool silent = daemonize || argumentHandler.GetArgumentBool('s');
113 if (!silent)
114 {
115 logger->AddConsoleLogger();
116 }
117
118 const string logFileName = argumentHandler.GetArgumentString('l', "railcontrol.log");
119 if (logFileName.length() > 0)
120 {
121 logger->AddFileLogger(logFileName);
122 }
123
124 logger->Info(Languages::TextStarting, RailControl);
125 logger->Info(Languages::TextVersion, GetVersionInfoRailControlVersion());
126 logger->Info(Languages::TextCompileDate, Utils::Utils::TimestampToDate(GetVersionInfoCompileTimestamp()));
127 logger->Info(Languages::TextGitHash, GetVersionInfoGitHash());
128 logger->Info(Languages::TextGitDate, Utils::Utils::TimestampToDate(GetVersionInfoGitTimestamp()));
129
130 const unsigned int changedFiles = GetVersionInfoGitDirty();
131 if (changedFiles)
132 {
133 logger->Info(Languages::TextGitDirty, changedFiles);
134 }
135
136 logger->Info(Languages::TextStartArgument, argv[0]);
137
138 const string configFileDefaultName("railcontrol.conf");
139 string configFileName = argumentHandler.GetArgumentString('c', configFileDefaultName);
140 if (configFileName.compare("") == 0)
141 {
142 configFileName = configFileDefaultName;
143 }
144
145 if (!Utils::Utils::FileExists(configFileName))
146 {
147 logger->Warning(Languages::TextConfigFileNotFound, configFileName, configFileDefaultName);
148 configFileName = configFileDefaultName;
149 }
150
151 if (configFileName.compare(configFileDefaultName) == 0 && !Utils::Utils::FileExists(configFileDefaultName))
152 {
153 Utils::Utils::CopyFile(logger, "railcontrol.conf.dist", configFileDefaultName);
154 }
155
156 Config config(configFileName);
157
158 unsigned int logKeepBackups = config.getIntValue("logkeepbackups", 10);
159 Utils::Utils::RemoveOldBackupFiles(logger, logFileName, logKeepBackups);
160
161 char input = 0;
162 {
163 // the main program is running in the manager.
164 Manager m(config);
165
166 // wait for q or r followed by \n or SIGINT or SIGTERM
167 do
168 {
169 if (silent)
170 {
171 Utils::Utils::SleepForSeconds(1);
172 }
173 else
174 {
175 struct timeval tv;
176 tv.tv_sec = 1;
177 tv.tv_usec = 0;
178 fd_set set;
179 FD_ZERO(&set);
180 FD_SET(STDIN_FILENO, &set);
181 int ret = TEMP_FAILURE_RETRY(select(FD_SETSIZE, &set, NULL, NULL, &tv));
182 if (ret > 0 && FD_ISSET(STDIN_FILENO, &set))
183 {
184 __attribute__((unused)) size_t unused = read(STDIN_FILENO, &input, sizeof(input));
185 }
186 }
187 } while ((input != 'q') && (input != 'r') && !isShutdownRunning());
188
189 logger->Info(Languages::TextShutdownRailControl);
190
191 } // here the destructor of manager is called and RailControl is shut down
192
193 if (input == 'r')
194 {
195 // restart RailControl
196 return execv(argv[0], argv);
197 }
198 else
199 {
200 // exit RailControl
201 return EXIT_SUCCESS;
202 }
203 }