| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 1 | /* | 
 | 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 Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 17 | #define LOG_TAG "cutils" | 
 | 18 |  | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 19 | /* 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 Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 24 | #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 Kralevich | 69ce489 | 2012-10-15 15:51:33 -0700 | [diff] [blame] | 34 | #include <stdlib.h> | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 35 | #include <dirent.h> | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 36 |  | 
 | 37 | #define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) | 
 | 38 | #define BUF_SIZE 64 | 
 | 39 |  | 
 | 40 | int 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 Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 43 |     if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) { | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 44 |         if (errno == ENOENT) { | 
 | 45 |             goto create; | 
 | 46 |         } else { | 
| Jeff Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 47 |             ALOGE("Failed to lstat(%s): %s", path, strerror(errno)); | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 48 |             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 |  | 
 | 63 | create: | 
| Jeff Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 64 |     if (TEMP_FAILURE_RETRY(mkdir(path, mode)) == -1) { | 
| Jeff Sharkey | 489609b | 2012-09-25 11:10:16 -0700 | [diff] [blame] | 65 |         if (errno != EEXIST) { | 
 | 66 |             ALOGE("Failed to mkdir(%s): %s", path, strerror(errno)); | 
 | 67 |             return -1; | 
 | 68 |         } | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 69 |     } | 
 | 70 |  | 
 | 71 | fixup: | 
| Jeff Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 72 |     if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) { | 
 | 73 |         ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno)); | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 74 |         return -1; | 
 | 75 |     } | 
| Jeff Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 76 |     if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) { | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 77 |         ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno)); | 
 | 78 |         return -1; | 
 | 79 |     } | 
 | 80 |  | 
 | 81 |     return 0; | 
 | 82 | } | 
 | 83 |  | 
 | 84 | int fs_read_atomic_int(const char* path, int* out_value) { | 
| Jeff Sharkey | 6de7026 | 2012-09-13 15:11:42 -0700 | [diff] [blame] | 85 |     int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY)); | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 86 |     if (fd == -1) { | 
 | 87 |         ALOGE("Failed to read %s: %s", path, strerror(errno)); | 
 | 88 |         return -1; | 
 | 89 |     } | 
 | 90 |  | 
 | 91 |     char buf[BUF_SIZE]; | 
| Jeff Sharkey | 6de7026 | 2012-09-13 15:11:42 -0700 | [diff] [blame] | 92 |     if (TEMP_FAILURE_RETRY(read(fd, buf, BUF_SIZE)) == -1) { | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 93 |         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 |  | 
 | 103 | fail: | 
 | 104 |     close(fd); | 
 | 105 |     *out_value = -1; | 
 | 106 |     return -1; | 
 | 107 | } | 
 | 108 |  | 
 | 109 | int 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 Sharkey | 6de7026 | 2012-09-13 15:11:42 -0700 | [diff] [blame] | 116 |     int fd = TEMP_FAILURE_RETRY(mkstemp(temp)); | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 117 |     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 Sharkey | 6de7026 | 2012-09-13 15:11:42 -0700 | [diff] [blame] | 128 |     if (TEMP_FAILURE_RETRY(write(fd, buf, len)) < len) { | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 129 |         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 |  | 
 | 144 | fail: | 
 | 145 |     close(fd); | 
 | 146 | fail_closed: | 
 | 147 |     unlink(temp); | 
 | 148 |     return -1; | 
 | 149 | } | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 150 |  | 
| Jeff Sharkey | 0ee7d8c | 2013-09-20 17:58:54 -0700 | [diff] [blame] | 151 | #ifndef __APPLE__ | 
 | 152 |  | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 153 | int 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 Kralevich | 30a86eb | 2014-06-16 15:32:49 -0700 | [diff] [blame] | 215 |             if ((next_fd = openat(fd, segment, O_NOFOLLOW | O_CLOEXEC)) == -1) { | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 216 |                 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 |  | 
 | 230 | done_close: | 
 | 231 |     close(fd); | 
 | 232 | done: | 
 | 233 |     free(buf); | 
 | 234 |     return res; | 
 | 235 | } | 
| Jeff Sharkey | 0ee7d8c | 2013-09-20 17:58:54 -0700 | [diff] [blame] | 236 |  | 
 | 237 | #endif |