Modify flora license version.
[platform/core/messaging/msg-service.git] / framework / transaction-manager / MsgTransManager.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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
22 #include <security-server.h>
23
24 #include "MsgDebug.h"
25 #include "MsgMemory.h"
26 #include "MsgException.h"
27 #include "MsgCppTypes.h"
28 #include "MsgContact.h"
29 #include "MsgIpcSocket.h"
30 #include "MsgGconfWrapper.h"
31 #include "MsgUtilFunction.h"
32 #include "MsgCmdHandler.h"
33 #include "MsgSettingHandler.h"
34 #include "MsgStorageHandler.h"
35 #include "MsgPluginManager.h"
36 #include "MsgTransManager.h"
37
38 /*==================================================================================================
39                                      FUNCTION IMPLEMENTATION
40 ==================================================================================================*/
41 void MsgContactChangedCallback()
42 {
43         msg_id_list_s msgIdList;
44         memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
45
46         MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_CONTACT, &msgIdList);
47 }
48
49 /*==================================================================================================
50                                      IMPLEMENTATION OF MsgTransactionManager - Member Functions
51 ==================================================================================================*/
52 MsgTransactionManager* MsgTransactionManager::pInstance = NULL;
53 MsgIpcServerSocket MsgTransactionManager::servSock;
54
55
56 MsgTransactionManager::MsgTransactionManager() : running(false), mx(), cv()
57 {
58         sentMsgMap.clear();
59
60         statusCBFdMap.clear();
61         newMsgCBList.clear();
62         newMMSConfMsgCBList.clear();
63         newSyncMLMsgCBList.clear();
64         newLBSMsgCBList.clear();
65         javaMMSList.clear();
66         operationSyncMLMsgCBList.clear();
67         storageChangeFdMap.clear();
68
69         handlerMap.clear();
70
71 //      Fill in mMsgHandlers, as given in the below.
72         handlerMap[MSG_CMD_ADD_MSG]                             = &MsgAddMessageHandler;
73         handlerMap[MSG_CMD_ADD_SYNCML_MSG]              = &MsgAddSyncMLMessageHandler;
74         handlerMap[MSG_CMD_UPDATE_MSG]                  = &MsgUpdateMessageHandler;
75         handlerMap[MSG_CMD_UPDATE_READ]                         = &MsgUpdateReadStatusHandler;
76         handlerMap[MSG_CMD_UPDATE_PROTECTED]            = &MsgUpdateProtectedStatusHandler;
77         handlerMap[MSG_CMD_DELETE_MSG]                  = &MsgDeleteMessageHandler;
78         handlerMap[MSG_CMD_DELALL_MSGINFOLDER]  = &MsgDeleteAllMessageInFolderHandler;
79         handlerMap[MSG_CMD_MOVE_MSGTOFOLDER]    = &MsgMoveMessageToFolderHandler;
80         handlerMap[MSG_CMD_MOVE_MSGTOSTORAGE]   = &MsgMoveMessageToStorageHandler;
81         handlerMap[MSG_CMD_COUNT_MSG]                   = &MsgCountMessageHandler;
82         handlerMap[MSG_CMD_GET_MSG]                             = &MsgGetMessageHandler;
83         handlerMap[MSG_CMD_GET_FOLDERVIEWLIST]  = &MsgGetFolderViewListHandler;
84
85         handlerMap[MSG_CMD_ADD_FOLDER]                  = &MsgAddFolderHandler;
86         handlerMap[MSG_CMD_UPDATE_FOLDER]               = &MsgUpdateFolderHandler;
87         handlerMap[MSG_CMD_DELETE_FOLDER]               = &MsgDeleteFolderHandler;
88         handlerMap[MSG_CMD_GET_FOLDERLIST]              = &MsgGetFolderListHandler;
89
90         handlerMap[MSG_CMD_ADD_FILTER]                  = &MsgAddFilterHandler;
91         handlerMap[MSG_CMD_UPDATE_FILTER]                       = &MsgUpdateFilterHandler;
92         handlerMap[MSG_CMD_DELETE_FILTER]               = &MsgDeleteFilterHandler;
93         handlerMap[MSG_CMD_GET_FILTERLIST]              = &MsgGetFilterListHandler;
94         handlerMap[MSG_CMD_SET_FILTER_OPERATION]        = &MsgSetFilterOperationHandler;
95         handlerMap[MSG_CMD_GET_FILTER_OPERATION]        = &MsgGetFilterOperationHandler;
96         handlerMap[MSG_CMD_SET_FILTER_ACTIVATION] = &MsgSetFilterActivationHandler;
97
98         handlerMap[MSG_CMD_GET_MSG_TYPE]                        = &MsgGetMsgTypeHandler;
99
100         handlerMap[MSG_CMD_SUBMIT_REQ]                  = &MsgSubmitReqHandler;
101         handlerMap[MSG_CMD_CANCEL_REQ]                  = &MsgCancelReqHandler;
102
103         handlerMap[MSG_CMD_REG_SENT_STATUS_CB]  = &MsgRegSentStatusCallbackHandler;
104         handlerMap[MSG_CMD_REG_STORAGE_CHANGE_CB] = &MsgRegStorageChangeCallbackHandler;
105         handlerMap[MSG_CMD_REG_INCOMING_MSG_CB] = &MsgRegIncomingMsgCallbackHandler;
106         handlerMap[MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB]        = &MsgRegIncomingMMSConfMsgCallbackHandler;
107         handlerMap[MSG_CMD_REG_INCOMING_SYNCML_MSG_CB]  = &MsgRegIncomingSyncMLMsgCallbackHandler;
108         handlerMap[MSG_CMD_REG_INCOMING_PUSH_MSG_CB]    = &MsgRegIncomingPushMsgCallbackHandler;
109         handlerMap[MSG_CMD_REG_INCOMING_CB_MSG_CB]      = &MsgRegIncomingCBMsgCallbackHandler;
110         handlerMap[MSG_CMD_REG_INCOMING_LBS_MSG_CB]     = &MsgRegIncomingLBSMsgCallbackHandler;
111         handlerMap[MSG_CMD_REG_SYNCML_MSG_OPERATION_CB] = &MsgRegSyncMLMsgOperationCallbackHandler;
112
113         handlerMap[MSG_CMD_PLG_SENT_STATUS_CNF] = &MsgSentStatusHandler;
114         handlerMap[MSG_CMD_PLG_STORAGE_CHANGE_IND]      = &MsgStorageChangeHandler;
115         handlerMap[MSG_CMD_PLG_INCOMING_MSG_IND]        = &MsgIncomingMsgHandler;
116         handlerMap[MSG_CMD_PLG_INCOMING_MMS_CONF]       = &MsgIncomingMMSConfMsgHandler;
117         handlerMap[MSG_CMD_PLG_INCOMING_PUSH_IND]       = &MsgIncomingPushMsgHandler;
118         handlerMap[MSG_CMD_PLG_INCOMING_CB_IND] = &MsgIncomingCBMsgHandler;
119
120         handlerMap[MSG_CMD_PLG_INCOMING_SYNCML_IND] = &MsgIncomingSyncMLMsgHandler;
121         handlerMap[MSG_CMD_PLG_INCOMING_LBS_IND] = &MsgIncomingLBSMsgHandler;
122         handlerMap[MSG_CMD_PLG_INIT_SIM_BY_SAT] = &MsgInitSimBySatHandler;
123
124         handlerMap[MSG_CMD_GET_THREADVIEWLIST]  = &MsgGetThreadViewListHandler;
125         handlerMap[MSG_CMD_GET_CONVERSATIONVIEWLIST]    = &MsgGetConversationViewListHandler;
126         handlerMap[MSG_CMD_DELETE_THREADMESSAGELIST]    = &MsgDeleteThreadMessageListHandler;
127
128         handlerMap[MSG_CMD_GET_CONTACT_COUNT]   = &MsgCountMsgByContactHandler;
129         handlerMap[MSG_CMD_GET_QUICKPANEL_DATA]         = &MsgGetQuickPanelDataHandler;
130         handlerMap[MSG_CMD_COUNT_BY_MSGTYPE]    = &MsgCountMsgByTypeHandler;
131         handlerMap[MSG_CMD_RESET_DB]    = &MsgResetDatabaseHandler;
132         handlerMap[MSG_CMD_GET_MEMSIZE]         = &MsgGetMemSizeHandler;
133
134         handlerMap[MSG_CMD_BACKUP_MESSAGE]      = &MsgBackupMessageHandler;
135         handlerMap[MSG_CMD_RESTORE_MESSAGE] = &MsgRestoreMessageHandler;
136
137         handlerMap[MSG_CMD_UPDATE_THREAD_READ] = &MsgUpdateThreadReadStatusHandler;
138
139         handlerMap[MSG_CMD_SYNCML_OPERATION] = &MsgSyncMLMsgOperationHandler;
140         handlerMap[MSG_CMD_GET_REPORT_STATUS]                   = &MsgGetReportStatusHandler;
141
142         handlerMap[MSG_CMD_GET_THREAD_ID_BY_ADDRESS] = &MsgGetThreadIdByAddressHandler;
143         handlerMap[MSG_CMD_GET_THREAD_INFO] = &MsgGetThreadInfoHandler;
144
145         handlerMap[MSG_CMD_GET_SMSC_OPT] = &MsgGetConfigHandler;
146         handlerMap[MSG_CMD_GET_CB_OPT] = &MsgGetConfigHandler;
147         handlerMap[MSG_CMD_GET_SMS_SEND_OPT] = &MsgGetConfigHandler;
148         handlerMap[MSG_CMD_GET_MMS_SEND_OPT] = &MsgGetConfigHandler;
149         handlerMap[MSG_CMD_GET_MMS_RECV_OPT] = &MsgGetConfigHandler;
150         handlerMap[MSG_CMD_GET_PUSH_MSG_OPT] = &MsgGetConfigHandler;
151         handlerMap[MSG_CMD_GET_VOICE_MSG_OPT] = &MsgGetConfigHandler;
152         handlerMap[MSG_CMD_GET_GENERAL_MSG_OPT] = &MsgGetConfigHandler;
153         handlerMap[MSG_CMD_GET_MSG_SIZE_OPT] = &MsgGetConfigHandler;
154
155         handlerMap[MSG_CMD_SET_SMSC_OPT] = &MsgSetConfigHandler;
156         handlerMap[MSG_CMD_SET_CB_OPT] = &MsgSetConfigHandler;
157         handlerMap[MSG_CMD_SET_SMS_SEND_OPT] = &MsgSetConfigHandler;
158         handlerMap[MSG_CMD_SET_MMS_SEND_OPT] = &MsgSetConfigHandler;
159         handlerMap[MSG_CMD_SET_MMS_RECV_OPT] = &MsgSetConfigHandler;
160         handlerMap[MSG_CMD_SET_PUSH_MSG_OPT] = &MsgSetConfigHandler;
161         handlerMap[MSG_CMD_SET_VOICE_MSG_OPT] = &MsgSetConfigHandler;
162         handlerMap[MSG_CMD_SET_GENERAL_MSG_OPT] = &MsgSetConfigHandler;
163         handlerMap[MSG_CMD_SET_MSG_SIZE_OPT] = &MsgSetConfigHandler;
164
165         handlerMap[MSG_CMD_ADD_PUSH_EVENT] = &MsgAddPushEventHandler;
166         handlerMap[MSG_CMD_DELETE_PUSH_EVENT] = &MsgDeletePushEventHandler;
167         handlerMap[MSG_CMD_UPDATE_PUSH_EVENT] = &MsgUpdatePushEventHandler;
168         handlerMap[MSG_CMD_DELETE_MESSAGE_BY_LIST] = &MsgDeleteMessageByListHandler;
169 }
170
171
172 MsgTransactionManager::~MsgTransactionManager()
173 {
174 //      pthread_cond_init(&retCV, NULL); // = PTHREAD_COND_INITIALIZER;
175
176 }
177
178
179 MsgTransactionManager* MsgTransactionManager::instance()
180 {
181         if (!pInstance)
182                 pInstance = new MsgTransactionManager();
183
184         return pInstance;
185 }
186
187
188 void MsgTransactionManager::run()
189 {
190         servSock.open(MSG_SOCKET_PATH);
191
192         fd_set readfds = servSock.fdSet();
193         int nfds = 0;
194
195         MSG_DEBUG("Start Transaction Manager");
196
197         while(1)
198         {
199                 readfds = servSock.fdSet();
200                 nfds = servSock.maxFd();
201
202                 MSG_DEBUG("Wait For Select() : nfds %d", nfds);
203
204                 // set Status;
205                 setTMStatus();
206
207                 if(select(nfds, &readfds, NULL, NULL, NULL) == -1) {
208                         THROW(MsgException::SELECT_ERROR, "select error : %s", strerror(errno));
209                 }
210
211                 try
212                 {
213                         for (int i=0 ; i < nfds; i++)
214                         {
215                                 if (FD_ISSET(i, &readfds))
216                                 {
217                                         if (i == servSock.fd()) // if it is socket connection request
218                                                 servSock.accept();
219                                         else
220                                                 handleRequest(i);
221                                 }
222                         }
223                 }
224                 catch (MsgException& e)
225                 {
226                         MSG_FATAL("%s", e.what());
227                 }
228                 catch (exception& e)
229                 {
230                         MSG_FATAL("%s", e.what());
231                 }
232
233                 // Release Memory
234                 //MsgReleaseMemory();
235         }
236 }
237
238
239 void MsgTransactionManager::write(int fd, const char* buf, int len)
240 {
241         servSock.write(fd, buf, len);
242 }
243
244
245 void MsgTransactionManager::insertSentMsg(int reqId, MSG_PROXY_INFO_S* pPrxInfo)
246 {
247         if (pPrxInfo == NULL)
248                 THROW(MsgException::SENT_STATUS_ERROR, "Input Parameter is NULL");
249
250         MSG_DEBUG("msg for submit: reqId %d listenerFd %d handleAddr %x", reqId, pPrxInfo->listenerFd, pPrxInfo->handleAddr);
251
252         fd_map::iterator it = statusCBFdMap.find(pPrxInfo->listenerFd);
253
254         if (it == statusCBFdMap.end()) { // if the status CB is not registered
255                 MSG_DEBUG("No sent_status registered for fd %d", pPrxInfo->listenerFd);
256         } else {
257                 sentMsgMap.insert(make_pair(reqId, *pPrxInfo));
258         }
259 }
260
261
262 MSG_PROXY_INFO_S* MsgTransactionManager::getProxyInfo(int reqId)
263 {
264         sentmsg_map::iterator it = sentMsgMap.find(reqId);
265
266         if (it == sentMsgMap.end())
267         {
268                 //THROW(MsgException::SENT_STATUS_ERROR, "No submit request for %d", reqId);
269                 MSG_DEBUG("No sent status cb found (exception: mobile tracker)");
270                 return NULL;
271         }
272
273         return &(it->second);
274 }
275
276
277 void MsgTransactionManager::delProxyInfo(int reqId)
278 {
279         sentmsg_map::iterator it = sentMsgMap.find(reqId);
280
281         if (it == sentMsgMap.end())
282         {
283                 THROW(MsgException::SENT_STATUS_ERROR, "channel info does not exist");
284         }
285
286         sentMsgMap.erase(it);
287 }
288
289
290 void MsgTransactionManager::handleRequest(int fd)
291 {
292         MSG_BEGIN();
293
294         MSG_DEBUG("Event from fd %d", fd);
295
296         char* buf = NULL;
297         AutoPtr<char> wrap(&buf);
298         int len;
299         int ret = servSock.read(fd, &buf, &len);
300
301         if( ret == CLOSE_CONNECTION_BY_SIGNAL || ret == CLOSE_CONNECTION_BY_USER || ret < 0)
302         {
303                 MSG_DEBUG("Read value [%d]", ret);
304                 cleanup(fd);
305                 return;
306         }
307
308         if (len <= 0)
309                 THROW(MsgException::INVALID_RESULT, "read buffer size <= 0");
310
311         char* pEventData = NULL;
312         AutoPtr<char> eventBuf(&pEventData);
313
314         int eventSize = 0;
315
316         // decoding cmd from APP
317         MSG_CMD_S* pCmd = (MSG_CMD_S*) buf;
318         MSG_DEBUG("Command Type [%d : %s]", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType));
319
320         if (pCmd->cmdType > MSG_CMD_NUM)
321                 THROW(MsgException::OUT_OF_RANGE, "request CMD is not defined");
322
323         if (pCmd->cmdType < MSG_CMD_GET_REPORT_STATUS)
324         {
325                 // check privilege
326                 if (checkPrivilege(pCmd->cmdType, pCmd->cmdCookie) == false)
327                 {
328 #ifdef MSG_CHECK_PRIVILEGE
329                         eventSize = sizeof(MSG_EVENT_S);
330
331                         pEventData = new char[eventSize];
332
333                         MSG_EVENT_S* pMsgEvent = (MSG_EVENT_S*)pEventData;
334
335                         pMsgEvent->eventType = pCmd->cmdType;
336                         pMsgEvent->result = MSG_ERR_SECURITY_ERROR;
337
338                         MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
339                         servSock.write(fd, pEventData, eventSize);
340
341                         return;
342 #endif
343                 }
344         }
345
346         // determine the handler based on pCmd->cmdType
347         int (*pfHandler)(const MSG_CMD_S*, char**) = NULL;
348
349         pfHandler = handlerMap[pCmd->cmdType];
350
351         if (!pfHandler)
352                 THROW(MsgException::INVALID_PARAM, "No handler for %d", pCmd->cmdType);
353
354         // run handler function
355         eventSize = pfHandler(pCmd, &pEventData);
356
357         if (eventSize == 0 || pEventData == NULL)
358                 THROW(MsgException::INVALID_RESULT, "event size = 0 or event data = NULL");
359
360         MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
361
362         servSock.write(fd, pEventData, eventSize);
363
364         MSG_END();
365 }
366
367
368 // terminating the socket connection between ipc server and ipc client
369 void MsgTransactionManager::cleanup(int fd)
370 {
371         MSG_BEGIN();
372
373         servSock.close(fd);
374
375         MSG_DEBUG("fd %d disonnected", fd);
376
377         // remove sent msg info for fd
378         sentmsg_map::iterator sentmsg_it = sentMsgMap.begin();
379
380         for (; sentmsg_it != sentMsgMap.end(); sentmsg_it++)
381         {
382                 if (sentmsg_it->second.listenerFd == fd)
383                 {
384                         sentmsg_it->second.listenerFd = 0;
385                         sentmsg_it->second.handleAddr = 0;
386                 }
387         }
388
389         // remove sent status callback for fd
390         statusCBFdMap.erase(fd);
391
392         // remove all newMsgCBs for fd
393         newmsg_list::iterator newmsg_it = newMsgCBList.begin();
394
395         while (newmsg_it != newMsgCBList.end())
396         {
397                 if (newmsg_it->listenerFd == fd)
398                 {
399                         newmsg_it = newMsgCBList.erase(newmsg_it);
400                 }
401                 else
402                 {
403                         ++newmsg_it;
404                 }
405         }
406
407         // remove all newMMSConfMsgCBs for fd
408         mmsconf_list::iterator mmsconf_it = newMMSConfMsgCBList.begin();
409
410         while (mmsconf_it != newMMSConfMsgCBList.end())
411         {
412                 if (mmsconf_it->listenerFd == fd)
413                 {
414                         mmsconf_it = newMMSConfMsgCBList.erase(mmsconf_it);
415                 }
416                 else
417                 {
418                         ++mmsconf_it;
419                 }
420         }
421
422         // remove all newSyncMLMsgCBs for fd
423         syncmlmsg_list::iterator syncmlmsg_it = newSyncMLMsgCBList.begin();
424
425         while (syncmlmsg_it != newSyncMLMsgCBList.end())
426         {
427                 if (syncmlmsg_it->listenerFd == fd)
428                 {
429                         syncmlmsg_it = newSyncMLMsgCBList.erase(syncmlmsg_it);
430                 }
431                 else
432                 {
433                         ++syncmlmsg_it;
434                 }
435         }
436
437         // remove all newLBSMsgCBs for fd
438         lbsmsg_list::iterator lbsmsg_it = newLBSMsgCBList.begin();
439
440         while (lbsmsg_it != newLBSMsgCBList.end())
441         {
442                 if (lbsmsg_it->listenerFd == fd)
443                 {
444                         lbsmsg_it = newLBSMsgCBList.erase(lbsmsg_it);
445                 }
446                 else
447                 {
448                         ++lbsmsg_it;
449                 }
450         }
451
452         // remove all newPushMsgCBs for fd
453         pushmsg_list::iterator pushmsg_it = newPushMsgCBList.begin();
454
455         while (pushmsg_it != newPushMsgCBList.end())
456         {
457                 if (pushmsg_it->listenerFd == fd)
458                 {
459                         pushmsg_it = newPushMsgCBList.erase(pushmsg_it);
460                 }
461                 else
462                 {
463                         ++pushmsg_it;
464                 }
465         }
466
467         // remove all newCBMsgCBs for fd
468         cbmsg_list::iterator cbmsg_it = newCBMsgCBList.begin();
469         bool bSave = false;
470
471         while (cbmsg_it != newCBMsgCBList.end())
472         {
473                 if (cbmsg_it->listenerFd == fd)
474                 {
475                         cbmsg_it = newCBMsgCBList.erase(cbmsg_it);
476                 }
477                 else
478                 {
479                         if(cbmsg_it->bsave == true)
480                                 bSave = true;
481                         ++cbmsg_it;
482                 }
483         }
484
485         // remove all operationSyncMLMsgCBs for fd
486         syncmlop_list::iterator syncmlop_it = operationSyncMLMsgCBList.begin();
487
488         while (syncmlop_it != operationSyncMLMsgCBList.end())
489         {
490                 if (syncmlop_it->listenerFd == fd)
491                 {
492                         syncmlop_it = operationSyncMLMsgCBList.erase(syncmlop_it);
493                 }
494                 else
495                 {
496                         ++syncmlop_it;
497                 }
498         }
499
500         // remove storage change callback for fd
501         storageChangeFdMap.erase(fd);
502
503         MSG_END();
504 }
505
506
507 bool MsgTransactionManager::checkPrivilege(MSG_CMD_TYPE_T CmdType, const char *pCookie)
508 {
509         if (CmdType >= MSG_CMD_PLG_SENT_STATUS_CNF && CmdType <= MSG_CMD_PLG_INIT_SIM_BY_SAT)
510         {
511                 MSG_DEBUG("Request from Plug-in");
512                 return true;
513         }
514
515         // Get Cookie from APP
516         if (pCookie == NULL)
517         {
518                 MSG_DEBUG("Cookie is NULL");
519                 return false;
520         }
521
522 #ifdef MSG_FOR_DEBUG
523         for (int i = 0; i < MAX_COOKIE_LEN; i++)
524         {
525                 MSG_DEBUG("cookie : [%02x]", pCookie[i]);
526         }
527 #endif
528
529         // Check Cookie
530         size_t cookieSize;
531         gid_t gid;
532
533         cookieSize = security_server_get_cookie_size();
534
535         MSG_DEBUG("cookie size : [%d]", cookieSize);
536
537 //      char cookie[MAX_COOKIE_LEN];
538
539         // Get GID
540         if (CmdType == MSG_CMD_REG_INCOMING_SYNCML_MSG_CB)
541         {
542                 MSG_DEBUG("get GID for message_sync");
543                 gid = security_server_get_gid("message_sync");
544         }
545         else if (CmdType == MSG_CMD_REG_INCOMING_LBS_MSG_CB)
546         {
547                 MSG_DEBUG("get GID for message_lbs");
548                 gid = security_server_get_gid("message_lbs");
549         }
550         else
551         {
552                 MSG_DEBUG("get GID for message");
553                 gid = security_server_get_gid("message");
554         }
555
556         MSG_DEBUG("gid [%d]", gid);
557
558         int retVal = 0;
559
560         retVal = security_server_check_privilege(pCookie, gid);
561
562         if (retVal < 0)
563         {
564                 if (retVal == SECURITY_SERVER_API_ERROR_ACCESS_DENIED)
565                 {
566                         MSG_DEBUG("access denied !! [%d]", retVal);
567                 }
568                 else
569                 {
570                         MSG_DEBUG("fail to check privilege [%d]", retVal);
571                 }
572
573                 return false;
574         }
575
576         MSG_DEBUG("privilege check success !!");
577
578         return true;
579 }
580
581
582 void MsgTransactionManager::setSentStatusCB(int listenerFd)
583 {
584         if (listenerFd <= 0)
585                 THROW(MsgException::INVALID_PARAM,"InParam Error: listenerFd %d",listenerFd);
586
587         statusCBFdMap[listenerFd] = true;
588 }
589
590
591 void MsgTransactionManager::setIncomingMsgCB(MSG_CMD_REG_INCOMING_MSG_CB_S *pCbInfo)
592 {
593         if (!pCbInfo)
594         {
595                 MSG_FATAL("cbinfo NULL");
596                 return;
597         }
598
599         newmsg_list::iterator it = newMsgCBList.begin();
600
601         for (; it != newMsgCBList.end(); it++)
602         {
603                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (it->port == pCbInfo->port))
604                 {
605                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d, port %d", it->listenerFd, it->msgType, it->port);
606                         return;
607                 }
608         }
609
610         newMsgCBList.push_back(*pCbInfo);
611 }
612
613
614 void MsgTransactionManager::setMMSConfMsgCB(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S *pCbInfo)
615 {
616         if (!pCbInfo)
617         {
618                 MSG_FATAL("cbinfo NULL");
619                 return;
620         }
621
622         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
623
624         for (; it != newMMSConfMsgCBList.end(); it++)
625         {
626                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (!strncmp(it->appId, pCbInfo->appId, MAX_MMS_JAVA_APPID_LEN)))
627                 {
628                         MSG_DEBUG("Duplicated MMSConfMessageCB info fd:%d, mType:%d, appId:%s", it->listenerFd, it->msgType, it->appId);
629                         return;
630                 }
631         }
632
633         newMMSConfMsgCBList.push_back(*pCbInfo);
634 }
635
636
637 void MsgTransactionManager::setPushMsgCB(MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S *pCbInfo)
638 {
639         if (!pCbInfo)
640         {
641                 MSG_FATAL("cbinfo NULL");
642                 return;
643         }
644
645         pushmsg_list::iterator it = newPushMsgCBList.begin();
646
647         for (; it != newPushMsgCBList.end(); it++)
648         {
649                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
650                 {
651                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
652                         return;
653                 }
654         }
655
656         newPushMsgCBList.push_back(*pCbInfo);
657 }
658
659 void MsgTransactionManager::setCBMsgCB(MSG_CMD_REG_INCOMING_CB_MSG_CB_S *pCbInfo)
660 {
661         if (!pCbInfo)
662         {
663                 MSG_FATAL("cbinfo NULL");
664                 return;
665         }
666
667         cbmsg_list::iterator it = newCBMsgCBList.begin();
668
669         for (; it != newCBMsgCBList.end(); it++)
670         {
671                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
672                 {
673                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
674                         return;
675                 }
676         }
677
678         if(pCbInfo->bsave)
679                 if(MsgSettingSetBool(CB_SAVE, pCbInfo->bsave) != MSG_SUCCESS)
680                         MSG_DEBUG("MsgSettingSetBool FAIL: CB_SAVE");
681
682
683         newCBMsgCBList.push_back(*pCbInfo);
684 }
685
686 void MsgTransactionManager::setSyncMLMsgCB(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S *pCbInfo)
687 {
688         if (!pCbInfo)
689         {
690                 MSG_FATAL("cbinfo NULL");
691                 return;
692         }
693
694         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
695
696         for (; it != newSyncMLMsgCBList.end(); it++)
697         {
698                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
699                 {
700                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
701                         return;
702                 }
703         }
704
705         newSyncMLMsgCBList.push_back(*pCbInfo);
706 }
707
708
709 void MsgTransactionManager::setLBSMsgCB(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S *pCbInfo)
710 {
711         if (!pCbInfo)
712         {
713                 MSG_FATAL("cbinfo NULL");
714                 return;
715         }
716
717         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
718
719         for (; it != newLBSMsgCBList.end(); it++)
720         {
721                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
722                 {
723                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
724                         return;
725                 }
726         }
727
728         newLBSMsgCBList.push_back(*pCbInfo);
729 }
730
731
732 void MsgTransactionManager::setJavaMMSList(MSG_CMD_REG_INCOMING_JAVAMMS_TRID_S *pTrId)
733 {
734         if (!pTrId)
735         {
736                 MSG_FATAL("trId NULL");
737                 return;
738         }
739
740         javamms_list::iterator it;
741
742         for (it = javaMMSList.begin(); it != javaMMSList.end(); it++)
743         {
744                 if (!strcmp(it->id, pTrId->id))
745                 {
746                         MSG_DEBUG("Duplicated javaMMS transaction Id:%s", it->id);
747                         return;
748                 }
749         }
750
751         javaMMSList.push_back(*pTrId);
752 }
753
754
755 void MsgTransactionManager::setSyncMLMsgOperationCB(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S *pCbInfo)
756 {
757         if (!pCbInfo)
758         {
759                 MSG_FATAL("cbinfo NULL");
760                 return;
761         }
762
763         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
764
765         for (; it != operationSyncMLMsgCBList.end(); it++)
766         {
767                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
768                 {
769                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
770                         return;
771                 }
772         }
773
774         operationSyncMLMsgCBList.push_back(*pCbInfo);
775 }
776
777
778 void MsgTransactionManager::setStorageChangeCB(int listenerFd)
779 {
780         if (listenerFd <= 0)
781                 THROW(MsgException::INVALID_PARAM,"InParam Error: listenerFd %d", listenerFd);
782
783         storageChangeFdMap[listenerFd] = true;
784 }
785
786
787 javamms_list& MsgTransactionManager::getJavaMMSList()
788 {
789         return javaMMSList;
790 }
791
792
793 void MsgTransactionManager::broadcastIncomingMsgCB(const msg_error_t err, const MSG_MESSAGE_INFO_S *msgInfo)
794 {
795         MSG_BEGIN();
796
797         char* pEventData = NULL;
798         AutoPtr<char> eventBuf(&pEventData);
799
800         int eventSize = MsgMakeEvent(msgInfo, sizeof(MSG_MESSAGE_INFO_S), MSG_EVENT_PLG_INCOMING_MSG_IND, err, (void**)(&pEventData));
801
802         MSG_DEBUG("valid %d dstport %d", msgInfo->msgPort.valid, msgInfo->msgPort.dstPort);
803
804         newmsg_list::iterator it = newMsgCBList.begin();
805
806         for (; it != newMsgCBList.end(); it++)
807         {
808                 MSG_DEBUG("fd %d dstport %d",it->listenerFd, it->port);
809
810                 if ((msgInfo->msgPort.valid == false) && (it->port == 0))
811                 {
812                         MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
813                         write(it->listenerFd, pEventData, eventSize);
814                 }
815                 else if ((msgInfo->msgPort.valid == true) && (it->port == msgInfo->msgPort.dstPort))
816                 {
817                         MSG_DEBUG("Send incoming port msg to listener %d", it->listenerFd);
818                         write(it->listenerFd, pEventData, eventSize);
819                 }
820         }
821
822         MSG_END();
823 }
824
825
826 void MsgTransactionManager::broadcastMMSConfCB(const msg_error_t err, const MSG_MESSAGE_INFO_S *msgInfo, const MMS_RECV_DATA_S *mmsRecvData)
827 {
828         MSG_BEGIN();
829
830         char* pEventData = NULL;
831         AutoPtr<char> eventBuf(&pEventData);
832
833         int eventSize = MsgMakeEvent(msgInfo, sizeof(MSG_MESSAGE_INFO_S), MSG_EVENT_PLG_INCOMING_MMS_CONF, err, (void**)(&pEventData));
834
835         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
836
837         for (; it != newMMSConfMsgCBList.end(); it++)
838         {
839                 MSG_DEBUG("fd:%d appId:%s",it->listenerFd, it->appId);
840
841                 if (mmsRecvData->msgAppId.valid == true)
842                 {
843                         if (!strcmp(it->appId, mmsRecvData->msgAppId.appId))
844                         {
845                                 MSG_DEBUG("Send incoming java msg to listener %d", it->listenerFd);
846                                 write(it->listenerFd, pEventData, eventSize);
847                         }
848                 }
849                 else
850                 {
851                         if (strlen(it->appId) <= 0)
852                         {
853                                 MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
854                                 write(it->listenerFd, pEventData, eventSize);
855                         }
856                 }
857         }
858
859
860         MSG_END();
861 }
862
863 void MsgTransactionManager::broadcastPushMsgCB(const msg_error_t err, const MSG_PUSH_MESSAGE_DATA_S *pushData)
864 {
865         MSG_BEGIN();
866
867         char* pEventData = NULL;
868         AutoPtr<char> eventBuf(&pEventData);
869
870         int eventSize = MsgMakeEvent(pushData, sizeof(MSG_PUSH_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND, err, (void**)(&pEventData));
871
872         pushmsg_list::iterator it = newPushMsgCBList.begin();
873
874         for (; it != newPushMsgCBList.end(); it++)
875         {
876                 if (!strcmp(it->appId, pushData->pushAppId))
877                 {
878                         MSG_DEBUG("Send incoming Push information to listener %d", it->listenerFd);
879                         write(it->listenerFd, pEventData, eventSize);
880                 }
881         }
882
883         MSG_END();
884 }
885
886 void MsgTransactionManager::broadcastCBMsgCB(const msg_error_t err, const MSG_CB_MSG_S *cbMsg)
887 {
888         MSG_BEGIN();
889
890         char* pEventData = NULL;
891         AutoPtr<char> eventBuf(&pEventData);
892
893         int eventSize = MsgMakeEvent(cbMsg, sizeof(MSG_CB_MSG_S), MSG_EVENT_PLG_INCOMING_CB_MSG_IND, err, (void**)(&pEventData));
894
895         cbmsg_list::iterator it = newCBMsgCBList.begin();
896
897         for (; it != newCBMsgCBList.end(); it++)
898         {
899                 MSG_DEBUG("Send incoming CB information to listener %d", it->listenerFd);
900                 write(it->listenerFd, pEventData, eventSize);
901         }
902
903         MSG_END();
904 }
905
906 void MsgTransactionManager::broadcastSyncMLMsgCB(const msg_error_t err, const MSG_SYNCML_MESSAGE_DATA_S *syncMLData)
907 {
908         MSG_BEGIN();
909
910         char* pEventData = NULL;
911         AutoPtr<char> eventBuf(&pEventData);
912
913         int eventSize = MsgMakeEvent(syncMLData, sizeof(MSG_SYNCML_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND, err, (void**)(&pEventData));
914
915         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
916
917         for (; it != newSyncMLMsgCBList.end(); it++)
918         {
919                 MSG_DEBUG("Send incoming SyncML information to listener %d", it->listenerFd);
920                 write(it->listenerFd, pEventData, eventSize);
921         }
922
923         MSG_END();
924 }
925
926
927 void MsgTransactionManager::broadcastLBSMsgCB(const msg_error_t err, const MSG_LBS_MESSAGE_DATA_S *lbsData)
928 {
929         MSG_BEGIN();
930
931         char* pEventData = NULL;
932         AutoPtr<char> eventBuf(&pEventData);
933
934         int eventSize = MsgMakeEvent(lbsData, sizeof(MSG_LBS_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_LBS_MSG_IND, err, (void**)(&pEventData));
935
936         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
937
938         for (; it != newLBSMsgCBList.end(); it++)
939         {
940                 MSG_DEBUG("Send incoming LBS msg to listener %d", it->listenerFd);
941                 write(it->listenerFd, pEventData, eventSize);
942         }
943
944         MSG_END();
945 }
946
947
948 void MsgTransactionManager::broadcastSyncMLMsgOperationCB(const msg_error_t err, const int msgId, const int extId)
949 {
950         MSG_BEGIN();
951
952         char* pEventData = NULL;
953         AutoPtr<char> eventBuf(&pEventData);
954
955         char* encodedData = NULL;
956         AutoPtr<char> buf(&encodedData);
957
958         // Encoding Storage Change Data
959         int dataSize = MsgEncodeSyncMLOperationData(msgId, extId, &encodedData);
960
961         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_SYNCML_OPERATION, err, (void**)(&pEventData));
962
963         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
964
965         for( ; it != operationSyncMLMsgCBList.end() ; it++ )
966         {
967                 MSG_DEBUG("Send SyncML operation to listener %d", it->listenerFd);
968                 write(it->listenerFd, pEventData, eventSize);
969         }
970
971         MSG_END();
972 }
973
974
975 void MsgTransactionManager::broadcastStorageChangeCB(const msg_error_t err, const msg_storage_change_type_t storageChangeType, const msg_id_list_s *pMsgIdList)
976 {
977         MSG_BEGIN();
978
979         if(pMsgIdList == NULL) {
980                 MSG_DEBUG("pMsgIdList is NULL.");
981                 return;
982         }
983
984         MSG_DEBUG("storageChangeType [%d]", storageChangeType);
985
986         int dataSize = 0;
987
988         char* pEventData = NULL;
989         AutoPtr<char> eventBuf(&pEventData);
990
991         char* encodedData = NULL;
992         AutoPtr<char> buf(&encodedData);
993
994         // Encoding Storage Change Data
995         dataSize = MsgEncodeStorageChangeData(storageChangeType, pMsgIdList, &encodedData);
996
997         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_STORAGE_CHANGE_IND, err, (void**)(&pEventData));
998
999         fd_map::iterator it = storageChangeFdMap.begin();
1000
1001         for (; it != storageChangeFdMap.end(); it++)
1002         {
1003                 MSG_DEBUG("Send Storage Change Callback to listener %d", it->first);
1004                 write(it->first, pEventData, eventSize);
1005         }
1006
1007         MSG_END();
1008 }
1009
1010
1011 void MsgTransactionManager::setTMStatus()
1012 {
1013         MSG_BEGIN();
1014         mx.lock();
1015         cv.signal();
1016         mx.unlock();
1017         MSG_END();
1018 }
1019
1020
1021 void MsgTransactionManager::getTMStatus()
1022 {
1023         MSG_BEGIN();
1024         mx.lock();
1025
1026         int ret = 0;
1027
1028         ret = cv.timedwait(mx.pMutex(), 3);
1029
1030         mx.unlock();
1031
1032         if (ret == ETIMEDOUT)
1033         {
1034                 MSG_DEBUG("MsgTransactionManager::getTMStatus TIME-OUT");
1035         }
1036         MSG_END();
1037 }