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