1 /*
2 * Copyright (C) 2012 The Android Open Source Project
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 <cutils/multiuser.h>
18 #include <private/android_filesystem_config.h>
19
multiuser_get_user_id(uid_t uid)20 userid_t multiuser_get_user_id(uid_t uid) {
21 return uid / AID_USER_OFFSET;
22 }
23
multiuser_get_app_id(uid_t uid)24 appid_t multiuser_get_app_id(uid_t uid) {
25 return uid % AID_USER_OFFSET;
26 }
27
multiuser_get_uid(userid_t user_id,appid_t app_id)28 uid_t multiuser_get_uid(userid_t user_id, appid_t app_id) {
29 return (user_id * AID_USER_OFFSET) + (app_id % AID_USER_OFFSET);
30 }
31
multiuser_get_sdk_sandbox_uid(userid_t user_id,appid_t app_id)32 uid_t multiuser_get_sdk_sandbox_uid(userid_t user_id, appid_t app_id) {
33 int sdk_sandbox_offset = AID_SDK_SANDBOX_PROCESS_START - AID_APP_START;
34 if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
35 return (user_id * AID_USER_OFFSET) + (app_id % AID_USER_OFFSET) + sdk_sandbox_offset;
36 } else {
37 return -1;
38 }
39 }
40
multiuser_convert_sdk_sandbox_to_app_uid(uid_t uid)41 uid_t multiuser_convert_sdk_sandbox_to_app_uid(uid_t uid) {
42 appid_t app_id = multiuser_get_app_id(uid);
43 int sdk_sandbox_offset = AID_SDK_SANDBOX_PROCESS_START - AID_APP_START;
44 if (app_id >= AID_SDK_SANDBOX_PROCESS_START && app_id <= AID_SDK_SANDBOX_PROCESS_END) {
45 return uid - sdk_sandbox_offset;
46 } else {
47 return -1;
48 }
49 }
50
multiuser_get_cache_gid(userid_t user_id,appid_t app_id)51 gid_t multiuser_get_cache_gid(userid_t user_id, appid_t app_id) {
52 if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
53 return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_CACHE_GID_START);
54 } else {
55 return -1;
56 }
57 }
58
multiuser_get_ext_gid(userid_t user_id,appid_t app_id)59 gid_t multiuser_get_ext_gid(userid_t user_id, appid_t app_id) {
60 if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
61 return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_EXT_GID_START);
62 } else {
63 return -1;
64 }
65 }
66
multiuser_get_ext_cache_gid(userid_t user_id,appid_t app_id)67 gid_t multiuser_get_ext_cache_gid(userid_t user_id, appid_t app_id) {
68 if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
69 return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_EXT_CACHE_GID_START);
70 } else {
71 return -1;
72 }
73 }
74
multiuser_get_shared_gid(userid_t,appid_t app_id)75 gid_t multiuser_get_shared_gid(userid_t, appid_t app_id) {
76 if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
77 return (app_id - AID_APP_START) + AID_SHARED_GID_START;
78 } else if (app_id >= AID_ROOT && app_id <= AID_APP_START) {
79 return app_id;
80 } else {
81 return -1;
82 }
83 }
84
multiuser_get_shared_app_gid(uid_t uid)85 gid_t multiuser_get_shared_app_gid(uid_t uid) {
86 return multiuser_get_shared_gid(multiuser_get_user_id(uid), multiuser_get_app_id(uid));
87 }
88