blob: e0e6a34574e6c3f11afc66a7e96d5872a7675b48 [file] [log] [blame]
Mohamad Ayyash83742762016-04-07 22:13:43 -07001/*
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 Ayyash83742762016-04-07 22:13:43 -070017#include <errno.h>
Mark Salyzyn768cbb02016-07-06 10:31:05 -070018#include <inttypes.h>
Mohamad Ayyash83742762016-04-07 22:13:43 -070019#include <limits.h>
Mark Salyzyn768cbb02016-07-06 10:31:05 -070020#include <stdio.h>
Mohamad Ayyash83742762016-04-07 22:13:43 -070021#include <stdlib.h>
Mark Salyzyn768cbb02016-07-06 10:31:05 -070022#include <string.h>
Mohamad Ayyash83742762016-04-07 22:13:43 -070023
24#include <private/android_filesystem_config.h>
25#include <private/canned_fs_config.h>
26
27typedef struct {
Mark Salyzyn768cbb02016-07-06 10:31:05 -070028 const char* path;
29 unsigned uid;
30 unsigned gid;
31 unsigned mode;
32 uint64_t capabilities;
Mohamad Ayyash83742762016-04-07 22:13:43 -070033} Path;
34
35static Path* canned_data = NULL;
36static int canned_alloc = 0;
37static int canned_used = 0;
38
39static int path_compare(const void* a, const void* b) {
Mark Salyzyn768cbb02016-07-06 10:31:05 -070040 return strcmp(((Path*)a)->path, ((Path*)b)->path);
Mohamad Ayyash83742762016-04-07 22:13:43 -070041}
42
43int load_canned_fs_config(const char* fn) {
Mark Salyzyn768cbb02016-07-06 10:31:05 -070044 char line[PATH_MAX + 200];
45 FILE* f;
Mohamad Ayyash83742762016-04-07 22:13:43 -070046
Mark Salyzyn768cbb02016-07-06 10:31:05 -070047 f = fopen(fn, "r");
48 if (f == NULL) {
49 fprintf(stderr, "failed to open %s: %s\n", fn, strerror(errno));
50 return -1;
51 }
Mohamad Ayyash83742762016-04-07 22:13:43 -070052
Mark Salyzyn768cbb02016-07-06 10:31:05 -070053 while (fgets(line, sizeof(line), f)) {
54 Path* p;
55 char* token;
Mohamad Ayyash83742762016-04-07 22:13:43 -070056
Mark Salyzyn768cbb02016-07-06 10:31:05 -070057 while (canned_used >= canned_alloc) {
58 canned_alloc = (canned_alloc+1) * 2;
59 canned_data = (Path*) realloc(canned_data, canned_alloc * sizeof(Path));
60 }
61 p = canned_data + canned_used;
62 p->path = strdup(strtok(line, " "));
63 p->uid = atoi(strtok(NULL, " "));
64 p->gid = atoi(strtok(NULL, " "));
65 p->mode = strtol(strtok(NULL, " "), NULL, 8); // mode is in octal
66 p->capabilities = 0;
Mohamad Ayyash83742762016-04-07 22:13:43 -070067
Mark Salyzyn768cbb02016-07-06 10:31:05 -070068 do {
69 token = strtok(NULL, " ");
70 if (token && strncmp(token, "capabilities=", 13) == 0) {
71 p->capabilities = strtoll(token+13, NULL, 0);
72 break;
73 }
74 } while (token);
Mohamad Ayyash83742762016-04-07 22:13:43 -070075
Mark Salyzyn768cbb02016-07-06 10:31:05 -070076 canned_used++;
77 }
Mohamad Ayyash83742762016-04-07 22:13:43 -070078
Mark Salyzyn768cbb02016-07-06 10:31:05 -070079 fclose(f);
80
81 qsort(canned_data, canned_used, sizeof(Path), path_compare);
82 printf("loaded %d fs_config entries\n", canned_used);
83
84 return 0;
Mohamad Ayyash83742762016-04-07 22:13:43 -070085}
86
87static const int kDebugCannedFsConfig = 0;
88
89void canned_fs_config(const char* path, int dir, const char* target_out_path,
Mark Salyzyn768cbb02016-07-06 10:31:05 -070090 unsigned* uid, unsigned* gid, unsigned* mode, uint64_t* capabilities) {
91 Path key, *p;
92
Mohamad Ayyash83742762016-04-07 22:13:43 -070093 key.path = path;
Mark Salyzyn768cbb02016-07-06 10:31:05 -070094 if (path[0] == '/') key.path++; // canned paths lack the leading '/'
95 p = (Path*) bsearch(&key, canned_data, canned_used, sizeof(Path), path_compare);
96 if (p == NULL) {
97 fprintf(stderr, "failed to find [%s] in canned fs_config\n", path);
98 exit(1);
99 }
100 *uid = p->uid;
101 *gid = p->gid;
102 *mode = p->mode;
103 *capabilities = p->capabilities;
Mohamad Ayyash83742762016-04-07 22:13:43 -0700104
Mark Salyzyn768cbb02016-07-06 10:31:05 -0700105 if (kDebugCannedFsConfig) {
106 // for debugging, run the built-in fs_config and compare the results.
Mohamad Ayyash83742762016-04-07 22:13:43 -0700107
Mark Salyzyn768cbb02016-07-06 10:31:05 -0700108 unsigned c_uid, c_gid, c_mode;
109 uint64_t c_capabilities;
Mohamad Ayyash83742762016-04-07 22:13:43 -0700110
Mark Salyzyn768cbb02016-07-06 10:31:05 -0700111 fs_config(path, dir, target_out_path, &c_uid, &c_gid, &c_mode, &c_capabilities);
112
113 if (c_uid != *uid) printf("%s uid %d %d\n", path, *uid, c_uid);
114 if (c_gid != *gid) printf("%s gid %d %d\n", path, *gid, c_gid);
115 if (c_mode != *mode) printf("%s mode 0%o 0%o\n", path, *mode, c_mode);
116 if (c_capabilities != *capabilities) {
117 printf("%s capabilities %" PRIx64 " %" PRIx64 "\n",
118 path,
119 *capabilities,
120 c_capabilities);
Mohamad Ayyash83742762016-04-07 22:13:43 -0700121 }
Mark Salyzyn768cbb02016-07-06 10:31:05 -0700122 }
Mohamad Ayyash83742762016-04-07 22:13:43 -0700123}