-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathOlad.cpp
More file actions
193 lines (168 loc) · 6.23 KB
/
Olad.cpp
File metadata and controls
193 lines (168 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Olad.cpp
* Main file for olad, parses the options, forks if required and runs the
* daemon.
* Copyright (C) 2005 Simon Newton
*
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <memory>
// On MinGW, OlaDaemon.h pulls in SocketAddress.h which pulls in WinSock2.h,
// which needs to be after WinSock2.h, hence this order
#include "olad/OlaDaemon.h"
#if HAVE_LIBSYSTEMD
#include "olad/Systemd.h"
#endif // HAVE_LIBSYSTEMD
#include "ola/Logging.h"
#include "ola/base/Credentials.h"
#include "ola/base/Flags.h"
#include "ola/base/Init.h"
#include "ola/base/SysExits.h"
#include "ola/base/Version.h"
#include "ola/thread/SignalThread.h"
using ola::OlaDaemon;
using ola::thread::SignalThread;
using std::cout;
using std::endl;
DEFINE_default_bool(http, true, "Disable the HTTP server.");
DEFINE_default_bool(http_quit, true, "Disable the HTTP /quit handler.");
#ifndef _WIN32
DEFINE_s_default_bool(daemon, f, false,
"Fork and run as a background process.");
#endif // _WIN32
DEFINE_s_string(http_data_dir, d, "", "The path to the static www content.");
DEFINE_s_string(interface, i, "",
"The interface name (e.g. eth0) or IP address of the network "
"interface to use for the web server.");
DEFINE_string(pid_location, "",
"The directory containing the PID definitions.");
DEFINE_s_uint16(http_port, p, ola::OlaServer::DEFAULT_HTTP_PORT,
"The port to run the HTTP server on. Defaults to 9090.");
/**
* This is called by the SelectServer loop to start up the SignalThread. If the
* thread fails to start, we terminate the SelectServer
*/
void StartSignalThread(ola::io::SelectServer *ss,
SignalThread *signal_thread) {
if (!signal_thread->Start()) {
ss->Terminate();
}
}
/*
* Main
*/
int main(int argc, char *argv[]) {
// Take a copy of the arguments otherwise the export map is incorrect.
const int original_argc = argc;
char *original_argv[original_argc];
for (int i = 0; i < original_argc; i++) {
original_argv[i] = argv[i];
}
// We don't use the longer form for ServerInit here because we need to check
// for root and possibly daemonise before doing the rest of the work from
// ServerInit.
ola::SetHelpString("[options]", "Start the OLA Daemon.");
ola::ParseFlags(&argc, argv);
ola::InitLoggingFromFlags();
OLA_INFO << "OLA Daemon version " << ola::base::Version::GetVersion();
#ifndef OLAD_SKIP_ROOT_CHECK
uid_t uid;
ola::GetEUID(&uid);
if (ola::SupportsUIDs() && !uid) {
OLA_FATAL << "Attempting to run as root, aborting.";
return ola::EXIT_UNAVAILABLE;
}
#endif // OLAD_SKIP_ROOT_CHECK
#ifndef _WIN32
if (FLAGS_daemon)
ola::Daemonise();
#endif // _WIN32
ola::ExportMap export_map;
if (!ola::ServerInit(original_argc, original_argv, &export_map)) {
return ola::EXIT_UNAVAILABLE;
}
// We need to block signals before we start any threads.
// Signal setup is complex. First of all we need to install NULL handlers to
// the signals are blocked before we start *any* threads. It's safest if we
// do this before creating the OlaDaemon.
SignalThread signal_thread;
signal_thread.InstallSignalHandler(SIGINT, NULL);
signal_thread.InstallSignalHandler(SIGTERM, NULL);
#ifndef _WIN32
signal_thread.InstallSignalHandler(SIGHUP, NULL);
signal_thread.InstallSignalHandler(
SIGUSR1, ola::NewCallback(&ola::IncrementLogLevel));
#endif // _WIN32
ola::OlaServer::Options options;
options.http_enable = FLAGS_http;
options.http_enable_quit = FLAGS_http_quit;
options.http_port = FLAGS_http_port;
options.http_data_dir = FLAGS_http_data_dir.str();
options.network_interface = FLAGS_interface.str();
options.pid_data_dir = FLAGS_pid_location.str();
std::auto_ptr<OlaDaemon> olad(new OlaDaemon(options, &export_map));
if (!olad.get()) {
return ola::EXIT_UNAVAILABLE;
}
// Now that the OlaDaemon has been created, we can reset the signal handlers
// to do what we actually want them to.
signal_thread.InstallSignalHandler(
SIGINT,
ola::NewCallback(olad->GetSelectServer(),
&ola::io::SelectServer::Terminate));
signal_thread.InstallSignalHandler(
SIGTERM,
ola::NewCallback(olad->GetSelectServer(),
&ola::io::SelectServer::Terminate));
// We can't start the signal thread here, otherwise there is a race
// condition if a signal arrives before we enter the SelectServer Run()
// method. Instead we schedule it to start from the SelectServer loop.
olad->GetSelectServer()->Execute(ola::NewSingleCallback(
&StartSignalThread,
olad->GetSelectServer(),
&signal_thread));
if (!olad->Init()) {
return ola::EXIT_UNAVAILABLE;
}
#ifndef _WIN32
// Finally the OlaServer is not-null.
signal_thread.InstallSignalHandler(
SIGHUP,
ola::NewCallback(olad->GetOlaServer(), &ola::OlaServer::ReloadPlugins));
#endif // _WIN32
#if HAVE_LIBSYSTEMD
if (ola::SystemdNotifyAvailable()) {
OLA_INFO << "Systemd notification socket address present, "
<< "sending notifications.";
} else {
OLA_WARN << "Systemd notification socket address not present, "
<< "not sending notifications.";
}
// Does not need to be guarded. sd_notify does its own internal check on
// the socket's presence, as well.
ola::SystemdNotify(0, "READY=1\nSTATUS=Startup complete\n");
#endif // HAVE_LIBSYSTEMD
olad->Run();
return ola::EXIT_OK;
}