2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Rafal Krypa <r.krypa@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
19 * @file server-main.cpp
20 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
22 * @brief Implementation of security-manager on basis of security-server
27 #include <dpl/log/log.h>
28 #include <dpl/singleton.h>
29 #include <dpl/singleton_safe_impl.h>
31 #include <boost/program_options.hpp>
34 #include <socket-manager.h>
35 #include <file-lock.h>
38 #include <master-service.h>
40 namespace po = boost::program_options;
42 IMPLEMENT_SAFE_SINGLETON(SecurityManager::Log::LogSystem);
44 #define REGISTER_SOCKET_SERVICE(manager, service, allocator) \
45 registerSocketService<service>(manager, #service, allocator)
48 bool registerSocketService(SecurityManager::SocketManager &manager,
49 const std::string& serviceName,
50 const std::function<T*(void)>& serviceAllocator)
54 service = serviceAllocator();
56 manager.RegisterSocketService(service);
58 } catch (const SecurityManager::Exception &exception) {
59 LogError("Error in creating service " << serviceName <<
60 ", details:\n" << exception.DumpToString());
61 } catch (const std::exception& e) {
62 LogError("Error in creating service " << serviceName <<
63 ", details:\n" << e.what());
65 LogError("Error in creating service " << serviceName <<
66 ", unknown exception occured");
73 int main(int argc, char* argv[])
75 UNHANDLED_EXCEPTION_HANDLER_BEGIN
78 SecurityManager::Singleton<SecurityManager::Log::LogSystem>::Instance().SetTag("SECURITY_MANAGER");
81 bool masterMode = false, slaveMode = false;
82 po::options_description optDesc("Allowed options");
85 ("help,h", "Print this help message")
86 ("master,m", "Enable master mode")
87 ("slave,s", "Enable slave mode")
91 po::basic_parsed_options<char> parsed =
92 po::command_line_parser(argc, argv).options(optDesc).allow_unregistered().run();
94 std::vector<std::string> unrecognizedOptions =
95 po::collect_unrecognized(parsed.options, po::include_positional);
97 if (!unrecognizedOptions.empty()) {
98 std::cerr << "Unrecognized options: ";
100 for (auto& uo : unrecognizedOptions) {
101 std::cerr << ' ' << uo;
104 std::cerr << std::endl << std::endl;
105 std::cerr << optDesc << std::endl;
110 po::store(parsed, vm);
113 if (vm.count("help")) {
114 std::cout << optDesc << std::endl;
118 masterMode = vm.count("master") > 0;
119 slaveMode = vm.count("slave") > 0;
121 if (masterMode && slaveMode) {
122 LogError("Cannot be both master and slave!");
126 SecurityManager::FileLocker serviceLock(SecurityManager::SERVICE_LOCK_FILE,
131 sigaddset(&mask, SIGTERM);
132 sigaddset(&mask, SIGPIPE);
133 if (-1 == pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
134 LogError("Error in pthread_sigmask");
139 SecurityManager::SocketManager manager;
142 if (!REGISTER_SOCKET_SERVICE(manager, SecurityManager::MasterService,
143 []() { return new SecurityManager::MasterService(); } )) {
144 LogError("Unable to create master socket service. Exiting.");
148 if (!REGISTER_SOCKET_SERVICE(manager, SecurityManager::Service,
149 [&slaveMode]() { return new SecurityManager::Service(slaveMode); } )) {
150 LogError("Unable to create socket service. Exiting.");
156 } catch (const SecurityManager::FileLocker::Exception::Base &e) {
157 LogError("Unable to get a file lock. Exiting.");
160 UNHANDLED_EXCEPTION_HANDLER_END