Use ms_user_XXX APIs instead of each local code
[platform/core/multimedia/media-server.git] / src / common / media-common-utils.c
1 /*
2  * Media Server
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Yong Yeon Kim <yy9875.kim@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 #define _GNU_SOURCE
23 #include <sys/types.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <vconf.h>
27 #include <aul/aul.h>
28 #include <grp.h>
29 #include <pwd.h>
30 #include <dbus/dbus-glib.h>
31 #include <dbus/dbus.h>
32 #include <dbus/dbus-glib-lowlevel.h>
33 #include <sys/statvfs.h>
34 #include <sys/stat.h>
35 #ifdef _SET_VIP_PROCESS
36 #include <sys/prctl.h>
37 #endif
38 #include <system_info.h>
39 #include <dd-display.h>
40
41 #ifndef _USE_DEVICED_DBUS
42 #include <usb-device.h>
43 #endif
44
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"
50
51 #define MS_DRM_CONTENT_TYPE_LENGTH 100
52
53 /* it's for 32bit file offset */
54 struct statvfs_32 {
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
65         int __f_unused;
66 #endif
67         unsigned long int f_flag;
68         unsigned long int f_namemax;
69         int __f_spare[6];
70 };
71
72 bool ms_is_mmc_inserted(void)
73 {
74         bool ret = FALSE;
75         ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
76         GArray *dev_list = NULL;
77
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_WARN("MMC FOUND[%d]", dev_list->len);
82                         ms_sys_release_device_list(&dev_list);
83                         ret = TRUE;
84                 } else {
85                         MS_DBG_ERR("MMC NOT FOUND");
86                 }
87         } else {
88                 MS_DBG_ERR("ms_sys_get_device_list failed");
89         }
90
91         return ret;
92 }
93
94 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
95 {
96         int len = 0;
97         int real_size = size - 1;
98
99         if (!res || !pattern || !str1 || !str2)
100                 return MS_MEDIA_ERR_INVALID_PARAMETER;
101
102         if (real_size < (int)(strlen(str1) + strlen(str2)))
103                 return MS_MEDIA_ERR_INVALID_PARAMETER;
104
105         len = snprintf(res, real_size, pattern, str1, str2);
106         if (len < 0) {
107                 return MS_MEDIA_ERR_INVALID_PARAMETER;
108         }
109
110         res[len] = '\0';
111
112         return MS_MEDIA_ERR_NONE;
113 }
114
115 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
116 {
117         int len = 0;
118         int real_size = size;
119
120         if (!res || !pattern || !str1) {
121                 MS_DBG_ERR("parameta is invalid");
122                 return MS_MEDIA_ERR_INVALID_PARAMETER;
123         }
124
125         if (real_size < (int)(strlen(str1))) {
126                 MS_DBG_ERR("size is wrong");
127                 return MS_MEDIA_ERR_INVALID_PARAMETER;
128         }
129
130         len = snprintf(res, real_size, pattern, str1);
131         if (len < 0) {
132                 MS_DBG_ERR("snprintf failed");
133                 return MS_MEDIA_ERR_INVALID_PARAMETER;
134         }
135
136         res[len] = '\0';
137
138         return MS_MEDIA_ERR_NONE;
139 }
140
141 bool ms_config_get_int(const char *key, int *value)
142 {
143         int err;
144
145         if (!key || !value) {
146                 MS_DBG_ERR("Arguments key or value is NULL");
147                 return false;
148         }
149
150         err = vconf_get_int(key, value);
151         if (err == 0)
152                 return true;
153         else if (err == -1)
154                 return false;
155         else
156                 MS_DBG_ERR("Unexpected error code: %d", err);
157
158         return false;
159 }
160
161 bool ms_config_set_int(const char *key, int value)
162 {
163         int err;
164
165         if (!key) {
166                 MS_DBG_ERR("Arguments key is NULL");
167                 return false;
168         }
169
170         err = vconf_set_int(key, value);
171         if (err == 0)
172                 return true;
173         else if (err == -1)
174                 return false;
175         else
176                 MS_DBG_ERR("Unexpected error code: %d", err);
177
178         return false;
179 }
180
181 bool ms_config_get_str(const char *key, char **value)
182 {
183         char *res = NULL;
184
185         if (key == NULL || value == NULL) {
186                 MS_DBG_ERR("Arguments key or value is NULL");
187                 return false;
188         }
189
190         res = vconf_get_str(key);
191         if (MS_STRING_VALID(res)) {
192                 *value = strdup(res);
193                 MS_SAFE_FREE(res);
194                 return true;
195         }
196
197         return false;
198 }
199
200 bool ms_config_set_str(const char *key, const char *value)
201 {
202         int err;
203
204         if (!key || !value) {
205                 MS_DBG_ERR("Arguments key or value is NULL");
206                 return false;
207         }
208
209         err = vconf_set_str(key, value);
210         if (err == 0)
211                 return true;
212         else
213                 MS_DBG_ERR("fail to vconf_set_str %d", err);
214
215         return false;
216 }
217
218 bool ms_config_get_bool(const char *key, int *value)
219 {
220         int err;
221
222         if (!key || !value) {
223                 MS_DBG_ERR("Arguments key or value is NULL");
224                 return false;
225         }
226
227         err = vconf_get_bool(key, value);
228         if (err == 0)
229                 return true;
230         else if (err == -1)
231                 return false;
232         else
233                 MS_DBG_ERR("Unexpected error code: %d", err);
234
235         return false;
236 }
237
238 static int get_memory_size(const char *path, struct statvfs_32 *buf)
239 {
240         struct statvfs s;
241         int ret;
242
243         ret = statvfs(path, &s);
244         if (ret) {
245                 MS_DBG_ERR("statvfs failed[%d]", ret);
246                 MS_DBG_STRERROR();
247                 return MS_MEDIA_ERR_INTERNAL;
248         }
249
250         buf->f_bsize = s.f_bsize;
251         buf->f_frsize = s.f_frsize;
252         buf->f_blocks = (unsigned long)s.f_blocks;
253         buf->f_bfree = (unsigned long)s.f_bfree;
254         buf->f_bavail = (unsigned long)s.f_bavail;
255         buf->f_files = (unsigned long)s.f_files;
256         buf->f_ffree = (unsigned long)s.f_ffree;
257         buf->f_favail = (unsigned long)s.f_favail;
258         buf->f_fsid = s.f_fsid;
259         buf->f_flag = s.f_flag;
260         buf->f_namemax = s.f_namemax;
261
262         return MS_MEDIA_ERR_NONE;
263 }
264
265 int ms_get_remain_space(double *free_space)
266 {
267         int ret = MS_MEDIA_ERR_NONE;
268         struct statvfs_32 temp;
269
270         ret = get_memory_size("/opt", &temp);
271         if (ret != MS_MEDIA_ERR_NONE) {
272                 MS_DBG_ERR("fail to get memory size");
273                 return ret;
274         }
275
276 //      MS_DBG_ERR("Total mem : %lf, Avail mem : %lf", (double)temp.f_frsize*temp.f_blocks, (double)temp.f_bsize*temp.f_bavail);
277
278         *free_space = (double)temp.f_bsize*temp.f_bavail;
279
280         return ret;
281 }
282
283 #ifdef _USE_RECORDED_CONTENT
284 bool ms_is_support_pvr(void)
285 {
286
287         int nSupportPVR = 0;
288         if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
289                 MS_DBG_ERR("Get PVR Support failed");
290                 return false;
291         }
292
293         MS_DBG("PVR Support : [%d]", nSupportPVR);
294
295         return (nSupportPVR != 0);
296 }
297 #endif
298
299 #ifdef _USE_SENIOR_MODE
300 bool ms_is_support_senior_mode()
301 {
302         bool bSupportSeniorMode = false;
303
304         if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
305                 MS_DBG_ERR("Get senior mode support failed");
306                 return false;
307         }
308
309         MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
310
311         return bSupportSeniorMode;
312 }
313 #endif
314
315 int ms_check_file_path(const char *file_path, uid_t uid)
316 {
317         int exist;
318         struct stat file_st;
319         ms_user_storage_type_t storage_type = -1;
320         int ret = MS_MEDIA_ERR_NONE;
321
322         /* check location of file */
323         /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
324         ret = ms_user_get_storage_type(uid, file_path, &storage_type);
325         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PATH, "Invalid path");
326
327         /* check the file exits actually */
328         exist = open(file_path, O_RDONLY);
329         if (exist < 0) {
330                 MS_DBG_ERR("[%s]open files");
331                 return MS_MEDIA_ERR_INVALID_PATH;
332         }
333         close(exist);
334
335         /* check type of the path */
336         /* It must be a regular file */
337         memset(&file_st, 0, sizeof(struct stat));
338         if (stat(file_path, &file_st) == 0) {
339                 if (!S_ISREG(file_st.st_mode)) {
340                         /* In this case, it is not a regula file */
341                         MS_DBG_ERR("this path is not a file");
342                         return MS_MEDIA_ERR_INVALID_PATH;
343                 }
344         } else {
345                 MS_DBG_STRERROR("stat failed");
346                 return MS_MEDIA_ERR_INVALID_PATH;
347         }
348
349         return MS_MEDIA_ERR_NONE;
350 }
351
352 int ms_check_ignore_dir(const char *full_path, uid_t uid)
353 {
354         int ret = MS_MEDIA_ERR_NONE;
355         char *dir_path = NULL;
356         char *leaf_path = NULL;
357         char *usr_path = NULL;
358
359         ret = ms_check_file_path(full_path, uid);
360         if (ret != MS_MEDIA_ERR_NONE) {
361                 MS_DBG_ERR("invalid path : %s", full_path);
362                 return MS_MEDIA_ERR_INVALID_PATH;
363         }
364
365         dir_path = g_path_get_dirname(full_path);
366         if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
367                 MS_DBG_ERR("getting directory path is failed : %s", full_path);
368                 MS_SAFE_FREE(dir_path);
369                 return MS_MEDIA_ERR_INVALID_PATH;
370         }
371
372         ret = ms_user_get_internal_root_path(uid, &usr_path);
373         if (ret != MS_MEDIA_ERR_NONE) {
374                 MS_DBG_ERR("ms_user_get_internal_root_path() fail");
375                 MS_SAFE_FREE(dir_path);
376                 return MS_MEDIA_ERR_INTERNAL;
377         }
378
379         while (1) {
380                 if (ms_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
381                         ret = MS_MEDIA_ERR_INVALID_PATH;
382                         break;
383                 }
384
385 #ifdef _USE_SENIOR_MODE
386                 if (ms_is_support_senior_mode()) {
387                         if (strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
388                                 break;
389                 }
390 #endif
391                 if (strcmp(dir_path, usr_path) == 0)
392                         break;
393                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
394                         break;
395                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
396                         break;
397                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(dir_path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
398                         break;
399
400                 leaf_path = strrchr(dir_path, '/');
401                 if (leaf_path != NULL) {
402                                 int seek_len = leaf_path -dir_path;
403                                 dir_path[seek_len] = '\0';
404                 } else {
405                         MS_DBG_ERR("Fail to find leaf path");
406                         ret = MS_MEDIA_ERR_INVALID_PATH;
407                         break;
408                 }
409         }
410
411         MS_SAFE_FREE(dir_path);
412         MS_SAFE_FREE(usr_path);
413
414         return ret;
415 }
416
417 int ms_check_scan_ignore(char * path)
418 {
419         int fd = -1;
420         int exist = -1;
421         const char *ignore_path = "/.scan_ignore";
422         char *check_ignore_file = NULL;
423         int ret = MS_MEDIA_ERR_NONE;
424
425         if (strstr(path, "/.")) {
426                 MS_DBG_ERR("hidden path");
427                 ret = MS_MEDIA_ERR_INVALID_PATH;
428                 goto ERROR;
429         }
430
431         fd = open(path, O_RDONLY | O_DIRECTORY);
432         if (fd == -1) {
433                 MS_DBG_ERR("%s folder opendir fails", path);
434                 ret = MS_MEDIA_ERR_INVALID_PATH;
435
436                 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
437                         MS_DBG_ERR("Fail to get USB path");
438                         goto ERROR;
439                 }
440
441                 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
442                         if (errno == ENOENT) {
443                                 /*if the directory does not exist, check the device is unmounted*/
444                                 if (!ms_storage_mount_status(path)) {
445                                         MS_DBG_ERR("Device is unmounted[%s]", path);
446                                         ret = MS_MEDIA_ERR_USB_UNMOUNTED;
447                                         goto ERROR;
448                                 }
449                         }
450                 }
451
452                 struct stat folder_st;
453                 if (stat(path, &folder_st) == 0) {
454                         MS_DBG_WARN("DEV[%ld] INODE[%lld] UID[%ld] GID[%ld] MODE[%lo] PATH[%s]", (long)folder_st.st_dev, (long long)folder_st.st_ino,
455                                 (long)folder_st.st_uid, (long)folder_st.st_gid, (unsigned long) folder_st.st_mode, path);
456                 } else {
457                         MS_DBG_ERR("%s folder stat fails", path);
458                 }
459
460                 goto ERROR;
461         } else {
462                 /* check the file exits actually */
463                 int path_len = 0;
464
465                 path_len = strlen(path) + strlen(ignore_path) + 1;
466                 check_ignore_file = malloc(path_len);
467                 if (check_ignore_file != NULL) {
468                         memset(check_ignore_file, 0x0, path_len);
469                         snprintf(check_ignore_file, path_len, "%s%s", path, ignore_path);
470
471                         exist = open(check_ignore_file, O_RDONLY);
472                         if (exist >= 0) {
473                                 MS_DBG_WARN("scan_ignore exists [%s]", check_ignore_file);
474                                 ret = MS_MEDIA_ERR_INVALID_PATH;
475                         }
476
477                         MS_SAFE_FREE(check_ignore_file);
478                 } else {
479                         MS_DBG_ERR("malloc failed");
480                         ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
481                 }
482         }
483
484 ERROR:
485
486         if (fd != -1) {
487                 close(fd);
488                 fd = -1;
489         }
490
491         if (exist >= 0) close(exist);
492
493         return ret;
494 }
495
496 bool ms_storage_mount_status(const char* start_path)
497 {
498         bool ret = false;
499 #ifndef _USE_DEVICED_DBUS
500         int count = 0;
501         int err = 0;
502         usb_device_list_h list;
503         usb_device_h device;
504         char *mount_path = NULL;
505
506         char *storage_path = NULL;
507         char *remain_path = NULL;
508         int remain_len = 0;
509
510         remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
511         if (remain_path != NULL)
512                 remain_len = strlen(remain_path);
513
514         storage_path = strndup(start_path, strlen(start_path) - remain_len);
515
516         MS_DBG_WARN("storage_path [%s]", storage_path);
517
518         err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
519         if (err == 0) {
520                 count = usb_device_list_get_count(list);
521                 if (count > 0) {
522                         err = usb_device_list_get_first(list, &device);
523                         if (err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
524                                 mount_path = usb_device_get_mountpath(device);
525                                 if (mount_path != NULL) {
526                                         MS_DBG_WARN("mount_path [%s]", mount_path);
527                                         if (strlen(mount_path) == strlen(storage_path)) {
528                                                 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
529                                                         MS_DBG_WARN("start path is mounted [%s]", start_path);
530                                                         ret = true;
531                                                 }
532                                         }
533                                 }
534                         }
535
536                         if (ret != true) {
537                                 while (usb_device_list_get_next(list, &device) == 0) {
538                                         if (device != NULL) {
539                                                 mount_path = usb_device_get_mountpath(device);
540                                                 if (mount_path != NULL) {
541                                                         MS_DBG_WARN("mount_path [%s]", mount_path);
542                                                         if (strlen(mount_path) == strlen(storage_path)) {
543                                                                 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
544                                                                         MS_DBG_WARN("start path is mounted [%s]", start_path);
545                                                                         ret = true;
546                                                                         break;
547                                                                 }
548                                                         }
549                                                 }
550                                         }
551                                 }
552                         }
553                 }
554
555                 usb_device_free_device_list(list);
556         } else {
557                 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
558         }
559
560         MS_SAFE_FREE(storage_path);
561 #endif
562         return ret;
563 }
564
565 int ms_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
566 {
567         int res = MS_MEDIA_ERR_NONE;
568         int err = 0;
569
570         if (status == MS_DB_UPDATING) {
571                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
572                         res = MS_MEDIA_ERR_VCONF_SET_FAIL;
573                         MS_DBG_ERR("ms_config_set_int failed");
574                 }
575
576                 if (storage_type == MS_STORAGE_EXTERNAL) {
577                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
578                                 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
579                                 MS_DBG_ERR("ms_config_set_int failed");
580                         }
581                 }
582         } else {
583                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
584                         res = MS_MEDIA_ERR_VCONF_SET_FAIL;
585                         MS_DBG_ERR("ms_config_set_int failed");
586                 }
587
588                 if (storage_type == MS_STORAGE_EXTERNAL) {
589                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
590                                 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
591                                 MS_DBG_ERR("ms_config_set_int failed");
592                         }
593                 }
594         }
595
596         err = ms_set_power_mode(status);
597         if (err != MS_MEDIA_ERR_NONE) {
598                 MS_DBG_ERR("ms_set_power_mode fail");
599                 res = err;
600         }
601
602         return res;
603 }
604
605 int ms_set_power_mode(ms_db_status_type_t status)
606 {
607         int res = MS_MEDIA_ERR_NONE;
608         int err;
609
610         switch (status) {
611         case MS_DB_UPDATING:
612                 err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
613                 if (err != 0)
614                         res = MS_MEDIA_ERR_INTERNAL;
615                 break;
616         case MS_DB_UPDATED:
617                 err = display_unlock_state(LCD_OFF, PM_RESET_TIMER);
618                 if (err != 0)
619                         res = MS_MEDIA_ERR_INTERNAL;
620                 break;
621         default:
622                 MS_DBG_ERR("Unacceptable type : %d", status);
623                 break;
624         }
625
626         return res;
627 }
628
629 void ms_trim_dir_path(char *dir_path)
630 {
631         /* need implementation */
632         /* if dir_path is not NULL terminated, this function will occure crash */
633         int len = strlen(dir_path);
634
635         if (dir_path[len -1] == '/')
636                 dir_path[len -1] = '\0';
637 }
638
639 bool ms_check_folder_path(const char *folder_path)
640 {
641         DIR *dp = NULL;
642
643         dp = opendir(folder_path);
644         if (dp == NULL) {
645                 MS_DBG_ERR("Deleted folder path");
646                 return false;
647         }
648         closedir(dp);
649
650         return true;
651 }
652
653 int ms_check_size_mediadb(uid_t uid, double *db_size)
654 {
655         int ret = MS_MEDIA_ERR_NONE;
656         char *db_path = NULL;
657         struct stat buf;
658
659         ret = ms_user_get_media_db_path(uid, &db_path);
660
661         if (stat(db_path, &buf) == 0) {
662                 *db_size = buf.st_size;
663         } else {
664                 MS_DBG_STRERROR("stat failed");
665                 ret = MS_MEDIA_ERR_INTERNAL;
666         }
667
668         MS_SAFE_FREE(db_path);
669
670         return ret;
671 }
672
673 #ifdef _SET_VIP_PROCESS
674 #define PROC_OOM_SCORE_ADJ_PATH         "/proc/%d/oom_score_adj"
675 #define VIP_OOM_SCORE_ADJ                       (-1000)
676 #define PROC_NAME_MAX 1024
677 #define PROC_BUF_MAX 64
678
679 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
680 {
681         char buf[PROC_BUF_MAX];
682         char cmdline_buf[PROC_NAME_MAX];
683         char *filename;
684         FILE *fp;
685
686         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
687         fp = fopen(buf, "r");
688         if (fp == NULL)
689                 return MS_MEDIA_ERR_INTERNAL;
690
691         if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
692                 fclose(fp);
693                 return MS_MEDIA_ERR_INTERNAL;
694         }
695         fclose(fp);
696
697         filename = strrchr(cmdline_buf, '/');
698         if (filename == NULL)
699                 filename = cmdline_buf;
700         else
701                 filename = filename + 1;
702
703         SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
704
705         return MS_MEDIA_ERR_NONE;
706 }
707
708 int ms_set_vip_process(void)
709 {
710         char buf[100] = {0};
711         int id = 0;
712         static pid_t pid = 0;
713         static char process_name[PROC_NAME_MAX] = {0};
714         static char *appid = NULL;
715
716         /* Get Pid */
717         pid = getpid();
718         if (ms_get_cmdline_from_proc(pid, process_name)) {
719                 MS_DBG_ERR("%s: Read process name failed pid[%d]\n", __func__, pid);
720                 return MS_MEDIA_ERR_INTERNAL;
721         }
722         appid = process_name;
723
724         MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
725
726         if (prctl(PR_GET_DUMPABLE) == 0)
727                 prctl(PR_SET_DUMPABLE, 1);
728
729         snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
730         id = open(buf, O_WRONLY, 0777);
731         if (id < 0) {
732                 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
733                 return MS_MEDIA_ERR_INTERNAL;
734         }
735         snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
736         if (write(id, buf, strlen(buf)) < 0) {
737                 MS_DBG_ERR("write() failed errno=%d", errno);
738                 close(id);
739                 return MS_MEDIA_ERR_INTERNAL;
740         }
741         close(id);
742         return MS_MEDIA_ERR_NONE;
743 }
744 #endif