Flags cleanup
[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 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <vconf.h>
25 #include <sys/statvfs.h>
26 #include <sys/stat.h>
27
28 #ifdef _USE_TVPD_MODE
29 #include <sys/prctl.h>
30 #endif
31 #include <system_info.h>
32 #include <device/power.h>
33
34 #ifndef _USE_DEVICED_DBUS
35 #include <usb-device.h>
36 #endif
37
38 #include "media-util.h"
39 #include "media-server-ipc.h"
40 #include "media-common-dbg.h"
41 #include "media-common-system.h"
42 #include "media-common-utils.h"
43
44 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
45 {
46         int len = 0;
47         int real_size = size - 1;
48
49         if (!res || !pattern || !str1 || !str2)
50                 return MS_MEDIA_ERR_INVALID_PARAMETER;
51
52         if (real_size < (int)(strlen(str1) + strlen(str2)))
53                 return MS_MEDIA_ERR_INVALID_PARAMETER;
54
55         len = snprintf(res, real_size, pattern, str1, str2);
56         if (len < 0)
57                 return MS_MEDIA_ERR_INVALID_PARAMETER;
58
59         res[len] = '\0';
60
61         return MS_MEDIA_ERR_NONE;
62 }
63
64 bool ms_config_get_int(const char *key, int *value)
65 {
66         int err;
67
68         if (!key || !value) {
69                 MS_DBG_ERR("Arguments key or value is NULL");
70                 return false;
71         }
72
73         err = vconf_get_int(key, value);
74         if (err == VCONF_OK)
75                 return true;
76
77         MS_DBG_ERR("Error code: %d", err);
78
79         return false;
80 }
81
82 bool ms_config_set_int(const char *key, int value)
83 {
84         int err;
85
86         if (!key) {
87                 MS_DBG_ERR("Arguments key is NULL");
88                 return false;
89         }
90
91         err = vconf_set_int(key, value);
92         if (err == VCONF_OK)
93                 return true;
94
95         MS_DBG_ERR("Error code: %d", err);
96
97         return false;
98 }
99
100 bool ms_config_get_str(const char *key, char **value)
101 {
102         char *res = NULL;
103
104         if (key == NULL || value == NULL) {
105                 MS_DBG_ERR("Arguments key or value is NULL");
106                 return false;
107         }
108
109         res = vconf_get_str(key);
110         if (MS_STRING_VALID(res)) {
111                 *value = g_strdup(res);
112                 MS_SAFE_FREE(res);
113                 return true;
114         }
115
116         return false;
117 }
118
119 int ms_get_remain_space(double *free_space)
120 {
121         int ret = MS_MEDIA_ERR_NONE;
122         const char *path = "/opt";
123         struct statvfs s;
124
125         ret = statvfs(path, &s);
126         if (ret != 0) {
127                 MS_DBG_ERR("statvfs failed[%d]", ret);
128                 MS_DBG_STRERROR();
129                 return MS_MEDIA_ERR_INTERNAL;
130         }
131
132         /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
133         *free_space = (double)(s.f_bsize * s.f_bavail);
134
135         return MS_MEDIA_ERR_NONE;
136 }
137
138 #ifdef _USE_TVPD_MODE
139 bool ms_is_support_pvr(void)
140 {
141         bool bSupportPVR = false;
142         if (system_info_get_custom_bool("com.samsung/featureconf/pvr.pvr_support", &bSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
143                 MS_DBG_ERR("Get PVR Support failed");
144                 return false;
145         }
146
147         MS_DBG("PVR Support : [%d]", bSupportPVR);
148
149         return bSupportPVR;
150 }
151 #endif
152
153 int ms_check_file_path(const char *file_path, uid_t uid)
154 {
155         ms_user_storage_type_e storage_type = -1;
156         int ret = MS_MEDIA_ERR_NONE;
157
158         if (!MS_STRING_VALID(file_path)) {
159                 MS_DBG_ERR("Invalid path");
160                 return MS_MEDIA_ERR_INVALID_PARAMETER;
161         }
162
163         ret = ms_user_get_storage_type(uid, file_path, &storage_type);
164         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
165
166         if (!g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
167                 MS_DBG_SERR("g_file_test fail [%s]", file_path);
168                 return MS_MEDIA_ERR_INVALID_PARAMETER;
169         }
170
171         return MS_MEDIA_ERR_NONE;
172 }
173
174 int ms_check_ignore_dir(const char *full_path, uid_t uid)
175 {
176         int ret = MS_MEDIA_ERR_NONE;
177         char *dir_path = NULL;
178         char *leaf_path = NULL;
179         char *usr_path = NULL;
180
181         ret = ms_check_file_path(full_path, uid);
182         if (ret != MS_MEDIA_ERR_NONE) {
183                 MS_DBG_ERR("invalid path : %s", full_path);
184                 return MS_MEDIA_ERR_INVALID_PARAMETER;
185         }
186
187         dir_path = g_path_get_dirname(full_path);
188         if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
189                 MS_DBG_ERR("getting directory path is failed : %s", full_path);
190                 g_free(dir_path);
191                 return MS_MEDIA_ERR_INVALID_PARAMETER;
192         }
193
194         ret = ms_user_get_internal_root_path(uid, &usr_path);
195         if (ret != MS_MEDIA_ERR_NONE) {
196                 MS_DBG_ERR("ms_user_get_internal_root_path() fail");
197                 g_free(dir_path);
198                 return MS_MEDIA_ERR_INTERNAL;
199         }
200
201         while (1) {
202                 if (ms_check_scan_ignore(dir_path, uid) != MS_MEDIA_ERR_NONE) {
203                         ret = MS_MEDIA_ERR_INVALID_PARAMETER;
204                         break;
205                 }
206
207                 if (strcmp(dir_path, usr_path) == 0)
208                         break;
209                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
210                         break;
211                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
212                         break;
213
214                 leaf_path = strrchr(dir_path, '/');
215                 if (leaf_path != NULL) {
216                                 int seek_len = leaf_path -dir_path;
217                                 dir_path[seek_len] = '\0';
218                 } else {
219                         MS_DBG_ERR("Fail to find leaf path");
220                         ret = MS_MEDIA_ERR_INVALID_PARAMETER;
221                         break;
222                 }
223         }
224
225         g_free(dir_path);
226         g_free(usr_path);
227
228         return ret;
229 }
230
231 static void __ms_trim_path(const char *input_path, char **output_path)
232 {
233         char buf[4096] = {0, };
234         char tmp[4096] = {0, };
235         char *pos = NULL;
236
237         SAFE_STRLCPY(buf, input_path, sizeof(buf));
238
239         while ((pos = strstr(buf, "//")) != NULL) {
240                 memset(tmp, 0, sizeof(tmp));
241                 SAFE_STRLCPY(tmp, buf, pos - buf + 1);
242                 SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
243
244                 memset(buf, 0, sizeof(buf));
245                 SAFE_STRLCPY(buf, tmp, sizeof(buf));
246         }
247
248         if (g_str_has_suffix(buf, "/"))
249                 *output_path = g_strndup(buf, strlen(buf) - 1);
250         else
251                 *output_path = g_strdup(buf);
252 }
253
254 int ms_check_scan_ignore(char * path, uid_t uid)
255 {
256         int ret = MS_MEDIA_ERR_NONE;
257         const char *ignore_file = ".scan_ignore";
258         char *tmp_path = NULL;
259         char *org_path = NULL;
260         char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
261
262 #ifndef _USE_TVPD_MODE
263         char replace[MS_FILE_PATH_LEN_MAX] = {0, };
264         char *mediashared = NULL;
265 #endif
266
267         /* Check for symbolic link */
268         tmp_path = realpath(path, NULL);
269         /* Get trimmed path */
270         __ms_trim_path(path, &org_path);
271
272 #ifdef _USE_TVPD_MODE
273         if (g_strcmp0(tmp_path, org_path) != 0) {
274                 MS_SAFE_FREE(tmp_path);
275                 g_free(org_path);
276                 MS_DBG_ERR("symbolic link(directory)");
277                 return MS_MEDIA_ERR_INVALID_PARAMETER;
278         }
279 #else
280         if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
281                 ms_user_get_mediashared_path(uid, &mediashared);
282                 snprintf(replace, MS_FILE_PATH_LEN_MAX, "%s%s", mediashared, tmp_path + strlen(MEDIA_SHARE_PATH));
283                 MS_SAFE_FREE(mediashared);
284                 if (g_strcmp0(replace, org_path) != 0) {
285                         MS_SAFE_FREE(tmp_path);
286                         g_free(org_path);
287                         MS_DBG_ERR("symbolic link(directory)");
288                         return MS_MEDIA_ERR_INVALID_PARAMETER;
289                 }
290         } else {
291                 if (g_strcmp0(tmp_path, org_path) != 0) {
292                         MS_SAFE_FREE(tmp_path);
293                         g_free(org_path);
294                         MS_DBG_ERR("symbolic link(directory)");
295                         return MS_MEDIA_ERR_INVALID_PARAMETER;
296                 }
297         }
298 #endif
299         MS_SAFE_FREE(tmp_path);
300         g_free(org_path);
301
302         if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
303                 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
304
305                 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
306                         MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
307                         return MS_MEDIA_ERR_INVALID_PARAMETER;
308                 }
309         } else {
310                 MS_DBG_ERR("g_file_test fails[%s]", path);
311                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
312
313                 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
314                         MS_DBG_ERR("Fail to get USB path");
315                         return ret;
316                 }
317
318                 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
319                         /*if the directory does not exist, check the device is unmounted*/
320                         if (!ms_storage_mount_status(path)) {
321                                 MS_DBG_ERR("Device is unmounted[%s]", path);
322                                 return MS_MEDIA_ERR_USB_UNMOUNTED;
323                         }
324                 }
325         }
326
327         return ret;
328 }
329
330 bool ms_storage_mount_status(const char* start_path)
331 {
332         bool ret = false;
333 #ifndef _USE_DEVICED_DBUS
334         int count = 0;
335         int err = 0;
336         usb_device_list_h list;
337         usb_device_h device;
338         char *mount_path = NULL;
339
340         char *storage_path = NULL;
341         char *remain_path = NULL;
342         int remain_len = 0;
343
344         remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
345         if (remain_path != NULL)
346                 remain_len = strlen(remain_path);
347
348         storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
349
350         MS_DBG_SWARN("storage_path [%s]", storage_path);
351
352         err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
353         if (err == 0) {
354                 count = usb_device_list_get_count(list);
355                 if (count > 0) {
356                         err = usb_device_list_get_first(list, &device);
357                         if (err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
358                                 mount_path = usb_device_get_mountpath(device);
359                                 if (mount_path != NULL) {
360                                         MS_DBG_SWARN("mount_path [%s]", mount_path);
361                                         if (strlen(mount_path) == strlen(storage_path)) {
362                                                 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
363                                                         MS_DBG_SWARN("start path is mounted [%s]", start_path);
364                                                         ret = true;
365                                                 }
366                                         }
367                                 }
368                         }
369
370                         if (ret != true) {
371                                 while (usb_device_list_get_next(list, &device) == 0) {
372                                         if (device != NULL) {
373                                                 mount_path = usb_device_get_mountpath(device);
374                                                 if (mount_path != NULL) {
375                                                         MS_DBG_SWARN("mount_path [%s]", mount_path);
376                                                         if (strlen(mount_path) == strlen(storage_path)) {
377                                                                 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
378                                                                         MS_DBG_SWARN("start path is mounted [%s]", start_path);
379                                                                         ret = true;
380                                                                         break;
381                                                                 }
382                                                         }
383                                                 }
384                                         }
385                                 }
386                         }
387                 }
388
389                 usb_device_free_device_list(list);
390         } else {
391                 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
392         }
393
394         g_free(storage_path);
395 #endif
396         return ret;
397 }
398
399 int ms_set_db_status(ms_db_status_type_t status, ms_user_storage_type_e storage_type)
400 {
401         int ret = MS_MEDIA_ERR_NONE;
402
403         if (status == MS_DB_UPDATING) {
404                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING))
405                                 goto ERROR;
406
407                 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
408                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING))
409                                 goto ERROR;
410                 }
411         } else {
412                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED))
413                                 goto ERROR;
414
415                 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
416                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED))
417                                 goto ERROR;
418                 }
419         }
420
421         ret = ms_set_power_mode(status);
422         if (ret != MS_MEDIA_ERR_NONE)
423                 MS_DBG_ERR("ms_set_power_mode fail");
424
425         return ret;
426 ERROR:
427         MS_DBG_ERR("ms_config_set_int failed");
428         return MS_MEDIA_ERR_INTERNAL;
429 }
430
431 int ms_set_power_mode(ms_db_status_type_t status)
432 {
433         int res = MS_MEDIA_ERR_NONE;
434         int err;
435
436         switch (status) {
437         case MS_DB_UPDATING:
438                 err = device_power_request_lock(POWER_LOCK_CPU, 0);
439                 if (err != 0)
440                         res = MS_MEDIA_ERR_INTERNAL;
441                 break;
442         case MS_DB_UPDATED:
443                 err = device_power_release_lock(POWER_LOCK_CPU);
444                 if (err != 0)
445                         res = MS_MEDIA_ERR_INTERNAL;
446                 break;
447         default:
448                 MS_DBG_ERR("Unacceptable type : %d", status);
449                 break;
450         }
451
452         return res;
453 }
454
455 void ms_trim_dir_path(char *dir_path)
456 {
457         /* need implementation */
458         /* if dir_path is not NULL terminated, this function will occure crash */
459         int len = strlen(dir_path);
460
461         if (dir_path[len -1] == '/')
462                 dir_path[len -1] = '\0';
463 }
464
465 int ms_check_size_mediadb(uid_t uid, double *db_size)
466 {
467         int ret = MS_MEDIA_ERR_NONE;
468         char *db_path = NULL;
469         struct stat buf;
470
471         ret = ms_user_get_media_db_path(uid, &db_path);
472
473         if (stat(db_path, &buf) == 0) {
474                 *db_size = buf.st_size;
475         } else {
476                 MS_DBG_STRERROR("stat failed");
477                 ret = MS_MEDIA_ERR_INTERNAL;
478         }
479
480         g_free(db_path);
481
482         return ret;
483 }
484
485 #ifdef _USE_TVPD_MODE
486 #define PROC_OOM_SCORE_ADJ_PATH         "/proc/%d/oom_score_adj"
487 #define VIP_OOM_SCORE_ADJ                       (-1000)
488 #define PROC_NAME_MAX 1024
489 #define PROC_BUF_MAX 64
490
491 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
492 {
493         char buf[PROC_BUF_MAX];
494         char cmdline_buf[PROC_NAME_MAX];
495         char *filename;
496         FILE *fp;
497
498         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
499         fp = fopen(buf, "r");
500         if (fp == NULL)
501                 return MS_MEDIA_ERR_INTERNAL;
502
503         if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
504                 fclose(fp);
505                 return MS_MEDIA_ERR_INTERNAL;
506         }
507         fclose(fp);
508
509         filename = strrchr(cmdline_buf, '/');
510         if (filename == NULL)
511                 filename = cmdline_buf;
512         else
513                 filename = filename + 1;
514
515         SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
516
517         return MS_MEDIA_ERR_NONE;
518 }
519
520 int ms_set_vip_process(void)
521 {
522         char buf[100] = {0};
523         int id = 0;
524         static pid_t pid = 0;
525         static char process_name[PROC_NAME_MAX] = {0};
526         static char *appid = NULL;
527
528         /* Get Pid */
529         pid = getpid();
530         if (ms_get_cmdline_from_proc(pid, process_name)) {
531                 MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
532                 return MS_MEDIA_ERR_INTERNAL;
533         }
534         appid = process_name;
535
536         MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
537
538         if (prctl(PR_GET_DUMPABLE) == 0)
539                 prctl(PR_SET_DUMPABLE, 1);
540
541         snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
542         id = open(buf, O_WRONLY, 0777);
543         if (id < 0) {
544                 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
545                 return MS_MEDIA_ERR_INTERNAL;
546         }
547         snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
548         if (write(id, buf, strlen(buf)) < 0) {
549                 MS_DBG_ERR("write() failed errno=%d", errno);
550                 close(id);
551                 return MS_MEDIA_ERR_INTERNAL;
552         }
553         close(id);
554         return MS_MEDIA_ERR_NONE;
555 }
556 #endif