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