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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Init.cpp
* A grab bag of functions useful for programs.
* Copyright (C) 2012 Simon Newton
*
* Maybe one day this will handle command line parsing as well. Wouldn't that
* be nice.
*/
/**
* @addtogroup init
* @{
* @file Init.cpp
* @}
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
#endif // HAVE_EXECINFO_H
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#ifdef _WIN32
#include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <ola/win/CleanWinSock2.h>
#else
#include <sys/resource.h>
#endif // _WIN32
#include <unistd.h>
#include <ola/Clock.h>
#include <ola/ExportMap.h>
#include <ola/Logging.h>
#include <ola/StringUtils.h>
#include <ola/base/Flags.h>
#include <ola/base/Init.h>
#include <ola/base/SysExits.h>
#include <ola/math/Random.h>
#include <ola/thread/Utils.h>
#include <ola/system/Limits.h>
#include <iomanip>
#include <iostream>
#include <string>
// Scheduling options.
DEFINE_string(scheduler_policy, "",
"The thread scheduling policy, one of {fifo, rr}.");
DEFINE_uint16(scheduler_priority, 0,
"The thread priority, only used if --scheduler-policy is set.");
namespace {
using std::cout;
using std::endl;
using std::string;
/*
* @private
* @brief Print a stack trace.
*/
static void _DumpStackAndExit(int sig) {
#ifdef _WIN32
cout << "Received " << sig << endl;
#else
cout << "Received " << strsignal(sig) << endl;
#endif // _WIN32
#ifdef HAVE_EXECINFO_H
enum {STACK_SIZE = 64};
void *array[STACK_SIZE];
size_t size = backtrace(array, STACK_SIZE);
backtrace_symbols_fd(array, size, STDERR_FILENO);
#endif // HAVE_EXECINFO_H
exit(ola::EXIT_SOFTWARE);
}
bool SetThreadScheduling() {
string policy_str = FLAGS_scheduler_policy.str();
ola::ToLower(&policy_str);
if (policy_str.empty()) {
if (FLAGS_scheduler_priority.present()) {
OLA_WARN << "Must provide both of --scheduler-policy & "
"--scheduler-priority";
return false;
}
return true;
}
int policy = 0;
if (policy_str == "fifo") {
policy = SCHED_FIFO;
} else if (policy_str == "rr") {
policy = SCHED_RR;
} else {
OLA_FATAL << "Unknown scheduling policy " << policy_str;
return false;
}
if (!FLAGS_scheduler_priority.present()) {
OLA_WARN << "Must provide both of --scheduler-policy & "
"--scheduler-priority";
return false;
}
int requested_priority = FLAGS_scheduler_priority;
#ifdef _POSIX_PRIORITY_SCHEDULING
int min = sched_get_priority_min(policy);
int max = sched_get_priority_max(policy);
if (requested_priority < min) {
OLA_WARN << "Minimum value for --scheduler-priority is " << min;
return false;
}
if (requested_priority > max) {
OLA_WARN << "Maximum value for --scheduler-priority is " << max;
return false;
}
#endif // _POSIX_PRIORITY_SCHEDULING
// Set the scheduling parameters.
struct sched_param param;
param.sched_priority = requested_priority;
OLA_INFO << "Scheduling policy is " << ola::thread::PolicyToString(policy)
<< ", priority " << param.sched_priority;
bool ok = ola::thread::SetSchedParam(pthread_self(), policy, param);
if (!ok) {
return false;
}
// If RLIMIT_RTTIME is available, set a bound on the length of uninterrupted
// CPU time we can consume.
#if HAVE_DECL_RLIMIT_RTTIME
struct rlimit rlim;
if (!ola::system::GetRLimit(RLIMIT_RTTIME, &rlim)) {
return false;
}
// Cap CPU time at 1s.
rlim.rlim_cur = 1000000;
OLA_DEBUG << "Setting RLIMIT_RTTIME " << rlim.rlim_cur << " / "
<< rlim.rlim_max;
if (!ola::system::SetRLimit(RLIMIT_RTTIME, rlim)) {
return false;
}
if (!ola::InstallSignal(SIGXCPU, _DumpStackAndExit)) {
return false;
}
#endif // HAVE_DECL_RLIMIT_RTTIME
return true;
}
} // namespace
namespace ola {
/**
* @addtogroup init
* @{
*/
using std::cout;
using std::endl;
using std::string;
bool ServerInit(int argc, char *argv[], ExportMap *export_map) {
ClockInit();
ola::math::InitRandom();
if (!InstallSEGVHandler()) {
return false;
}
if (export_map) {
InitExportMap(argc, argv, export_map);
}
return SetThreadScheduling() && NetworkInit();
}
bool ServerInit(int *argc,
char *argv[],
ExportMap *export_map,
const string &first_line,
const string &description) {
// Take a copy of the arguments otherwise the export map is incorrect.
int original_argc = *argc;
char *original_argv[original_argc];
for (int i = 0; i < original_argc; i++) {
original_argv[i] = argv[i];
}
SetHelpString(first_line, description);
ParseFlags(argc, argv);
InitLoggingFromFlags();
return ServerInit(original_argc, original_argv, export_map);
}
bool AppInit(int *argc,
char *argv[],
const string &first_line,
const string &description) {
ClockInit();
ola::math::InitRandom();
SetHelpString(first_line, description);
ParseFlags(argc, argv);
InitLoggingFromFlags();
if (!InstallSEGVHandler()) {
return false;
}
return SetThreadScheduling() && NetworkInit();
}
#ifdef _WIN32
static void NetworkShutdown() {
WSACleanup();
}
#endif // _WIN32
bool NetworkInit() {
#ifdef _WIN32
WSADATA wsa_data;
int result = WSAStartup(MAKEWORD(2, 0), &wsa_data);
if (result != 0) {
OLA_WARN << "WinSock initialization failed with " << result;
return false;
} else {
atexit(NetworkShutdown);
return true;
}
#else
return true;
#endif // _WIN32
}
bool InstallSignal(int sig, void(*fp)(int signo)) {
#ifdef _WIN32
if (::signal(sig, fp) == SIG_ERR) {
OLA_WARN << "signal(" << sig << ": " << strerror(errno);
return false;
}
#else
struct sigaction action;
action.sa_handler = fp;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
if (sigaction(sig, &action, NULL) < 0) {
OLA_WARN << "sigaction(" << strsignal(sig) << ": " << strerror(errno);
return false;
}
#endif // _WIN32
return true;
}
bool InstallSEGVHandler() {
#ifndef _WIN32
if (!InstallSignal(SIGBUS, _DumpStackAndExit)) {
return false;
}
#endif // !_WIN32
if (!InstallSignal(SIGSEGV, _DumpStackAndExit)) {
return false;
}
return true;
}
void InitExportMap(int argc, char* argv[], ExportMap *export_map) {
ola::StringVariable *var = export_map->GetStringVar("binary");
var->Set(argv[0]);
var = export_map->GetStringVar("cmd-line");
std::ostringstream out;
for (int i = 1; i < argc; i++) {
out << argv[i] << " ";
}
var->Set(out.str());
var = export_map->GetStringVar("fd-limit");
#ifdef _WIN32
{
std::ostringstream out;
out << _getmaxstdio();
var->Set(out.str());
}
#else
struct rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
var->Set("undetermined");
} else {
std::ostringstream out;
out << rl.rlim_cur;
var->Set(out.str());
}
#endif // _WIN32
}
void Daemonise() {
#ifndef _WIN32
struct rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
OLA_FATAL << "Could not determine file limit";
exit(EXIT_OSFILE);
}
// fork so we're not the process group leader
pid_t pid;
if ((pid = fork()) < 0) {
OLA_FATAL << "Could not fork\n";
exit(EXIT_OSERR);
} else if (pid != 0) {
exit(EXIT_OK);
}
// start a new session so we're the session leader and we free ourselves from
// the controlling terminal.
setsid();
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGHUP, &sa, NULL) < 0) {
OLA_FATAL << "Could not install signal\n";
exit(EXIT_OSERR);
}
// fork to ensure we're not the session leader.
if ((pid = fork()) < 0) {
OLA_FATAL << "Could not fork\n";
exit(EXIT_OSERR);
} else if (pid != 0) {
exit(EXIT_OK);
}
// change the current working directory
if (chdir("/") < 0) {
OLA_FATAL << "Can't change directory to /";
exit(EXIT_OSERR);
}
// close all fds
int maxfd = sysconf(_SC_OPEN_MAX);
if (maxfd == -1) {
if (rl.rlim_max == RLIM_INFINITY) {
maxfd = 1024;
} else {
maxfd = rl.rlim_max;
}
}
for (int fd = 0; fd < maxfd; fd++) {
close(fd);
}
// send stdout, in and err to /dev/null
int fd0 = open("/dev/null", O_RDWR);
int fd1 = dup(0);
int fd2 = dup(0);
if (fd0 != STDIN_FILENO || fd1 != STDOUT_FILENO || fd2 != STDERR_FILENO) {
OLA_FATAL << "Unexpected file descriptors: " << fd0 << ", " << fd1
<< ", " << fd2;
exit(EXIT_OSERR);
}
#endif // _WIN32
}
void ClockInit() {
Clock clock;
TimeStamp now_monotonic;
TimeStamp now_realtime;
#ifndef CLOCK_MONOTONIC
OLA_DEBUG << "Monotonic clock unavailable. Falling back to real time clock.";
#endif
clock.CurrentMonotonicTime(&now_monotonic);
clock.CurrentRealTime(&now_realtime);
OLA_DEBUG << "Monotonic clock: " << std::setw(18) << now_monotonic;
OLA_DEBUG << "Real clock : " << std::setw(18) << now_realtime;
}
/**@}*/
} // namespace ola
|