5c34d3f682e6d1b69b88c948ba86143b8ff1a9ff
[platform/core/security/security-manager.git] / src / server / main / server-main.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Rafal Krypa <r.krypa@samsung.com>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18 /*
19  * @file        server-main.cpp
20  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
21  * @version     1.0
22  * @brief       Implementation of security-manager on basis of security-server
23  */
24 #include <stdlib.h>
25 #include <signal.h>
26
27 #include <dpl/log/log.h>
28 #include <dpl/singleton.h>
29 #include <dpl/singleton_safe_impl.h>
30
31 #include <socket-manager.h>
32 #include <file-lock.h>
33
34 #include <service.h>
35
36 IMPLEMENT_SAFE_SINGLETON(SecurityManager::Log::LogSystem);
37
38 #define REGISTER_SOCKET_SERVICE(manager, service) \
39     registerSocketService<service>(manager, #service)
40
41 template<typename T>
42 void registerSocketService(SecurityManager::SocketManager &manager, const std::string& serviceName)
43 {
44     T *service = NULL;
45     try {
46         service = new T();
47         service->Create();
48         manager.RegisterSocketService(service);
49         service = NULL;
50     } catch (const SecurityManager::Exception &exception) {
51         LogError("Error in creating service " << serviceName <<
52                  ", details:\n" << exception.DumpToString());
53     } catch (const std::exception& e) {
54         LogError("Error in creating service " << serviceName <<
55                  ", details:\n" << e.what());
56     } catch (...) {
57         LogError("Error in creating service " << serviceName <<
58                  ", unknown exception occured");
59     }
60     if (service)
61         delete service;
62 }
63
64 int main(void) {
65
66     UNHANDLED_EXCEPTION_HANDLER_BEGIN
67     {
68         SecurityManager::Singleton<SecurityManager::Log::LogSystem>::Instance().SetTag("SECURITY_MANAGER");
69
70         SecurityManager::FileLocker serviceLock(SecurityManager::SERVICE_LOCK_FILE,
71                                                 true);
72
73         sigset_t mask;
74         sigemptyset(&mask);
75         sigaddset(&mask, SIGTERM);
76         sigaddset(&mask, SIGPIPE);
77         if (-1 == pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
78             LogError("Error in pthread_sigmask");
79             return 1;
80         }
81
82         LogInfo("Start!");
83         SecurityManager::SocketManager manager;
84
85         REGISTER_SOCKET_SERVICE(manager, SecurityManager::Service);
86
87         manager.MainLoop();
88     } catch (const SecurityManager::FileLocker::Exception::Base &e) {
89         LogError("Unable to get a file lock. Exiting.");
90         return EXIT_FAILURE;
91     }
92     UNHANDLED_EXCEPTION_HANDLER_END
93     return 0;
94 }