de21bfbd13a4267d8e727bead92a3c7bf7e61901
[framework/base/rpm-installer.git] / backend / src / coretpk / coretpk-dbus.c
1 /*
2  * coretpk-dbus
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Vineet Mimrot <v.mimrot@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #ifdef _APPFW_FEATURE_MOUNT_INSTALL
23 #include <dbus/dbus.h>
24 #include <dbus/dbus-glib-lowlevel.h>
25 #include <unistd.h>
26 #include <stdio.h>
27
28 #include "coretpk-installer-internal.h"
29 #include "installer-type.h"
30 #include <stdlib.h>
31
32 int _coretpk_dbus_mount_file(char *mnt_path[], const char *pkgid)
33 {
34
35         _LOGD("_coretpk_dbus_mount_file called");
36         DBusMessage *msg;
37         int func_ret = 0;
38         int rv = 0;
39         struct stat link_buf = {0,};
40
41         DBusConnection *conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
42         if(!conn){
43                 _LOGE("DBUS Connection Error");
44                 return -1;
45         }
46
47         rv = lstat(mnt_path[0], &link_buf);
48         if (rv == 0) {
49                 rv = unlink(mnt_path[0]);
50                 if (rv)
51                         _LOGE("Unable to remove link file [%s]", mnt_path[0]);
52         }
53
54         msg = dbus_message_new_method_call(TZIP_BUS_NAME, TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_MOUNT_METHOD);
55         if(!msg) {
56                 _LOGE("dbus_message_new_method_call(%s:%s-%s)", TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_MOUNT_METHOD);
57                 return -1;
58         }
59
60         if (!dbus_message_append_args(msg,
61                                         DBUS_TYPE_STRING, &mnt_path[0],
62                                         DBUS_TYPE_STRING, &mnt_path[1],
63                                         DBUS_TYPE_STRING, &pkgid,
64                                         DBUS_TYPE_INVALID))
65         {
66                 _LOGE("Ran out of memory while constructing args\n");
67                 func_ret = -1;
68                 goto func_out;
69         }
70
71         if(dbus_connection_send(conn, msg, NULL) == FALSE) {
72                 _LOGE("dbus_connection_send error");
73                 func_ret = -1;
74                 goto func_out;
75         }
76 func_out :
77         dbus_message_unref(msg);
78         _LOGE("__tpk_mount finished");
79         return func_ret;
80 }
81
82
83 int _coretpk_dbus_unmount_file(char *mnt_path)
84 {
85         _LOGD("__tpk_unmount called [%s]", mnt_path);
86
87         DBusConnection *conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
88         if(!conn){
89                 _LOGE("DBUS Connection Error");
90                 return -1;
91         }
92
93         DBusMessage *msg = NULL;
94         msg = dbus_message_new_method_call(TZIP_BUS_NAME, TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_UNMOUNT_METHOD);
95         if(!msg) {
96                 _LOGE("dbus_message_new_method_call(%s:%s-%s)", TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_UNMOUNT_METHOD);
97                 return -1;
98         }
99
100         if (!dbus_message_append_args(msg,
101                                         DBUS_TYPE_STRING, &mnt_path,
102                                         DBUS_TYPE_INVALID))
103         {
104                 _LOGE("Ran out of memory while constructing args\n");
105                 dbus_message_unref(msg);
106                 return -1;
107         }
108
109         if(dbus_connection_send(conn, msg, NULL) == FALSE)
110         {
111                 _LOGE("dbus send error");
112                 dbus_message_unref(msg);
113                 return -1;
114         }
115         dbus_message_unref(msg);
116         _LOGE("__tpk_unmount finished");
117         return 0;
118 }
119
120
121 int _coretpk_dbus_is_mount_done(const char *mnt_path)
122 {
123         _LOGD("_coretpk_dbus_is_mount_done called [%s]", mnt_path);
124         DBusMessage *msg;
125         DBusMessage *reply;
126         DBusError err;
127         int ret = -1;
128         int r = -1;
129
130         DBusConnection *conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
131         if(!conn){
132                 _LOGE("DBUS Connection Error");
133                 return -1;
134         }
135
136         msg = dbus_message_new_method_call(TZIP_BUS_NAME, TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_IS_MOUNTED_METHOD);
137         if(!msg) {
138                 _LOGE("dbus_message_new_method_call(%s:%s-%s)", TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_IS_MOUNTED_METHOD);
139                 return ret;
140         }
141
142         if (!dbus_message_append_args(msg,
143                                         DBUS_TYPE_STRING, &mnt_path,
144                                         DBUS_TYPE_INVALID)) {
145                 _LOGE("Ran out of memory while constructing args\n");
146                 dbus_message_unref(msg);
147                 return ret;
148         }
149
150         dbus_error_init(&err);
151         reply = dbus_connection_send_with_reply_and_block(conn, msg, 500, &err);
152         if (!reply) {
153                 _LOGE("dbus_connection_send error(%s:%s)", err.name, err.message);
154                 goto func_out;
155         }
156
157         r = dbus_message_get_args(reply, &err, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
158         if (!r) {
159                 _LOGE("no message : [%s:%s]", err.name, err.message);
160                 goto func_out;
161         }
162
163 func_out :
164         dbus_message_unref(msg);
165         dbus_error_free(&err);
166         return ret;
167 }
168
169 int _coretpk_dbus_wait_for_tep_mount(const char *mnt_path)
170 {
171         _LOGD("_coretpk_dbus_wait_for_tep_mount called [%s]", mnt_path);
172         if(mnt_path) {
173                 int rv = -1;
174                 int cnt = 0;
175                 while(cnt < TZIP_MOUNT_MAXIMUM_RETRY_CNT) {
176                         rv = _coretpk_dbus_is_mount_done(mnt_path);
177                         _LOGE("cnt:%d",cnt);
178                         if(rv == 1)
179                                 break;
180                         sleep(1);
181                         cnt++;
182                 }
183                 /* incase after trying 15 sec, not getting mounted then quit */
184                 if( rv != 1) {
185                         _LOGE("Not able to mount within 15 sec");
186                         return -1;
187                 }
188         }
189         return 0;
190 }
191 #endif
192