Openssl: add thread support and fix initialization
[platform/core/security/key-manager.git] / src / manager / main / key-manager-main.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file        ckm-manager-main.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Implementation of central key manager
21  */
22 #include <stdlib.h>
23 #include <signal.h>
24
25 #include <dpl/log/log.h>
26 #include <dpl/singleton.h>
27
28 #include <socket-manager.h>
29
30 #include <ckm-service.h>
31 #include <ocsp-service.h>
32 #include <encryption-service.h>
33 #include <crypto-init.h>
34
35 #include <key-provider.h>
36 #include <file-system.h>
37
38 #define REGISTER_SOCKET_SERVICE(manager, service) \
39     registerSocketService<service>(manager, #service)
40
41 template<typename T>
42 void registerSocketService(CKM::SocketManager &manager, const std::string& serviceName)
43 {
44     T *service = NULL;
45     try {
46         service = new T();
47         service->Start();
48         manager.RegisterSocketService(service);
49         service = NULL;
50     } catch (const CKM::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     UNHANDLED_EXCEPTION_HANDLER_BEGIN
66     {
67         CKM::Singleton<CKM::Log::LogSystem>::Instance().SetTag("CKM");
68
69         int retCode = CKM::FileSystem::init();
70         if (retCode) {
71             LogError("Fatal error in FileSystem::init()");
72             return 1;
73         }
74
75         CKM::FileLock fl = CKM::FileSystem::lock();
76
77         sigset_t mask;
78         sigemptyset(&mask);
79         sigaddset(&mask, SIGTERM);
80         sigaddset(&mask, SIGPIPE);
81         if (-1 == pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
82             LogError("Error in pthread_sigmask");
83             return 1;
84         }
85         LogInfo("Init external libraries SKMM and openssl");
86
87         CKM::initOpenSsl();
88
89         CKM::KeyProvider::initializeLibrary();
90
91         {
92             LogInfo("Start!");
93             CKM::SocketManager manager;
94
95             REGISTER_SOCKET_SERVICE(manager, CKM::CKMService);
96             REGISTER_SOCKET_SERVICE(manager, CKM::OCSPService);
97             REGISTER_SOCKET_SERVICE(manager, CKM::EncryptionService);
98
99             manager.MainLoop();
100         }
101         // Manager has been destroyed and we may close external libraries.
102         LogInfo("Deinit SKMM and openssl");
103         CKM::KeyProvider::closeLibrary();
104
105         CKM::deinitOpenSsl();
106     }
107     catch (const std::runtime_error& e)
108     {
109         LogError(e.what());
110     }
111     UNHANDLED_EXCEPTION_HANDLER_END
112     return 0;
113 }
114