]> granicus.if.org Git - icinga2/blob - icinga-app/icinga.cpp
Make sure that 'icinga2 console' serializes temporary attributes (rather than just...
[icinga2] / icinga-app / icinga.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "cli/clicommand.hpp"
21 #include "config/configcompilercontext.hpp"
22 #include "config/configcompiler.hpp"
23 #include "config/configitembuilder.hpp"
24 #include "base/application.hpp"
25 #include "base/logger.hpp"
26 #include "base/timer.hpp"
27 #include "base/utility.hpp"
28 #include "base/exception.hpp"
29 #include "base/convert.hpp"
30 #include "base/scriptglobal.hpp"
31 #include "base/context.hpp"
32 #include "base/console.hpp"
33 #include "config.h"
34 #include <boost/program_options.hpp>
35 #include <boost/tuple/tuple.hpp>
36 #include <boost/foreach.hpp>
37
38 #ifndef _WIN32
39 #       include <sys/types.h>
40 #       include <pwd.h>
41 #       include <grp.h>
42 #endif /* _WIN32 */
43
44 using namespace icinga;
45 namespace po = boost::program_options;
46
47 #ifdef _WIN32
48 SERVICE_STATUS l_SvcStatus;
49 SERVICE_STATUS_HANDLE l_SvcStatusHandle;
50 #endif /* _WIN32 */
51
52 static std::vector<String> GetLogLevelCompletionSuggestions(const String& arg)
53 {
54         std::vector<String> result;
55         
56         String debugLevel = "debug";
57         if (debugLevel.Find(arg) == 0)
58                 result.push_back(debugLevel);
59
60         String noticeLevel = "notice";
61         if (noticeLevel.Find(arg) == 0)
62                 result.push_back(noticeLevel);
63
64         String informationLevel = "information";
65         if (informationLevel.Find(arg) == 0)
66                 result.push_back(informationLevel);
67
68         String warningLevel = "warning";
69         if (warningLevel.Find(arg) == 0)
70                 result.push_back(warningLevel);
71
72         String criticalLevel = "critical";
73         if (criticalLevel.Find(arg) == 0)
74                 result.push_back(criticalLevel);
75
76         return result;
77 }
78
79 static std::vector<String> GlobalArgumentCompletion(const String& argument, const String& word)
80 {
81         if (argument == "include")
82                 return GetBashCompletionSuggestions("directory", word);
83         else if (argument == "log-level")
84                 return GetLogLevelCompletionSuggestions(word);
85         else
86                 return std::vector<String>();
87 }
88
89 int Main(void)
90 {
91         int argc = Application::GetArgC();
92         char **argv = Application::GetArgV();
93
94         bool autocomplete = false;
95         int autoindex = 0;
96
97         if (argc >= 4 && strcmp(argv[1], "--autocomplete") == 0) {
98                 autocomplete = true;
99
100                 try {
101                         autoindex = Convert::ToLong(argv[2]);
102                 } catch (const std::invalid_argument& ex) {
103                         Log(LogCritical, "icinga-app")
104                             << "Invalid index for --autocomplete: " << argv[2];
105                         return EXIT_FAILURE;
106                 }
107
108                 argc -= 3;
109                 argv += 3;
110         }
111
112         Application::SetStartTime(Utility::GetTime());
113
114         if (!autocomplete)
115                 Application::SetResourceLimits();
116
117         /* Set thread title. */
118         Utility::SetThreadName("Main Thread", false);
119
120         /* Install exception handlers to make debugging easier. */
121         Application::InstallExceptionHandlers();
122
123 #ifdef _WIN32
124         bool builtinPaths = true;
125
126         HKEY hKey;
127         if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Icinga Development Team\\ICINGA2", 0,
128             KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
129                 BYTE pvData[MAX_PATH];
130                 DWORD cbData = sizeof(pvData)-1;
131                 DWORD lType;
132                 if (RegQueryValueEx(hKey, NULL, NULL, &lType, pvData, &cbData) == ERROR_SUCCESS && lType == REG_SZ) {
133                         pvData[cbData] = '\0';
134
135                         String prefix = (char *)pvData;
136                         Application::DeclarePrefixDir(prefix);
137                         Application::DeclareSysconfDir(prefix + "\\etc");
138                         Application::DeclareRunDir(prefix + "\\var\\run");
139                         Application::DeclareLocalStateDir(prefix + "\\var");
140                         Application::DeclarePkgDataDir(prefix + "\\share\\icinga2");
141                         Application::DeclareIncludeConfDir(prefix + "\\share\\icinga2\\include");
142
143                         builtinPaths = false;
144                 }
145
146                 RegCloseKey(hKey);
147         }
148
149         if (builtinPaths) {
150                 Log(LogWarning, "icinga-app", "Registry key could not be read. Falling back to built-in paths.");
151
152 #endif /* _WIN32 */
153                 Application::DeclarePrefixDir(ICINGA_PREFIX);
154                 Application::DeclareSysconfDir(ICINGA_SYSCONFDIR);
155                 Application::DeclareRunDir(ICINGA_RUNDIR);
156                 Application::DeclareLocalStateDir(ICINGA_LOCALSTATEDIR);
157                 Application::DeclarePkgDataDir(ICINGA_PKGDATADIR);
158                 Application::DeclareIncludeConfDir(ICINGA_INCLUDECONFDIR);
159 #ifdef _WIN32
160         }
161 #endif /* _WIN32 */
162
163         Application::DeclareZonesDir(Application::GetSysconfDir() + "/icinga2/zones.d");
164         Application::DeclareApplicationType("icinga/IcingaApplication");
165         Application::DeclareRunAsUser(ICINGA_USER);
166         Application::DeclareRunAsGroup(ICINGA_GROUP);
167         Application::DeclareConcurrency(boost::thread::hardware_concurrency());
168
169         LogSeverity logLevel = Logger::GetConsoleLogSeverity();
170         Logger::SetConsoleLogSeverity(LogWarning);
171
172         Utility::LoadExtensionLibrary("cli");
173
174         po::options_description visibleDesc("Global options");
175
176         visibleDesc.add_options()
177                 ("help,h", "show this help message")
178                 ("version,V", "show version information")
179 #ifndef _WIN32
180                 ("color", "use VT100 color codes even when stdout is not a terminal")
181 #endif /* _WIN32 */
182                 ("define,D", po::value<std::vector<std::string> >(), "define a constant")
183                 ("library,l", po::value<std::vector<std::string> >(), "load a library")
184                 ("include,I", po::value<std::vector<std::string> >(), "add include search directory")
185                 ("log-level,x", po::value<std::string>(), "specify the log level for the console log");
186
187         po::options_description hiddenDesc("Hidden options");
188
189         hiddenDesc.add_options()
190 #ifndef _WIN32
191                 ("no-stack-rlimit", "used internally, do not specify manually")
192 #else /* _WIN32 */
193                 ("no-stack-rlimit", "used internally, do not specify manually")
194 #endif /* _WIN32 */
195                 ("arg", po::value<std::vector<std::string> >(), "positional argument");
196
197         po::positional_options_description positionalDesc;
198         positionalDesc.add("arg", -1);
199
200         String cmdname;
201         CLICommand::Ptr command;
202         po::variables_map vm;
203
204         try {
205                 CLICommand::ParseCommand(argc, argv, visibleDesc, hiddenDesc, positionalDesc,
206                     vm, cmdname, command, autocomplete);
207         } catch (const std::exception& ex) {
208                 Log(LogCritical, "icinga-app")
209                     << "Error while parsing command-line options: " << ex.what();
210                 return EXIT_FAILURE;
211         }
212
213         String initconfig = Application::GetSysconfDir() + "/icinga2/init.conf";
214
215         if (Utility::PathExists(initconfig)) {
216                 Expression *expression;
217                 try {
218                         expression = ConfigCompiler::CompileFile(initconfig);
219
220                         ScriptFrame frame;
221                         expression->Evaluate(frame);
222                 } catch (const std::exception& ex) {
223                         delete expression;
224
225                         Log(LogCritical, "config", DiagnosticInformation(ex));
226                         return EXIT_FAILURE;
227                 }
228
229                 delete expression;
230         }
231
232 #ifndef _WIN32
233         if (vm.count("color")) {
234                 Console::SetType(std::cout, Console_VT100);
235                 Console::SetType(std::cerr, Console_VT100);
236         }
237 #endif /* _WIN32 */
238
239         if (vm.count("define")) {
240                 BOOST_FOREACH(const String& define, vm["define"].as<std::vector<std::string> >()) {
241                         String key, value;
242                         size_t pos = define.FindFirstOf('=');
243                         if (pos != String::NPos) {
244                                 key = define.SubStr(0, pos);
245                                 value = define.SubStr(pos + 1);
246                         } else {
247                                 key = define;
248                                 value = "1";
249                         }
250                         ScriptGlobal::Set(key, value);
251                 }
252         }
253
254         Application::DeclareStatePath(Application::GetLocalStateDir() + "/lib/icinga2/icinga2.state");
255         Application::DeclareObjectsPath(Application::GetLocalStateDir() + "/cache/icinga2/icinga2.debug");
256         Application::DeclareVarsPath(Application::GetLocalStateDir() + "/cache/icinga2/icinga2.vars");
257         Application::DeclarePidPath(Application::GetRunDir() + "/icinga2/icinga2.pid");
258
259         ConfigCompiler::AddIncludeSearchDir(Application::GetIncludeConfDir());
260
261         if (!autocomplete && vm.count("include")) {
262                 BOOST_FOREACH(const String& includePath, vm["include"].as<std::vector<std::string> >()) {
263                         ConfigCompiler::AddIncludeSearchDir(includePath);
264                 }
265         }
266
267         if (!autocomplete) {
268                 Logger::SetConsoleLogSeverity(logLevel);
269
270                 if (vm.count("log-level")) {
271                         String severity = vm["log-level"].as<std::string>();
272
273                         LogSeverity logLevel = LogInformation;
274                         try {
275                                 logLevel = Logger::StringToSeverity(severity);
276                         } catch (std::exception&) {
277                                 /* Inform user and exit */
278                                 Log(LogCritical, "icinga-app", "Invalid log level set. Default is 'information'.");
279                                 return EXIT_FAILURE;
280                         }
281
282                         Logger::SetConsoleLogSeverity(logLevel);
283                 }
284
285                 if (vm.count("library")) {
286                         BOOST_FOREACH(const String& libraryName, vm["library"].as<std::vector<std::string> >()) {
287                                 try {
288                                         (void) Utility::LoadExtensionLibrary(libraryName);
289                                 } catch (const std::exception& ex) {
290                                         Log(LogCritical, "icinga-app")
291                                             <<  "Could not load library \"" << libraryName << "\": " << DiagnosticInformation(ex);
292                                         return EXIT_FAILURE;
293                                 }
294                         }
295                 } 
296
297                 if (!command || vm.count("help") || vm.count("version")) {
298                         String appName;
299
300                         try {
301                                 appName = Utility::BaseName(Application::GetArgV()[0]);
302                         } catch (const std::bad_alloc&) {
303                                 Log(LogCritical, "icinga-app", "Allocation failed.");
304                                 return EXIT_FAILURE;
305                         }
306
307                         if (appName.GetLength() > 3 && appName.SubStr(0, 3) == "lt-")
308                                 appName = appName.SubStr(3, appName.GetLength() - 3);
309
310                         std::cout << appName << " " << "- The Icinga 2 network monitoring daemon (version: "
311                             << ConsoleColorTag(vm.count("version") ? Console_ForegroundRed : Console_Normal)
312                             << Application::GetVersion()
313 #ifdef I2_DEBUG
314                             << "; debug"
315 #endif /* I2_DEBUG */
316                             << ConsoleColorTag(Console_Normal)
317                             << ")" << std::endl << std::endl;
318
319                         if ((!command || vm.count("help")) && !vm.count("version")) {
320                                 std::cout << "Usage:" << std::endl
321                                     << "  " << argv[0] << " ";
322
323                                 if (cmdname.IsEmpty())
324                                         std::cout << "<command>";
325                                 else
326                                         std::cout << cmdname;
327
328                                 std::cout << " [<arguments>]" << std::endl;
329
330                                 if (command) {
331                                         std::cout << std::endl
332                                                   << command->GetDescription() << std::endl;
333                                 }
334                         }
335
336                         if (vm.count("version")) {
337                                 std::cout << "Copyright (c) 2012-2015 Icinga Development Team (https://www.icinga.org)" << std::endl
338                                         << "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl2.html>" << std::endl
339                                         << "This is free software: you are free to change and redistribute it." << std::endl
340                                         << "There is NO WARRANTY, to the extent permitted by law.";
341                         }
342
343                         std::cout << std::endl;
344
345                         if (vm.count("version")) {
346                                 std::cout << std::endl;
347
348                                 Application::DisplayInfoMessage(std::cout, true);
349
350                                 return EXIT_SUCCESS;
351                         }
352                 }
353
354                 if (!command || vm.count("help")) {
355                         if (!command)
356                                 CLICommand::ShowCommands(argc, argv, NULL);
357
358                         std::cout << visibleDesc << std::endl
359                                 << "Report bugs at <https://dev.icinga.org/>" << std::endl
360                                 << "Icinga home page: <https://www.icinga.org/>" << std::endl;
361                         return EXIT_SUCCESS;
362                 }
363         }
364
365         int rc = 1;
366
367         if (autocomplete) {
368                 CLICommand::ShowCommands(argc, argv, &visibleDesc, &hiddenDesc,
369                     &GlobalArgumentCompletion, true, autoindex);
370                 rc = 0;
371         } else if (command) {
372                 Logger::DisableTimestamp(true);
373 #ifndef _WIN32
374                 if (command->GetImpersonationLevel() == ImpersonateRoot) {
375                         if (getuid() != 0) {
376                                 Log(LogCritical, "cli", "This command must be run as root.");
377                                 return 0;
378                         }
379                 } else if (command && command->GetImpersonationLevel() == ImpersonateIcinga) {
380                         String group = Application::GetRunAsGroup();
381                         String user = Application::GetRunAsUser();
382         
383                         errno = 0;
384                         struct group *gr = getgrnam(group.CStr());
385         
386                         if (!gr) {
387                                 if (errno == 0) {
388                                         Log(LogCritical, "cli")
389                                             << "Invalid group specified: " << group;
390                                         return EXIT_FAILURE;
391                                 } else {
392                                         Log(LogCritical, "cli")
393                                             << "getgrnam() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
394                                         return EXIT_FAILURE;
395                                 }
396                         }
397         
398                         if (getgid() != gr->gr_gid) {
399                                 if (!vm.count("reload-internal") && setgroups(0, NULL) < 0) {
400                                         Log(LogCritical, "cli")
401                                             << "setgroups() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
402                                         Log(LogCritical, "cli")
403                                             << "Please re-run this command as a privileged user or using the \"" << user << "\" account.";
404                                         return EXIT_FAILURE;
405                                 }
406         
407                                 if (setgid(gr->gr_gid) < 0) {
408                                         Log(LogCritical, "cli")
409                                             << "setgid() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
410                                         return EXIT_FAILURE;
411                                 }
412                         }
413         
414                         errno = 0;
415                         struct passwd *pw = getpwnam(user.CStr());
416         
417                         if (!pw) {
418                                 if (errno == 0) {
419                                         Log(LogCritical, "cli")
420                                             << "Invalid user specified: " << user;
421                                         return EXIT_FAILURE;
422                                 } else {
423                                         Log(LogCritical, "cli")
424                                             << "getpwnam() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
425                                         return EXIT_FAILURE;
426                                 }
427                         }
428         
429                         // also activate the additional groups the configured user is member of
430                         if (getuid() != pw->pw_uid) {
431                                 if (!vm.count("reload-internal") && initgroups(user.CStr(), pw->pw_gid) < 0) {
432                                         Log(LogCritical, "cli")
433                                             << "initgroups() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
434                                         Log(LogCritical, "cli")
435                                             << "Please re-run this command as a privileged user or using the \"" << user << "\" account.";
436                                         return EXIT_FAILURE;
437                                 }
438         
439                                 if (setuid(pw->pw_uid) < 0) {
440                                         Log(LogCritical, "cli")
441                                             << "setuid() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
442                                         Log(LogCritical, "cli")
443                                             << "Please re-run this command as a privileged user or using the \"" << user << "\" account.";
444                                         return EXIT_FAILURE;
445                                 }
446                         }
447                 }
448 #endif /* _WIN32 */
449
450                 std::vector<std::string> args;
451                 if (vm.count("arg"))
452                         args = vm["arg"].as<std::vector<std::string> >();
453
454                 if (args.size() < command->GetMinArguments()) {
455                         Log(LogCritical, "cli")
456                             << "Too few arguments. Command needs at least " << command->GetMinArguments()
457                             << " argument" << (command->GetMinArguments() != 1 ? "s" : "") << ".";
458                         return EXIT_FAILURE;
459                 }
460
461                 if (command->GetMaxArguments() >= 0 && args.size() > command->GetMaxArguments()) {
462                         Log(LogCritical, "cli")
463                             << "Too many arguments. At most " << command->GetMaxArguments()
464                             << " argument" << (command->GetMaxArguments() != 1 ? "s" : "") << " may be specified.";
465                         return EXIT_FAILURE;
466                 }
467
468                 rc = command->Run(vm, args);
469         }
470
471         return rc;
472 }
473
474 #ifdef _WIN32
475 static int SetupService(bool install, int argc, char **argv)
476 {
477         SC_HANDLE schSCManager = OpenSCManager(
478                 NULL,
479                 NULL,
480                 SC_MANAGER_ALL_ACCESS);
481
482         if (NULL == schSCManager) {
483                 printf("OpenSCManager failed (%d)\n", GetLastError());
484                 return 1;
485         }
486
487         TCHAR szPath[MAX_PATH];
488
489         if (!GetModuleFileName(NULL, szPath, MAX_PATH)) {
490                 printf("Cannot install service (%d)\n", GetLastError());
491                 return 1;
492         }
493
494         String szArgs;
495         szArgs = Utility::EscapeShellArg(szPath) + " --scm";
496
497         for (int i = 0; i < argc; i++)
498                 szArgs += " " + Utility::EscapeShellArg(argv[i]);
499
500         SC_HANDLE schService = OpenService(schSCManager, "icinga2", DELETE | SERVICE_STOP | SERVICE_QUERY_STATUS);
501
502         if (schService != NULL) {
503                 SERVICE_STATUS status;
504                 ControlService(schService, SERVICE_CONTROL_STOP, &status);
505
506                 double start = Utility::GetTime();
507                 while (status.dwCurrentState != SERVICE_STOPPED) {
508                         double end = Utility::GetTime();
509
510                         if (end - start > 30) {
511                                 printf("Could not stop the service.\n");
512                                 break;
513                         }
514
515                         Utility::Sleep(5);
516
517                         if (!QueryServiceStatus(schService, &status)) {
518                                 printf("QueryServiceStatus failed (%d)\n", GetLastError());
519                                 return 1;
520                         }
521                 }
522
523                 if (!DeleteService(schService)) {
524                         printf("DeleteService failed (%d)\n", GetLastError());
525                         CloseServiceHandle(schService);
526                         CloseServiceHandle(schSCManager);
527                         return 1;
528                 }
529
530                 if (!install)
531                         printf("Service uninstalled successfully\n");
532
533                 CloseServiceHandle(schService);
534         }
535
536         if (install) {
537                 schService = CreateService(
538                         schSCManager,
539                         "icinga2",
540                         "Icinga 2",
541                         SERVICE_ALL_ACCESS,
542                         SERVICE_WIN32_OWN_PROCESS,
543                         SERVICE_DEMAND_START,
544                         SERVICE_ERROR_NORMAL,
545                         szArgs.CStr(),
546                         NULL,
547                         NULL,
548                         NULL,
549                         "NT AUTHORITY\\NetworkService",
550                         NULL);
551
552                 if (schService == NULL) {
553                         printf("CreateService failed (%d)\n", GetLastError());
554                         CloseServiceHandle(schSCManager);
555                         return 1;
556                 } else
557                         printf("Service installed successfully\n");
558
559                 ChangeServiceConfig(schService, SERVICE_NO_CHANGE, SERVICE_AUTO_START,
560                     SERVICE_ERROR_NORMAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
561
562                 SERVICE_DESCRIPTION sdDescription = { "The Icinga 2 monitoring application" };
563                 ChangeServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, &sdDescription);
564
565                 if (!StartService(schService, 0, NULL)) {
566                         printf("StartService failed (%d)\n", GetLastError());
567                         CloseServiceHandle(schService);
568                         CloseServiceHandle(schSCManager);
569                         return 1;
570                 }
571
572                 CloseServiceHandle(schService);
573         }
574
575         CloseServiceHandle(schSCManager);
576
577         return 0;
578 }
579
580 VOID ReportSvcStatus(DWORD dwCurrentState,
581         DWORD dwWin32ExitCode,
582         DWORD dwWaitHint)
583 {
584         static DWORD dwCheckPoint = 1;
585
586         l_SvcStatus.dwCurrentState = dwCurrentState;
587         l_SvcStatus.dwWin32ExitCode = dwWin32ExitCode;
588         l_SvcStatus.dwWaitHint = dwWaitHint;
589
590         if (dwCurrentState == SERVICE_START_PENDING)
591                 l_SvcStatus.dwControlsAccepted = 0;
592         else
593                 l_SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
594
595         if ((dwCurrentState == SERVICE_RUNNING) ||
596             (dwCurrentState == SERVICE_STOPPED))
597                 l_SvcStatus.dwCheckPoint = 0;
598         else
599                 l_SvcStatus.dwCheckPoint = dwCheckPoint++;
600
601         SetServiceStatus(l_SvcStatusHandle, &l_SvcStatus);
602 }
603
604 VOID WINAPI ServiceControlHandler(DWORD dwCtrl)
605 {
606         if (dwCtrl == SERVICE_CONTROL_STOP) {
607                 ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
608                 Application::RequestShutdown();
609         }
610 }
611
612 VOID WINAPI ServiceMain(DWORD argc, LPSTR *argv)
613 {
614         l_SvcStatusHandle = RegisterServiceCtrlHandler(
615                 "icinga2",
616                 ServiceControlHandler);
617
618         l_SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
619         l_SvcStatus.dwServiceSpecificExitCode = 0;
620
621         ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0);
622
623         int rc = Main();
624
625         ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, rc);
626
627         Application::Exit(rc);
628 }
629 #endif /* _WIN32 */
630
631 /**
632 * Entry point for the Icinga application.
633 *
634 * @params argc Number of command line arguments.
635 * @params argv Command line arguments.
636 * @returns The application's exit status.
637 */
638 int main(int argc, char **argv)
639 {
640 #ifndef _WIN32
641         rlimit rl;
642         if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) {
643                 rlim_t maxfds = rl.rlim_max;
644
645                 if (maxfds == RLIM_INFINITY)
646                         maxfds = 65536;
647
648                 for (rlim_t i = 3; i < maxfds; i++) {
649                         int rc = close(i);
650
651 #ifdef I2_DEBUG
652                         if (rc >= 0)
653                                 std::cerr << "Closed FD " << i << " which we inherited from our parent process." << std::endl;
654 #endif /* I2_DEBUG */
655                 }
656         }
657 #endif /* _WIN32 */
658
659         /* must be called before using any other libbase functions */
660         Application::InitializeBase();
661
662         /* Set command-line arguments. */
663         Application::SetArgC(argc);
664         Application::SetArgV(argv);
665
666 #ifdef _WIN32
667         if (argc > 1 && strcmp(argv[1], "--scm-install") == 0) {
668                 return SetupService(true, argc - 2, &argv[2]);
669         }
670
671         if (argc > 1 && strcmp(argv[1], "--scm-uninstall") == 0) {
672                 return SetupService(false, argc - 2, &argv[2]);
673         }
674
675         if (argc > 1 && strcmp(argv[1], "--scm") == 0) {
676                 SERVICE_TABLE_ENTRY dispatchTable[] = {
677                         { "icinga2", ServiceMain },
678                         { NULL, NULL }
679                 };
680
681                 StartServiceCtrlDispatcher(dispatchTable);
682                 Application::Exit(EXIT_FAILURE);
683         }
684 #endif /* _WIN32 */
685
686         int rc = Main();
687
688         Application::Exit(rc);
689 }