tizen 2.4 release
[framework/security/key-manager.git] / src / manager / listener / listener-thread.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        listener-daemon.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Listener daemon handle some events for key-manager.
21  */
22
23 #include <fcntl.h>
24 #include <unistd.h>
25
26 #include <thread>
27 #include <glib.h>
28 #include <ckm/ckm-control.h>
29 #include <ckm/ckm-type.h>
30 #include <dlog.h>
31 #include <listener-thread.h>
32
33
34 #include <map>
35 #include <utility>
36 #include <string>
37 #include <package-manager-zone.h>
38
39 #define CKM_LISTENER_TAG "CKM_LISTENER"
40
41 #define LISTENER_SLOGD(format, arg...) SLOG(LOG_DEBUG, CKM_LISTENER_TAG, format, ##arg)
42 #define LISTENER_SLOGE(format, arg...) SLOG(LOG_ERROR, CKM_LISTENER_TAG, format, ##arg)
43
44 namespace { // anonymous namespace
45 const char *const ZONE_DEFAULT = "host";
46 typedef std::pair<std::string, CKM::Label> PkgmgrEvent;
47 typedef std::map<const int, PkgmgrEvent> EventMap;
48
49 int _pkgmgr_event_callback(
50     int req_id,
51     const char *pkg_type,
52     const char *pkgid,
53     const char *key,
54     const char *val,
55     const void *pmsg,
56     void *data,
57     const char *zone)
58 {
59     (void) pmsg;
60     EventMap *eventMap = static_cast<EventMap *>(data);
61
62     LISTENER_SLOGD(
63         "req_id(%d), pkg_type(%s), pkgid(%s), key(%s), val(%s)",
64         req_id, pkg_type, pkgid, key, val);
65
66     // uninstall package start event
67     if (strncmp(key, "start", strlen(key)) == 0
68      && strncmp(val, "uninstall", strlen(val)) == 0) {
69         if (zone) {
70             eventMap->insert(
71                 std::pair<const int, PkgmgrEvent>(
72                     req_id,
73                     PkgmgrEvent(std::string(zone), CKM::Label(pkgid))
74                 )
75             );
76         }
77         else {
78             eventMap->insert(
79                 std::pair<const int, PkgmgrEvent>(
80                     req_id,
81                     PkgmgrEvent(std::string(ZONE_DEFAULT), CKM::Label(pkgid))
82                 )
83             );
84         }
85         return 0;
86     }
87     // uninstall package success event
88     else if (strncmp(key, "end", strlen(key)) == 0
89           && strncmp(val, "ok", strlen(val)) == 0) {
90         EventMap::iterator it;
91         it = eventMap->find(req_id);
92
93         if (it == eventMap->end()) {
94             LISTENER_SLOGE("cannot find req_id(%d) in eventMap. Maybe not in case of uninstallation.", req_id);
95         }
96         else {
97             LISTENER_SLOGD("Uninstallation success. pkgid(%s)", pkgid);
98
99             auto control = CKM::Control::create();
100             int ret = control->removeApplicationData(std::get<0>(it->second), std::get<1>(it->second));
101             if (ret != CKM_API_SUCCESS) {
102                 LISTENER_SLOGE("removeApplicationData error. ret(%d)", ret);
103             }
104             eventMap->erase(it);
105         }
106     }
107
108     // zone can be "personal", "knox" or "host".
109     LISTENER_SLOGD("zone_name is (%s)", zone);
110
111     return 0;
112 }
113
114 int listener_main(GMainLoop *main_loop) {
115     LISTENER_SLOGD("Start!");
116
117     EventMap eventMap;
118     int req_id = 0;
119     pkgmgr_client *client = pkgmgr_client_new(PC_LISTENING);
120     if (client == NULL) {
121         LISTENER_SLOGE("Error in pkgmgr client creation");
122         return -1;
123     }
124
125     req_id = pkgmgr_client_listen_status_with_zone(client, _pkgmgr_event_callback, &eventMap);
126     if (req_id < 0) {
127         LISTENER_SLOGE("Error in pkgmgr callback registeration req_id(%d)", req_id);
128         pkgmgr_client_free(client);
129         return -1;
130     }
131
132     LISTENER_SLOGD("Ready to listen!");
133     g_main_loop_run(main_loop);
134     SLOG(LOG_ERROR, CKM_LISTENER_TAG, "%s", "Listener main loop ended.");
135     return 0;
136 }
137
138 } // namespace  anonymous
139
140 namespace CKM {
141
142 ListenerThread::ListenerThread()
143 {
144     main_loop = g_main_loop_new(NULL, FALSE);
145 }
146
147 void ListenerThread::start()
148 {
149     SLOG(LOG_INFO, CKM_LISTENER_TAG, "%s", "Listener will start!");
150     std::thread thread(listener_main, main_loop);
151     thread.detach();
152 }
153
154 ListenerThread::~ListenerThread()
155 {
156 }
157
158
159 } // namespace CKM