2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <bundle_internal.h>
21 #include <pkgmgr-info.h>
24 #include <sys/socket.h>
29 #include <openssl/md5.h>
32 #include <glib-unix.h>
35 #include "message_port_log.h"
36 #include "message_port.h"
37 #include "message_port_common.h"
39 #define MAX_PACKAGE_STR_SIZE 512
40 #define MAX_RETRY_CNT 10
42 static const int MAX_MESSAGE_SIZE = 16 * 1024;
44 int write_socket(int fd,
47 unsigned int *bytes_write,
50 #define SEND_TIMEOUT 500 /* milliseconds */
52 unsigned int left = nbytes;
62 fds[0].events = POLLOUT;
65 ret = poll(fds, 1, SEND_TIMEOUT);
67 LOGE("write_socket: : fd %d poll timeout", fd);
68 return MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
71 while (left && (retry_cnt < MAX_RETRY_CNT)) {
72 nb = write(fd, buffer, left);
75 LOGE("write_socket: EINTR error continue ...");
79 LOGE("write_socket: ...error fd %d: errno %d\n", fd, errno);
81 if (errno == EWOULDBLOCK || errno == EAGAIN)
82 return MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
84 return MESSAGE_PORT_ERROR_IO_ERROR;
92 return MESSAGE_PORT_ERROR_NONE;
95 int write_string_to_socket(int fd,
98 unsigned int *bytes_write,
103 ret = write_socket(fd, (char *)&string_len, sizeof(string_len),
104 bytes_write, sequence);
105 if (ret != MESSAGE_PORT_ERROR_NONE) {
106 _LOGE("write string_len fail");
110 if (string_len > 0) {
111 ret = write_socket(fd, buffer, string_len, bytes_write, sequence);
112 if (ret != MESSAGE_PORT_ERROR_NONE) {
113 _LOGE("wirte buffer fail");
120 return MESSAGE_PORT_ERROR_NONE;
123 int read_socket(int fd,
126 unsigned int *bytes_read)
128 unsigned int left = nbytes;
131 const struct timespec TRY_SLEEP_TIME = { 0, 500 * 1000 * 1000 };
134 while (left && (retry_cnt < MAX_RETRY_CNT)) {
135 nb = read(fd, buffer, left);
137 LOGE("read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
138 return MESSAGE_PORT_ERROR_IO_ERROR;
139 } else if (nb == -1) {
140 /* wrt(nodejs) could change socket to none-blocking socket :-( */
141 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
142 LOGE("read_socket: %d errno, sleep and retry ...", errno);
144 nanosleep(&TRY_SLEEP_TIME, 0);
147 LOGE("read_socket: ...error fd %d: errno %d\n", fd, errno);
148 return MESSAGE_PORT_ERROR_IO_ERROR;
156 return MESSAGE_PORT_ERROR_NONE;
159 int read_string_from_socket(int fd, char **buffer, int *string_len)
162 if (read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGE_PORT_ERROR_NONE) {
163 LOGE("read socket fail");
164 return MESSAGE_PORT_ERROR_IO_ERROR;
166 if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
167 *buffer = (char *)calloc(*string_len, sizeof(char));
168 if (*buffer == NULL) {
169 LOGE("Out of memory.");
170 return MESSAGE_PORT_ERROR_IO_ERROR;
172 if (read_socket(fd, *buffer, *string_len, &nb) != MESSAGE_PORT_ERROR_NONE) {
173 LOGE("read socket fail");
174 return MESSAGE_PORT_ERROR_IO_ERROR;
177 LOGE("Invalid string len %d", *string_len);
178 return MESSAGE_PORT_ERROR_IO_ERROR;
180 return MESSAGE_PORT_ERROR_NONE;
183 static int __dbus_init(void)
186 GError *error = NULL;
188 gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
189 if (gdbus_conn == NULL) {
191 _LOGE("Failed to get dbus [%s]", error->message);
201 g_object_unref(gdbus_conn);
207 bool initialize_common(void)
210 #if !GLIB_CHECK_VERSION(2, 35, 0)
216 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
218 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
219 retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
221 app_id = strdup(buffer);
222 retvm_if(!app_id, false, "Malloc failed");
223 _LOGI("init : %s", app_id);
227 initialized_common = true;
232 bool is_preloaded(const char *local_appid, const char *remote_appid)
234 _LOGD("IsPreloaded");
236 bool preload_local = false;
237 bool preload_remote = false;
239 pkgmgrinfo_appinfo_h handle = NULL;
240 int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
241 if (ret != PMINFO_R_OK) {
242 _LOGE("Failed to get the appinfo. %d", ret);
243 pkgmgrinfo_appinfo_destroy_appinfo(handle);
246 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
247 if (ret != PMINFO_R_OK) {
248 _LOGE("Failed to check the preloaded application. %d", ret);
249 pkgmgrinfo_appinfo_destroy_appinfo(handle);
252 pkgmgrinfo_appinfo_destroy_appinfo(handle);
254 ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
255 if (ret != PMINFO_R_OK) {
256 _LOGE("Failed to get the appinfo. %d", ret);
257 pkgmgrinfo_appinfo_destroy_appinfo(handle);
260 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
261 if (ret != PMINFO_R_OK) {
262 _LOGE("Failed to check the preloaded application. %d", ret);
263 pkgmgrinfo_appinfo_destroy_appinfo(handle);
267 if (preload_local && preload_remote) {
268 pkgmgrinfo_appinfo_destroy_appinfo(handle);
271 pkgmgrinfo_appinfo_destroy_appinfo(handle);
275 int check_certificate(const char *local_appid, const char *remote_appid)
277 _LOGD("CheckCertificate");
279 pkgmgrinfo_cert_compare_result_type_e res;
280 int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
282 _LOGE(":CheckCertificate() Failed");
283 return MESSAGE_PORT_ERROR_IO_ERROR;
285 if (res != PMINFO_CERT_COMPARE_MATCH) {
286 _LOGE("CheckCertificate() Failed : MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH");
287 return MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH;
290 return MESSAGE_PORT_ERROR_NONE;
293 char *get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
296 int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
298 char *postfix = is_trusted ? "1" : "0";
300 unsigned char c[MD5_DIGEST_LENGTH] = {0};
301 char *md5_interface = NULL;
305 int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
306 int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
307 char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
308 if (bus_name == NULL) {
309 _LOGE("bus_name calloc failed");
313 snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
315 MD5_Init(&mdContext);
316 MD5_Update(&mdContext, bus_name, bus_name_len);
317 MD5_Final(c, &mdContext);
319 md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
320 if (md5_interface == NULL) {
324 _LOGE("md5_interface calloc failed!!");
328 snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
329 temp = md5_interface;
332 for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
333 snprintf(temp, 3, "%02x", c[index]);
337 if (postfix && postfix_len > 0)
338 snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
342 _LOGD("encoded_bus_name : %s ", md5_interface);
344 return md5_interface;