60e6f8c4203c347dd6953b0ef038b6476edce581
[platform/core/security/security-server.git] / src / server / main / server2-main.cpp
1 /*
2  *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bumjin Im <bj.im@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        sever2-main.cpp
20  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
21  * @version     1.0
22  * @brief       Implementation of security-server2
23  */
24 #include <stdlib.h>
25 #include <signal.h>
26
27 #include <dpl/log/log.h>
28 #include <dpl/log/audit-smack-log.h>
29 #include <dpl/singleton.h>
30 #include <dpl/singleton_safe_impl.h>
31
32 #include <socket-manager.h>
33
34 #include <data-share.h>
35 #include <get-gid.h>
36 #include <privilege-by-pid.h>
37 #include <app-permissions.h>
38 #include <cookie.h>
39 #include <password.h>
40 #include <installer.h>
41
42 IMPLEMENT_SAFE_SINGLETON(SecurityServer::Log::LogSystem);
43
44 #define REGISTER_SOCKET_SERVICE(manager, service) \
45     registerSocketService<service>(manager, #service)
46
47 template<typename T>
48 void registerSocketService(SecurityServer::SocketManager &manager, const std::string& serviceName)
49 {
50     T *service = NULL;
51     try {
52         service = new T();
53         service->Create();
54         manager.RegisterSocketService(service);
55         service = NULL;
56     } catch (const SecurityServer::Exception &exception) {
57         LogError("Error in creating service " << serviceName <<
58                  ", details:\n" << exception.DumpToString());
59     } catch (const std::exception& e) {
60         LogError("Error in creating service " << serviceName <<
61                  ", details:\n" << e.what());
62     } catch (...) {
63         LogError("Error in creating service " << serviceName <<
64                  ", unknown exception occured");
65     }
66     if (service)
67         delete service;
68 }
69
70 int main(void) {
71
72     UNHANDLED_EXCEPTION_HANDLER_BEGIN
73     {
74         SecurityServer::Singleton<SecurityServer::Log::LogSystem>::Instance().SetTag("SECURITY_SERVER");
75
76         // This provider may be used in security-server only.
77         // If we add it inside LogSystem constructor it also
78         // will be used by security-server-client library.
79         SecurityServer::Log::AuditSmackLog *smackLog = new SecurityServer::Log::AuditSmackLog;
80         if (smackLog->Fail())
81             delete smackLog;
82         else
83             SecurityServer::Singleton<SecurityServer::Log::LogSystem>::Instance().AddProvider(smackLog);
84
85         sigset_t mask;
86         sigemptyset(&mask);
87         sigaddset(&mask, SIGTERM);
88         sigaddset(&mask, SIGPIPE);
89         if (-1 == pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
90             LogError("Error in pthread_sigmask");
91             return 1;
92         }
93
94         LogInfo("Start!");
95         SecurityServer::SocketManager manager;
96
97         REGISTER_SOCKET_SERVICE(manager, SecurityServer::CookieService);
98         REGISTER_SOCKET_SERVICE(manager, SecurityServer::SharedMemoryService);
99         REGISTER_SOCKET_SERVICE(manager, SecurityServer::GetGidService);
100         REGISTER_SOCKET_SERVICE(manager, SecurityServer::PrivilegeByPidService);
101         REGISTER_SOCKET_SERVICE(manager, SecurityServer::AppPermissionsService);
102         REGISTER_SOCKET_SERVICE(manager, SecurityServer::PasswordService);
103         REGISTER_SOCKET_SERVICE(manager, SecurityServer::InstallerService);
104
105         manager.MainLoop();
106     }
107     UNHANDLED_EXCEPTION_HANDLER_END
108     return 0;
109 }