2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "private/android_filesystem_config.h"
25 #include "canned_fs_config.h"
32 uint64_t capabilities
;
35 static Path
* canned_data
= NULL
;
36 static int canned_alloc
= 0;
37 static int canned_used
= 0;
39 static int path_compare(const void* a
, const void* b
) {
40 return strcmp(((Path
*)a
)->path
, ((Path
*)b
)->path
);
43 int load_canned_fs_config(const char* fn
) {
44 FILE* f
= fopen(fn
, "r");
46 fprintf(stderr
, "failed to open %s: %s\n", fn
, strerror(errno
));
50 char line
[PATH_MAX
+ 200];
51 while (fgets(line
, sizeof(line
), f
)) {
52 while (canned_used
>= canned_alloc
) {
53 canned_alloc
= (canned_alloc
+1) * 2;
54 canned_data
= (Path
*) realloc(canned_data
, canned_alloc
* sizeof(Path
));
56 Path
* p
= canned_data
+ canned_used
;
57 p
->path
= strdup(strtok(line
, " "));
58 p
->uid
= atoi(strtok(NULL
, " "));
59 p
->gid
= atoi(strtok(NULL
, " "));
60 p
->mode
= strtol(strtok(NULL
, " "), NULL
, 8); // mode is in octal
65 token
= strtok(NULL
, " ");
66 if (token
&& strncmp(token
, "capabilities=", 13) == 0) {
67 p
->capabilities
= strtoll(token
+13, NULL
, 0);
77 qsort(canned_data
, canned_used
, sizeof(Path
), path_compare
);
78 printf("loaded %d fs_config entries\n", canned_used
);
83 void canned_fs_config(const char* path
, int dir
,
84 unsigned* uid
, unsigned* gid
, unsigned* mode
, uint64_t* capabilities
) {
86 key
.path
= path
+1; // canned paths lack the leading '/'
87 Path
* p
= (Path
*) bsearch(&key
, canned_data
, canned_used
, sizeof(Path
), path_compare
);
89 fprintf(stderr
, "failed to find [%s] in canned fs_config\n", path
);
95 *capabilities
= p
->capabilities
;
98 // for debugging, run the built-in fs_config and compare the results.
100 unsigned c_uid
, c_gid
, c_mode
;
101 uint64_t c_capabilities
;
102 fs_config(path
, dir
, &c_uid
, &c_gid
, &c_mode
, &c_capabilities
);
104 if (c_uid
!= *uid
) printf("%s uid %d %d\n", path
, *uid
, c_uid
);
105 if (c_gid
!= *gid
) printf("%s gid %d %d\n", path
, *gid
, c_gid
);
106 if (c_mode
!= *mode
) printf("%s mode 0%o 0%o\n", path
, *mode
, c_mode
);
107 if (c_capabilities
!= *capabilities
) printf("%s capabilities %llx %llx\n", path
, *capabilities
, c_capabilities
);