blob: 979cbf43ba689de8b1f7e0ac4ce145794832310f [file] [log] [blame]
Jeff Sharkey9509d272012-08-07 17:56:30 -07001/*
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
Jeff Sharkey9509d272012-08-07 17:56:30 -070017#include <cutils/multiuser.h>
Jeff Sharkeydff44702016-12-13 11:55:19 -070018#include <private/android_filesystem_config.h>
Jeff Sharkey9509d272012-08-07 17:56:30 -070019
Jeff Sharkey96851942012-08-27 15:03:37 -070020userid_t multiuser_get_user_id(uid_t uid) {
Jeff Sharkeydff44702016-12-13 11:55:19 -070021 return uid / AID_USER_OFFSET;
Jeff Sharkey9509d272012-08-07 17:56:30 -070022}
23
Jeff Sharkey96851942012-08-27 15:03:37 -070024appid_t multiuser_get_app_id(uid_t uid) {
Jeff Sharkeydff44702016-12-13 11:55:19 -070025 return uid % AID_USER_OFFSET;
Jeff Sharkey9509d272012-08-07 17:56:30 -070026}
27
Jeff Sharkeydff44702016-12-13 11:55:19 -070028uid_t multiuser_get_uid(userid_t user_id, appid_t app_id) {
29 return (user_id * AID_USER_OFFSET) + (app_id % AID_USER_OFFSET);
Jeff Sharkey9509d272012-08-07 17:56:30 -070030}
Calin Juravle807f23a2016-02-01 19:27:01 +000031
Nikita Ioffe45f88372022-02-21 18:50:23 +000032uid_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;
Samiul Islamb18fea12022-01-15 18:59:14 +000034 if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
Nikita Ioffe45f88372022-02-21 18:50:23 +000035 return (user_id * AID_USER_OFFSET) + (app_id % AID_USER_OFFSET) + sdk_sandbox_offset;
Samiul Islamb18fea12022-01-15 18:59:14 +000036 } else {
37 return -1;
38 }
39}
40
Jeff Sharkeydff44702016-12-13 11:55:19 -070041gid_t multiuser_get_cache_gid(userid_t user_id, appid_t app_id) {
42 if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
43 return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_CACHE_GID_START);
44 } else {
45 return -1;
46 }
47}
Calin Juravle807f23a2016-02-01 19:27:01 +000048
Jeff Sharkey7e5d0b12017-01-18 17:04:21 -070049gid_t multiuser_get_ext_gid(userid_t user_id, appid_t app_id) {
50 if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
51 return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_EXT_GID_START);
52 } else {
53 return -1;
54 }
55}
56
Jeff Sharkeybd2ecd82017-04-17 14:45:16 -060057gid_t multiuser_get_ext_cache_gid(userid_t user_id, appid_t app_id) {
58 if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
59 return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_EXT_CACHE_GID_START);
60 } else {
61 return -1;
62 }
63}
64
Jeff Sharkey53d37ba2017-11-09 17:41:09 -070065gid_t multiuser_get_shared_gid(userid_t, appid_t app_id) {
Jeff Sharkeydff44702016-12-13 11:55:19 -070066 if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
Jeff Sharkey53d37ba2017-11-09 17:41:09 -070067 return (app_id - AID_APP_START) + AID_SHARED_GID_START;
68 } else if (app_id >= AID_ROOT && app_id <= AID_APP_START) {
69 return app_id;
Jeff Sharkeydff44702016-12-13 11:55:19 -070070 } else {
71 return -1;
72 }
73}
74
75gid_t multiuser_get_shared_app_gid(uid_t uid) {
76 return multiuser_get_shared_gid(multiuser_get_user_id(uid), multiuser_get_app_id(uid));
Calin Juravle807f23a2016-02-01 19:27:01 +000077}