Initialize Tizen 2.3
[framework/system/oma-dm-agent.git] / src / agent / main.c
1 /*
2  * oma-dm-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /*lib*/
19 #include <unistd.h>
20 #include <libxml/parser.h>
21 #include <signal.h>
22
23 #include <dbus/dbus-glib.h>
24 #include <dbus/dbus.h>
25 //#include <appcore-common.h>
26
27 #include <vconf.h>
28 #include <wait.h>
29
30 /*sync-agent*/
31 #include <sync_agent.h>
32
33 /*dm-agent*/
34 #include "common/dm_common.h"
35 #include "common/util/util.h"
36 #include "framework/task/oma_dm_task_request.h"
37 #include "ipc_agent.h"
38
39 #ifndef OMADM_AGENT_LOG
40 #undef LOG_TAG
41 #define LOG_TAG "OMA_DM"
42 #endif
43
44 #define MC_SERVICE_OBJECT_PATH  "/com/samsung/omadmagent"       /* Don't use special character */
45 #define MC_SERVICE_DBUS                 "com.samsung.omadmagent"
46 #define MC_SERVICE_INTERFACE    "com.samsung.omadmagent"
47
48 static DBusConnection *connection = NULL;
49
50 static void _unregistered_path(DBusConnection * connection, void *user_data)
51 {
52         /* connection was finalized */
53         _INNER_FUNC_ENTER;
54
55         _INNER_FUNC_EXIT;
56 }
57
58 static DBusHandlerResult _message_path(DBusConnection * connection, DBusMessage * message, void *user_data)
59 {
60         _INNER_FUNC_ENTER;
61         if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Echo")) {
62                 DBusMessage *reply;
63                 _DEBUG_INFO("Recived the HelloFromSelf message\n");
64
65                 reply = dbus_message_new_method_return(message);
66                 if (reply == NULL)
67                         _DEBUG_ERROR("no memory\n");
68
69                 if (!dbus_connection_send(connection, reply, NULL))
70                         _DEBUG_ERROR("no memory\n");
71         } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Hello_Agent")) {
72                 _DEBUG_INFO("Hello_Agent");
73         } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Goodbye_Agent")) {
74                 _DEBUG_INFO("Goodbye_Agent");
75                 FILE *file_exist = NULL;
76                 file_exist = fopen("/opt/data/fota/oma-dm/.oma-dm-agent-enabled", "r");
77                 if (file_exist != NULL) {
78                         _DEBUG_INFO("file oma-dm-agent-enabled exist!!");
79                         fclose(file_exist);
80                 } else {
81                         _INNER_FUNC_EXIT;
82                         exit(1);
83                 }
84         } else {
85                 _DEBUG_INFO("can't match message");
86         }
87
88         _INNER_FUNC_EXIT;
89         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
90 }
91
92 static DBusObjectPathVTable echo_vtable = {
93         _unregistered_path,
94         _message_path,
95         NULL,
96 };
97
98 static int _register_dbus_service(void)
99 {
100         _INNER_FUNC_ENTER;
101         int result;
102         DBusError error;
103         void *d;
104
105         dbus_error_init(&error);
106         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
107         if (connection == NULL) {
108                 _DEBUG_ERROR("*** Failed to open connection to activating message bus: %s\n", error.message);
109                 dbus_error_free(&error);
110                 return 1;
111         }
112
113         if (!dbus_connection_register_object_path(connection, MC_SERVICE_OBJECT_PATH, &echo_vtable, (void *)0xdeadbeef)) {
114                 _DEBUG_ERROR("no memory\n");
115                 goto failed;
116         }
117
118         if (!dbus_connection_get_object_path_data(connection, MC_SERVICE_OBJECT_PATH, &d)) {
119                 _DEBUG_ERROR("no memory\n");
120                 goto failed;
121         }
122
123         if (d != (void *)0xdeadbeef) {
124                 _DEBUG_ERROR("dbus_connection_get_object_path_data() doesn't seem to work right\n");
125                 goto failed;
126         }
127
128         result = dbus_bus_request_name(connection, MC_SERVICE_DBUS, 0, &error);
129         if (dbus_error_is_set(&error)) {
130                 _DEBUG_ERROR("Error %s\n", error.message);
131                 dbus_error_free(&error);
132                 dbus_connection_unref(connection);
133                 _INNER_FUNC_EXIT;
134                 exit(1);
135         }
136
137         _INNER_FUNC_EXIT;
138         return 0;
139
140  failed:
141         dbus_connection_unref(connection);
142         _INNER_FUNC_EXIT;
143         return 1;
144 }
145
146 static void _agent_daemon_signal_handler(int signo, siginfo_t * info, void *p_context)
147 {
148         _EXTERN_FUNC_ENTER;
149
150         switch (signo) {
151         case SIGTERM:
152                 _DEBUG_INFO("Got SIGTERM");
153
154                 /* stop gmain loop */
155                 sync_agent_stop_main_loop(0);
156
157                 break;
158
159         default:
160                 break;
161         }
162
163         _EXTERN_FUNC_EXIT;
164 }
165
166 static int _system_command(const char *command)
167 {
168         _EXTERN_FUNC_ENTER;
169
170         int pid = 0, status = 0;
171         char *const environ[] = { NULL };
172
173         retvm_if((command) == NULL, -1, "command is NULL!!");
174
175         pid = fork();
176         if (pid == -1) {
177                 _DEBUG_INFO("ERROR : fork failed to create a process\n");
178                 _EXTERN_FUNC_EXIT;
179                 return -1;
180
181         } else if (pid == 0) {
182                 char *argv[4];
183                 argv[0] = "sh";
184                 argv[1] = "-c";
185                 argv[2] = (char *)command;
186                 argv[3] = 0;
187
188                 execve("/bin/sh", argv, environ);
189                 abort();
190         } else {
191                 do {
192                         if (waitpid(pid, &status, 0) == -1) {
193                                 if (errno != EINTR) {
194                                         return -1;
195                                 }
196
197                         } else {
198                                 _EXTERN_FUNC_EXIT;
199                                 return status;
200                         }
201
202                 } while (1);
203         }
204         _EXTERN_FUNC_EXIT;
205         return -1;
206 }
207
208 static void _confversion_check()
209 {
210    _INNER_FUNC_ENTER;
211         char *value = NULL;
212         value = vconf_get_str("db/SyncML/oma-dm-service/DevManagement/ConfVersion");
213
214         if (value != NULL) {
215                 if (strcmp(value, "") == 0) {
216                         if (value != NULL)
217                                 free(value);
218                         goto failed;
219                 } else if(strncmp(value, "0.1", strlen("0.1")) == 0) {
220                    _DEBUG_INFO("ConfVersion is same value existed!");
221                 } else {
222                         goto failed;
223                 }
224         } else {
225                         goto failed;
226         }
227
228         _INNER_FUNC_EXIT;
229         return ;
230
231  failed:
232    _DEBUG_INFO("ConfVersion is not same value existed !!");
233    int ret = _system_command("/usr/share/oma-dm-cfg/csc-setting/change_csc_value.sh");
234    if (ret != -1) {
235                 _DEBUG_INFO("success");
236    } else {
237                 _DEBUG_INFO("fail");
238    }
239    _INNER_FUNC_EXIT;
240         return ;
241 }
242
243 int main(int argc, char *argv[])
244 {
245         _EXTERN_FUNC_ENTER;
246
247         if( argc == 2  ) {
248                 if(strncmp(argv[1], "booting", strlen("booting")) == 0) {
249                         FILE * pFile_fota_enabled = NULL;
250                         FILE * pFile_lawmo_enabled = NULL;
251                         pFile_fota_enabled = fopen("/opt/data/fota/oma-dm/.oma-dm-agent-enabled", "r");
252                         pFile_lawmo_enabled = fopen("/opt/data/fota/oma-dm/.lawmo_oma-dm-agent-enabled", "r");
253                         if(pFile_fota_enabled == NULL && pFile_lawmo_enabled == NULL) {
254                                 exit(0);
255                         }
256
257                         if(pFile_fota_enabled != NULL) {
258                                 fclose(pFile_fota_enabled);
259                                 pFile_fota_enabled = NULL;
260                         }
261                         if(pFile_lawmo_enabled != NULL) {
262                                 fclose(pFile_lawmo_enabled);
263                                 pFile_lawmo_enabled = NULL;
264                         }
265                 }
266         }
267
268
269
270
271
272 //      _DEBUG_INFO("[TIME] 1. main : %d msec\n", appcore_measure_time_from("APP_START_TIME"));
273 //      appcore_measure_start();
274
275         sync_agent_deinit_error_e deinit_error = SYNC_AGENT_DEINIT_SUCCESS;
276         sync_agent_init_error_e init_error = SYNC_AGENT_INIT_SUCCESS;
277         struct sigaction sig_act;
278
279         if (sync_agent_daemonize() < 0) {
280                 _DEBUG_ERROR("daemonize error");
281                 return -1;
282         }
283 //      _DEBUG_INFO("[TIME] 2. main : %d msec\n", appcore_measure_time());
284 //      appcore_measure_start();
285         xmlInitParser();
286    _confversion_check();
287         _register_dbus_service();
288
289         init_error = sync_agent_init("/usr/share/oma-dm-cfg/fw-init/omadm_fw_config.xml");
290         if (init_error != SYNC_AGENT_INIT_SUCCESS) {
291                 _DEBUG_ERROR("Failed to OMA DM sync_agent_init() : %d", init_error);
292                 return -1;
293         } else {
294                 _DEBUG_INFO("done init_Framework - OMA DM");
295         }
296
297         /*for network connection */
298         DM_ERROR ret = DM_OK;
299         int return_Ipc = 0;
300         //sleep(4);
301         //network_connection_delay();
302         return_Ipc = set_IPC();
303         _DEBUG_INFO("return ipc : %d", return_Ipc);
304
305         check_csc();
306
307         ret = init_dm();
308         _DEBUG_INFO("return init dm : %d", ret);
309 /*      if (ret != DM_OK) {
310                 goto error;
311         }*/
312
313         ret = dm_init_task_request();
314         if (ret != DM_OK) {
315                 goto error;
316         }
317
318         /* Block SIGPIPE signal (client may close socket abnormally) */
319         signal(SIGPIPE, SIG_IGN);
320
321         /* register signal handler. will be called by kill command */
322         sig_act.sa_handler = NULL;
323         sig_act.sa_sigaction = _agent_daemon_signal_handler;
324         sig_act.sa_flags = SA_SIGINFO;
325         sigemptyset(&sig_act.sa_mask);
326         sigaction(SIGTERM, &sig_act, NULL);
327
328         sync_agent_run_main_loop(0);
329
330         ret = end_dm();
331         if (ret != DM_OK) {
332                 _DEBUG_ERROR("end_dm() failed !!");
333                 goto error;
334         }
335
336         if (connection) {
337                 dbus_connection_unref(connection);
338                 connection = NULL;
339         }
340
341         deinit_error = sync_agent_deinit();
342         if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) {
343                 _DEBUG_ERROR("deinit fail : %d!!", deinit_error);
344         }
345
346         xmlCleanupParser();
347
348         _EXTERN_FUNC_EXIT;
349
350         return 1;
351
352  error:
353         _DEBUG_ERROR("end error : %d\n", ret);
354
355         if (connection) {
356                 dbus_connection_unref(connection);
357                 connection = NULL;
358         }
359
360         deinit_error = sync_agent_deinit();
361         if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) {
362                 _DEBUG_ERROR("deinit fail : %d!!", deinit_error);
363         }
364
365         xmlCleanupParser();
366
367         _EXTERN_FUNC_EXIT;
368
369         return 0;
370 }