Tizen 2.1 base
[framework/security/security-server.git] / src / daemon / security_daemon.cpp
1 /*
2  * Copyright (c) 2011 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        security_daemon.cpp
18  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version     1.0
20  * @brief       This is implementation file of Security Daemon
21  */
22
23 #include "security_daemon.h"
24
25 #include <dpl/assert.h>
26 #include <dpl/foreach.h>
27 #include <dpl/log/log.h>
28
29 #include <dpl/framework_efl.h>
30
31 #include <dpl/singleton_impl.h>
32 IMPLEMENT_SINGLETON(SecurityDaemon::SecurityDaemon)
33
34 #include <ace-dao-rw/AceDAO.h>
35
36 namespace SecurityDaemon {
37
38 //This is declared not in SecurityDaemon class, so no Ecore.h is needed there.
39 static Ecore_Event_Handler *g_exitHandler;
40 static Eina_Bool exitHandler(void */*data*/, int /*type*/, void */*event*/)
41 {
42     auto& daemon = SecurityDaemonSingleton::Instance();
43     daemon.terminate(0);
44
45     return ECORE_CALLBACK_CANCEL;
46 }
47
48 SecurityDaemon::SecurityDaemon() :
49     m_initialized(false),
50     m_terminating(false),
51     m_returnValue(0)
52 {
53 }
54
55 void SecurityDaemon::initialize(int& /*argc*/, char** /*argv*/)
56 {
57     DPL::Log::LogSystemSingleton::Instance().SetTag("SECURITY_DAEMON");
58     LogDebug("Initializing");
59     Assert(!m_initialized && "Already Initialized");
60
61     g_exitHandler = ecore_event_handler_add(ECORE_EVENT_SIGNAL_EXIT,
62                                             &exitHandler,
63                                             NULL);
64
65     DatabaseService::initialize();
66     FOREACH (service, m_servicesList) {
67         (*service)->initialize();
68     }
69     m_initialized = true;
70     LogDebug("Initialized");
71 }
72
73 int SecurityDaemon::execute()
74 {
75     Assert(m_initialized && "Not Initialized");
76     LogDebug("Starting execute");
77     FOREACH (service, m_servicesList) {
78         (*service)->start();
79     }
80     ecore_main_loop_begin();
81     return m_returnValue;
82 }
83
84 void SecurityDaemon::terminate(int returnValue)
85 {
86     Assert(m_initialized && "Not Initialized");
87     Assert(!m_terminating && "Already terminating");
88     LogDebug("Terminating");
89
90     ecore_event_handler_del(g_exitHandler);
91
92     m_returnValue = returnValue;
93     m_terminating = true;
94
95     FOREACH (service, m_servicesList) {
96         (*service)->stop();
97     }
98
99     ecore_main_loop_quit();
100 }
101
102 void SecurityDaemon::shutdown()
103 {
104     LogDebug("Shutdown");
105     Assert(m_initialized && "Not Initialized");
106     Assert(m_terminating && "Not terminated");
107
108     DatabaseService::deinitialize();
109     FOREACH (service, m_servicesList) {
110         (*service)->deinitialize();
111     }
112
113     m_initialized = false;
114 }
115
116 namespace DatabaseService {
117
118 void initialize(void)
119 {
120     LogDebug("Ace/Wrt database services initializing...");
121     AceDB::AceDAO::attachToThreadRW();
122 }
123
124 void deinitialize(void)
125 {
126     LogDebug("Ace/Wrt database services deinitializing...");
127     AceDB::AceDAO::detachFromThread();
128 }
129
130 } //namespace DatabaseService
131
132 } //namespace SecurityDaemon