Tizen 2.0 Release
[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 /*sync-agent*/
28 #include <sync_agent.h>
29
30 /*dm-agent*/
31 #include "common/dm_common.h"
32 #include "common/util/util.h"
33 #include "framework/task/oma_dm_task_request.h"
34 #include "ipc_agent.h"
35
36 #ifndef OMADM_AGENT_LOG
37 #undef LOG_TAG
38 #define LOG_TAG "OMA_DM"
39 #endif
40
41 #define MC_SERVICE_OBJECT_PATH  "/com/samsung/omadmagent"       /* Don't use special character */
42 #define MC_SERVICE_DBUS                 "com.samsung.omadmagent"
43 #define MC_SERVICE_INTERFACE    "com.samsung.omadmagent"
44
45 static DBusConnection *connection = NULL;
46
47 static void _unregistered_path(DBusConnection * connection, void *user_data)
48 {
49         /* connection was finalized */
50         _INNER_FUNC_ENTER;
51
52         _INNER_FUNC_EXIT;
53 }
54
55 static DBusHandlerResult _message_path(DBusConnection * connection, DBusMessage * message, void *user_data)
56 {
57         _INNER_FUNC_ENTER;
58         if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Echo")) {
59                 DBusMessage *reply;
60                 _DEBUG_INFO("Recived the HelloFromSelf message\n");
61
62                 reply = dbus_message_new_method_return(message);
63                 if (reply == NULL)
64                         _DEBUG_ERROR("no memory\n");
65
66                 if (!dbus_connection_send(connection, reply, NULL))
67                         _DEBUG_ERROR("no memory\n");
68         } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Hello_Agent")) {
69                 _DEBUG_INFO("Hello_Agent");
70         } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Goodbye_Agent")) {
71                 _DEBUG_INFO("Goodbye_Agent");
72                 FILE *file_exist = NULL;
73                 file_exist = fopen("/opt/data/fota/oma-dm/.oma-dm-agent-enabled", "r");
74                 if (file_exist != NULL) {
75                         _DEBUG_INFO("file oma-dm-agent-enabled exist!!");
76                         fclose(file_exist);
77                 } else {
78                         _INNER_FUNC_EXIT;
79                         exit(1);
80                 }
81         } else {
82                 _DEBUG_INFO("can't match message");
83         }
84
85         _INNER_FUNC_EXIT;
86         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
87 }
88
89 static DBusObjectPathVTable echo_vtable = {
90         _unregistered_path,
91         _message_path,
92         NULL,
93 };
94
95 static int _register_dbus_service(void)
96 {
97         _INNER_FUNC_ENTER;
98         int result;
99         DBusError error;
100         void *d;
101
102         dbus_error_init(&error);
103         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
104         if (connection == NULL) {
105                 _DEBUG_ERROR("*** Failed to open connection to activating message bus: %s\n", error.message);
106                 dbus_error_free(&error);
107                 return 1;
108         }
109
110         if (!dbus_connection_register_object_path(connection, MC_SERVICE_OBJECT_PATH, &echo_vtable, (void *)0xdeadbeef)) {
111                 _DEBUG_ERROR("no memory\n");
112                 goto failed;
113         }
114
115         if (!dbus_connection_get_object_path_data(connection, MC_SERVICE_OBJECT_PATH, &d)) {
116                 _DEBUG_ERROR("no memory\n");
117                 goto failed;
118         }
119
120         if (d != (void *)0xdeadbeef) {
121                 _DEBUG_ERROR("dbus_connection_get_object_path_data() doesn't seem to work right\n");
122                 goto failed;
123         }
124
125         result = dbus_bus_request_name(connection, MC_SERVICE_DBUS, 0, &error);
126         if (dbus_error_is_set(&error)) {
127                 _DEBUG_ERROR("Error %s\n", error.message);
128                 dbus_error_free(&error);
129                 dbus_connection_unref(connection);
130                 _INNER_FUNC_EXIT;
131                 exit(1);
132         }
133
134         _INNER_FUNC_EXIT;
135         return 0;
136
137  failed:
138         dbus_connection_unref(connection);
139         _INNER_FUNC_EXIT;
140         return 1;
141 }
142
143 static void _agent_daemon_signal_handler(int signo, siginfo_t * info, void *p_context)
144 {
145         _EXTERN_FUNC_ENTER;
146
147         switch (signo) {
148         case SIGTERM:
149                 _DEBUG_INFO("Got SIGTERM");
150
151                 /* stop gmain loop */
152                 sync_agent_stop_main_loop(0);
153
154                 break;
155
156         default:
157                 break;
158         }
159
160         _EXTERN_FUNC_EXIT;
161 }
162
163 int main()
164 {
165         _EXTERN_FUNC_ENTER;
166 //      _DEBUG_INFO("[TIME] 1. main : %d msec\n", appcore_measure_time_from("APP_START_TIME"));
167 //      appcore_measure_start();
168
169         sync_agent_deinit_error_e deinit_error = SYNC_AGENT_DEINIT_SUCCESS;
170         sync_agent_init_error_e init_error = SYNC_AGENT_INIT_SUCCESS;
171         struct sigaction sig_act;
172
173         if (sync_agent_daemonize() < 0) {
174                 _DEBUG_ERROR("daemonize error");
175                 return -1;
176         }
177 //      _DEBUG_INFO("[TIME] 2. main : %d msec\n", appcore_measure_time());
178 //      appcore_measure_start();
179         xmlInitParser();
180
181         _register_dbus_service();
182
183         init_error = sync_agent_init("/usr/share/oma-dm-cfg/fw-init/omadm_fw_config.xml");
184         if (init_error != SYNC_AGENT_INIT_SUCCESS) {
185                 _DEBUG_ERROR("Failed to OMA DM sync_agent_init() : %d", init_error);
186                 return -1;
187         } else {
188                 _DEBUG_INFO("done init_Framework - OMA DM");
189         }
190
191         /*for network connection */
192         DM_ERROR ret = DM_OK;
193         int return_Ipc = 0;
194         //sleep(4);
195         //network_connection_delay();
196         return_Ipc = set_IPC();
197         _DEBUG_INFO("return ipc : %d", return_Ipc);
198
199         check_csc();
200
201         ret = init_dm();
202         _DEBUG_INFO("return init dm : %d", ret);
203 /*      if (ret != DM_OK) {
204                 goto error;
205         }*/
206
207         ret = dm_init_task_request();
208         if (ret != DM_OK) {
209                 goto error;
210         }
211
212         /* Block SIGPIPE signal (client may close socket abnormally) */
213         signal(SIGPIPE, SIG_IGN);
214
215         /* register signal handler. will be called by kill command */
216         sig_act.sa_handler = NULL;
217         sig_act.sa_sigaction = _agent_daemon_signal_handler;
218         sig_act.sa_flags = SA_SIGINFO;
219         sigemptyset(&sig_act.sa_mask);
220         sigaction(SIGTERM, &sig_act, NULL);
221
222         sync_agent_run_main_loop(0);
223
224         ret = end_dm();
225         if (ret != DM_OK) {
226                 _DEBUG_ERROR("end_dm() failed !!");
227                 goto error;
228         }
229
230         if (connection) {
231                 dbus_connection_unref(connection);
232                 connection = NULL;
233         }
234
235         deinit_error = sync_agent_deinit();
236         if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) {
237                 _DEBUG_ERROR("deinit fail : %d!!", deinit_error);
238         }
239
240         xmlCleanupParser();
241
242         _EXTERN_FUNC_EXIT;
243
244         return 1;
245
246  error:
247         _DEBUG_ERROR("end error : %d\n", ret);
248
249         if (connection) {
250                 dbus_connection_unref(connection);
251                 connection = NULL;
252         }
253
254         deinit_error = sync_agent_deinit();
255         if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) {
256                 _DEBUG_ERROR("deinit fail : %d!!", deinit_error);
257         }
258
259         xmlCleanupParser();
260
261         _EXTERN_FUNC_EXIT;
262
263         return 0;
264 }