canned_fs_config.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2014 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 <private/android_filesystem_config.h>
  17. #include <private/canned_fs_config.h>
  18. #include <private/fs_config.h>
  19. #include <errno.h>
  20. #include <inttypes.h>
  21. #include <limits.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. typedef struct {
  26. const char* path;
  27. unsigned uid;
  28. unsigned gid;
  29. unsigned mode;
  30. uint64_t capabilities;
  31. } Path;
  32. static Path* canned_data = NULL;
  33. static int canned_alloc = 0;
  34. static int canned_used = 0;
  35. static int path_compare(const void* a, const void* b) {
  36. return strcmp(((Path*)a)->path, ((Path*)b)->path);
  37. }
  38. int load_canned_fs_config(const char* fn) {
  39. char buf[PATH_MAX + 200];
  40. FILE* f;
  41. f = fopen(fn, "r");
  42. if (f == NULL) {
  43. fprintf(stderr, "failed to open %s: %s\n", fn, strerror(errno));
  44. return -1;
  45. }
  46. while (fgets(buf, sizeof(buf), f)) {
  47. Path* p;
  48. char* token;
  49. char* line = buf;
  50. bool rootdir;
  51. while (canned_used >= canned_alloc) {
  52. canned_alloc = (canned_alloc+1) * 2;
  53. canned_data = (Path*) realloc(canned_data, canned_alloc * sizeof(Path));
  54. }
  55. p = canned_data + canned_used;
  56. if (line[0] == '/') line++;
  57. rootdir = line[0] == ' ';
  58. p->path = strdup(rootdir ? "" : strtok(line, " "));
  59. p->uid = atoi(strtok(rootdir ? line : NULL, " "));
  60. p->gid = atoi(strtok(NULL, " "));
  61. p->mode = strtol(strtok(NULL, " "), NULL, 8); // mode is in octal
  62. p->capabilities = 0;
  63. do {
  64. token = strtok(NULL, " ");
  65. if (token && strncmp(token, "capabilities=", 13) == 0) {
  66. p->capabilities = strtoll(token+13, NULL, 0);
  67. break;
  68. }
  69. } while (token);
  70. canned_used++;
  71. }
  72. fclose(f);
  73. qsort(canned_data, canned_used, sizeof(Path), path_compare);
  74. printf("loaded %d fs_config entries\n", canned_used);
  75. return 0;
  76. }
  77. static const int kDebugCannedFsConfig = 0;
  78. void canned_fs_config(const char* path, int dir, const char* target_out_path,
  79. unsigned* uid, unsigned* gid, unsigned* mode, uint64_t* capabilities) {
  80. Path key, *p;
  81. key.path = path;
  82. if (path[0] == '/') key.path++; // canned paths lack the leading '/'
  83. p = (Path*) bsearch(&key, canned_data, canned_used, sizeof(Path), path_compare);
  84. if (p == NULL) {
  85. fprintf(stderr, "failed to find [%s] in canned fs_config\n", path);
  86. exit(1);
  87. }
  88. *uid = p->uid;
  89. *gid = p->gid;
  90. *mode = p->mode;
  91. *capabilities = p->capabilities;
  92. if (kDebugCannedFsConfig) {
  93. // for debugging, run the built-in fs_config and compare the results.
  94. unsigned c_uid, c_gid, c_mode;
  95. uint64_t c_capabilities;
  96. fs_config(path, dir, target_out_path, &c_uid, &c_gid, &c_mode, &c_capabilities);
  97. if (c_uid != *uid) printf("%s uid %d %d\n", path, *uid, c_uid);
  98. if (c_gid != *gid) printf("%s gid %d %d\n", path, *gid, c_gid);
  99. if (c_mode != *mode) printf("%s mode 0%o 0%o\n", path, *mode, c_mode);
  100. if (c_capabilities != *capabilities) {
  101. printf("%s capabilities %" PRIx64 " %" PRIx64 "\n",
  102. path,
  103. *capabilities,
  104. c_capabilities);
  105. }
  106. }
  107. }