Sync with tizen 2.4
[platform/core/messaging/msg-service.git] / framework / main.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 /*==================================================================================================
18                                          INCLUDE FILES
19 ==================================================================================================*/
20 #include "MsgDebug.h"
21 #include "MsgException.h"
22 #include "MsgContact.h"
23 #include "MsgMemory.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgPluginManager.h"
26 #include "MsgSettingHandler.h"
27 #include "MsgStorageHandler.h"
28 #include "MsgSubmitHandler.h"
29 #include "MsgDeliverHandler.h"
30 #include "MsgTransManager.h"
31 #include "MsgStorageTypes.h"
32 #include "MsgSoundPlayer.h"
33 #include "MsgCmdHandler.h"
34 #include "MsgUtilFile.h"
35 #include "MsgUtilStorage.h"
36 #include "MsgNotificationWrapper.h"
37 #include "MsgZoneManager.h"
38
39 #include <errno.h>
40 #include <glib.h>
41 #include <dbus/dbus-glib.h>
42 #include <sys/stat.h>
43 #include <wait.h>
44
45 static GMainLoop* mainloop = NULL;
46
47 /*==================================================================================================
48                                      FUNCTION IMPLEMENTATION
49 ==================================================================================================*/
50
51 msg_error_t InitMmsDir()
52 {
53         if (mkdir(MSG_DATA_ROOT_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
54         {
55                 if (errno == EEXIST)
56                 {
57                         MSG_DEBUG("The %s already exists", MSG_DATA_ROOT_PATH);
58                 }
59                 else
60                 {
61                         MSG_DEBUG("Error while mkdir %s", MSG_DATA_ROOT_PATH);
62                         return MSG_ERR_DB_MAKE_DIR;
63                 }
64         }
65
66         if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
67         {
68                 if (errno == EEXIST)
69                 {
70                         MSG_SEC_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
71                 }
72                 else
73                 {
74                         MSG_SEC_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
75                         return MSG_ERR_DB_MAKE_DIR;
76                 }
77         }
78
79         if (mkdir(MSG_DATA_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
80         {
81                 if (errno == EEXIST)
82                 {
83                         MSG_DEBUG("The %s already exists", MSG_DATA_PATH);
84                 }
85                 else
86                 {
87                         MSG_DEBUG("Error while mkdir %s", MSG_DATA_PATH);
88                         return MSG_ERR_DB_MAKE_DIR;
89                 }
90         }
91
92         if (mkdir(MSG_THUMBNAIL_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
93                 if (errno == EEXIST) {
94                         MSG_DEBUG("The %s already exists.", MSG_THUMBNAIL_PATH);
95                 } else {
96                         MSG_DEBUG(" Error while mkdir %s", MSG_THUMBNAIL_PATH);
97                         return MSG_ERR_DB_MAKE_DIR;
98                 }
99         }
100
101         if (mkdir(MSG_IPC_DATA_PATH, S_IRWXU ) < 0)
102         {
103                 if (errno == EEXIST)
104                 {
105                         MSG_DEBUG("The %s already exists", MSG_IPC_DATA_PATH);
106                         // if IPC data path is already exist, clear all files in folder.
107                         MsgRmRf((char *)MSG_IPC_DATA_PATH);
108                 }
109                 else
110                 {
111                         MSG_DEBUG("Error while mkdir %s", MSG_IPC_DATA_PATH);
112                         return MSG_ERR_DB_MAKE_DIR;
113                 }
114         }
115
116         if (MsgChmod( MSG_IPC_DATA_PATH, S_IRWXU | S_IRWXG) == 0) { //public shared file: pass data by file
117                 MSG_DEBUG("Fail to chmod [%s].", MSG_IPC_DATA_PATH);
118         }
119         if (MsgChown(MSG_DATA_ROOT_PATH, 200, 5000) == 0) {
120                 MSG_DEBUG("Fail to chown [%s].", MSG_DATA_ROOT_PATH);
121         }
122         if (MsgChown(MSG_DATA_PATH, 200, 5000) == 0) {
123                 MSG_DEBUG("Fail to chown [%s].", MSG_DATA_PATH);
124         }
125         if (MsgChown(MSG_SMIL_FILE_PATH, 200, 5000) == 0) {
126                 MSG_DEBUG("Fail to chown [%s].", MSG_SMIL_FILE_PATH);
127         }
128         if (MsgChown(MSG_IPC_DATA_PATH, 200, 5000) == 0) {
129                 MSG_DEBUG("Fail to chown [%s].", MSG_IPC_DATA_PATH);
130         }
131         if (MsgChown(MSG_THUMBNAIL_PATH, 200, 5000) == 0) {
132                 MSG_DEBUG("Fail to chown [%s].", MSG_THUMBNAIL_PATH);
133         }
134
135         return MSG_SUCCESS;
136 }
137
138
139 void* InitMsgServer(void*)
140 {
141         msg_error_t err = MSG_SUCCESS;
142         MSG_DEBUG("Start InitMsgServer.");
143
144         //CID 356902: Moving try block up to include MsgStoInitDB which also throws MsgException
145         try
146         {
147                 // storage handler initialize
148                 err = MsgStoInitDB(false);
149                 if (err != MSG_SUCCESS) {
150                         MSG_ERR("FAIL TO INITIALIZE STORAGE HANDLER [%d]", err);
151                 }
152
153                 MsgInitNoti();
154
155                 // plugin manager initialize
156                 MsgPluginManager::instance()->initialize();
157         }
158         catch (MsgException& e)
159         {
160                 MSG_FATAL("%s", e.what());
161         }
162         catch (exception& e)
163         {
164                 MSG_FATAL("%s", e.what());
165         }
166
167
168 //      MsgSoundPlayer::instance()->MsgSoundInitRepeatAlarm();
169
170         MsgStoDisconnectDB();
171
172         MsgReleaseMemory();
173         MSG_DEBUG("End InitMsgServer.");
174
175         return (void*)0;
176 }
177
178
179 void* StartMsgServer(void*)
180 {
181         try
182         {
183                 MsgTransactionManager::instance()->run();
184         }
185         catch (MsgException& e)
186         {
187                 MSG_FATAL("%s", e.what());
188         }
189         catch (exception& e)
190         {
191                 MSG_FATAL("%s", e.what());
192         }
193
194         if (g_main_loop_is_running(mainloop))
195                 g_main_loop_quit(mainloop);
196
197         return (void*)0;
198 }
199
200
201 int main(void)
202 {
203 #if !GLIB_CHECK_VERSION(2, 31, 0)
204         g_thread_init(NULL);
205 #endif
206         /* set to ignore child process terminated signal */
207         signal(SIGCHLD, SIG_IGN);
208
209         MSG_INFO("===========START MESSAGING FRAMEWORK==========");
210
211 #if !GLIB_CHECK_VERSION(2, 36, 0)
212         g_type_init();
213 #endif
214         // Reset message server ready flag
215         if(MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, false) != MSG_SUCCESS)
216                 MSG_DEBUG("MsgSettingSetBool FAIL: VCONFKEY_MSG_SERVER_READY");
217
218         // Connect to DB
219         //      MsgStoConnectDB();
220
221         // Clean up mms dir
222         InitMmsDir();
223
224         // init server
225         InitMsgServer(NULL);
226
227         pthread_t startThreadId;
228
229         // start transaction manager
230         if (pthread_create(&startThreadId, NULL, StartMsgServer, NULL) != 0)
231         {
232                 MSG_DEBUG("StartMsgServer not invoked: %s", strerror(errno));
233                 return -1;
234         }
235
236         // Regist vconf CB.
237         MsgSettingRegVconfCB();
238
239         mainloop = g_main_loop_new(NULL, FALSE);
240
241         if (mainloop != NULL)
242         {
243                 MSG_DEBUG("Start Messaging Framework!!!");
244
245                 // Run GMainLoop
246                 g_main_loop_run(mainloop);
247         }
248         else
249         {
250                 MSG_DEBUG("Fail to start Messaging Framework!!!");
251         }
252
253         // Remove vconf CB
254         MsgSettingRemoveVconfCB();
255         //contacts-service is not used for gear
256 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
257         // Close Contact Sevice
258         MsgCloseContactSvc();
259 #endif //MSG_CONTACTS_SERVICE_NOT_SUPPORTED
260
261         // Disconnect to DB
262         MsgStoDisconnectDB();
263
264         return 0;
265 }
266