2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
22 * @author Jan Olszak (j.olszak@samsung.com)
23 * @brief Main file for the Vasum Daemon
28 #include "exception.hpp"
31 #include "logger/logger.hpp"
32 #include "logger/backend-stderr.hpp"
33 #include "logger/backend-journal.hpp"
34 #include "logger/backend-syslog.hpp"
35 #include "utils/typeinfo.hpp"
36 #include "utils/signal.hpp"
37 #include "utils/daemon.hpp"
39 #include <boost/program_options.hpp>
42 using namespace logger;
43 using namespace vasum;
45 namespace po = boost::program_options;
50 const std::string PROGRAM_NAME_AND_VERSION =
51 "Vasum Server " PROGRAM_VERSION;
53 const std::string CONFIG_PATH = "/etc/vasum/daemon.conf";
58 int main(int argc, char* argv[])
60 bool runAsRoot = false;
63 const char *defaultLoggingBackend = "stderr";
65 const char *defaultLoggingBackend = "syslog";
67 // 100 - wider terminal, nicer option descriptions
68 po::options_description desc("Allowed options", 100);
71 ("help,h", "print this help")
72 ("root,r", "Don't drop root privileges at startup")
73 ("daemon,d", "Run server as daemon")
74 ("log-level,l", po::value<std::string>()->default_value("DEBUG"), "set log level")
75 ("log-backend,b", po::value<std::string>()->default_value(defaultLoggingBackend),
76 "set log backend [stderr,syslog,file"
81 ("log-file,f", po::value<std::string>()->default_value("vasum.log"),
82 "set filename for file logging, optional")
83 ("check,c", "check runtime environment and exit")
84 ("version,v", "show application version")
88 po::basic_parsed_options< char > parsed =
89 po::command_line_parser(argc, argv).options(desc).allow_unregistered().run();
91 std::vector<std::string> unrecognized_options =
92 po::collect_unrecognized(parsed.options, po::include_positional);
94 if (!unrecognized_options.empty()) {
95 std::cerr << "Unrecognized options: ";
97 for (auto& uo : unrecognized_options) {
98 std::cerr << ' ' << uo;
101 std::cerr << std::endl << std::endl;
102 std::cerr << desc << std::endl;
107 po::store(parsed, vm);
110 if (vm.count("help")) {
111 std::cout << desc << std::endl;
113 } else if (vm.count("version")) {
114 std::cout << PROGRAM_NAME_AND_VERSION << std::endl;
116 } else if (vm.count("check")) {
117 std::cout << "Checking runtime environment..." << std::endl;
118 return Server::checkEnvironment() ? 0 : 1;
121 bool runAsDaemon = vm.count("daemon") > 0;
123 if (runAsDaemon && !utils::daemonize()) {
124 std::cerr << "Failed to daemonize" << std::endl;
128 Logger::setLogLevel(vm["log-level"].as<std::string>());
129 const std::string logBackend = vm["log-backend"].as<std::string>();
130 if(logBackend.compare("stderr") == 0) {
131 Logger::setLogBackend(new StderrBackend());
133 else if(logBackend.compare("file") == 0) {
134 const std::string logFilename = vm["log-file"].as<std::string>();
135 if(logFilename.empty()) {
136 std::cerr << "Error: invalid log file provided for file logging backend" << std::endl;
139 Logger::setLogBackend(new FileBackend(logFilename));
142 else if(logBackend.compare("journal") == 0) {
143 Logger::setLogBackend(new SystemdJournalBackend());
146 else if(logBackend.compare("syslog") == 0) {
147 Logger::setLogBackend(new SyslogBackend());
150 std::cerr << "Error: unrecognized logging backend option: " << logBackend << std::endl;
154 runAsRoot = vm.count("root") > 0;
156 } catch (std::exception& e) {
157 std::cerr << e.what() << std::endl;
163 // Server will unblock handled signals
164 utils::signalBlockAllExcept({SIGTERM});
166 // TODO: SIGTERM used by lxc, get rid of this
167 utils::signalIgnore({SIGTERM});
169 LOGI("Starting daemon...");
170 Server server(CONFIG_PATH);
171 server.run(runAsRoot);
172 server.reloadIfRequired(argv);
173 LOGI("Daemon stopped");
175 } catch (std::exception& e) {
176 LOGE("Unexpected: " << utils::getTypeName(e) << ": " << e.what());