90b183359dd48feadb56e3424e2305bd33a1b8a1
[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         DBusError err;
41
42         DBusConnection *conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
43         if(!conn){
44                 _LOGE("DBUS Connection Error");
45                 return -1;
46         }
47
48         rv = lstat(mnt_path[0], &link_buf);
49         if (rv == 0) {
50                 rv = unlink(mnt_path[0]);
51                 if (rv)
52                         _LOGE("Unable to remove link file [%s]", mnt_path[0]);
53         }
54
55         msg = dbus_message_new_method_call(TZIP_BUS_NAME, TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_MOUNT_METHOD);
56         if(!msg) {
57                 _LOGE("dbus_message_new_method_call(%s:%s-%s)", TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_MOUNT_METHOD);
58                 return -1;
59         }
60
61         if (!dbus_message_append_args(msg,
62                                         DBUS_TYPE_STRING, &mnt_path[0],
63                                         DBUS_TYPE_STRING, &mnt_path[1],
64                                         DBUS_TYPE_STRING, &pkgid,
65                                         DBUS_TYPE_INVALID))
66         {
67                 _LOGE("Ran out of memory while constructing args\n");
68                 func_ret = -1;
69                 goto func_out;
70         }
71
72         if(dbus_connection_send(conn, msg, NULL) == FALSE) {
73                 _LOGE("dbus_connection_send error");
74                 func_ret = -1;
75                 goto func_out;
76         }
77 func_out :
78         dbus_message_unref(msg);
79         _LOGE("__tpk_mount finished");
80         return func_ret;
81 }
82
83
84 int _coretpk_dbus_unmount_file(char *mnt_path)
85 {
86         DBusError err;
87         _LOGD("__tpk_unmount called [%s]", mnt_path);
88
89         DBusConnection *conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
90         if(!conn){
91                 _LOGE("DBUS Connection Error");
92                 return -1;
93         }
94
95         DBusMessage *msg = NULL;
96         msg = dbus_message_new_method_call(TZIP_BUS_NAME, TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_UNMOUNT_METHOD);
97         if(!msg) {
98                 _LOGE("dbus_message_new_method_call(%s:%s-%s)", TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_UNMOUNT_METHOD);
99                 return -1;
100         }
101
102         if (!dbus_message_append_args(msg,
103                                         DBUS_TYPE_STRING, &mnt_path,
104                                         DBUS_TYPE_INVALID))
105         {
106                 _LOGE("Ran out of memory while constructing args\n");
107                 dbus_message_unref(msg);
108                 return -1;
109         }
110
111         if(dbus_connection_send(conn, msg, NULL) == FALSE)
112         {
113                 _LOGE("dbus send error");
114                 dbus_message_unref(msg);
115                 return -1;
116         }
117         dbus_message_unref(msg);
118         _LOGE("__tpk_unmount finished");
119         return 0;
120 }
121
122
123 int _coretpk_dbus_is_mount_done(const char *mnt_path)
124 {
125         _LOGD("_coretpk_dbus_is_mount_done called [%s]", mnt_path);
126         DBusMessage *msg;
127         DBusMessage *reply;
128         DBusError err;
129         int ret = -1;
130         int r = -1;
131
132         DBusConnection *conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
133         if(!conn){
134                 _LOGE("DBUS Connection Error");
135                 return -1;
136         }
137
138         msg = dbus_message_new_method_call(TZIP_BUS_NAME, TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_IS_MOUNTED_METHOD);
139         if(!msg) {
140                 _LOGE("dbus_message_new_method_call(%s:%s-%s)", TZIP_OBJECT_PATH, TZIP_INTERFACE_NAME, TZIP_IS_MOUNTED_METHOD);
141                 return ret;
142         }
143
144         if (!dbus_message_append_args(msg,
145                                         DBUS_TYPE_STRING, &mnt_path,
146                                         DBUS_TYPE_INVALID)) {
147                 _LOGE("Ran out of memory while constructing args\n");
148                 dbus_message_unref(msg);
149                 return ret;
150         }
151
152         dbus_error_init(&err);
153         reply = dbus_connection_send_with_reply_and_block(conn, msg, 500, &err);
154         if (!reply) {
155                 _LOGE("dbus_connection_send error(%s:%s)", err.name, err.message);
156                 goto func_out;
157         }
158
159         r = dbus_message_get_args(reply, &err, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
160         if (!r) {
161                 _LOGE("no message : [%s:%s]", err.name, err.message);
162                 goto func_out;
163         }
164
165 func_out :
166         dbus_message_unref(msg);
167         dbus_error_free(&err);
168         return ret;
169 }
170
171 int _coretpk_dbus_wait_for_tep_mount(const char *mnt_path)
172 {
173         _LOGD("_coretpk_dbus_wait_for_tep_mount called [%s]", mnt_path);
174         if(mnt_path) {
175                 int rv = -1;
176                 int cnt = 0;
177                 while(cnt < TZIP_MOUNT_MAXIMUM_RETRY_CNT) {
178                         rv = _coretpk_dbus_is_mount_done(mnt_path);
179                         _LOGE("cnt:%d",cnt);
180                         if(rv == 1)
181                                 break;
182                         sleep(1);
183                         cnt++;
184                 }
185                 /* incase after trying 15 sec, not getting mounted then quit */
186                 if( rv != 1) {
187                         _LOGE("Not able to mount within 15 sec");
188                         return -1;
189                 }
190         }
191         return 0;
192 }
193 #endif
194