2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @file listener-daemon.cpp
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Listener daemon handle some events for key-manager.
28 #include <ckm/ckm-control.h>
29 #include <ckm/ckm-type.h>
31 #include <listener-thread.h>
37 #include <package-manager-zone.h>
39 #define CKM_LISTENER_TAG "CKM_LISTENER"
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)
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;
49 int _pkgmgr_event_callback(
60 EventMap *eventMap = static_cast<EventMap *>(data);
63 "req_id(%d), pkg_type(%s), pkgid(%s), key(%s), val(%s)",
64 req_id, pkg_type, pkgid, key, val);
66 // uninstall package start event
67 if (strncmp(key, "start", strlen(key)) == 0
68 && strncmp(val, "uninstall", strlen(val)) == 0) {
71 std::pair<const int, PkgmgrEvent>(
73 PkgmgrEvent(std::string(zone), CKM::Label(pkgid))
79 std::pair<const int, PkgmgrEvent>(
81 PkgmgrEvent(std::string(ZONE_DEFAULT), CKM::Label(pkgid))
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);
93 if (it == eventMap->end()) {
94 LISTENER_SLOGE("cannot find req_id(%d) in eventMap. Maybe not in case of uninstallation.", req_id);
97 LISTENER_SLOGD("Uninstallation success. pkgid(%s)", pkgid);
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);
108 // zone can be "personal", "knox" or "host".
109 LISTENER_SLOGD("zone_name is (%s)", zone);
114 int listener_main(GMainLoop *main_loop) {
115 LISTENER_SLOGD("Start!");
119 pkgmgr_client *client = pkgmgr_client_new(PC_LISTENING);
120 if (client == NULL) {
121 LISTENER_SLOGE("Error in pkgmgr client creation");
125 req_id = pkgmgr_client_listen_status_with_zone(client, _pkgmgr_event_callback, &eventMap);
127 LISTENER_SLOGE("Error in pkgmgr callback registeration req_id(%d)", req_id);
128 pkgmgr_client_free(client);
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.");
138 } // namespace anonymous
142 ListenerThread::ListenerThread()
144 main_loop = g_main_loop_new(NULL, FALSE);
147 void ListenerThread::start()
149 SLOG(LOG_INFO, CKM_LISTENER_TAG, "%s", "Listener will start!");
150 std::thread thread(listener_main, main_loop);
154 ListenerThread::~ListenerThread()