Add permission check for open handle
[platform/core/messaging/msg-service.git] / framework / transaction-manager / MsgTransManager.cpp
1 /*
2  * Copyright (c) 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 #include <errno.h>
18 #include <stdlib.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <pthread.h>
22
23 #include <bundle.h>
24 #include <eventsystem.h>
25
26 #include "MsgDebug.h"
27 #include "MsgMemory.h"
28 #include "MsgException.h"
29 #include "MsgCppTypes.h"
30 #include "MsgContact.h"
31 #include "MsgIpcSocket.h"
32 #include "MsgGconfWrapper.h"
33 #include "MsgUtilFunction.h"
34 #include "MsgUtilFile.h"
35 #include "MsgLbs.h"
36 #include "MsgCmdHandler.h"
37 #include "MsgSettingHandler.h"
38 #include "MsgStorageHandler.h"
39 #include "MsgPluginManager.h"
40 #include "MsgTransManager.h"
41
42 #define MSG_CHECK_PRIVILEGE
43
44 void MsgMakeErrorEvent(MSG_CMD_TYPE_T cmdType, msg_error_t errType, int *pEventSize, char **ppEvent)
45 {
46         if (*ppEvent) delete [] *ppEvent;
47
48         *pEventSize = sizeof(MSG_EVENT_S);
49         *ppEvent = new char[sizeof(MSG_EVENT_S)];
50
51         MSG_EVENT_S* pMsgEvent = (MSG_EVENT_S*)*ppEvent;
52
53         pMsgEvent->eventType = cmdType;
54         pMsgEvent->result = errType;
55 }
56
57 /*==================================================================================================
58                                      IMPLEMENTATION OF MsgTransactionManager - Member Functions
59 ==================================================================================================*/
60 MsgTransactionManager* MsgTransactionManager::pInstance = NULL;
61 MsgIpcServerSocket MsgTransactionManager::servSock;
62
63
64 MsgTransactionManager::MsgTransactionManager() : running(false), mx(), mxQ(), cv(), eventQueue()
65 {
66         sentMsgMap.clear();
67         statusCBFdMap.clear();
68         newMsgCBList.clear();
69         newMMSConfMsgCBList.clear();
70         newSyncMLMsgCBList.clear();
71         newLBSMsgCBList.clear();
72         javaMMSList.clear();
73         operationSyncMLMsgCBList.clear();
74         storageChangeFdMap.clear();
75
76         handlerMap.clear();
77
78 /*      Fill in mMsgHandlers, as given in the below. */
79         handlerMap[MSG_CMD_ADD_MSG]                             = &MsgAddMessageHandler;
80         handlerMap[MSG_CMD_ADD_SYNCML_MSG]              = &MsgAddSyncMLMessageHandler;
81         handlerMap[MSG_CMD_UPDATE_MSG]                  = &MsgUpdateMessageHandler;
82         handlerMap[MSG_CMD_UPDATE_READ]                         = &MsgUpdateReadStatusHandler;
83         handlerMap[MSG_CMD_UPDATE_PROTECTED]            = &MsgUpdateProtectedStatusHandler;
84         handlerMap[MSG_CMD_DELETE_MSG]                  = &MsgDeleteMessageHandler;
85         handlerMap[MSG_CMD_DELALL_MSGINFOLDER]  = &MsgDeleteAllMessageInFolderHandler;
86         handlerMap[MSG_CMD_MOVE_MSGTOFOLDER]    = &MsgMoveMessageToFolderHandler;
87         handlerMap[MSG_CMD_MOVE_MSGTOSTORAGE]   = &MsgMoveMessageToStorageHandler;
88         handlerMap[MSG_CMD_COUNT_MSG]                   = &MsgCountMessageHandler;
89         handlerMap[MSG_CMD_GET_MSG]                             = &MsgGetMessageHandler;
90
91         handlerMap[MSG_CMD_ADD_FOLDER]                  = &MsgAddFolderHandler;
92         handlerMap[MSG_CMD_UPDATE_FOLDER]               = &MsgUpdateFolderHandler;
93         handlerMap[MSG_CMD_DELETE_FOLDER]               = &MsgDeleteFolderHandler;
94         handlerMap[MSG_CMD_GET_FOLDERLIST]              = &MsgGetFolderListHandler;
95
96         handlerMap[MSG_CMD_ADD_FILTER]                  = &MsgAddFilterHandler;
97         handlerMap[MSG_CMD_UPDATE_FILTER]                       = &MsgUpdateFilterHandler;
98         handlerMap[MSG_CMD_DELETE_FILTER]               = &MsgDeleteFilterHandler;
99         handlerMap[MSG_CMD_GET_FILTERLIST]              = &MsgGetFilterListHandler;
100         handlerMap[MSG_CMD_SET_FILTER_OPERATION]        = &MsgSetFilterOperationHandler;
101         handlerMap[MSG_CMD_GET_FILTER_OPERATION]        = &MsgGetFilterOperationHandler;
102         handlerMap[MSG_CMD_SET_FILTER_ACTIVATION] = &MsgSetFilterActivationHandler;
103
104         handlerMap[MSG_CMD_SUBMIT_REQ]                  = &MsgSubmitReqHandler;
105
106         handlerMap[MSG_CMD_REG_SENT_STATUS_CB]  = &MsgRegSentStatusCallbackHandler;
107         handlerMap[MSG_CMD_REG_STORAGE_CHANGE_CB] = &MsgRegStorageChangeCallbackHandler;
108         handlerMap[MSG_CMD_REG_INCOMING_MSG_CB] = &MsgRegIncomingMsgCallbackHandler;
109         handlerMap[MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB]        = &MsgRegIncomingMMSConfMsgCallbackHandler;
110         handlerMap[MSG_CMD_REG_INCOMING_SYNCML_MSG_CB]  = &MsgRegIncomingSyncMLMsgCallbackHandler;
111         handlerMap[MSG_CMD_REG_INCOMING_PUSH_MSG_CB]    = &MsgRegIncomingPushMsgCallbackHandler;
112         handlerMap[MSG_CMD_REG_INCOMING_CB_MSG_CB]      = &MsgRegIncomingCBMsgCallbackHandler;
113         handlerMap[MSG_CMD_REG_INCOMING_LBS_MSG_CB]     = &MsgRegIncomingLBSMsgCallbackHandler;
114         handlerMap[MSG_CMD_REG_SYNCML_MSG_OPERATION_CB] = &MsgRegSyncMLMsgOperationCallbackHandler;
115         handlerMap[MSG_CMD_REG_REPORT_MSG_INCOMING_CB] = &MsgRegIncomingReportMsgCallbackHandler;
116
117         handlerMap[MSG_CMD_PLG_SENT_STATUS_CNF] = &MsgSentStatusHandler;
118         handlerMap[MSG_CMD_PLG_STORAGE_CHANGE_IND]      = &MsgStorageChangeHandler;
119         handlerMap[MSG_CMD_PLG_INCOMING_MSG_IND]        = &MsgIncomingMsgHandler;
120         handlerMap[MSG_CMD_PLG_INCOMING_MMS_CONF]       = &MsgIncomingMMSConfMsgHandler;
121         handlerMap[MSG_CMD_PLG_INCOMING_PUSH_IND]       = &MsgIncomingPushMsgHandler;
122         handlerMap[MSG_CMD_PLG_INCOMING_CB_IND] = &MsgIncomingCBMsgHandler;
123
124         handlerMap[MSG_CMD_PLG_INCOMING_SYNCML_IND] = &MsgIncomingSyncMLMsgHandler;
125         handlerMap[MSG_CMD_PLG_INCOMING_LBS_IND] = &MsgIncomingLBSMsgHandler;
126         handlerMap[MSG_CMD_PLG_INIT_SIM_BY_SAT] = &MsgInitSimBySatHandler;
127
128         handlerMap[MSG_CMD_GET_THREADVIEWLIST]  = &MsgGetThreadViewListHandler;
129         handlerMap[MSG_CMD_DELETE_THREADMESSAGELIST]    = &MsgDeleteThreadMessageListHandler;
130         handlerMap[MSG_CMD_SET_TEMP_ADDRESS_TABLE]      = &MsgSetTempAddressTableHandler;
131
132         handlerMap[MSG_CMD_GET_CONTACT_COUNT]   = &MsgCountMsgByContactHandler;
133         handlerMap[MSG_CMD_GET_QUICKPANEL_DATA]         = &MsgGetQuickPanelDataHandler;
134         handlerMap[MSG_CMD_COUNT_BY_MSGTYPE]    = &MsgCountMsgByTypeHandler;
135         handlerMap[MSG_CMD_RESET_DB]    = &MsgResetDatabaseHandler;
136         handlerMap[MSG_CMD_GET_MEMSIZE]         = &MsgGetMemSizeHandler;
137
138         handlerMap[MSG_CMD_BACKUP_MESSAGE]      = &MsgBackupMessageHandler;
139         handlerMap[MSG_CMD_RESTORE_MESSAGE] = &MsgRestoreMessageHandler;
140
141         handlerMap[MSG_CMD_UPDATE_THREAD_READ] = &MsgUpdateThreadReadStatusHandler;
142
143         handlerMap[MSG_CMD_SYNCML_OPERATION] = &MsgSyncMLMsgOperationHandler;
144         handlerMap[MSG_CMD_GET_REPORT_STATUS]                   = &MsgGetReportStatusHandler;
145
146         handlerMap[MSG_CMD_GET_THREAD_ID_BY_ADDRESS] = &MsgGetThreadIdByAddressHandler;
147         handlerMap[MSG_CMD_GET_THREAD_INFO] = &MsgGetThreadInfoHandler;
148         handlerMap[MSG_CMD_GET_SMSC_OPT] = &MsgGetConfigHandler;
149         handlerMap[MSG_CMD_GET_CB_OPT] = &MsgGetConfigHandler;
150         handlerMap[MSG_CMD_GET_SMS_SEND_OPT] = &MsgGetConfigHandler;
151         handlerMap[MSG_CMD_GET_MMS_SEND_OPT] = &MsgGetConfigHandler;
152         handlerMap[MSG_CMD_GET_MMS_RECV_OPT] = &MsgGetConfigHandler;
153         handlerMap[MSG_CMD_GET_PUSH_MSG_OPT] = &MsgGetConfigHandler;
154         handlerMap[MSG_CMD_GET_VOICE_MSG_OPT] = &MsgGetConfigHandler;
155         handlerMap[MSG_CMD_GET_GENERAL_MSG_OPT] = &MsgGetConfigHandler;
156         handlerMap[MSG_CMD_GET_MSG_SIZE_OPT] = &MsgGetConfigHandler;
157
158         handlerMap[MSG_CMD_SET_SMSC_OPT] = &MsgSetConfigHandler;
159         handlerMap[MSG_CMD_SET_CB_OPT] = &MsgSetConfigHandler;
160         handlerMap[MSG_CMD_SET_SMS_SEND_OPT] = &MsgSetConfigHandler;
161         handlerMap[MSG_CMD_SET_MMS_SEND_OPT] = &MsgSetConfigHandler;
162         handlerMap[MSG_CMD_SET_MMS_RECV_OPT] = &MsgSetConfigHandler;
163         handlerMap[MSG_CMD_SET_PUSH_MSG_OPT] = &MsgSetConfigHandler;
164         handlerMap[MSG_CMD_SET_VOICE_MSG_OPT] = &MsgSetConfigHandler;
165         handlerMap[MSG_CMD_SET_GENERAL_MSG_OPT] = &MsgSetConfigHandler;
166         handlerMap[MSG_CMD_SET_MSG_SIZE_OPT] = &MsgSetConfigHandler;
167
168         handlerMap[MSG_CMD_ADD_PUSH_EVENT] = &MsgAddPushEventHandler;
169         handlerMap[MSG_CMD_DELETE_PUSH_EVENT] = &MsgDeletePushEventHandler;
170         handlerMap[MSG_CMD_UPDATE_PUSH_EVENT] = &MsgUpdatePushEventHandler;
171         handlerMap[MSG_CMD_DELETE_MESSAGE_BY_LIST] = &MsgDeleteMessageByListHandler;
172         handlerMap[MSG_CMD_ADD_SIM_MSG] = &MsgAddSimMessageHandler;
173         handlerMap[MSG_CMD_PLG_RESEND_MESSAGE] = &MsgResendMessageHandler;
174 #ifdef FEATURE_SMS_CDMA
175         handlerMap[MSG_CMD_PLG_CHECK_UNIQUENESS] = &MsgCheckUniquenessHandler;
176 #endif
177         handlerMap[MSG_CMD_UPDATE_IMSI] = &MsgUpdateIMSIHandler;
178 }
179
180
181 MsgTransactionManager::~MsgTransactionManager()
182 {
183 /*      pthread_cond_init(&retCV, NULL); */ /* = PTHREAD_COND_INITIALIZER; */
184 }
185
186
187 MsgTransactionManager* MsgTransactionManager::instance()
188 {
189         if (!pInstance)
190                 pInstance = new MsgTransactionManager();
191
192         return pInstance;
193 }
194
195 static void* worker_event_queue(void* arg)
196 {
197         MsgTransactionManager::instance()->workerEventQueue();
198         return NULL;
199 }
200
201 void MsgTransactionManager::run()
202 {
203         servSock.open(MSG_SOCKET_PATH);
204
205         fd_set readfds = servSock.fdSet();
206         int nfds = 0;
207
208         MSG_DEBUG("Start Transaction Manager");
209
210         /* Set Msg FW Ready Flag */
211         if(MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true) != MSG_SUCCESS)
212                 MSG_DEBUG("MsgSettingSetBool FAIL : VCONFKEY_MSG_SERVER_READY");
213         MSG_INFO("### VCONFKEY_MSG_SERVER_READY ###");
214
215      /* running worker for plg task */
216         pthread_t tv;
217         if (pthread_create(&tv, NULL, &worker_event_queue, NULL) != 0) {
218                 THROW(MsgException::SERVER_READY_ERROR, "cannot create thread [%d]", errno);
219         }
220
221         while(1) {
222                 readfds = servSock.fdSet();
223                 nfds = servSock.maxFd();
224
225                 MSG_DEBUG("Wait For Select() : nfds %d", nfds);
226
227                 if(select(nfds, &readfds, NULL, NULL, NULL) == -1) {
228                         THROW(MsgException::SELECT_ERROR, "select error : %s", g_strerror(errno));
229                 }
230
231                 try {
232                         for (int i=0 ; i < nfds; i++) {
233                                 if (FD_ISSET(i, &readfds)) {
234                                         if (i == servSock.fd()) /* if it is socket connection request */
235                                                 servSock.accept();
236                                         else
237                                                 handleRequest(i);
238                                 }
239                         }
240                 } catch (MsgException& e) {
241                         MSG_FATAL("%s", e.what());
242                 } catch (exception& e) {
243                         MSG_FATAL("%s", e.what());
244                 }
245         }
246 }
247
248
249 void MsgTransactionManager::write(int fd, const char* buf, int len)
250 {
251         servSock.write(fd, buf, len);
252 }
253
254
255 void MsgTransactionManager::insertSentMsg(int reqId, MSG_PROXY_INFO_S* pPrxInfo)
256 {
257         if (pPrxInfo == NULL)
258                 THROW(MsgException::SENT_STATUS_ERROR, "Input Parameter is NULL");
259
260         MSG_DEBUG("msg for submit: reqId %d listenerFd %d handleAddr %x", reqId, pPrxInfo->listenerFd, pPrxInfo->handleAddr);
261
262         MsgMutexLocker lock(mx);
263
264         fd_map::iterator it = statusCBFdMap.find(pPrxInfo->listenerFd);
265
266         if (it == statusCBFdMap.end()) { /* if the status CB is not registered */
267                 MSG_DEBUG("No sent_status registered for fd %d", pPrxInfo->listenerFd);
268         } else {
269                 sentMsgMap.insert(make_pair(reqId, *pPrxInfo));
270         }
271 }
272
273
274 MSG_PROXY_INFO_S* MsgTransactionManager::getProxyInfo(int reqId)
275 {
276         sentmsg_map::iterator it = sentMsgMap.find(reqId);
277
278         if (it == sentMsgMap.end()) {
279                 MSG_DEBUG("No sent status cb found");
280                 return NULL;
281         }
282
283         return &(it->second);
284 }
285
286
287 void MsgTransactionManager::delProxyInfo(int reqId)
288 {
289         MsgMutexLocker lock(mx);
290
291         sentmsg_map::iterator it = sentMsgMap.find(reqId);
292
293         if (it == sentMsgMap.end()) {
294                 THROW(MsgException::SENT_STATUS_ERROR, "channel info does not exist");
295         }
296
297         sentMsgMap.erase(it);
298 }
299
300 void MsgTransactionManager::workerEventQueue()
301 {
302         MSG_CMD_S* pCmd = NULL;
303         int (*pfHandler)(const MSG_CMD_S*, char**) = NULL;
304         char* pEventData = NULL;
305         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
306
307         int fd = -1;
308         int eventSize = 0;
309
310         while (1) {
311                 mxQ.lock();
312                 while (!eventQueue.front(&pCmd)) { /* if no item, wait */
313                         MSG_DEBUG("waiting for task");
314                         cv.wait(mxQ.pMsgMutex());
315                 }
316                 eventQueue.pop_front(); /* pop it from queue*/
317                 mxQ.unlock();
318
319                 if (!pCmd) {
320                         MSG_FATAL("pCmd NULL");
321                         continue;
322                 }
323
324                 memcpy (&fd, pCmd->cmdCookie, sizeof(int));
325                 if (fd < 0) {
326                         MSG_FATAL("fd [%d] < 0", fd);
327                         g_free(pCmd); pCmd = NULL;
328                         continue;
329                 }
330                 pfHandler = handlerMap[pCmd->cmdType];
331                 if (!pfHandler) {
332                         MSG_FATAL("No handler for %d", pCmd->cmdType);
333                         MsgMakeErrorEvent(pCmd->cmdType, MSG_ERR_INVALID_PARAMETER, &eventSize, &pEventData);
334                 } else {
335                         /* run handler function */
336                         eventSize = pfHandler(pCmd, &pEventData);
337
338                         if (eventSize == 0 || pEventData == NULL) {
339                                 MSG_FATAL("event size[%d] = 0 or event data = NULL", eventSize);
340                                 MsgMakeErrorEvent(pCmd->cmdType, MSG_ERR_INVALID_PARAMETER, &eventSize, &pEventData);
341                         }
342                 }
343
344                 MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
345                 servSock.write(fd, pEventData, eventSize);
346                 g_free(pCmd); pCmd = NULL;
347         }
348 }
349
350 void MsgTransactionManager::handleRequest(int fd)
351 {
352         MSG_BEGIN();
353
354         MSG_DEBUG("Event from fd %d", fd);
355
356         char* buf = NULL;
357         unique_ptr<char*, void(*)(char**)> wrap(&buf, unique_ptr_deleter);
358         int len = 0;
359         int ret = servSock.read(fd, &buf, &len);
360
361         if (ret == CLOSE_CONNECTION_BY_SIGNAL || ret == CLOSE_CONNECTION_BY_USER || ret < 0) {
362                 MSG_DEBUG("Read value [%d]", ret);
363                 cleanup(fd);
364                 return;
365         }
366
367         if (len <= 0 && len >= MSG_MAX_IPC_SIZE)
368                 THROW(MsgException::INVALID_RESULT, "read buffer size <= 0 or over max ipc size");
369
370         char* pEventData = NULL;
371         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
372
373         int eventSize = 0;
374
375         /* decoding cmd from APP */
376         MSG_CMD_S* pCmd = (MSG_CMD_S*) buf;
377         MSG_DEBUG("Command Type [%d : %s]", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType));
378
379         if (pCmd->cmdType > MSG_CMD_NUM)
380                 THROW(MsgException::OUT_OF_RANGE, "request CMD is not defined");
381
382         /* check privilege */
383         if (checkPrivilege(fd, pCmd->cmdType) == false) {
384                 MSG_DEBUG("No Privilege rule. Not allowed.");
385 #ifdef MSG_CHECK_PRIVILEGE
386                 MsgMakeErrorEvent(pCmd->cmdType, MSG_ERR_PERMISSION_DENIED, &eventSize, &pEventData);
387
388                 MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
389                 servSock.write(fd, pEventData, eventSize);
390
391                 return;
392 #endif
393         }
394
395         /* determine the handler based on pCmd->cmdType */
396         int (*pfHandler)(const MSG_CMD_S*, char**) = NULL;
397         MSG_CMD_S* pCmdDup = NULL;
398
399         switch (pCmd->cmdType) {
400         case MSG_CMD_PLG_SENT_STATUS_CNF:
401         case MSG_CMD_PLG_STORAGE_CHANGE_IND:
402         case MSG_CMD_PLG_INCOMING_MSG_IND:
403         case MSG_CMD_PLG_INCOMING_MMS_CONF:
404         case MSG_CMD_PLG_INCOMING_SYNCML_IND:
405         case MSG_CMD_PLG_INCOMING_LBS_IND:
406         case MSG_CMD_PLG_INIT_SIM_BY_SAT:
407         case MSG_CMD_PLG_INCOMING_PUSH_IND:
408         case MSG_CMD_PLG_INCOMING_CB_IND:
409                 pCmdDup = (MSG_CMD_S*)calloc(1, len); /* pCmdDup should be freed afterward */
410                 if (pCmdDup != NULL) {
411                         memcpy(pCmdDup, pCmd, len);
412                         memcpy(pCmdDup->cmdCookie, &fd, sizeof(int)); /* Now, cmdCookie keeps fd for return */
413
414                         mxQ.lock(); /* aquire lock before adding cmd */
415                         eventQueue.push_back(pCmdDup);
416                         cv.signal(); /* wake up worker */
417                         mxQ.unlock();
418                 }
419                 break;
420         case MSG_CMD_CHECK_PERMISSION:
421                 MSG_DEBUG("Client has privilege. Allowed.");
422                 MsgMakeErrorEvent(pCmd->cmdType, MSG_SUCCESS, &eventSize, &pEventData);
423                 MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
424                 servSock.write(fd, pEventData, eventSize);
425                 break;
426         default:
427                 pfHandler = handlerMap[pCmd->cmdType];
428                 if (!pfHandler) {
429                         MSG_FATAL("No handler for %d", pCmd->cmdType);
430                         MsgMakeErrorEvent(pCmd->cmdType, MSG_ERR_INVALID_PARAMETER, &eventSize, &pEventData);
431                 } else {
432                         /* run handler function */
433                         memcpy (pCmd->cmdCookie, &fd, sizeof(int)); /* Now, cmdCookie keeps fd for return */
434                         eventSize = pfHandler(pCmd, &pEventData);
435
436                         if (eventSize == 0 || pEventData == NULL) {
437                                 MSG_FATAL("event size[%d] = 0 or event data = NULL", eventSize);
438                                 MsgMakeErrorEvent(pCmd->cmdType, MSG_ERR_INVALID_PARAMETER, &eventSize, &pEventData);
439                         }
440                 }
441
442                 MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
443                 servSock.write(fd, pEventData, eventSize);
444                 break;
445         }
446
447         MSG_END();
448 }
449
450
451 /* terminating the socket connection between ipc server and ipc client */
452 void MsgTransactionManager::cleanup(int fd)
453 {
454         MSG_BEGIN();
455
456         MsgMutexLocker lock(mx);
457
458         servSock.close(fd);
459
460         MSG_DEBUG("fd %d disonnected", fd);
461
462         /* remove sent msg info for fd */
463         sentmsg_map::iterator sentmsg_it = sentMsgMap.begin();
464
465         for (; sentmsg_it != sentMsgMap.end(); sentmsg_it++) {
466                 if (sentmsg_it->second.listenerFd == fd) {
467                         sentmsg_it->second.listenerFd = 0;
468                         sentmsg_it->second.handleAddr = 0;
469                 }
470         }
471
472         /* remove sent status callback for fd */
473         statusCBFdMap.erase(fd);
474
475         MSG_DEBUG("After erase fd [%d], statusCBFdMap has below.", fd);
476         fd_map::iterator it = statusCBFdMap.begin();
477         for (; it != statusCBFdMap.end(); ++it)
478                 MSG_DEBUG("[%d]", it->first);
479
480         /* remove all newMsgCBs for fd */
481         newmsg_list::iterator newmsg_it = newMsgCBList.begin();
482
483         while (newmsg_it != newMsgCBList.end()) {
484                 if (newmsg_it->listenerFd == fd) {
485                         newmsg_it = newMsgCBList.erase(newmsg_it);
486                 } else {
487                         ++newmsg_it;
488                 }
489         }
490
491         /* remove all newMMSConfMsgCBs for fd */
492         mmsconf_list::iterator mmsconf_it = newMMSConfMsgCBList.begin();
493
494         while (mmsconf_it != newMMSConfMsgCBList.end()) {
495                 if (mmsconf_it->listenerFd == fd) {
496                         mmsconf_it = newMMSConfMsgCBList.erase(mmsconf_it);
497                 } else {
498                         ++mmsconf_it;
499                 }
500         }
501
502         /* remove all newSyncMLMsgCBs for fd */
503         syncmlmsg_list::iterator syncmlmsg_it = newSyncMLMsgCBList.begin();
504
505         while (syncmlmsg_it != newSyncMLMsgCBList.end()) {
506                 if (syncmlmsg_it->listenerFd == fd) {
507                         syncmlmsg_it = newSyncMLMsgCBList.erase(syncmlmsg_it);
508                 } else {
509                         ++syncmlmsg_it;
510                 }
511         }
512
513         /* remove all newLBSMsgCBs for fd */
514         lbsmsg_list::iterator lbsmsg_it = newLBSMsgCBList.begin();
515
516         while (lbsmsg_it != newLBSMsgCBList.end()) {
517                 if (lbsmsg_it->listenerFd == fd) {
518                         lbsmsg_it = newLBSMsgCBList.erase(lbsmsg_it);
519                 } else {
520                         ++lbsmsg_it;
521                 }
522         }
523
524         /* remove all newPushMsgCBs for fd */
525         pushmsg_list::iterator pushmsg_it = newPushMsgCBList.begin();
526
527         while (pushmsg_it != newPushMsgCBList.end()) {
528                 if (pushmsg_it->listenerFd == fd) {
529                         pushmsg_it = newPushMsgCBList.erase(pushmsg_it);
530                 } else {
531                         ++pushmsg_it;
532                 }
533         }
534
535         /* remove all newCBMsgCBs for fd */
536         cbmsg_list::iterator cbmsg_it = newCBMsgCBList.begin();
537         /*bool bSave = false; */
538
539         while (cbmsg_it != newCBMsgCBList.end()) {
540                 if (cbmsg_it->listenerFd == fd) {
541                         cbmsg_it = newCBMsgCBList.erase(cbmsg_it);
542                 } else {
543                         /*if (cbmsg_it->bsave == true) */
544                         /*      bSave = true; */
545                         ++cbmsg_it;
546                 }
547         }
548
549         /* remove all operationSyncMLMsgCBs for fd */
550         syncmlop_list::iterator syncmlop_it = operationSyncMLMsgCBList.begin();
551
552         while (syncmlop_it != operationSyncMLMsgCBList.end()) {
553                 if (syncmlop_it->listenerFd == fd) {
554                         syncmlop_it = operationSyncMLMsgCBList.erase(syncmlop_it);
555                 } else {
556                         ++syncmlop_it;
557                 }
558         }
559
560         /* remove storage change callback for fd */
561         storageChangeFdMap.erase(fd);
562
563         MSG_DEBUG("After erase fd [%d], storageChangeFdMap has below.", fd);
564         it = storageChangeFdMap.begin();
565         for (; it != storageChangeFdMap.end(); ++it)
566                 MSG_DEBUG("[%d]", it->first);
567
568         /* remove report msg incoming callback for fd */
569         reportMsgCBFdMap.erase(fd);
570
571         MSG_DEBUG("After erase fd [%d], reportMsgCBFdMap has below.", fd);
572         it = reportMsgCBFdMap.begin();
573         for (; it != reportMsgCBFdMap.end(); ++it)
574                 MSG_DEBUG("[%d]", it->first);
575
576         MSG_END();
577 }
578
579
580 bool MsgTransactionManager::checkPrivilege(int fd, MSG_CMD_TYPE_T CmdType)
581 {
582         bool bAllowed = true;
583
584         int ret;
585         char *peer_client = NULL;
586         char *peer_user = NULL;
587         char *peer_session = NULL;
588         pid_t peer_pid;
589
590         if (p_cynara == NULL) {
591                 if (this->initCynara() == false) {
592                         MSG_ERR("Cynara initialize failed. It will try again when API is called.");
593                         bAllowed = false;
594                         goto _END_OF_FUNC;
595                 }
596         }
597
598         ret = cynara_creds_socket_get_client(fd, client_method, &peer_client);
599         if (ret != CYNARA_API_SUCCESS) {
600                 MSG_ERR("cynara_creds_socket_get_client() is failed [%d]", ret);
601                 bAllowed = false;
602                 goto _END_OF_FUNC;
603         }
604
605         ret = cynara_creds_socket_get_user(fd, user_method, &peer_user);
606         if (ret != CYNARA_API_SUCCESS) {
607                 MSG_ERR("cynara_creds_socket_get_user() is failed [%d]", ret);
608                 bAllowed = false;
609                 goto _END_OF_FUNC;
610         }
611
612         ret =  cynara_creds_socket_get_pid(fd, &peer_pid);
613         if (ret != CYNARA_API_SUCCESS) {
614                 MSG_ERR("cynara_creds_socket_get_pid() is failed [%d]", ret);
615                 bAllowed = false;
616                 goto _END_OF_FUNC;
617         }
618
619         peer_session = cynara_session_from_pid(peer_pid);
620         if (peer_session == NULL) {
621                 MSG_ERR("cynara_session_from_pid() is failed");
622                 bAllowed = false;
623                 goto _END_OF_FUNC;
624         }
625
626         switch(CmdType) {
627         case MSG_CMD_GET_MSG:
628         case MSG_CMD_COUNT_MSG:
629         case MSG_CMD_COUNT_BY_MSGTYPE:
630         case MSG_CMD_REG_INCOMING_MSG_CB:
631         case MSG_CMD_REG_INCOMING_CB_MSG_CB:
632         case MSG_CMD_REG_INCOMING_PUSH_MSG_CB:
633         case MSG_CMD_REG_SENT_STATUS_CB:
634         case MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB:
635         case MSG_CMD_REG_INCOMING_SYNCML_MSG_CB:
636         case MSG_CMD_REG_INCOMING_LBS_MSG_CB:
637         case MSG_CMD_REG_SYNCML_MSG_OPERATION_CB:
638         case MSG_CMD_REG_REPORT_MSG_INCOMING_CB:
639         case MSG_CMD_GET_CONTACT_COUNT:
640         case MSG_CMD_GET_FOLDERLIST:
641         case MSG_CMD_GET_QUICKPANEL_DATA:
642         case MSG_CMD_GET_MEMSIZE:
643         case MSG_CMD_BACKUP_MESSAGE:
644         case MSG_CMD_REG_STORAGE_CHANGE_CB:
645         case MSG_CMD_GET_REPORT_STATUS:
646         case MSG_CMD_GET_THREAD_ID_BY_ADDRESS:
647         case MSG_CMD_GET_THREAD_INFO:
648         case MSG_CMD_SYNCML_OPERATION:
649         case MSG_CMD_GET_FILTERLIST:
650         case MSG_CMD_GET_FILTER_OPERATION:
651         case MSG_CMD_GET_SMSC_OPT:
652         case MSG_CMD_GET_CB_OPT:
653         case MSG_CMD_GET_SMS_SEND_OPT:
654         case MSG_CMD_GET_MMS_SEND_OPT:
655         case MSG_CMD_GET_MMS_RECV_OPT:
656         case MSG_CMD_GET_PUSH_MSG_OPT:
657         case MSG_CMD_GET_VOICE_MSG_OPT:
658         case MSG_CMD_GET_GENERAL_MSG_OPT:
659         case MSG_CMD_GET_MSG_SIZE_OPT:
660                 ret = cynara_check(p_cynara, peer_client, peer_session, peer_user,
661                                 "http://tizen.org/privilege/message.read");
662                 if (ret != CYNARA_API_ACCESS_ALLOWED) {
663                         MSG_INFO("privilege [read] not allowd : [%d]", ret);
664                         bAllowed = false;
665                 }
666                 break;
667         case MSG_CMD_SUBMIT_REQ:
668         case MSG_CMD_SET_CB_OPT:
669         case MSG_CMD_ADD_PUSH_EVENT:
670         case MSG_CMD_DELETE_PUSH_EVENT:
671         case MSG_CMD_UPDATE_PUSH_EVENT:
672         case MSG_CMD_ADD_MSG:
673         case MSG_CMD_ADD_SYNCML_MSG:
674         case MSG_CMD_UPDATE_MSG:
675         case MSG_CMD_UPDATE_READ:
676         case MSG_CMD_UPDATE_PROTECTED:
677         case MSG_CMD_DELETE_MSG:
678         case MSG_CMD_DELALL_MSGINFOLDER:
679         case MSG_CMD_MOVE_MSGTOFOLDER:
680         case MSG_CMD_MOVE_MSGTOSTORAGE:
681         case MSG_CMD_DELETE_THREADMESSAGELIST:
682         case MSG_CMD_ADD_FOLDER:
683         case MSG_CMD_UPDATE_FOLDER:
684         case MSG_CMD_DELETE_FOLDER:
685         case MSG_CMD_RESET_DB:
686         case MSG_CMD_RESTORE_MESSAGE:
687         case MSG_CMD_DELETE_MESSAGE_BY_LIST:
688         case MSG_CMD_UPDATE_THREAD_READ:
689         case MSG_CMD_ADD_FILTER:
690         case MSG_CMD_UPDATE_FILTER:
691         case MSG_CMD_DELETE_FILTER:
692         case MSG_CMD_SET_FILTER_OPERATION:
693         case MSG_CMD_SET_FILTER_ACTIVATION:
694         case MSG_CMD_SET_SMSC_OPT:
695         case MSG_CMD_SET_SMS_SEND_OPT:
696         case MSG_CMD_SET_MMS_SEND_OPT:
697         case MSG_CMD_SET_MMS_RECV_OPT:
698         case MSG_CMD_SET_PUSH_MSG_OPT:
699         case MSG_CMD_SET_VOICE_MSG_OPT:
700         case MSG_CMD_SET_GENERAL_MSG_OPT:
701         case MSG_CMD_SET_MSG_SIZE_OPT:
702                 ret = cynara_check(p_cynara, peer_client, peer_session, peer_user,
703                                 "http://tizen.org/privilege/message.write");
704                 if (ret != CYNARA_API_ACCESS_ALLOWED) {
705                         MSG_INFO("privilege [write] not allowd : [%d]", ret);
706                         bAllowed = false;
707                 }
708                 break;
709         case MSG_CMD_CHECK_PERMISSION:
710                 ret = cynara_check(p_cynara, peer_client, peer_session, peer_user,
711                                 "http://tizen.org/privilege/message.read");
712                 if (ret != CYNARA_API_ACCESS_ALLOWED) {
713                         ret = cynara_check(p_cynara, peer_client, peer_session, peer_user,
714                                         "http://tizen.org/privilege/message.write");
715                         if (ret != CYNARA_API_ACCESS_ALLOWED) {
716                                 MSG_INFO("privilege [write] not allowd : [%d]", ret);
717                                 bAllowed = false;
718                         }
719                 }
720                 break;
721         default :
722                 break;
723         }
724
725 _END_OF_FUNC:
726         MSG_FREE(peer_client);
727         MSG_FREE(peer_user);
728         MSG_FREE(peer_session);
729
730         return bAllowed;
731 }
732
733
734 void MsgTransactionManager::setSentStatusCB(int listenerFd)
735 {
736         if (listenerFd <= 0)
737                 THROW(MsgException::INVALID_PARAM, "InParam Error: listenerFd %d", listenerFd);
738
739         statusCBFdMap[listenerFd] = true;
740 }
741
742
743 void MsgTransactionManager::setIncomingMsgCB(MSG_CMD_REG_INCOMING_MSG_CB_S *pCbInfo)
744 {
745         if (!pCbInfo) {
746                 MSG_FATAL("cbinfo NULL");
747                 return;
748         }
749
750         MsgMutexLocker lock(mx);
751
752         newmsg_list::iterator it = newMsgCBList.begin();
753
754         for (; it != newMsgCBList.end(); it++) {
755                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (it->port == pCbInfo->port)) {
756                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d, port %d", it->listenerFd, it->msgType, it->port);
757                         return;
758                 }
759         }
760
761         newMsgCBList.push_back(*pCbInfo);
762 }
763
764
765 void MsgTransactionManager::setMMSConfMsgCB(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S *pCbInfo)
766 {
767         if (!pCbInfo) {
768                 MSG_FATAL("cbinfo NULL");
769                 return;
770         }
771
772         MsgMutexLocker lock(mx);
773
774         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
775
776         for (; it != newMMSConfMsgCBList.end(); it++) {
777                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (!strncmp(it->appId, pCbInfo->appId, MAX_MMS_JAVA_APPID_LEN))) {
778                         MSG_DEBUG("Duplicated MMSConfMessageCB info fd:%d, mType:%d, appId:%s", it->listenerFd, it->msgType, it->appId);
779                         return;
780                 }
781         }
782
783         newMMSConfMsgCBList.push_back(*pCbInfo);
784 }
785
786
787 void MsgTransactionManager::setPushMsgCB(MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S *pCbInfo)
788 {
789         if (!pCbInfo) {
790                 MSG_FATAL("cbinfo NULL");
791                 return;
792         }
793
794         MsgMutexLocker lock(mx);
795
796         pushmsg_list::iterator it = newPushMsgCBList.begin();
797
798         for (; it != newPushMsgCBList.end(); it++) {
799                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && !strncmp(it->appId, pCbInfo->appId, MAX_WAPPUSH_ID_LEN)) {
800                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
801                         return;
802                 }
803         }
804
805         newPushMsgCBList.push_back(*pCbInfo);
806 }
807
808 void MsgTransactionManager::setCBMsgCB(MSG_CMD_REG_INCOMING_CB_MSG_CB_S *pCbInfo)
809 {
810         MSG_BEGIN();
811         if (!pCbInfo) {
812                 MSG_FATAL("cbinfo NULL");
813                 return;
814         }
815
816         MsgMutexLocker lock(mx);
817
818         cbmsg_list::iterator it = newCBMsgCBList.begin();
819
820         for (; it != newCBMsgCBList.end(); it++) {
821                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType)) {
822                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
823                         return;
824                 }
825         }
826         MSG_DEBUG("bSave : [%d]", pCbInfo->bsave);
827
828         if(pCbInfo->bsave) {
829                 if(MsgSettingSetBool(CB_SAVE, pCbInfo->bsave) != MSG_SUCCESS)
830                         MSG_DEBUG("MsgSettingSetBool FAIL: CB_SAVE");
831         }
832
833
834         newCBMsgCBList.push_back(*pCbInfo);
835
836         MSG_END();
837 }
838
839
840 void MsgTransactionManager::setSyncMLMsgCB(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S *pCbInfo)
841 {
842         if (!pCbInfo) {
843                 MSG_FATAL("cbinfo NULL");
844                 return;
845         }
846
847         MsgMutexLocker lock(mx);
848
849         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
850
851         for (; it != newSyncMLMsgCBList.end(); it++) {
852                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType)) {
853                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
854                         return;
855                 }
856         }
857
858         newSyncMLMsgCBList.push_back(*pCbInfo);
859 }
860
861
862 void MsgTransactionManager::setLBSMsgCB(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S *pCbInfo)
863 {
864         if (!pCbInfo) {
865                 MSG_FATAL("cbinfo NULL");
866                 return;
867         }
868
869         MsgMutexLocker lock(mx);
870
871         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
872
873         for (; it != newLBSMsgCBList.end(); it++) {
874                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType)) {
875                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
876                         return;
877                 }
878         }
879
880         newLBSMsgCBList.push_back(*pCbInfo);
881 }
882
883
884 void MsgTransactionManager::setJavaMMSList(MSG_CMD_REG_INCOMING_JAVAMMS_TRID_S *pTrId)
885 {
886         if (!pTrId) {
887                 MSG_FATAL("trId NULL");
888                 return;
889         }
890
891         javamms_list::iterator it;
892
893         for (it = javaMMSList.begin(); it != javaMMSList.end(); it++) {
894                 if (!strcmp(it->id, pTrId->id)) {
895                         MSG_SEC_DEBUG("Duplicated javaMMS transaction Id:%s", it->id);
896                         return;
897                 }
898         }
899
900         javaMMSList.push_back(*pTrId);
901 }
902
903
904 void MsgTransactionManager::setSyncMLMsgOperationCB(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S *pCbInfo)
905 {
906         if (!pCbInfo) {
907                 MSG_FATAL("cbinfo NULL");
908                 return;
909         }
910
911         MsgMutexLocker lock(mx);
912
913         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
914
915         for (; it != operationSyncMLMsgCBList.end(); it++) {
916                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType)) {
917                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
918                         return;
919                 }
920         }
921
922         operationSyncMLMsgCBList.push_back(*pCbInfo);
923 }
924
925
926 void MsgTransactionManager::setStorageChangeCB(int listenerFd)
927 {
928         if (listenerFd <= 0)
929                 THROW(MsgException::INVALID_PARAM, "InParam Error: listenerFd %d", listenerFd);
930
931         MsgMutexLocker lock(mx);
932
933         storageChangeFdMap[listenerFd] = true;
934 }
935
936
937 void MsgTransactionManager::setReportMsgCB(int listenerFd)
938 {
939         if (listenerFd <= 0)
940                 THROW(MsgException::INVALID_PARAM, "InParam Error: listenerFd %d", listenerFd);
941
942         MsgMutexLocker lock(mx);
943
944         reportMsgCBFdMap[listenerFd] = true;
945 }
946
947
948 javamms_list& MsgTransactionManager::getJavaMMSList()
949 {
950         return javaMMSList;
951 }
952
953
954 void MsgTransactionManager::broadcastIncomingMsgCB(const msg_error_t err, const MSG_MESSAGE_INFO_S *msgInfo)
955 {
956         MSG_BEGIN();
957
958         if ((msgInfo->msgPort.valid == true) && (msgInfo->msgPort.dstPort == MSG_LBS_PORT)) {
959                 MSG_DEBUG("Message for LBS.");
960
961                 if (msgInfo->bTextSms == false) {
962                         MSG_DEBUG("msgInfo->bTextSms == false");
963
964                         int fileSize = 0;
965
966                         char* pFileData = NULL;
967                         unique_ptr<char*, void(*)(char**)> buf(&pFileData, unique_ptr_deleter);
968
969                         if (MsgOpenAndReadFile(msgInfo->msgData, &pFileData, &fileSize) == true)
970                                 MsgLbsSms(pFileData, fileSize);
971                         else
972                                 MSG_DEBUG("MsgOpenAndReadFile failed.");
973                 } else {
974                         MsgLbsSms(msgInfo->msgText, (int)msgInfo->dataSize);
975                 }
976                 return;
977         }
978
979         char* pEventData = NULL;
980         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
981
982         char* encodedData = NULL;
983         unique_ptr<char*, void(*)(char**)> buf(&encodedData, unique_ptr_deleter);
984         int dataSize = MsgEncodeMsgInfo(msgInfo, &encodedData);
985
986         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_INCOMING_MSG_IND, err, (void**)(&pEventData));
987
988         MSG_DEBUG("valid %d dstport %d", msgInfo->msgPort.valid, msgInfo->msgPort.dstPort);
989
990         MsgMutexLocker lock(mx);
991
992         newmsg_list::iterator it = newMsgCBList.begin();
993
994         for (; it != newMsgCBList.end(); it++) {
995                 MSG_DEBUG("fd %d dstport %d", it->listenerFd, it->port);
996
997                 if ((msgInfo->msgPort.valid == false) && (it->port == 0)) {
998                         MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
999                         write(it->listenerFd, pEventData, eventSize);
1000                 } else if ((msgInfo->msgPort.valid == true) && (it->port == msgInfo->msgPort.dstPort)) {
1001                         MSG_DEBUG("Send incoming port msg to listener %d", it->listenerFd);
1002                         write(it->listenerFd, pEventData, eventSize);
1003                 }
1004         }
1005
1006         MSG_END();
1007 }
1008
1009
1010 void MsgTransactionManager::broadcastMMSConfCB(const msg_error_t err, const MSG_MESSAGE_INFO_S *msgInfo, const MMS_RECV_DATA_S *mmsRecvData)
1011 {
1012         MSG_BEGIN();
1013
1014         char* pEventData = NULL;
1015         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1016
1017         char* encodedData = NULL;
1018         unique_ptr<char*, void(*)(char**)> buf(&encodedData, unique_ptr_deleter);
1019         int dataSize = MsgEncodeMsgInfo(msgInfo, &encodedData);
1020
1021         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_INCOMING_MMS_CONF, err, (void**)(&pEventData));
1022
1023         MsgMutexLocker lock(mx);
1024
1025         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
1026
1027         for (; it != newMMSConfMsgCBList.end(); it++) {
1028                 MSG_DEBUG("fd:%d appId:%s", it->listenerFd, it->appId);
1029
1030                 if (mmsRecvData->msgAppId.valid == true) {
1031                         if (!strcmp(it->appId, mmsRecvData->msgAppId.appId)) {
1032                                 MSG_DEBUG("Send incoming java msg to listener %d", it->listenerFd);
1033                                 write(it->listenerFd, pEventData, eventSize);
1034                         }
1035                 } else {
1036                         if (strlen(it->appId) <= 0) {
1037                                 MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
1038                                 write(it->listenerFd, pEventData, eventSize);
1039                         }
1040                 }
1041         }
1042
1043
1044         MSG_END();
1045 }
1046
1047 void MsgTransactionManager::broadcastPushMsgCB(const msg_error_t err, const MSG_PUSH_MESSAGE_DATA_S *pushData)
1048 {
1049         MSG_BEGIN();
1050
1051         char* pEventData = NULL;
1052         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1053
1054         int eventSize = MsgMakeEvent(pushData, sizeof(MSG_PUSH_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND, err, (void**)(&pEventData));
1055
1056         MsgMutexLocker lock(mx);
1057
1058         pushmsg_list::iterator it = newPushMsgCBList.begin();
1059
1060         for (; it != newPushMsgCBList.end(); it++) {
1061                 MSG_DEBUG("registered_appid : %s, incoming_appid: %s", it->appId, pushData->pushAppId);
1062                 if (!strcmp(it->appId, pushData->pushAppId)) {
1063                         MSG_DEBUG("Send incoming Push information to listener %d", it->listenerFd);
1064                         write(it->listenerFd, pEventData, eventSize);
1065                 }
1066         }
1067
1068         MSG_END();
1069 }
1070
1071 void MsgTransactionManager::broadcastCBMsgCB(const msg_error_t err, const MSG_CB_MSG_S *cbMsg, msg_message_id_t cbMsgId)
1072 {
1073         MSG_BEGIN();
1074
1075         char* pEventData = NULL;
1076         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1077
1078         int eventSize = MsgMakeEvent(cbMsg, sizeof(MSG_CB_MSG_S), MSG_EVENT_PLG_INCOMING_CB_MSG_IND, err, (void**)(&pEventData));
1079
1080         MsgMutexLocker lock(mx);
1081
1082         cbmsg_list::iterator it = newCBMsgCBList.begin();
1083
1084         for (; it != newCBMsgCBList.end(); it++) {
1085                 MSG_DEBUG("Send incoming CB information to listener %d", it->listenerFd);
1086                 write(it->listenerFd, pEventData, eventSize);
1087         }
1088
1089         /* Send system event */
1090         bundle *b = NULL;
1091         b = bundle_create();
1092         if (b) {
1093                 bundle_add_str(b, EVT_KEY_MSG_TYPE, EVT_VAL_CB);
1094                 char msgId[MSG_EVENT_MSG_ID_LEN] = {0, };
1095                 snprintf(msgId, sizeof(msgId), "%u", cbMsgId);
1096                 bundle_add_str(b, EVT_KEY_MSG_ID, msgId);
1097                 eventsystem_send_system_event(SYS_EVENT_INCOMMING_MSG, b);
1098                 bundle_free(b);
1099         }
1100
1101         MSG_END();
1102 }
1103
1104 void MsgTransactionManager::broadcastSyncMLMsgCB(const msg_error_t err, const MSG_SYNCML_MESSAGE_DATA_S *syncMLData)
1105 {
1106         MSG_BEGIN();
1107
1108         char* pEventData = NULL;
1109         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1110
1111         int eventSize = MsgMakeEvent(syncMLData, sizeof(MSG_SYNCML_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND, err, (void**)(&pEventData));
1112
1113         MsgMutexLocker lock(mx);
1114
1115         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
1116
1117         for (; it != newSyncMLMsgCBList.end(); it++) {
1118                 MSG_DEBUG("Send incoming SyncML information to listener %d", it->listenerFd);
1119                 write(it->listenerFd, pEventData, eventSize);
1120         }
1121
1122         MSG_END();
1123 }
1124
1125
1126 void MsgTransactionManager::broadcastLBSMsgCB(const msg_error_t err, const MSG_LBS_MESSAGE_DATA_S *lbsData)
1127 {
1128         MSG_BEGIN();
1129
1130 #if 0
1131         char* pEventData = NULL;
1132         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1133
1134         int eventSize = MsgMakeEvent(lbsData, sizeof(MSG_LBS_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_LBS_MSG_IND, err, (void**)(&pEventData));
1135
1136         MsgMutexLocker lock(mx);
1137
1138         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
1139
1140         for (; it != newLBSMsgCBList.end(); it++) {
1141                 MSG_DEBUG("Send incoming LBS msg to listener %d", it->listenerFd);
1142                 write(it->listenerFd, pEventData, eventSize);
1143         }
1144 #else
1145         MsgLbsWapPush(lbsData->pushHeader, lbsData->pushBody, lbsData->pushBodyLen);
1146 #endif
1147         MSG_END();
1148 }
1149
1150
1151 void MsgTransactionManager::broadcastSyncMLMsgOperationCB(const msg_error_t err, const int msgId, const int extId)
1152 {
1153         MSG_BEGIN();
1154
1155         char* pEventData = NULL;
1156         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1157
1158         char* encodedData = NULL;
1159         unique_ptr<char*, void(*)(char**)> buf(&encodedData, unique_ptr_deleter);
1160
1161         /* Encoding Storage Change Data */
1162         int dataSize = MsgEncodeSyncMLOperationData(msgId, extId, &encodedData);
1163
1164         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_SYNCML_OPERATION, err, (void**)(&pEventData));
1165
1166         MsgMutexLocker lock(mx);
1167
1168         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
1169
1170         for( ; it != operationSyncMLMsgCBList.end() ; it++ ) {
1171                 MSG_DEBUG("Send SyncML operation to listener %d", it->listenerFd);
1172                 write(it->listenerFd, pEventData, eventSize);
1173         }
1174
1175         MSG_END();
1176 }
1177
1178
1179 void MsgTransactionManager::broadcastStorageChangeCB(const msg_error_t err, const msg_storage_change_type_t storageChangeType, const msg_id_list_s *pMsgIdList)
1180 {
1181         MSG_BEGIN();
1182
1183         if(pMsgIdList == NULL) {
1184                 MSG_DEBUG("pMsgIdList is NULL.");
1185                 return;
1186         }
1187
1188         MSG_DEBUG("storageChangeType [%d]", storageChangeType);
1189
1190         int dataSize = 0;
1191
1192         char* pEventData = NULL;
1193         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1194
1195         char* encodedData = NULL;
1196         unique_ptr<char*, void(*)(char**)> buf(&encodedData, unique_ptr_deleter);
1197
1198         /* Encoding Storage Change Data */
1199         dataSize = MsgEncodeStorageChangeData(storageChangeType, pMsgIdList, &encodedData);
1200
1201         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_STORAGE_CHANGE_IND, err, (void**)(&pEventData));
1202
1203         MsgMutexLocker lock(mx);
1204
1205         fd_map::iterator it = storageChangeFdMap.begin();
1206
1207         for (; it != storageChangeFdMap.end(); it++) {
1208                 MSG_DEBUG("Send Storage Change Callback to listener %d", it->first);
1209                 write(it->first, pEventData, eventSize);
1210         }
1211
1212         MSG_END();
1213 }
1214
1215
1216 void MsgTransactionManager::broadcastReportMsgCB(const msg_error_t err, const msg_report_type_t reportMsgType, const MSG_MESSAGE_INFO_S *pMsgInfo)
1217 {
1218         MSG_BEGIN();
1219
1220         if(pMsgInfo == NULL) {
1221                 MSG_DEBUG("pMsgInfo is NULL.");
1222                 return;
1223         }
1224
1225         MSG_DEBUG("reportMsgType [%d]", reportMsgType);
1226
1227         int dataSize = 0;
1228
1229         char* pEventData = NULL;
1230         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1231
1232         char* encodedData = NULL;
1233         unique_ptr<char*, void(*)(char**)> buf(&encodedData, unique_ptr_deleter);
1234
1235         /* Encoding Storage Change Data */
1236         dataSize = MsgEncodeReportMsgData(reportMsgType, pMsgInfo, &encodedData);
1237
1238         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_REPORT_MSG_INCOMING_IND, err, (void**)(&pEventData));
1239
1240         MsgMutexLocker lock(mx);
1241
1242         fd_map::iterator it = reportMsgCBFdMap.begin();
1243
1244         for (; it != reportMsgCBFdMap.end(); it++) {
1245                 MSG_DEBUG("Send Report Message Incoming Callback to listener %d", it->first);
1246                 write(it->first, pEventData, eventSize);
1247         }
1248
1249         MSG_END();
1250 }
1251
1252
1253 bool MsgTransactionManager::initCynara()
1254 {
1255         int ret;
1256
1257         ret = cynara_initialize(&p_cynara, NULL);
1258
1259         if (ret == CYNARA_API_SUCCESS) {
1260                 MSG_INFO("cynara_initialize() is successful");
1261         } else {
1262                 MSG_INFO("cynara_initialize() is failed [%d]", ret);
1263                 return false;
1264         }
1265
1266         ret = cynara_creds_get_default_client_method(&client_method);
1267         if (ret != CYNARA_API_SUCCESS) {
1268                 MSG_ERR("cynara_creds_get_default_client_method() is failed [%d]", ret);
1269                 return false;
1270         }
1271
1272         ret = cynara_creds_get_default_user_method(&user_method);
1273         if (ret != CYNARA_API_SUCCESS) {
1274                 MSG_ERR("cynara_creds_get_default_user_method() is failed [%d]", ret);
1275                 return false;
1276         }
1277
1278         return true;
1279 }
1280
1281
1282 void MsgTransactionManager::finishCynara()
1283 {
1284         int ret;
1285
1286         ret = cynara_finish(p_cynara);
1287
1288         if (ret == CYNARA_API_SUCCESS) {
1289                 MSG_INFO("cynara_finish() is successful");
1290         } else {
1291                 MSG_INFO("cynara_finish() is failed [%d]", ret);
1292         }
1293
1294         p_cynara = NULL;
1295 }