multiuser.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include <cutils/multiuser.h>
  17. #include <private/android_filesystem_config.h>
  18. userid_t multiuser_get_user_id(uid_t uid) {
  19. return uid / AID_USER_OFFSET;
  20. }
  21. appid_t multiuser_get_app_id(uid_t uid) {
  22. return uid % AID_USER_OFFSET;
  23. }
  24. uid_t multiuser_get_uid(userid_t user_id, appid_t app_id) {
  25. return (user_id * AID_USER_OFFSET) + (app_id % AID_USER_OFFSET);
  26. }
  27. gid_t multiuser_get_cache_gid(userid_t user_id, appid_t app_id) {
  28. if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
  29. return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_CACHE_GID_START);
  30. } else {
  31. return -1;
  32. }
  33. }
  34. gid_t multiuser_get_ext_gid(userid_t user_id, appid_t app_id) {
  35. if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
  36. return multiuser_get_uid(user_id, (app_id - AID_APP_START) + AID_EXT_GID_START);
  37. } else {
  38. return -1;
  39. }
  40. }
  41. gid_t multiuser_get_ext_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_EXT_CACHE_GID_START);
  44. } else {
  45. return -1;
  46. }
  47. }
  48. gid_t multiuser_get_shared_gid(userid_t, appid_t app_id) {
  49. if (app_id >= AID_APP_START && app_id <= AID_APP_END) {
  50. return (app_id - AID_APP_START) + AID_SHARED_GID_START;
  51. } else if (app_id >= AID_ROOT && app_id <= AID_APP_START) {
  52. return app_id;
  53. } else {
  54. return -1;
  55. }
  56. }
  57. gid_t multiuser_get_shared_app_gid(uid_t uid) {
  58. return multiuser_get_shared_gid(multiuser_get_user_id(uid), multiuser_get_app_id(uid));
  59. }