| Jeff Sharkey | 9509d27 | 2012-08-07 17:56:30 -0700 | [diff] [blame] | 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 |  | 
| Jeff Sharkey | 9509d27 | 2012-08-07 17:56:30 -0700 | [diff] [blame] | 17 | #include <cutils/multiuser.h> | 
| Jeff Sharkey | dff4470 | 2016-12-13 11:55:19 -0700 | [diff] [blame] | 18 | #include <private/android_filesystem_config.h> | 
| Jeff Sharkey | 9509d27 | 2012-08-07 17:56:30 -0700 | [diff] [blame] | 19 |  | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 20 | userid_t multiuser_get_user_id(uid_t uid) { | 
| Jeff Sharkey | dff4470 | 2016-12-13 11:55:19 -0700 | [diff] [blame] | 21 |     return uid / AID_USER_OFFSET; | 
| Jeff Sharkey | 9509d27 | 2012-08-07 17:56:30 -0700 | [diff] [blame] | 22 | } | 
 | 23 |  | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 24 | appid_t multiuser_get_app_id(uid_t uid) { | 
| Jeff Sharkey | dff4470 | 2016-12-13 11:55:19 -0700 | [diff] [blame] | 25 |     return uid % AID_USER_OFFSET; | 
| Jeff Sharkey | 9509d27 | 2012-08-07 17:56:30 -0700 | [diff] [blame] | 26 | } | 
 | 27 |  | 
| Jeff Sharkey | dff4470 | 2016-12-13 11:55:19 -0700 | [diff] [blame] | 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); | 
| Jeff Sharkey | 9509d27 | 2012-08-07 17:56:30 -0700 | [diff] [blame] | 30 | } | 
| Calin Juravle | 807f23a | 2016-02-01 19:27:01 +0000 | [diff] [blame] | 31 |  | 
| Jeff Sharkey | dff4470 | 2016-12-13 11:55:19 -0700 | [diff] [blame] | 32 | gid_t multiuser_get_cache_gid(userid_t user_id, appid_t app_id) { | 
 | 33 |     if (app_id >= AID_APP_START && app_id <= AID_APP_END) { | 
 | 34 |         return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_CACHE_GID_START); | 
 | 35 |     } else { | 
 | 36 |         return -1; | 
 | 37 |     } | 
 | 38 | } | 
| Calin Juravle | 807f23a | 2016-02-01 19:27:01 +0000 | [diff] [blame] | 39 |  | 
| Jeff Sharkey | 7e5d0b1 | 2017-01-18 17:04:21 -0700 | [diff] [blame] | 40 | gid_t multiuser_get_ext_gid(userid_t user_id, appid_t app_id) { | 
 | 41 |     if (app_id >= AID_APP_START && app_id <= AID_APP_END) { | 
 | 42 |         return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_EXT_GID_START); | 
 | 43 |     } else { | 
 | 44 |         return -1; | 
 | 45 |     } | 
 | 46 | } | 
 | 47 |  | 
| Jeff Sharkey | fa16473 | 2017-04-17 14:45:16 -0600 | [diff] [blame] | 48 | gid_t multiuser_get_ext_cache_gid(userid_t user_id, appid_t app_id) { | 
 | 49 |     if (app_id >= AID_APP_START && app_id <= AID_APP_END) { | 
 | 50 |         return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_EXT_CACHE_GID_START); | 
 | 51 |     } else { | 
 | 52 |         return -1; | 
 | 53 |     } | 
 | 54 | } | 
 | 55 |  | 
| Jeff Sharkey | dff4470 | 2016-12-13 11:55:19 -0700 | [diff] [blame] | 56 | gid_t multiuser_get_shared_gid(userid_t user_id, appid_t app_id) { | 
 | 57 |     if (app_id >= AID_APP_START && app_id <= AID_APP_END) { | 
 | 58 |         return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_SHARED_GID_START); | 
 | 59 |     } else { | 
 | 60 |         return -1; | 
 | 61 |     } | 
 | 62 | } | 
 | 63 |  | 
 | 64 | gid_t multiuser_get_shared_app_gid(uid_t uid) { | 
 | 65 |     return multiuser_get_shared_gid(multiuser_get_user_id(uid), multiuser_get_app_id(uid)); | 
| Calin Juravle | 807f23a | 2016-02-01 19:27:01 +0000 | [diff] [blame] | 66 | } |