4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/types.h>
25 #include <sys/statvfs.h>
29 #include <sys/prctl.h>
31 #include <system_info.h>
32 #include <device/power.h>
34 #ifndef _USE_DEVICED_DBUS
35 #include <usb-device.h>
38 #include "media-util.h"
39 #include "media-server-ipc.h"
40 #include "media-common-dbg.h"
41 #include "media-common-system.h"
42 #include "media-common-utils.h"
44 bool ms_config_get_int(const char *key, int *value)
49 MS_DBG_ERR("Arguments key or value is NULL");
53 err = vconf_get_int(key, value);
57 MS_DBG_ERR("Error code: %d", err);
62 bool ms_config_set_int(const char *key, int value)
67 MS_DBG_ERR("Arguments key is NULL");
71 err = vconf_set_int(key, value);
75 MS_DBG_ERR("Error code: %d", err);
80 bool ms_config_get_str(const char *key, char **value)
84 if (key == NULL || value == NULL) {
85 MS_DBG_ERR("Arguments key or value is NULL");
89 res = vconf_get_str(key);
90 if (MS_STRING_VALID(res)) {
91 *value = g_strdup(res);
99 int ms_get_remain_space(double *free_space)
101 int ret = MS_MEDIA_ERR_NONE;
102 const char *path = "/opt";
105 ret = statvfs(path, &s);
107 MS_DBG_ERR("statvfs failed[%d]", ret);
109 return MS_MEDIA_ERR_INTERNAL;
112 /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
113 *free_space = (double)(s.f_bsize * s.f_bavail);
115 return MS_MEDIA_ERR_NONE;
118 #ifdef _USE_TVPD_MODE
119 bool ms_is_support_pvr(void)
121 bool bSupportPVR = false;
122 if (system_info_get_custom_bool("com.samsung/featureconf/pvr.pvr_support", &bSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
123 MS_DBG_ERR("Get PVR Support failed");
127 MS_DBG("PVR Support : [%d]", bSupportPVR);
133 int ms_check_file_path(const char *file_path, uid_t uid)
135 ms_user_storage_type_e storage_type = -1;
136 int ret = MS_MEDIA_ERR_NONE;
138 if (!MS_STRING_VALID(file_path)) {
139 MS_DBG_ERR("Invalid path");
140 return MS_MEDIA_ERR_INVALID_PARAMETER;
143 ret = ms_user_get_storage_type(uid, file_path, &storage_type);
144 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
146 if (!g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
147 MS_DBG_SERR("g_file_test fail [%s]", file_path);
148 return MS_MEDIA_ERR_INVALID_PARAMETER;
151 return MS_MEDIA_ERR_NONE;
154 int ms_check_ignore_dir(const char *full_path, uid_t uid)
156 int ret = MS_MEDIA_ERR_NONE;
157 char *dir_path = NULL;
161 ret = ms_check_file_path(full_path, uid);
162 MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
164 if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && !strncmp(full_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB))) {
165 next_pos = strlen(MEDIA_ROOT_PATH_USB) + 1;
167 ret = ms_user_get_internal_root_path(uid, &dir_path);
168 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_internal_root_path() fail");
170 next_pos = strlen(dir_path);
175 while ((next = strstr(full_path + next_pos, "/"))) {
176 next_pos = (next - full_path);
177 dir_path = g_strndup(full_path, next_pos);
180 ret = ms_check_scan_ignore(dir_path, uid);
182 if (ret != MS_MEDIA_ERR_NONE)
189 int ms_check_scan_ignore(char * path, uid_t uid)
191 int ret = MS_MEDIA_ERR_NONE;
192 const char *ignore_file = ".scan_ignore";
193 char *tmp_path = NULL;
194 char *org_path = NULL;
195 char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
197 #ifndef _USE_TVPD_MODE
198 char replace[MS_FILE_PATH_LEN_MAX] = {0, };
199 char *mediashared = NULL;
202 /* Check for symbolic link */
203 tmp_path = realpath(path, NULL);
204 /* Get trimmed path */
205 org_path = g_canonicalize_filename(path, NULL);
207 #ifdef _USE_TVPD_MODE
208 if (g_strcmp0(tmp_path, org_path) != 0) {
209 MS_SAFE_FREE(tmp_path);
211 MS_DBG_ERR("symbolic link(directory)");
212 return MS_MEDIA_ERR_INVALID_PARAMETER;
215 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
216 ms_user_get_mediashared_path(uid, &mediashared);
217 snprintf(replace, MS_FILE_PATH_LEN_MAX, "%s%s", mediashared, tmp_path + strlen(MEDIA_SHARE_PATH));
218 MS_SAFE_FREE(mediashared);
219 if (g_strcmp0(replace, org_path) != 0) {
220 MS_SAFE_FREE(tmp_path);
222 MS_DBG_ERR("symbolic link(directory)");
223 return MS_MEDIA_ERR_INVALID_PARAMETER;
226 if (g_strcmp0(tmp_path, org_path) != 0) {
227 MS_SAFE_FREE(tmp_path);
229 MS_DBG_ERR("symbolic link(directory)");
230 return MS_MEDIA_ERR_INVALID_PARAMETER;
234 MS_SAFE_FREE(tmp_path);
237 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
238 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
240 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
241 MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
242 return MS_MEDIA_ERR_INVALID_PARAMETER;
245 MS_DBG_ERR("g_file_test fails[%s]", path);
246 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
248 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
249 MS_DBG_ERR("Fail to get USB path");
253 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
254 /*if the directory does not exist, check the device is unmounted*/
255 if (!ms_storage_mount_status(path)) {
256 MS_DBG_ERR("Device is unmounted[%s]", path);
257 return MS_MEDIA_ERR_USB_UNMOUNTED;
265 #ifndef _USE_DEVICED_DBUS
267 typedef struct storage_result {
272 static void __ms_check_mount_status(usb_device_h usb_device, void *user_data)
274 storage_result_s *data = (storage_result_s *)user_data;
275 char *mount_path = NULL;
277 mount_path = usb_device_get_mountpath(usb_device);
281 MS_DBG_SWARN("mount_path [%s]", mount_path);
282 data->result = (g_strcmp0(mount_path, data->storage_path) == 0);
286 bool ms_storage_mount_status(const char *start_path)
289 #ifndef _USE_DEVICED_DBUS
290 storage_result_s res = {0, };
291 char *remain_path = NULL;
294 remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_USB) + 1, "/");
295 if (remain_path != NULL)
296 remain_len = strlen(remain_path);
298 res.storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
300 MS_DBG_SWARN("storage_path [%s]", res.storage_path);
302 usb_mass_storage_foreach(__ms_check_mount_status, &res);
303 g_free(res.storage_path);
307 MS_DBG_SWARN("start path is mounted [%s]", start_path);
312 int ms_set_db_status(ms_db_status_type_t status, ms_user_storage_type_e storage_type)
314 int ret = MS_MEDIA_ERR_NONE;
316 if (status == MS_DB_UPDATING) {
317 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING))
320 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
321 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING))
325 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED))
328 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
329 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED))
334 ret = ms_set_power_mode(status);
335 if (ret != MS_MEDIA_ERR_NONE)
336 MS_DBG_ERR("ms_set_power_mode fail");
340 MS_DBG_ERR("ms_config_set_int failed");
341 return MS_MEDIA_ERR_INTERNAL;
344 int ms_set_power_mode(ms_db_status_type_t status)
346 int res = MS_MEDIA_ERR_NONE;
351 err = device_power_request_lock(POWER_LOCK_CPU, 0);
353 res = MS_MEDIA_ERR_INTERNAL;
356 err = device_power_release_lock(POWER_LOCK_CPU);
358 res = MS_MEDIA_ERR_INTERNAL;
361 MS_DBG_ERR("Unacceptable type : %d", status);
368 void ms_trim_dir_path(char *dir_path)
370 /* need implementation */
371 /* if dir_path is not NULL terminated, this function will occure crash */
372 int len = strlen(dir_path);
374 if (dir_path[len -1] == '/')
375 dir_path[len -1] = '\0';
378 int ms_check_size_mediadb(uid_t uid, double *db_size)
380 int ret = MS_MEDIA_ERR_NONE;
381 char *db_path = NULL;
384 ret = ms_user_get_media_db_path(uid, &db_path);
386 if (stat(db_path, &buf) == 0) {
387 *db_size = buf.st_size;
389 MS_DBG_STRERROR("stat failed");
390 ret = MS_MEDIA_ERR_INTERNAL;
398 #ifdef _USE_TVPD_MODE
399 #define PROC_OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"
400 #define VIP_OOM_SCORE_ADJ (-1000)
401 #define PROC_NAME_MAX 1024
402 #define PROC_BUF_MAX 64
404 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
406 char buf[PROC_BUF_MAX];
407 char cmdline_buf[PROC_NAME_MAX];
411 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
412 fp = fopen(buf, "r");
414 return MS_MEDIA_ERR_INTERNAL;
416 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
418 return MS_MEDIA_ERR_INTERNAL;
422 filename = strrchr(cmdline_buf, '/');
423 if (filename == NULL)
424 filename = cmdline_buf;
426 filename = filename + 1;
428 SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
430 return MS_MEDIA_ERR_NONE;
433 int ms_set_vip_process(void)
437 static pid_t pid = 0;
438 static char process_name[PROC_NAME_MAX] = {0};
439 static char *appid = NULL;
443 if (ms_get_cmdline_from_proc(pid, process_name)) {
444 MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
445 return MS_MEDIA_ERR_INTERNAL;
447 appid = process_name;
449 MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
451 if (prctl(PR_GET_DUMPABLE) == 0)
452 prctl(PR_SET_DUMPABLE, 1);
454 snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
455 id = open(buf, O_WRONLY, 0777);
457 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
458 return MS_MEDIA_ERR_INTERNAL;
460 snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
461 if (write(id, buf, strlen(buf)) < 0) {
462 MS_DBG_ERR("write() failed errno=%d", errno);
464 return MS_MEDIA_ERR_INTERNAL;
467 return MS_MEDIA_ERR_NONE;