Release version 0.5.14
[platform/core/appfw/librua.git] / src / rua_util.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <linux/limits.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <grp.h>
23 #include <pwd.h>
24
25 #include <sys/stat.h>
26 #include <sqlite3.h>
27 #include <tzplatform_config.h>
28 #include <dlog.h>
29
30 #include "rua_util.h"
31 #include "rua_private.h"
32
33 #define BASE_UID 5000
34
35 char *_rua_util_get_db_path(uid_t uid, char *db_name)
36 {
37         char db_path[PATH_MAX];
38         const char *db_path_prefix;
39
40         if (strcmp(db_name, RUA_DB_NAME) == 0) {
41                 snprintf(db_path, sizeof(db_path), "/run/aul/dbspace/%d/%s",
42                         uid, db_name);
43         } else {
44                 tzplatform_set_user(uid);
45                 db_path_prefix = tzplatform_getenv(TZ_USER_DB);
46                 tzplatform_reset_user();
47                 snprintf(db_path, sizeof(db_path), "%s/%s", db_path_prefix, db_name);
48         }
49         LOGD("db path %s", db_path);
50         return strdup(db_path);
51 }
52
53 #define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
54 #define BUSY_WAITING_MAX 20 /* wait for max 1 sec */
55 static int __db_busy_handler(void *data, int count)
56 {
57         /* sqlite3_prepare_V2 will return SQLITE_BUSY */
58         if (count >= BUSY_WAITING_MAX)
59                 return 0;
60
61         usleep(BUSY_WAITING_USEC);
62
63         return 1;
64 }
65
66 int _rua_util_open_db(sqlite3 **db, int flags, uid_t uid, char *db_name)
67 {
68         int r;
69         char *db_path;
70
71         db_path = _rua_util_get_db_path(uid, db_name);
72         if (db_path == NULL) {
73                 LOGE("out of memory _rua_util_get_db_path fail");
74                 return -1;
75         }
76
77         r = sqlite3_open_v2(db_path, db, flags, NULL);
78         free(db_path);
79         if (r != SQLITE_OK) {
80                 LOGE("open db failed: %d", r);
81                 return -1;
82         }
83
84         r = sqlite3_busy_handler(*db, __db_busy_handler, NULL);
85         if (r != SQLITE_OK) {
86                 LOGE("register busy handler failed: %s", sqlite3_errmsg(*db));
87                 sqlite3_close_v2(*db);
88                 return -1;
89         }
90
91         return 0;
92 }
93
94 int _rua_util_check_uid(uid_t target_uid)
95 {
96         uid_t uid = getuid();
97
98         if (uid > BASE_UID && uid != target_uid) {
99                 LOGE("Invalid UID : %d, target UID : %d", uid, target_uid);
100                 return -1;
101         }
102         return 0;
103 }