1. fix memory leak from previos change
[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         int ret = -1;
97         char *grpbuf = NULL;
98         char *result_passwd = NULL;
99         struct group grpinfo;
100         struct group *grpresult = NULL;
101         size_t grpbufsize;
102
103         grpbufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
104         if (grpbufsize == -1)           /* Value was indeterminate */
105                 grpbufsize = 16384;             /* Should be more than enough (16*1024) */
106
107         grpbuf = malloc(grpbufsize);
108         if (grpbuf == NULL) {
109                 MS_DBG_ERR("malloc grpbuf grpbufsize[%d] failed", grpbufsize);
110                 goto END;
111         }
112
113         ret = getgrnam_r("users", &grpinfo, grpbuf, grpbufsize, &grpresult);
114         if((ret == 0) && (grpresult != NULL)) {
115                 MS_DBG("getgrnam_r users success...\n");
116         } else {
117                 MS_DBG_ERR("getgrnam_r users failed ret[%d]", ret);
118                 goto END;
119         }
120
121         if (uid == getuid()) {
122                 if (MS_STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
123                         result_passwd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
124         } else {
125                 struct passwd pwdinfo;
126                 struct passwd *pwdresult = NULL;
127                 char *pwdbuf;
128                 size_t pwdbufsize;
129
130                 pwdbufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
131                 if (pwdbufsize == -1)           /* Value was indeterminate */
132                         pwdbufsize = 16384;             /* Should be more than enough (16*1024) */
133
134                 pwdbuf = malloc(pwdbufsize);
135                 if (pwdbuf == NULL) {
136                         MS_DBG_ERR("malloc pwdbuf pwdbufsize[%d] failed", pwdbufsize);
137                         goto END;
138                 }
139
140                 ret = getpwuid_r(uid, &pwdinfo, pwdbuf, pwdbufsize, &pwdresult);
141                 if((ret == 0) && (pwdresult != NULL)) {
142                         MS_DBG("getpwuid uid[%d] success\n", uid);
143                 } else {
144                         MS_DBG_ERR("getpwuid uid[%d] failed ret[%d]", uid, ret);
145                         MS_SAFE_FREE(pwdbuf);
146                         goto END;
147                 }
148
149                 // Compare git_t type and not group name
150                 if (grpinfo.gr_gid != pwdinfo.pw_gid) {
151                         MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
152                         MS_SAFE_FREE(pwdbuf);
153                         goto END;
154                 }
155                 result_passwd = strndup(pwdinfo.pw_dir, strlen(pwdinfo.pw_dir));
156
157                 MS_SAFE_FREE(pwdbuf);
158         }
159
160 END:
161         MS_SAFE_FREE(grpbuf);
162
163         return result_passwd;
164 }
165
166
167 ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
168 {
169         int length_path;
170         int ret = MS_MEDIA_ERR_NONE;
171         char * user_path = NULL;
172
173         if (path == NULL)
174                 return MS_MEDIA_ERR_INVALID_PATH;
175
176 #ifdef _USE_SENIOR_MODE
177         if(ms_is_support_senior_mode()) {
178                 if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0) {
179                         return MS_STORAGE_EXTERNAL;
180                 }
181         }
182 #endif
183
184         user_path = __media_get_path(uid);
185         if (user_path == NULL)
186                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
187
188         length_path = strlen(user_path);
189
190         if (strncmp(path, user_path, length_path) == 0) {
191                 ret = MS_STORAGE_INTERNAL;
192         } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
193                 ret = MS_STORAGE_EXTERNAL;
194         } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
195                 ret = MS_STORAGE_EXTERNAL_USB;
196         } else {
197                 MS_DBG_ERR("[%s][%s][%s]", MEDIA_ROOT_PATH_SDCARD, MEDIA_ROOT_PATH_USB, path);
198                 ret = MS_MEDIA_ERR_INVALID_PATH;
199         }
200
201         MS_SAFE_FREE(user_path);
202
203         return ret;
204 }
205
206 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
207 {
208         int len = 0;
209         int real_size = size - 1;
210
211         if (!res || !pattern || !str1 || !str2)
212                 return MS_MEDIA_ERR_INVALID_PARAMETER;
213
214         if (real_size < (int)(strlen(str1) + strlen(str2)))
215                 return MS_MEDIA_ERR_INVALID_PARAMETER;
216
217         len = snprintf(res, real_size, pattern, str1, str2);
218         if (len < 0) {
219                 return MS_MEDIA_ERR_INVALID_PARAMETER;
220         }
221
222         res[len] = '\0';
223
224         return MS_MEDIA_ERR_NONE;
225 }
226
227 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
228 {
229         int len = 0;
230         int real_size = size;
231
232         if (!res || !pattern || !str1) {
233                 MS_DBG_ERR("parameta is invalid");
234                 return MS_MEDIA_ERR_INVALID_PARAMETER;
235         }
236
237         if (real_size < (int)(strlen(str1))) {
238                 MS_DBG_ERR("size is wrong");
239                 return MS_MEDIA_ERR_INVALID_PARAMETER;
240         }
241
242         len = snprintf(res, real_size, pattern, str1);
243         if (len < 0) {
244                 MS_DBG_ERR("snprintf failed");
245                 return MS_MEDIA_ERR_INVALID_PARAMETER;
246         }
247
248         res[len] = '\0';
249
250         return MS_MEDIA_ERR_NONE;
251 }
252
253 bool ms_config_get_int(const char *key, int *value)
254 {
255         int err;
256
257         if (!key || !value) {
258                 MS_DBG_ERR("Arguments key or value is NULL");
259                 return false;
260         }
261
262         err = vconf_get_int(key, value);
263         if (err == 0)
264                 return true;
265         else if (err == -1)
266                 return false;
267         else
268                 MS_DBG_ERR("Unexpected error code: %d", err);
269
270         return false;
271 }
272
273 bool ms_config_set_int(const char *key, int value)
274 {
275         int err;
276
277         if (!key) {
278                 MS_DBG_ERR("Arguments key is NULL");
279                 return false;
280         }
281
282         err = vconf_set_int(key, value);
283         if (err == 0)
284                 return true;
285         else if (err == -1)
286                 return false;
287         else
288                 MS_DBG_ERR("Unexpected error code: %d", err);
289
290         return false;
291 }
292
293 bool ms_config_get_str(const char *key, char **value)
294 {
295         char *res = NULL;
296
297         if (key == NULL || value == NULL) {
298                 MS_DBG_ERR("Arguments key or value is NULL");
299                 return false;
300         }
301
302         res = vconf_get_str(key);
303         if (MS_STRING_VALID(res)) {
304                 *value = strdup(res);
305                 MS_SAFE_FREE(res);
306                 return true;
307         }
308
309         return false;
310 }
311
312 bool ms_config_set_str(const char *key, const char *value)
313 {
314         int err;
315
316         if (!key || !value) {
317                 MS_DBG_ERR("Arguments key or value is NULL");
318                 return false;
319         }
320
321         err = vconf_set_str(key, value);
322         if (err == 0)
323                 return true;
324         else
325                 MS_DBG_ERR("fail to vconf_set_str %d", err);
326
327         return false;
328 }
329
330 bool ms_config_get_bool(const char *key, int *value)
331 {
332         int err;
333
334         if (!key || !value) {
335                 MS_DBG_ERR("Arguments key or value is NULL");
336                 return false;
337         }
338
339         err = vconf_get_bool(key, value);
340         if (err == 0)
341                 return true;
342         else if (err == -1)
343                 return false;
344         else
345                 MS_DBG_ERR("Unexpected error code: %d", err);
346
347         return false;
348 }
349
350 static int get_memory_size(const char *path, struct statvfs_32 *buf)
351 {
352         struct statvfs s;
353         int ret;
354
355         ret = statvfs(path, &s);
356         if (ret) {
357                 MS_DBG_ERR("statvfs failed[%d]", ret);
358                 MS_DBG_STRERROR();
359                 return MS_MEDIA_ERR_INTERNAL;
360         }
361
362         buf->f_bsize  = s.f_bsize;
363         buf->f_frsize = s.f_frsize;
364         buf->f_blocks = (unsigned long)s.f_blocks;
365         buf->f_bfree  = (unsigned long)s.f_bfree;
366         buf->f_bavail = (unsigned long)s.f_bavail;
367         buf->f_files  = (unsigned long)s.f_files;
368         buf->f_ffree  = (unsigned long)s.f_ffree;
369         buf->f_favail = (unsigned long)s.f_favail;
370         buf->f_fsid = s.f_fsid;
371         buf->f_flag = s.f_flag;
372         buf->f_namemax = s.f_namemax;
373
374         return MS_MEDIA_ERR_NONE;
375 }
376
377 int ms_get_remain_space(double *free_space)
378 {
379         int ret = MS_MEDIA_ERR_NONE;
380         struct statvfs_32 temp;
381
382         ret = get_memory_size("/opt", &temp);
383         if (ret != MS_MEDIA_ERR_NONE) {
384                 MS_DBG_ERR("fail to get memory size");
385                 return ret;
386         }
387
388 //      MS_DBG_ERR("Total mem : %lf, Avail mem : %lf", (double)temp.f_frsize*temp.f_blocks, (double)temp.f_bsize*temp.f_bavail);
389
390         *free_space = (double)temp.f_bsize*temp.f_bavail;
391
392         return ret;
393 }
394
395 #ifdef _USE_RECORDED_CONTENT
396 bool ms_is_support_pvr(void)
397 {
398
399         int nSupportPVR = 0;
400         if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
401                 MS_DBG_ERR("Get PVR Support failed");
402                 return false;
403         }
404
405         MS_DBG("PVR Support : [%d]", nSupportPVR);
406
407         return (nSupportPVR != 0);
408 }
409 #endif
410
411 #ifdef _USE_SENIOR_MODE
412 bool ms_is_support_senior_mode()
413 {
414         bool bSupportSeniorMode = false;
415
416         if(system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
417                 MS_DBG_ERR("Get senior mode support failed");
418                 return false;
419         }
420
421         MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
422
423         return bSupportSeniorMode;
424 }
425 #endif
426
427 int ms_check_file_path(const char *file_path, uid_t uid)
428 {
429         int exist;
430         struct stat file_st;
431
432         /* check location of file */
433         /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
434         if (!ms_is_valid_path(file_path, uid)) {
435                 MS_DBG_ERR("Invalid path : %s", file_path);
436                 return MS_MEDIA_ERR_INVALID_PATH;
437         }
438
439         /* check the file exits actually */
440         exist = open(file_path, O_RDONLY);
441         if (exist < 0) {
442                 MS_DBG_ERR("[%s]open files");
443                 return MS_MEDIA_ERR_INVALID_PATH;
444         }
445         close(exist);
446
447         /* check type of the path */
448         /* It must be a regular file */
449         memset(&file_st, 0, sizeof(struct stat));
450         if (stat(file_path, &file_st) == 0) {
451                 if (!S_ISREG(file_st.st_mode)) {
452                         /* In this case, it is not a regula file */
453                         MS_DBG_ERR("this path is not a file");
454                         return MS_MEDIA_ERR_INVALID_PATH;
455                 }
456         } else {
457                 MS_DBG_STRERROR("stat failed");
458                 return MS_MEDIA_ERR_INVALID_PATH;
459         }
460
461         return MS_MEDIA_ERR_NONE;
462 }
463
464 int ms_check_ignore_dir(const char *full_path, uid_t uid)
465 {
466         int ret = MS_MEDIA_ERR_NONE;
467         char *dir_path = NULL;
468         char *leaf_path = NULL;
469         char *usr_path = NULL;
470
471         ret = ms_check_file_path(full_path, uid);
472         if (ret != MS_MEDIA_ERR_NONE) {
473                 MS_DBG_ERR("invalid path : %s", full_path);
474                 return MS_MEDIA_ERR_INVALID_PATH;
475         }
476
477         dir_path = g_path_get_dirname(full_path);
478         if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
479                 MS_DBG_ERR("getting directory path is failed : %s", full_path);
480                 MS_SAFE_FREE(dir_path);
481                 return MS_MEDIA_ERR_INVALID_PATH;
482         }
483
484         usr_path = ms_get_path(uid);
485         if (usr_path == NULL) {
486                 MS_DBG_ERR("ms_get_path() fail");
487                 MS_SAFE_FREE(dir_path);
488                 return MS_MEDIA_ERR_INTERNAL;
489         }
490
491         while (1) {
492                 if (ms_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
493                         ret = MS_MEDIA_ERR_INVALID_PATH;
494                         break;
495                 }
496
497 #ifdef _USE_SENIOR_MODE
498                 if(ms_is_support_senior_mode()) {
499                         if(strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
500                                 break;
501                 }
502 #endif
503                 if (strcmp(dir_path, usr_path) == 0)
504                         break;
505                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
506                         break;
507                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
508                         break;
509
510                 leaf_path = strrchr(dir_path, '/');
511                 if (leaf_path != NULL) {
512                                 int seek_len = leaf_path -dir_path;
513                                 dir_path[seek_len] = '\0';
514                 } else {
515                         MS_DBG_ERR("Fail to find leaf path");
516                         ret = MS_MEDIA_ERR_INVALID_PATH;
517                         break;
518                 }
519         }
520
521         MS_SAFE_FREE(dir_path);
522         MS_SAFE_FREE(usr_path);
523
524         return ret;
525 }
526
527 int ms_check_scan_ignore(char * path)
528 {
529         int fd = -1;
530         int exist = -1;
531         const char *ignore_path = "/.scan_ignore";
532         char *check_ignore_file = NULL;
533         int ret = MS_MEDIA_ERR_NONE;
534
535         if (strstr(path, "/.")) {
536                 MS_DBG_ERR("hidden path");
537                 ret = MS_MEDIA_ERR_INVALID_PATH;
538                 goto ERROR;
539         }
540
541         fd = open(path, O_RDONLY | O_DIRECTORY);
542         if (fd == -1) {
543                 MS_DBG_ERR("%s folder opendir fails", path);
544                 ret = MS_MEDIA_ERR_INVALID_PATH;
545
546                 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
547                         MS_DBG_ERR("Fail to get USB path");
548                         goto ERROR;
549                 }
550
551                 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
552                         if (errno == ENOENT) {
553                                 /*if the directory does not exist, check the device is unmounted*/
554                                 if (!ms_storage_mount_status(path)) {
555                                         MS_DBG_ERR("Device is unmounted[%s]", path);
556                                         ret = MS_MEDIA_ERR_USB_UNMOUNTED;
557                                         goto ERROR;
558                                 }
559                         }
560                 }
561
562                 struct stat folder_st;
563                 if (stat(path, &folder_st) == 0) {
564                         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,
565                                 (long)folder_st.st_uid, (long)folder_st.st_gid, (unsigned long) folder_st.st_mode, path);
566                 } else {
567                         MS_DBG_ERR("%s folder stat fails", path);
568                 }
569
570                 goto ERROR;
571         } else {
572                 /* check the file exits actually */
573                 int path_len = 0;
574
575                 path_len = strlen(path) + strlen(ignore_path) + 1;
576                 check_ignore_file = malloc(path_len);
577                 if (check_ignore_file != NULL) {
578                         memset(check_ignore_file, 0x0, path_len);
579                         snprintf(check_ignore_file, path_len, "%s%s", path, ignore_path);
580
581                         exist = open(check_ignore_file, O_RDONLY);
582                         if (exist >= 0) {
583                                 MS_DBG_ERR("scan_ignore exists [%s]", check_ignore_file);
584                                 ret = MS_MEDIA_ERR_INVALID_PATH;
585                         }
586
587                         MS_SAFE_FREE(check_ignore_file);
588                 } else {
589                         MS_DBG_ERR("malloc failed");
590                         ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
591                 }
592         }
593
594 ERROR:
595
596         if (fd != -1) {
597                 close(fd);
598                 fd = -1;
599         }
600
601         if (exist >= 0) close(exist);
602
603         return ret;
604 }
605
606 char* ms_get_path(uid_t uid)
607 {
608         int len = 0;
609         char *result_passwd = NULL;
610         int ret = -1;
611         char* grpbuf;
612         struct group grpinfo;
613         struct group* grpresult = NULL;
614         size_t grpbufsize;
615
616         grpbufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
617         if (grpbufsize == -1)           /* Value was indeterminate */
618                 grpbufsize = 16384;             /* Should be more than enough (16*1024) */
619
620         grpbuf = malloc(grpbufsize);
621         if (grpbuf == NULL) {
622                 MS_DBG_ERR("malloc grpbuf grpbufsize[%d] failed", grpbufsize);
623                 return NULL;
624         }
625
626         ret = getgrnam_r("users", &grpinfo, grpbuf, grpbufsize, &grpresult);
627         if((ret == 0) && (grpresult != NULL)) {
628                 MS_DBG("getgrnam_r users success...\n");
629         } else {
630                 MS_DBG_ERR("getgrnam_r users failed ret[%d]", ret);
631                 goto END;
632         }
633
634         if (uid == getuid()) {
635                 if (MS_STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
636                         result_passwd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
637         } else {
638                 char passwd_str[MAX_FILEPATH_LEN] = {0, };
639                 struct passwd pwdinfo;
640                 struct passwd* pwdresult = NULL;
641                 char* pwdbuf;
642                 size_t pwdbufsize;
643
644                 pwdbufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
645                 if (pwdbufsize == -1)           /* Value was indeterminate */
646                         pwdbufsize = 16384;             /* Should be more than enough (16*1024) */
647
648                 pwdbuf = malloc(pwdbufsize);
649                 if (pwdbuf == NULL) {
650                         MS_DBG_ERR("malloc pwdbuf pwdbufsize[%d] failed", pwdbufsize);
651                         goto END;
652                 }
653
654                 ret = getpwuid_r(uid, &pwdinfo, pwdbuf, pwdbufsize, &pwdresult);
655                 if((ret == 0) && (pwdresult != NULL)) {
656                         MS_DBG("getpwuid uid[%d] success\n", uid);
657                 } else {
658                         MS_DBG_ERR("getpwuid uid[%d] failed ret[%d]", uid, ret);
659                         MS_SAFE_FREE(pwdbuf);
660                         goto END;
661                 }
662
663                 // Compare git_t type and not group name
664                 if (grpinfo.gr_gid != pwdinfo.pw_gid) {
665                         MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
666                         MS_SAFE_FREE(pwdbuf);
667                         goto END;
668                 }
669
670                 len = snprintf(passwd_str, sizeof(passwd_str), "%s/%s", pwdinfo.pw_dir, MEDIA_CONTENT_PATH);
671                 if (len > 0)
672                         result_passwd = strndup(passwd_str, len);
673
674                 MS_SAFE_FREE(pwdbuf);
675         }
676
677 END:
678         MS_SAFE_FREE(grpbuf);
679
680         return result_passwd;
681 }
682
683 bool ms_storage_mount_status(const char* start_path)
684 {
685         bool ret = false;
686 #ifndef _USE_DEVICED_DBUS
687         int count = 0;
688         int err = 0;
689         usb_device_list_h list;
690         usb_device_h device;
691         char *mount_path = NULL;
692
693         char *storage_path = NULL;
694         char *remain_path = NULL;
695         int remain_len = 0;
696
697         remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
698         if (remain_path != NULL)
699                 remain_len = strlen(remain_path);
700
701         storage_path = strndup(start_path, strlen(start_path) - remain_len);
702
703         MS_DBG_ERR("storage_path [%s]", storage_path);
704
705         err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
706         if (err == 0) {
707                 count = usb_device_list_get_count(list);
708                 if (count > 0) {
709                         err = usb_device_list_get_first(list, &device);
710                         if(err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
711                                 mount_path = usb_device_get_mountpath(device);
712                                 if (mount_path != NULL) {
713                                         MS_DBG_ERR("mount_path [%s]", mount_path);
714                                         if (strlen(mount_path) == strlen(storage_path)) {
715                                                 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
716                                                         MS_DBG_ERR("start path is mounted [%s]", start_path);
717                                                         ret = true;
718                                                 }
719                                         }
720                                 }
721                         }
722
723                         if (ret != true) {
724                                 while(usb_device_list_get_next(list, &device) == 0) {
725                                         if(device != NULL) {
726                                                 mount_path = usb_device_get_mountpath(device);
727                                                 if (mount_path != NULL) {
728                                                         MS_DBG_ERR("mount_path [%s]", mount_path);
729                                                         if (strlen(mount_path) == strlen(storage_path)) {
730                                                                 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
731                                                                         MS_DBG_ERR("start path is mounted [%s]", start_path);
732                                                                         ret = true;
733                                                                         break;
734                                                                 }
735                                                         }
736                                                 }
737                                         }
738                                 }
739                         }
740                 }
741
742                 usb_device_free_device_list(list);
743         } else {
744                 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
745         }
746
747         MS_SAFE_FREE(storage_path);
748 #endif
749         return ret;
750 }
751
752 bool ms_is_valid_path(const char *path, uid_t uid)
753 {
754         bool ret = false;
755         char *usr_path = NULL;
756
757         if (path == NULL)
758                 return false;
759
760         usr_path = ms_get_path(uid);
761         if (usr_path == NULL)
762                 return false;
763
764 #ifdef _USE_SENIOR_MODE
765         if(ms_is_support_senior_mode()) {
766                 if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0)
767                         return true;
768         }
769 #endif
770
771         if (strncmp(path, usr_path, strlen(usr_path)) == 0)
772                 ret = true;
773         else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
774                 ret = true;
775         else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
776                 ret = true;
777         else
778                 ret = false;
779
780         MS_SAFE_FREE(usr_path);
781
782         return ret;
783 }
784
785 int ms_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
786 {
787         int res = MS_MEDIA_ERR_NONE;
788         int err = 0;
789
790         if (status == MS_DB_UPDATING) {
791                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
792                         res = MS_MEDIA_ERR_VCONF_SET_FAIL;
793                         MS_DBG_ERR("ms_config_set_int failed");
794                 }
795
796                 if (storage_type == MS_STORAGE_EXTERNAL) {
797                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
798                                 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
799                                 MS_DBG_ERR("ms_config_set_int failed");
800                         }
801                 }
802         } else {
803                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
804                         res = MS_MEDIA_ERR_VCONF_SET_FAIL;
805                         MS_DBG_ERR("ms_config_set_int failed");
806                 }
807
808                 if (storage_type == MS_STORAGE_EXTERNAL) {
809                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
810                                 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
811                                 MS_DBG_ERR("ms_config_set_int failed");
812                         }
813                 }
814         }
815
816         err = ms_set_power_mode(status);
817         if (err != MS_MEDIA_ERR_NONE) {
818                 MS_DBG_ERR("ms_set_power_mode fail");
819                 res = err;
820         }
821
822         return res;
823 }
824
825 int ms_set_power_mode(ms_db_status_type_t status)
826 {
827         int res = MS_MEDIA_ERR_NONE;
828         int err;
829
830         switch (status) {
831         case MS_DB_UPDATING:
832                 err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
833                 if (err != 0)
834                         res = MS_MEDIA_ERR_INTERNAL;
835                 break;
836         case MS_DB_UPDATED:
837                 err = display_unlock_state(LCD_OFF, PM_RESET_TIMER);
838                 if (err != 0)
839                         res = MS_MEDIA_ERR_INTERNAL;
840                 break;
841         default:
842                 MS_DBG_ERR("Unacceptable type : %d", status);
843                 break;
844         }
845
846         return res;
847 }
848
849 void ms_trim_dir_path(char *dir_path)
850 {
851         /* need implementation */
852         /* if dir_path is not NULL terminated, this function will occure crash */
853         int len = strlen(dir_path);
854
855         if (dir_path[len -1] == '/')
856                 dir_path[len -1] = '\0';
857 }
858
859 bool ms_check_folder_path(const char *folder_path)
860 {
861         DIR *dp = NULL;
862
863         dp = opendir(folder_path);
864         if (dp == NULL) {
865                 MS_DBG_ERR("Deleted folder path");
866                 return false;
867         }
868         closedir(dp);
869
870         return true;
871 }
872
873 int ms_check_size_mediadb(uid_t uid, double *db_size)
874 {
875         int ret = MS_MEDIA_ERR_NONE;
876         char *db_path = NULL;
877         struct stat buf;
878
879         ret = media_db_get_media_db_path(uid, &db_path);
880
881         if(stat(db_path, &buf) == 0) {
882                 *db_size = buf.st_size;
883         } else {
884                 MS_DBG_STRERROR("stat failed");
885                 ret = MS_MEDIA_ERR_INTERNAL;
886         }
887
888         MS_SAFE_FREE(db_path);
889
890         return ret;
891 }
892
893 #ifdef _SET_VIP_PROCESS
894 #define PROC_OOM_SCORE_ADJ_PATH         "/proc/%d/oom_score_adj"
895 #define VIP_OOM_SCORE_ADJ                       (-1000)
896 #define PROC_NAME_MAX 1024
897 #define PROC_BUF_MAX 64
898
899 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
900 {
901         char buf[PROC_BUF_MAX];
902         char cmdline_buf[PROC_NAME_MAX];
903         char *filename;
904         FILE *fp;
905
906         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
907         fp = fopen(buf, "r");
908         if (fp == NULL)
909                 return MS_MEDIA_ERR_INTERNAL;
910
911         if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
912                 fclose(fp);
913                 return MS_MEDIA_ERR_INTERNAL;
914         }
915         fclose(fp);
916
917         filename = strrchr(cmdline_buf, '/');
918         if (filename == NULL)
919                 filename = cmdline_buf;
920         else
921                 filename = filename + 1;
922
923         strncpy(cmdline, filename, PROC_NAME_MAX-1);
924
925         return MS_MEDIA_ERR_NONE;
926 }
927
928 int ms_set_vip_process(void)
929 {
930         char buf[100] = {0};
931         int id = 0;
932         static pid_t pid = 0;
933         static char process_name[PROC_NAME_MAX] = {0};
934         static char *appid = NULL;
935
936         /* Get Pid */
937         pid = getpid();
938         if (ms_get_cmdline_from_proc(pid, process_name)) {
939                 MS_DBG_ERR("%s: Read process name failed pid[%d]\n", __func__, pid);
940                 return MS_MEDIA_ERR_INTERNAL;
941         }
942         appid = process_name;
943
944         MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
945
946         if (prctl(PR_GET_DUMPABLE) == 0)
947                 prctl(PR_SET_DUMPABLE, 1);
948
949         snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
950         id = open(buf, O_WRONLY, 0777);
951         if (id < 0) {
952                 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
953                 return MS_MEDIA_ERR_INTERNAL;
954         }
955         snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
956         if (write(id, buf, strlen(buf)) < 0) {
957                 MS_DBG_ERR("write() failed errno=%d", errno);
958                 close(id);
959                 return MS_MEDIA_ERR_INTERNAL;
960         }
961         close(id);
962         return MS_MEDIA_ERR_NONE;
963 }
964 #endif