use getpwuid_r getgrnam_r to guarantee thread safety
[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_ERR("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 static char* __media_get_path(uid_t uid)
95 {
96         char *result_passwd = NULL;
97         struct group *grpinfo = NULL;
98
99         if (uid == getuid()) {
100                 grpinfo = getgrnam("users");
101                 if (grpinfo == NULL) {
102                         MS_DBG_ERR("getgrnam(users) returns NULL !");
103                         return NULL;
104                 }
105                 if (MS_STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
106                         result_passwd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
107         } else {
108                 struct passwd *userinfo = getpwuid(uid);
109                 if (userinfo == NULL) {
110                         MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
111                         return NULL;
112                 }
113                 grpinfo = getgrnam("users");
114                 if (grpinfo == NULL) {
115                         MS_DBG_ERR("getgrnam(users) returns NULL !");
116                         return NULL;
117                 }
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);
121                         return NULL;
122                 }
123                 result_passwd = strndup(userinfo->pw_dir, strlen(userinfo->pw_dir));
124         }
125
126         return result_passwd;
127 }
128
129 ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
130 {
131         int length_path;
132         int ret = MS_MEDIA_ERR_NONE;
133         char * user_path = NULL;
134
135         if (path == NULL)
136                 return MS_MEDIA_ERR_INVALID_PATH;
137
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;
142                 }
143         }
144 #endif
145
146         user_path = __media_get_path(uid);
147         if (user_path == NULL)
148                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
149
150         length_path = strlen(user_path);
151
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;
158         } else {
159                 MS_DBG_ERR("[%s][%s][%s]", MEDIA_ROOT_PATH_SDCARD, MEDIA_ROOT_PATH_USB, path);
160                 ret = MS_MEDIA_ERR_INVALID_PATH;
161         }
162
163         MS_SAFE_FREE(user_path);
164
165         return ret;
166 }
167
168 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
169 {
170         int len = 0;
171         int real_size = size - 1;
172
173         if (!res || !pattern || !str1 || !str2)
174                 return MS_MEDIA_ERR_INVALID_PARAMETER;
175
176         if (real_size < (int)(strlen(str1) + strlen(str2)))
177                 return MS_MEDIA_ERR_INVALID_PARAMETER;
178
179         len = snprintf(res, real_size, pattern, str1, str2);
180         if (len < 0) {
181                 return MS_MEDIA_ERR_INVALID_PARAMETER;
182         }
183
184         res[len] = '\0';
185
186         return MS_MEDIA_ERR_NONE;
187 }
188
189 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
190 {
191         int len = 0;
192         int real_size = size;
193
194         if (!res || !pattern || !str1) {
195                 MS_DBG_ERR("parameta is invalid");
196                 return MS_MEDIA_ERR_INVALID_PARAMETER;
197         }
198
199         if (real_size < (int)(strlen(str1))) {
200                 MS_DBG_ERR("size is wrong");
201                 return MS_MEDIA_ERR_INVALID_PARAMETER;
202         }
203
204         len = snprintf(res, real_size, pattern, str1);
205         if (len < 0) {
206                 MS_DBG_ERR("snprintf failed");
207                 return MS_MEDIA_ERR_INVALID_PARAMETER;
208         }
209
210         res[len] = '\0';
211
212         return MS_MEDIA_ERR_NONE;
213 }
214
215 bool ms_config_get_int(const char *key, int *value)
216 {
217         int err;
218
219         if (!key || !value) {
220                 MS_DBG_ERR("Arguments key or value is NULL");
221                 return false;
222         }
223
224         err = vconf_get_int(key, value);
225         if (err == 0)
226                 return true;
227         else if (err == -1)
228                 return false;
229         else
230                 MS_DBG_ERR("Unexpected error code: %d", err);
231
232         return false;
233 }
234
235 bool ms_config_set_int(const char *key, int value)
236 {
237         int err;
238
239         if (!key) {
240                 MS_DBG_ERR("Arguments key is NULL");
241                 return false;
242         }
243
244         err = vconf_set_int(key, value);
245         if (err == 0)
246                 return true;
247         else if (err == -1)
248                 return false;
249         else
250                 MS_DBG_ERR("Unexpected error code: %d", err);
251
252         return false;
253 }
254
255 bool ms_config_get_str(const char *key, char **value)
256 {
257         char *res = NULL;
258
259         if (key == NULL || value == NULL) {
260                 MS_DBG_ERR("Arguments key or value is NULL");
261                 return false;
262         }
263
264         res = vconf_get_str(key);
265         if (MS_STRING_VALID(res)) {
266                 *value = strdup(res);
267                 MS_SAFE_FREE(res);
268                 return true;
269         }
270
271         return false;
272 }
273
274 bool ms_config_set_str(const char *key, const char *value)
275 {
276         int err;
277
278         if (!key || !value) {
279                 MS_DBG_ERR("Arguments key or value is NULL");
280                 return false;
281         }
282
283         err = vconf_set_str(key, value);
284         if (err == 0)
285                 return true;
286         else
287                 MS_DBG_ERR("fail to vconf_set_str %d", err);
288
289         return false;
290 }
291
292 bool ms_config_get_bool(const char *key, int *value)
293 {
294         int err;
295
296         if (!key || !value) {
297                 MS_DBG_ERR("Arguments key or value is NULL");
298                 return false;
299         }
300
301         err = vconf_get_bool(key, value);
302         if (err == 0)
303                 return true;
304         else if (err == -1)
305                 return false;
306         else
307                 MS_DBG_ERR("Unexpected error code: %d", err);
308
309         return false;
310 }
311
312 static int get_memory_size(const char *path, struct statvfs_32 *buf)
313 {
314         struct statvfs s;
315         int ret;
316
317         ret = statvfs(path, &s);
318         if (ret) {
319                 MS_DBG_ERR("statvfs failed[%d]", ret);
320                 MS_DBG_STRERROR();
321                 return MS_MEDIA_ERR_INTERNAL;
322         }
323
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;
335
336         return MS_MEDIA_ERR_NONE;
337 }
338
339 int ms_get_remain_space(double *free_space)
340 {
341         int ret = MS_MEDIA_ERR_NONE;
342         struct statvfs_32 temp;
343
344         ret = get_memory_size("/opt", &temp);
345         if (ret != MS_MEDIA_ERR_NONE) {
346                 MS_DBG_ERR("fail to get memory size");
347                 return ret;
348         }
349
350 //      MS_DBG_ERR("Total mem : %lf, Avail mem : %lf", (double)temp.f_frsize*temp.f_blocks, (double)temp.f_bsize*temp.f_bavail);
351
352         *free_space = (double)temp.f_bsize*temp.f_bavail;
353
354         return ret;
355 }
356
357 #ifdef _USE_RECORDED_CONTENT
358 bool ms_is_support_pvr(void)
359 {
360
361         int nSupportPVR = 0;
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");
364                 return false;
365         }
366
367         MS_DBG("PVR Support : [%d]", nSupportPVR);
368
369         return (nSupportPVR != 0);
370 }
371 #endif
372
373 #ifdef _USE_SENIOR_MODE
374 bool ms_is_support_senior_mode()
375 {
376         bool bSupportSeniorMode = false;
377
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");
380                 return false;
381         }
382
383         MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
384
385         return bSupportSeniorMode;
386 }
387 #endif
388
389 int ms_check_file_path(const char *file_path, uid_t uid)
390 {
391         int exist;
392         struct stat file_st;
393
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;
399         }
400
401         /* check the file exits actually */
402         exist = open(file_path, O_RDONLY);
403         if (exist < 0) {
404                 MS_DBG_ERR("[%s]open files");
405                 return MS_MEDIA_ERR_INVALID_PATH;
406         }
407         close(exist);
408
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;
417                 }
418         } else {
419                 MS_DBG_STRERROR("stat failed");
420                 return MS_MEDIA_ERR_INVALID_PATH;
421         }
422
423         return MS_MEDIA_ERR_NONE;
424 }
425
426 int ms_check_ignore_dir(const char *full_path, uid_t uid)
427 {
428         int ret = MS_MEDIA_ERR_NONE;
429         char *dir_path = NULL;
430         char *leaf_path = NULL;
431         char *usr_path = NULL;
432
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;
437         }
438
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;
444         }
445
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;
451         }
452
453         while (1) {
454                 if (ms_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
455                         ret = MS_MEDIA_ERR_INVALID_PATH;
456                         break;
457                 }
458
459 #ifdef _USE_SENIOR_MODE
460                 if(ms_is_support_senior_mode()) {
461                         if(strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
462                                 break;
463                 }
464 #endif
465                 if (strcmp(dir_path, usr_path) == 0)
466                         break;
467                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
468                         break;
469                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
470                         break;
471
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';
476                 } else {
477                         MS_DBG_ERR("Fail to find leaf path");
478                         ret = MS_MEDIA_ERR_INVALID_PATH;
479                         break;
480                 }
481         }
482
483         MS_SAFE_FREE(dir_path);
484         MS_SAFE_FREE(usr_path);
485
486         return ret;
487 }
488
489 int ms_check_scan_ignore(char * path)
490 {
491         int fd = -1;
492         int exist = -1;
493         const char *ignore_path = "/.scan_ignore";
494         char *check_ignore_file = NULL;
495         int ret = MS_MEDIA_ERR_NONE;
496
497         if (strstr(path, "/.")) {
498                 MS_DBG_ERR("hidden path");
499                 ret = MS_MEDIA_ERR_INVALID_PATH;
500                 goto ERROR;
501         }
502
503         fd = open(path, O_RDONLY | O_DIRECTORY);
504         if (fd == -1) {
505                 MS_DBG_ERR("%s folder opendir fails", path);
506                 ret = MS_MEDIA_ERR_INVALID_PATH;
507
508                 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
509                         MS_DBG_ERR("Fail to get USB path");
510                         goto ERROR;
511                 }
512
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;
519                                         goto ERROR;
520                                 }
521                         }
522                 }
523
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);
528                 } else {
529                         MS_DBG_ERR("%s folder stat fails", path);
530                 }
531
532                 goto ERROR;
533         } else {
534                 /* check the file exits actually */
535                 int path_len = 0;
536
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);
542
543                         exist = open(check_ignore_file, O_RDONLY);
544                         if (exist >= 0) {
545                                 MS_DBG_ERR("scan_ignore exists [%s]", check_ignore_file);
546                                 ret = MS_MEDIA_ERR_INVALID_PATH;
547                         }
548
549                         MS_SAFE_FREE(check_ignore_file);
550                 } else {
551                         MS_DBG_ERR("malloc failed");
552                         ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
553                 }
554         }
555
556 ERROR:
557
558         if (fd != -1) {
559                 close(fd);
560                 fd = -1;
561         }
562
563         if (exist >= 0) close(exist);
564
565         return ret;
566 }
567
568 char* ms_get_path(uid_t uid)
569 {
570         int len = 0;
571         char *result_passwd = NULL;
572         int ret = -1;
573         char* grpbuf;
574         struct group grpinfo;
575         struct group* grpresult = NULL;
576         size_t grpbufsize;
577
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) */
581
582         grpbuf = malloc(grpbufsize);
583         if (grpbuf == NULL) {
584                 MS_DBG_ERR("malloc grpbuf grpbufsize[%d] failed", grpbufsize);
585                 return NULL;
586         }
587
588         ret = getgrnam_r("users", &grpinfo, grpbuf, grpbufsize, &grpresult);
589         if((ret == 0) && (grpresult != NULL)) {
590                 MS_DBG("getgrnam_r users success...\n");
591         } else {
592                 MS_DBG_ERR("getgrnam_r users failed ret[%d]", ret);
593                 goto END;
594         }
595
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));
599         } else {
600                 char passwd_str[MAX_FILEPATH_LEN] = {0, };
601                 struct passwd pwdinfo;
602                 struct passwd* pwdresult = NULL;
603                 char* pwdbuf;
604                 size_t pwdbufsize;
605
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) */
609
610                 pwdbuf = malloc(pwdbufsize);
611                 if (pwdbuf == NULL) {
612                         MS_DBG_ERR("malloc pwdbuf pwdbufsize[%d] failed", pwdbufsize);
613                         goto END;
614                 }
615
616                 ret = getpwuid_r(uid, &pwdinfo, pwdbuf, pwdbufsize, &pwdresult);
617                 if((ret == 0) && (pwdresult != NULL)) {
618                         MS_DBG("getpwuid uid[%d] success\n", uid);
619                 } else {
620                         MS_DBG_ERR("getpwuid uid[%d] failed ret[%d]", uid, ret);
621                         MS_SAFE_FREE(pwdbuf);
622                         goto END;
623                 }
624
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);
628                         return NULL;
629                 }
630
631                 len = snprintf(passwd_str, sizeof(passwd_str), "%s/%s", pwdinfo.pw_dir, MEDIA_CONTENT_PATH);
632                 if (len > 0)
633                         result_passwd = strndup(passwd_str, len);
634         }
635
636 END:
637         MS_SAFE_FREE(grpbuf);
638         return result_passwd;
639 }
640
641 bool ms_storage_mount_status(const char* start_path)
642 {
643         bool ret = false;
644 #ifndef _USE_DEVICED_DBUS
645         int count = 0;
646         int err = 0;
647         usb_device_list_h list;
648         usb_device_h device;
649         char *mount_path = NULL;
650
651         char *storage_path = NULL;
652         char *remain_path = NULL;
653         int remain_len = 0;
654
655         remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
656         if (remain_path != NULL)
657                 remain_len = strlen(remain_path);
658
659         storage_path = strndup(start_path, strlen(start_path) - remain_len);
660
661         MS_DBG_ERR("storage_path [%s]", storage_path);
662
663         err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
664         if (err == 0) {
665                 count = usb_device_list_get_count(list);
666                 if (count > 0) {
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);
675                                                         ret = true;
676                                                 }
677                                         }
678                                 }
679                         }
680
681                         if (ret != true) {
682                                 while(usb_device_list_get_next(list, &device) == 0) {
683                                         if(device != NULL) {
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);
690                                                                         ret = true;
691                                                                         break;
692                                                                 }
693                                                         }
694                                                 }
695                                         }
696                                 }
697                         }
698                 }
699
700                 usb_device_free_device_list(list);
701         } else {
702                 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
703         }
704
705         MS_SAFE_FREE(storage_path);
706 #endif
707         return ret;
708 }
709
710 bool ms_is_valid_path(const char *path, uid_t uid)
711 {
712         bool ret = false;
713         char *usr_path = NULL;
714
715         if (path == NULL)
716                 return false;
717
718         usr_path = ms_get_path(uid);
719         if (usr_path == NULL)
720                 return false;
721
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)
725                         return true;
726         }
727 #endif
728
729         if (strncmp(path, usr_path, strlen(usr_path)) == 0)
730                 ret = true;
731         else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
732                 ret = true;
733         else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
734                 ret = true;
735         else
736                 ret = false;
737
738         MS_SAFE_FREE(usr_path);
739
740         return ret;
741 }
742
743 int ms_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
744 {
745         int res = MS_MEDIA_ERR_NONE;
746         int err = 0;
747
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");
752                 }
753
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");
758                         }
759                 }
760         } else {
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");
764                 }
765
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");
770                         }
771                 }
772         }
773
774         err = ms_set_power_mode(status);
775         if (err != MS_MEDIA_ERR_NONE) {
776                 MS_DBG_ERR("ms_set_power_mode fail");
777                 res = err;
778         }
779
780         return res;
781 }
782
783 int ms_set_power_mode(ms_db_status_type_t status)
784 {
785         int res = MS_MEDIA_ERR_NONE;
786         int err;
787
788         switch (status) {
789         case MS_DB_UPDATING:
790                 err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
791                 if (err != 0)
792                         res = MS_MEDIA_ERR_INTERNAL;
793                 break;
794         case MS_DB_UPDATED:
795                 err = display_unlock_state(LCD_OFF, PM_RESET_TIMER);
796                 if (err != 0)
797                         res = MS_MEDIA_ERR_INTERNAL;
798                 break;
799         default:
800                 MS_DBG_ERR("Unacceptable type : %d", status);
801                 break;
802         }
803
804         return res;
805 }
806
807 void ms_trim_dir_path(char *dir_path)
808 {
809         /* need implementation */
810         /* if dir_path is not NULL terminated, this function will occure crash */
811         int len = strlen(dir_path);
812
813         if (dir_path[len -1] == '/')
814                 dir_path[len -1] = '\0';
815 }
816
817 bool ms_check_folder_path(const char *folder_path)
818 {
819         DIR *dp = NULL;
820
821         dp = opendir(folder_path);
822         if (dp == NULL) {
823                 MS_DBG_ERR("Deleted folder path");
824                 return false;
825         }
826         closedir(dp);
827
828         return true;
829 }
830
831 int ms_check_size_mediadb(uid_t uid, double *db_size)
832 {
833         int ret = MS_MEDIA_ERR_NONE;
834         char *db_path = NULL;
835         struct stat buf;
836
837         ret = media_db_get_media_db_path(uid, &db_path);
838
839         if(stat(db_path, &buf) == 0) {
840                 *db_size = buf.st_size;
841         } else {
842                 MS_DBG_STRERROR("stat failed");
843                 ret = MS_MEDIA_ERR_INTERNAL;
844         }
845
846         MS_SAFE_FREE(db_path);
847
848         return ret;
849 }
850
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
856
857 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
858 {
859         char buf[PROC_BUF_MAX];
860         char cmdline_buf[PROC_NAME_MAX];
861         char *filename;
862         FILE *fp;
863
864         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
865         fp = fopen(buf, "r");
866         if (fp == NULL)
867                 return MS_MEDIA_ERR_INTERNAL;
868
869         if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
870                 fclose(fp);
871                 return MS_MEDIA_ERR_INTERNAL;
872         }
873         fclose(fp);
874
875         filename = strrchr(cmdline_buf, '/');
876         if (filename == NULL)
877                 filename = cmdline_buf;
878         else
879                 filename = filename + 1;
880
881         strncpy(cmdline, filename, PROC_NAME_MAX-1);
882
883         return MS_MEDIA_ERR_NONE;
884 }
885
886 int ms_set_vip_process(void)
887 {
888         char buf[100] = {0};
889         int id = 0;
890         static pid_t pid = 0;
891         static char process_name[PROC_NAME_MAX] = {0};
892         static char *appid = NULL;
893
894         /* Get Pid */
895         pid = getpid();
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;
899         }
900         appid = process_name;
901
902         MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
903
904         if (prctl(PR_GET_DUMPABLE) == 0)
905                 prctl(PR_SET_DUMPABLE, 1);
906
907         snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
908         id = open(buf, O_WRONLY, 0777);
909         if (id < 0) {
910                 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
911                 return MS_MEDIA_ERR_INTERNAL;
912         }
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);
916                 close(id);
917                 return MS_MEDIA_ERR_INTERNAL;
918         }
919         close(id);
920         return MS_MEDIA_ERR_NONE;
921 }
922 #endif