summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Sharkey2016-01-12 13:42:32 -0600
committerJeff Sharkey2016-01-12 14:07:52 -0600
commitcf94fe152e220110d606dcbe2f8a2bd9da0546bc (patch)
treeb6ce4cab5fce43bfc2485d5a6635adac5cb0cc09 /libcutils/fs.c
parentba01a14659a981deab606223baaba7b22ef5c849 (diff)
downloadplatform-system-core-cf94fe152e220110d606dcbe2f8a2bd9da0546bc.tar.gz
platform-system-core-cf94fe152e220110d606dcbe2f8a2bd9da0546bc.tar.xz
platform-system-core-cf94fe152e220110d606dcbe2f8a2bd9da0546bc.zip
Offer a stricter way to prepare directories.
Some callers, such as installd, have stricter requirements around directory preparation, where they want to assert ownership and mode without quietly fixing the values. Bug: 26466827 Change-Id: Id44db5f29a3326cfe178b443fb450ad2edeaefd8
Diffstat (limited to 'libcutils/fs.c')
-rw-r--r--libcutils/fs.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/libcutils/fs.c b/libcutils/fs.c
index 45c7add4c..88c488cba 100644
--- a/libcutils/fs.c
+++ b/libcutils/fs.c
@@ -37,7 +37,8 @@
37#define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 37#define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
38#define BUF_SIZE 64 38#define BUF_SIZE 64
39 39
40int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) { 40static int fs_prepare_dir_impl(const char* path, mode_t mode, uid_t uid, gid_t gid,
41 int allow_fixup) {
41 // Check if path needs to be created 42 // Check if path needs to be created
42 struct stat sb; 43 struct stat sb;
43 if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) { 44 if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) {
@@ -56,8 +57,11 @@ int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
56 } 57 }
57 if (((sb.st_mode & ALL_PERMS) == mode) && (sb.st_uid == uid) && (sb.st_gid == gid)) { 58 if (((sb.st_mode & ALL_PERMS) == mode) && (sb.st_uid == uid) && (sb.st_gid == gid)) {
58 return 0; 59 return 0;
59 } else { 60 } else if (allow_fixup) {
60 goto fixup; 61 goto fixup;
62 } else {
63 ALOGE("Path %s exists with unexpected permissions", path);
64 return -1;
61 } 65 }
62 66
63create: 67create:
@@ -81,6 +85,14 @@ fixup:
81 return 0; 85 return 0;
82} 86}
83 87
88int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
89 return fs_prepare_dir_impl(path, mode, uid, gid, 1);
90}
91
92int fs_prepare_dir_strict(const char* path, mode_t mode, uid_t uid, gid_t gid) {
93 return fs_prepare_dir_impl(path, mode, uid, gid, 0);
94}
95
84int fs_read_atomic_int(const char* path, int* out_value) { 96int fs_read_atomic_int(const char* path, int* out_value) {
85 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY)); 97 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY));
86 if (fd == -1) { 98 if (fd == -1) {