Add file lock to key-manager server
[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 <openssl/ssl.h>
26 #include <openssl/conf.h>
27 #include <openssl/err.h>
28 #include <openssl/evp.h>
29
30 #include <dpl/log/log.h>
31 #include <dpl/singleton.h>
32 #include <dpl/singleton_safe_impl.h>
33
34 #include <socket-manager.h>
35
36 #include <ckm-service.h>
37 #include <ocsp-service.h>
38
39 #include <key-provider.h>
40 #include <CryptoService.h>
41 #include <file-system.h>
42
43 IMPLEMENT_SAFE_SINGLETON(CKM::Log::LogSystem);
44
45 #define REGISTER_SOCKET_SERVICE(manager, service) \
46     registerSocketService<service>(manager, #service)
47
48 template<typename T>
49 void registerSocketService(CKM::SocketManager &manager, const std::string& serviceName)
50 {
51     T *service = NULL;
52     try {
53         service = new T();
54         service->Create();
55         manager.RegisterSocketService(service);
56         service = NULL;
57     } catch (const CKM::Exception &exception) {
58         LogError("Error in creating service " << serviceName <<
59                  ", details:\n" << exception.DumpToString());
60     } catch (const std::exception& e) {
61         LogError("Error in creating service " << serviceName <<
62                  ", details:\n" << e.what());
63     } catch (...) {
64         LogError("Error in creating service " << serviceName <<
65                  ", unknown exception occured");
66     }
67     if (service)
68         delete service;
69 }
70
71 int main(void) {
72     UNHANDLED_EXCEPTION_HANDLER_BEGIN
73     {
74         CKM::Singleton<CKM::Log::LogSystem>::Instance().SetTag("CKM");
75
76         int retCode = CKM::FileSystem::init();
77         if (retCode) {
78             LogError("Fatal error in FileSystem::init()");
79             return 1;
80         }
81
82         CKM::FileLock fl = CKM::FileSystem::lock();
83
84         sigset_t mask;
85         sigemptyset(&mask);
86         sigaddset(&mask, SIGTERM);
87         sigaddset(&mask, SIGPIPE);
88         if (-1 == pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
89             LogError("Error in pthread_sigmask");
90             return 1;
91         }
92         LogInfo("Init external libraries SKMM and openssl");
93
94         SSL_load_error_strings();
95         SSL_library_init();
96         OpenSSL_add_all_ciphers();
97         OPENSSL_config(NULL);
98
99         CKM::KeyProvider::initializeLibrary();
100         CKM::CryptoService::initialize();
101
102         {
103             LogInfo("Start!");
104             CKM::SocketManager manager;
105
106             REGISTER_SOCKET_SERVICE(manager, CKM::CKMService);
107             REGISTER_SOCKET_SERVICE(manager, CKM::OCSPService);
108
109             manager.MainLoop();
110         }
111         // Manager has been destroyed and we may close external libraries.
112         LogInfo("Deinit SKMM and openssl");
113         CKM::KeyProvider::closeLibrary();
114         // Deinit OPENSSL ?
115         EVP_cleanup();
116         ERR_free_strings();
117     }
118     catch (const std::runtime_error& e)
119     {
120         LogError(e.what());
121     }
122     UNHANDLED_EXCEPTION_HANDLER_END
123     return 0;
124 }
125