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.
23 #include <sys/types.h>
30 #include <dbus/dbus-glib.h>
31 #include <dbus/dbus.h>
32 #include <dbus/dbus-glib-lowlevel.h>
33 #include <sys/statvfs.h>
35 #ifdef _SET_VIP_PROCESS
36 #include <sys/prctl.h>
38 #include <system_info.h>
39 #include <dd-display.h>
41 #ifndef _USE_DEVICED_DBUS
42 #include <usb-device.h>
45 #include "media-util.h"
46 #include "media-server-ipc.h"
47 #include "media-common-dbg.h"
48 #include "media-common-system.h"
49 #include "media-common-utils.h"
51 #define MS_DRM_CONTENT_TYPE_LENGTH 100
53 /* it's for 32bit file offset */
55 unsigned long int f_bsize;
56 unsigned long int f_frsize;
57 unsigned long int f_blocks;
58 unsigned long int f_bfree;
59 unsigned long int f_bavail;
60 unsigned long int f_files;
61 unsigned long int f_ffree;
62 unsigned long int f_favail;
63 unsigned long int f_fsid;
64 #ifdef _STATVFSBUF_F_UNUSED
67 unsigned long int f_flag;
68 unsigned long int f_namemax;
72 bool ms_is_mmc_inserted(void)
75 ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
76 GArray *dev_list = NULL;
78 ret = ms_sys_get_device_list(stg_type, &dev_list);
79 if (ret == MS_MEDIA_ERR_NONE) {
80 if (dev_list != NULL) {
81 MS_DBG_ERR("MMC FOUND[%d]", dev_list->len);
82 ms_sys_release_device_list(&dev_list);
85 MS_DBG_ERR("MMC NOT FOUND");
88 MS_DBG_ERR("ms_sys_get_device_list failed");
94 static char* __media_get_path(uid_t uid)
96 char *result_passwd = NULL;
97 struct group *grpinfo = NULL;
99 if (uid == getuid()) {
100 grpinfo = getgrnam("users");
101 if (grpinfo == NULL) {
102 MS_DBG_ERR("getgrnam(users) returns NULL !");
105 if (MS_STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
106 result_passwd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
108 struct passwd *userinfo = getpwuid(uid);
109 if (userinfo == NULL) {
110 MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
113 grpinfo = getgrnam("users");
114 if (grpinfo == NULL) {
115 MS_DBG_ERR("getgrnam(users) returns NULL !");
118 // Compare git_t type and not group name
119 if (grpinfo->gr_gid != userinfo->pw_gid) {
120 MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
123 result_passwd = strndup(userinfo->pw_dir, strlen(userinfo->pw_dir));
126 return result_passwd;
129 ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
132 int ret = MS_MEDIA_ERR_NONE;
133 char * user_path = NULL;
136 return MS_MEDIA_ERR_INVALID_PATH;
138 #ifdef _USE_SENIOR_MODE
139 if(ms_is_support_senior_mode()) {
140 if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0) {
141 return MS_STORAGE_EXTERNAL;
146 user_path = __media_get_path(uid);
147 if (user_path == NULL)
148 return MS_MEDIA_ERR_OUT_OF_MEMORY;
150 length_path = strlen(user_path);
152 if (strncmp(path, user_path, length_path) == 0) {
153 ret = MS_STORAGE_INTERNAL;
154 } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
155 ret = MS_STORAGE_EXTERNAL;
156 } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
157 ret = MS_STORAGE_EXTERNAL_USB;
159 MS_DBG_ERR("[%s][%s][%s]", MEDIA_ROOT_PATH_SDCARD, MEDIA_ROOT_PATH_USB, path);
160 ret = MS_MEDIA_ERR_INVALID_PATH;
163 MS_SAFE_FREE(user_path);
168 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
171 int real_size = size - 1;
173 if (!res || !pattern || !str1 || !str2)
174 return MS_MEDIA_ERR_INVALID_PARAMETER;
176 if (real_size < (int)(strlen(str1) + strlen(str2)))
177 return MS_MEDIA_ERR_INVALID_PARAMETER;
179 len = snprintf(res, real_size, pattern, str1, str2);
181 return MS_MEDIA_ERR_INVALID_PARAMETER;
186 return MS_MEDIA_ERR_NONE;
189 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
192 int real_size = size;
194 if (!res || !pattern || !str1) {
195 MS_DBG_ERR("parameta is invalid");
196 return MS_MEDIA_ERR_INVALID_PARAMETER;
199 if (real_size < (int)(strlen(str1))) {
200 MS_DBG_ERR("size is wrong");
201 return MS_MEDIA_ERR_INVALID_PARAMETER;
204 len = snprintf(res, real_size, pattern, str1);
206 MS_DBG_ERR("snprintf failed");
207 return MS_MEDIA_ERR_INVALID_PARAMETER;
212 return MS_MEDIA_ERR_NONE;
215 bool ms_config_get_int(const char *key, int *value)
219 if (!key || !value) {
220 MS_DBG_ERR("Arguments key or value is NULL");
224 err = vconf_get_int(key, value);
230 MS_DBG_ERR("Unexpected error code: %d", err);
235 bool ms_config_set_int(const char *key, int value)
240 MS_DBG_ERR("Arguments key is NULL");
244 err = vconf_set_int(key, value);
250 MS_DBG_ERR("Unexpected error code: %d", err);
255 bool ms_config_get_str(const char *key, char **value)
259 if (key == NULL || value == NULL) {
260 MS_DBG_ERR("Arguments key or value is NULL");
264 res = vconf_get_str(key);
265 if (MS_STRING_VALID(res)) {
266 *value = strdup(res);
274 bool ms_config_set_str(const char *key, const char *value)
278 if (!key || !value) {
279 MS_DBG_ERR("Arguments key or value is NULL");
283 err = vconf_set_str(key, value);
287 MS_DBG_ERR("fail to vconf_set_str %d", err);
292 bool ms_config_get_bool(const char *key, int *value)
296 if (!key || !value) {
297 MS_DBG_ERR("Arguments key or value is NULL");
301 err = vconf_get_bool(key, value);
307 MS_DBG_ERR("Unexpected error code: %d", err);
312 static int get_memory_size(const char *path, struct statvfs_32 *buf)
317 ret = statvfs(path, &s);
319 MS_DBG_ERR("statvfs failed[%d]", ret);
321 return MS_MEDIA_ERR_INTERNAL;
324 buf->f_bsize = s.f_bsize;
325 buf->f_frsize = s.f_frsize;
326 buf->f_blocks = (unsigned long)s.f_blocks;
327 buf->f_bfree = (unsigned long)s.f_bfree;
328 buf->f_bavail = (unsigned long)s.f_bavail;
329 buf->f_files = (unsigned long)s.f_files;
330 buf->f_ffree = (unsigned long)s.f_ffree;
331 buf->f_favail = (unsigned long)s.f_favail;
332 buf->f_fsid = s.f_fsid;
333 buf->f_flag = s.f_flag;
334 buf->f_namemax = s.f_namemax;
336 return MS_MEDIA_ERR_NONE;
339 int ms_get_remain_space(double *free_space)
341 int ret = MS_MEDIA_ERR_NONE;
342 struct statvfs_32 temp;
344 ret = get_memory_size("/opt", &temp);
345 if (ret != MS_MEDIA_ERR_NONE) {
346 MS_DBG_ERR("fail to get memory size");
350 // MS_DBG_ERR("Total mem : %lf, Avail mem : %lf", (double)temp.f_frsize*temp.f_blocks, (double)temp.f_bsize*temp.f_bavail);
352 *free_space = (double)temp.f_bsize*temp.f_bavail;
357 #ifdef _USE_RECORDED_CONTENT
358 bool ms_is_support_pvr(void)
362 if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
363 MS_DBG_ERR("Get PVR Support failed");
367 MS_DBG("PVR Support : [%d]", nSupportPVR);
369 return (nSupportPVR != 0);
373 #ifdef _USE_SENIOR_MODE
374 bool ms_is_support_senior_mode()
376 bool bSupportSeniorMode = false;
378 if(system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
379 MS_DBG_ERR("Get senior mode support failed");
383 MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
385 return bSupportSeniorMode;
389 int ms_check_file_path(const char *file_path, uid_t uid)
394 /* check location of file */
395 /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
396 if (!ms_is_valid_path(file_path, uid)) {
397 MS_DBG_ERR("Invalid path : %s", file_path);
398 return MS_MEDIA_ERR_INVALID_PATH;
401 /* check the file exits actually */
402 exist = open(file_path, O_RDONLY);
404 MS_DBG_ERR("[%s]open files");
405 return MS_MEDIA_ERR_INVALID_PATH;
409 /* check type of the path */
410 /* It must be a regular file */
411 memset(&file_st, 0, sizeof(struct stat));
412 if (stat(file_path, &file_st) == 0) {
413 if (!S_ISREG(file_st.st_mode)) {
414 /* In this case, it is not a regula file */
415 MS_DBG_ERR("this path is not a file");
416 return MS_MEDIA_ERR_INVALID_PATH;
419 MS_DBG_STRERROR("stat failed");
420 return MS_MEDIA_ERR_INVALID_PATH;
423 return MS_MEDIA_ERR_NONE;
426 int ms_check_ignore_dir(const char *full_path, uid_t uid)
428 int ret = MS_MEDIA_ERR_NONE;
429 char *dir_path = NULL;
430 char *leaf_path = NULL;
431 char *usr_path = NULL;
433 ret = ms_check_file_path(full_path, uid);
434 if (ret != MS_MEDIA_ERR_NONE) {
435 MS_DBG_ERR("invalid path : %s", full_path);
436 return MS_MEDIA_ERR_INVALID_PATH;
439 dir_path = g_path_get_dirname(full_path);
440 if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
441 MS_DBG_ERR("getting directory path is failed : %s", full_path);
442 MS_SAFE_FREE(dir_path);
443 return MS_MEDIA_ERR_INVALID_PATH;
446 usr_path = ms_get_path(uid);
447 if (usr_path == NULL) {
448 MS_DBG_ERR("ms_get_path() fail");
449 MS_SAFE_FREE(dir_path);
450 return MS_MEDIA_ERR_INTERNAL;
454 if (ms_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
455 ret = MS_MEDIA_ERR_INVALID_PATH;
459 #ifdef _USE_SENIOR_MODE
460 if(ms_is_support_senior_mode()) {
461 if(strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
465 if (strcmp(dir_path, usr_path) == 0)
467 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
469 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
472 leaf_path = strrchr(dir_path, '/');
473 if (leaf_path != NULL) {
474 int seek_len = leaf_path -dir_path;
475 dir_path[seek_len] = '\0';
477 MS_DBG_ERR("Fail to find leaf path");
478 ret = MS_MEDIA_ERR_INVALID_PATH;
483 MS_SAFE_FREE(dir_path);
484 MS_SAFE_FREE(usr_path);
489 int ms_check_scan_ignore(char * path)
493 const char *ignore_path = "/.scan_ignore";
494 char *check_ignore_file = NULL;
495 int ret = MS_MEDIA_ERR_NONE;
497 if (strstr(path, "/.")) {
498 MS_DBG_ERR("hidden path");
499 ret = MS_MEDIA_ERR_INVALID_PATH;
503 fd = open(path, O_RDONLY | O_DIRECTORY);
505 MS_DBG_ERR("%s folder opendir fails", path);
506 ret = MS_MEDIA_ERR_INVALID_PATH;
508 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
509 MS_DBG_ERR("Fail to get USB path");
513 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
514 if (errno == ENOENT) {
515 /*if the directory does not exist, check the device is unmounted*/
516 if (!ms_storage_mount_status(path)) {
517 MS_DBG_ERR("Device is unmounted[%s]", path);
518 ret = MS_MEDIA_ERR_USB_UNMOUNTED;
524 struct stat folder_st;
525 if (stat(path, &folder_st) == 0) {
526 MS_DBG_ERR("DEV[%ld] INODE[%lld] UID[%ld] GID[%ld] MODE[%lo] PATH[%s]", (long)folder_st.st_dev, (long long)folder_st.st_ino,
527 (long)folder_st.st_uid, (long)folder_st.st_gid, (unsigned long) folder_st.st_mode, path);
529 MS_DBG_ERR("%s folder stat fails", path);
534 /* check the file exits actually */
537 path_len = strlen(path) + strlen(ignore_path) + 1;
538 check_ignore_file = malloc(path_len);
539 if (check_ignore_file != NULL) {
540 memset(check_ignore_file, 0x0, path_len);
541 snprintf(check_ignore_file, path_len, "%s%s", path, ignore_path);
543 exist = open(check_ignore_file, O_RDONLY);
545 MS_DBG_ERR("scan_ignore exists [%s]", check_ignore_file);
546 ret = MS_MEDIA_ERR_INVALID_PATH;
549 MS_SAFE_FREE(check_ignore_file);
551 MS_DBG_ERR("malloc failed");
552 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
563 if (exist >= 0) close(exist);
568 char* ms_get_path(uid_t uid)
571 char *result_passwd = NULL;
574 struct group grpinfo;
575 struct group* grpresult = NULL;
578 grpbufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
579 if (grpbufsize == -1) /* Value was indeterminate */
580 grpbufsize = 16384; /* Should be more than enough (16*1024) */
582 grpbuf = malloc(grpbufsize);
583 if (grpbuf == NULL) {
584 MS_DBG_ERR("malloc grpbuf grpbufsize[%d] failed", grpbufsize);
588 ret = getgrnam_r("users", &grpinfo, grpbuf, grpbufsize, &grpresult);
589 if((ret == 0) && (grpresult != NULL)) {
590 MS_DBG("getgrnam_r users success...\n");
592 MS_DBG_ERR("getgrnam_r users failed ret[%d]", ret);
596 if (uid == getuid()) {
597 if (MS_STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
598 result_passwd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
600 char passwd_str[MAX_FILEPATH_LEN] = {0, };
601 struct passwd pwdinfo;
602 struct passwd* pwdresult = NULL;
606 pwdbufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
607 if (pwdbufsize == -1) /* Value was indeterminate */
608 pwdbufsize = 16384; /* Should be more than enough (16*1024) */
610 pwdbuf = malloc(pwdbufsize);
611 if (pwdbuf == NULL) {
612 MS_DBG_ERR("malloc pwdbuf pwdbufsize[%d] failed", pwdbufsize);
616 ret = getpwuid_r(uid, &pwdinfo, pwdbuf, pwdbufsize, &pwdresult);
617 if((ret == 0) && (pwdresult != NULL)) {
618 MS_DBG("getpwuid uid[%d] success\n", uid);
620 MS_DBG_ERR("getpwuid uid[%d] failed ret[%d]", uid, ret);
621 MS_SAFE_FREE(pwdbuf);
625 // Compare git_t type and not group name
626 if (grpinfo.gr_gid != pwdinfo.pw_gid) {
627 MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
631 len = snprintf(passwd_str, sizeof(passwd_str), "%s/%s", pwdinfo.pw_dir, MEDIA_CONTENT_PATH);
633 result_passwd = strndup(passwd_str, len);
637 MS_SAFE_FREE(grpbuf);
638 return result_passwd;
641 bool ms_storage_mount_status(const char* start_path)
644 #ifndef _USE_DEVICED_DBUS
647 usb_device_list_h list;
649 char *mount_path = NULL;
651 char *storage_path = NULL;
652 char *remain_path = NULL;
655 remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
656 if (remain_path != NULL)
657 remain_len = strlen(remain_path);
659 storage_path = strndup(start_path, strlen(start_path) - remain_len);
661 MS_DBG_ERR("storage_path [%s]", storage_path);
663 err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
665 count = usb_device_list_get_count(list);
667 err = usb_device_list_get_first(list, &device);
668 if(err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
669 mount_path = usb_device_get_mountpath(device);
670 if (mount_path != NULL) {
671 MS_DBG_ERR("mount_path [%s]", mount_path);
672 if (strlen(mount_path) == strlen(storage_path)) {
673 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
674 MS_DBG_ERR("start path is mounted [%s]", start_path);
682 while(usb_device_list_get_next(list, &device) == 0) {
684 mount_path = usb_device_get_mountpath(device);
685 if (mount_path != NULL) {
686 MS_DBG_ERR("mount_path [%s]", mount_path);
687 if (strlen(mount_path) == strlen(storage_path)) {
688 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
689 MS_DBG_ERR("start path is mounted [%s]", start_path);
700 usb_device_free_device_list(list);
702 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
705 MS_SAFE_FREE(storage_path);
710 bool ms_is_valid_path(const char *path, uid_t uid)
713 char *usr_path = NULL;
718 usr_path = ms_get_path(uid);
719 if (usr_path == NULL)
722 #ifdef _USE_SENIOR_MODE
723 if(ms_is_support_senior_mode()) {
724 if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0)
729 if (strncmp(path, usr_path, strlen(usr_path)) == 0)
731 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
733 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
738 MS_SAFE_FREE(usr_path);
743 int ms_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
745 int res = MS_MEDIA_ERR_NONE;
748 if (status == MS_DB_UPDATING) {
749 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
750 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
751 MS_DBG_ERR("ms_config_set_int failed");
754 if (storage_type == MS_STORAGE_EXTERNAL) {
755 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
756 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
757 MS_DBG_ERR("ms_config_set_int failed");
761 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
762 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
763 MS_DBG_ERR("ms_config_set_int failed");
766 if (storage_type == MS_STORAGE_EXTERNAL) {
767 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
768 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
769 MS_DBG_ERR("ms_config_set_int failed");
774 err = ms_set_power_mode(status);
775 if (err != MS_MEDIA_ERR_NONE) {
776 MS_DBG_ERR("ms_set_power_mode fail");
783 int ms_set_power_mode(ms_db_status_type_t status)
785 int res = MS_MEDIA_ERR_NONE;
790 err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
792 res = MS_MEDIA_ERR_INTERNAL;
795 err = display_unlock_state(LCD_OFF, PM_RESET_TIMER);
797 res = MS_MEDIA_ERR_INTERNAL;
800 MS_DBG_ERR("Unacceptable type : %d", status);
807 void ms_trim_dir_path(char *dir_path)
809 /* need implementation */
810 /* if dir_path is not NULL terminated, this function will occure crash */
811 int len = strlen(dir_path);
813 if (dir_path[len -1] == '/')
814 dir_path[len -1] = '\0';
817 bool ms_check_folder_path(const char *folder_path)
821 dp = opendir(folder_path);
823 MS_DBG_ERR("Deleted folder path");
831 int ms_check_size_mediadb(uid_t uid, double *db_size)
833 int ret = MS_MEDIA_ERR_NONE;
834 char *db_path = NULL;
837 ret = media_db_get_media_db_path(uid, &db_path);
839 if(stat(db_path, &buf) == 0) {
840 *db_size = buf.st_size;
842 MS_DBG_STRERROR("stat failed");
843 ret = MS_MEDIA_ERR_INTERNAL;
846 MS_SAFE_FREE(db_path);
851 #ifdef _SET_VIP_PROCESS
852 #define PROC_OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"
853 #define VIP_OOM_SCORE_ADJ (-1000)
854 #define PROC_NAME_MAX 1024
855 #define PROC_BUF_MAX 64
857 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
859 char buf[PROC_BUF_MAX];
860 char cmdline_buf[PROC_NAME_MAX];
864 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
865 fp = fopen(buf, "r");
867 return MS_MEDIA_ERR_INTERNAL;
869 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
871 return MS_MEDIA_ERR_INTERNAL;
875 filename = strrchr(cmdline_buf, '/');
876 if (filename == NULL)
877 filename = cmdline_buf;
879 filename = filename + 1;
881 strncpy(cmdline, filename, PROC_NAME_MAX-1);
883 return MS_MEDIA_ERR_NONE;
886 int ms_set_vip_process(void)
890 static pid_t pid = 0;
891 static char process_name[PROC_NAME_MAX] = {0};
892 static char *appid = NULL;
896 if (ms_get_cmdline_from_proc(pid, process_name)) {
897 MS_DBG_ERR("%s: Read process name failed pid[%d]\n", __func__, pid);
898 return MS_MEDIA_ERR_INTERNAL;
900 appid = process_name;
902 MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
904 if (prctl(PR_GET_DUMPABLE) == 0)
905 prctl(PR_SET_DUMPABLE, 1);
907 snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
908 id = open(buf, O_WRONLY, 0777);
910 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
911 return MS_MEDIA_ERR_INTERNAL;
913 snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
914 if (write(id, buf, strlen(buf)) < 0) {
915 MS_DBG_ERR("write() failed errno=%d", errno);
917 return MS_MEDIA_ERR_INTERNAL;
920 return MS_MEDIA_ERR_NONE;