blob: 45c7add4c53ca35d0752ee1a3b5661ba05c6bc3f [file] [log] [blame]
Jeff Sharkey96851942012-08-27 15:03:37 -07001/*
2 * Copyright (C) 2012 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
Jeff Sharkeyddb17332012-09-13 14:47:23 -070017#define LOG_TAG "cutils"
18
Jeff Sharkey44d63422013-09-12 09:44:48 -070019/* These defines are only needed because prebuilt headers are out of date */
20#define __USE_XOPEN2K8 1
21#define _ATFILE_SOURCE 1
22#define _GNU_SOURCE 1
23
Jeff Sharkey96851942012-08-27 15:03:37 -070024#include <cutils/fs.h>
25#include <cutils/log.h>
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <unistd.h>
31#include <errno.h>
32#include <string.h>
33#include <limits.h>
Nick Kralevich69ce4892012-10-15 15:51:33 -070034#include <stdlib.h>
Jeff Sharkey44d63422013-09-12 09:44:48 -070035#include <dirent.h>
Jeff Sharkey96851942012-08-27 15:03:37 -070036
37#define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
38#define BUF_SIZE 64
39
40int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
41 // Check if path needs to be created
42 struct stat sb;
Jeff Sharkeyddb17332012-09-13 14:47:23 -070043 if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -070044 if (errno == ENOENT) {
45 goto create;
46 } else {
Jeff Sharkeyddb17332012-09-13 14:47:23 -070047 ALOGE("Failed to lstat(%s): %s", path, strerror(errno));
Jeff Sharkey96851942012-08-27 15:03:37 -070048 return -1;
49 }
50 }
51
52 // Exists, verify status
53 if (!S_ISDIR(sb.st_mode)) {
54 ALOGE("Not a directory: %s", path);
55 return -1;
56 }
57 if (((sb.st_mode & ALL_PERMS) == mode) && (sb.st_uid == uid) && (sb.st_gid == gid)) {
58 return 0;
59 } else {
60 goto fixup;
61 }
62
63create:
Jeff Sharkeyddb17332012-09-13 14:47:23 -070064 if (TEMP_FAILURE_RETRY(mkdir(path, mode)) == -1) {
Jeff Sharkey489609b2012-09-25 11:10:16 -070065 if (errno != EEXIST) {
66 ALOGE("Failed to mkdir(%s): %s", path, strerror(errno));
67 return -1;
68 }
Jeff Sharkey96851942012-08-27 15:03:37 -070069 }
70
71fixup:
Jeff Sharkeyddb17332012-09-13 14:47:23 -070072 if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) {
73 ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno));
Jeff Sharkey96851942012-08-27 15:03:37 -070074 return -1;
75 }
Jeff Sharkeyddb17332012-09-13 14:47:23 -070076 if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -070077 ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno));
78 return -1;
79 }
80
81 return 0;
82}
83
84int fs_read_atomic_int(const char* path, int* out_value) {
Jeff Sharkey6de70262012-09-13 15:11:42 -070085 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY));
Jeff Sharkey96851942012-08-27 15:03:37 -070086 if (fd == -1) {
87 ALOGE("Failed to read %s: %s", path, strerror(errno));
88 return -1;
89 }
90
91 char buf[BUF_SIZE];
Jeff Sharkey6de70262012-09-13 15:11:42 -070092 if (TEMP_FAILURE_RETRY(read(fd, buf, BUF_SIZE)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -070093 ALOGE("Failed to read %s: %s", path, strerror(errno));
94 goto fail;
95 }
96 if (sscanf(buf, "%d", out_value) != 1) {
97 ALOGE("Failed to parse %s: %s", path, strerror(errno));
98 goto fail;
99 }
100 close(fd);
101 return 0;
102
103fail:
104 close(fd);
105 *out_value = -1;
106 return -1;
107}
108
109int fs_write_atomic_int(const char* path, int value) {
110 char temp[PATH_MAX];
111 if (snprintf(temp, PATH_MAX, "%s.XXXXXX", path) >= PATH_MAX) {
112 ALOGE("Path too long");
113 return -1;
114 }
115
Jeff Sharkey6de70262012-09-13 15:11:42 -0700116 int fd = TEMP_FAILURE_RETRY(mkstemp(temp));
Jeff Sharkey96851942012-08-27 15:03:37 -0700117 if (fd == -1) {
118 ALOGE("Failed to open %s: %s", temp, strerror(errno));
119 return -1;
120 }
121
122 char buf[BUF_SIZE];
123 int len = snprintf(buf, BUF_SIZE, "%d", value) + 1;
124 if (len > BUF_SIZE) {
125 ALOGE("Value %d too large: %s", value, strerror(errno));
126 goto fail;
127 }
Jeff Sharkey6de70262012-09-13 15:11:42 -0700128 if (TEMP_FAILURE_RETRY(write(fd, buf, len)) < len) {
Jeff Sharkey96851942012-08-27 15:03:37 -0700129 ALOGE("Failed to write %s: %s", temp, strerror(errno));
130 goto fail;
131 }
132 if (close(fd) == -1) {
133 ALOGE("Failed to close %s: %s", temp, strerror(errno));
134 goto fail_closed;
135 }
136
137 if (rename(temp, path) == -1) {
138 ALOGE("Failed to rename %s to %s: %s", temp, path, strerror(errno));
139 goto fail_closed;
140 }
141
142 return 0;
143
144fail:
145 close(fd);
146fail_closed:
147 unlink(temp);
148 return -1;
149}
Jeff Sharkey44d63422013-09-12 09:44:48 -0700150
Jeff Sharkey0ee7d8c2013-09-20 17:58:54 -0700151#ifndef __APPLE__
152
Jeff Sharkey44d63422013-09-12 09:44:48 -0700153int fs_mkdirs(const char* path, mode_t mode) {
154 int res = 0;
155 int fd = 0;
156 struct stat sb;
157 char* buf = strdup(path);
158
159 if (*buf != '/') {
160 ALOGE("Relative paths are not allowed: %s", buf);
161 res = -EINVAL;
162 goto done;
163 }
164
165 if ((fd = open("/", 0)) == -1) {
166 ALOGE("Failed to open(/): %s", strerror(errno));
167 res = -errno;
168 goto done;
169 }
170
171 char* segment = buf + 1;
172 char* p = segment;
173 while (*p != '\0') {
174 if (*p == '/') {
175 *p = '\0';
176
177 if (!strcmp(segment, "..") || !strcmp(segment, ".") || !strcmp(segment, "")) {
178 ALOGE("Invalid path: %s", buf);
179 res = -EINVAL;
180 goto done_close;
181 }
182
183 if (fstatat(fd, segment, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
184 if (errno == ENOENT) {
185 /* Nothing there yet; let's create it! */
186 if (mkdirat(fd, segment, mode) != 0) {
187 if (errno == EEXIST) {
188 /* We raced with someone; ignore */
189 } else {
190 ALOGE("Failed to mkdirat(%s): %s", buf, strerror(errno));
191 res = -errno;
192 goto done_close;
193 }
194 }
195 } else {
196 ALOGE("Failed to fstatat(%s): %s", buf, strerror(errno));
197 res = -errno;
198 goto done_close;
199 }
200 } else {
201 if (S_ISLNK(sb.st_mode)) {
202 ALOGE("Symbolic links are not allowed: %s", buf);
203 res = -ELOOP;
204 goto done_close;
205 }
206 if (!S_ISDIR(sb.st_mode)) {
207 ALOGE("Existing segment not a directory: %s", buf);
208 res = -ENOTDIR;
209 goto done_close;
210 }
211 }
212
213 /* Yay, segment is ready for us to step into */
214 int next_fd;
Nick Kralevich30a86eb2014-06-16 15:32:49 -0700215 if ((next_fd = openat(fd, segment, O_NOFOLLOW | O_CLOEXEC)) == -1) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700216 ALOGE("Failed to openat(%s): %s", buf, strerror(errno));
217 res = -errno;
218 goto done_close;
219 }
220
221 close(fd);
222 fd = next_fd;
223
224 *p = '/';
225 segment = p + 1;
226 }
227 p++;
228 }
229
230done_close:
231 close(fd);
232done:
233 free(buf);
234 return res;
235}
Jeff Sharkey0ee7d8c2013-09-20 17:58:54 -0700236
237#endif