fs.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #include <cutils/fs.h>
  17. #define LOG_TAG "cutils"
  18. /* These defines are only needed because prebuilt headers are out of date */
  19. #define __USE_XOPEN2K8 1
  20. #define _ATFILE_SOURCE 1
  21. #define _GNU_SOURCE 1
  22. #include <dirent.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <limits.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/stat.h>
  30. #include <sys/types.h>
  31. #include <unistd.h>
  32. #include <log/log.h>
  33. #define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
  34. #define BUF_SIZE 64
  35. static int fs_prepare_path_impl(const char* path, mode_t mode, uid_t uid, gid_t gid,
  36. int allow_fixup, int prepare_as_dir) {
  37. // TODO: fix the goto hell below.
  38. int type_ok;
  39. int owner_match;
  40. int mode_match;
  41. // Check if path needs to be created
  42. struct stat sb;
  43. int create_result = -1;
  44. if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) {
  45. if (errno == ENOENT) {
  46. goto create;
  47. } else {
  48. ALOGE("Failed to lstat(%s): %s", path, strerror(errno));
  49. return -1;
  50. }
  51. }
  52. // Exists, verify status
  53. type_ok = prepare_as_dir ? S_ISDIR(sb.st_mode) : S_ISREG(sb.st_mode);
  54. if (!type_ok) {
  55. ALOGE("Not a %s: %s", (prepare_as_dir ? "directory" : "regular file"), path);
  56. return -1;
  57. }
  58. owner_match = ((sb.st_uid == uid) && (sb.st_gid == gid));
  59. mode_match = ((sb.st_mode & ALL_PERMS) == mode);
  60. if (owner_match && mode_match) {
  61. return 0;
  62. } else if (allow_fixup) {
  63. goto fixup;
  64. } else {
  65. if (!owner_match) {
  66. ALOGE("Expected path %s with owner %d:%d but found %d:%d",
  67. path, uid, gid, sb.st_uid, sb.st_gid);
  68. return -1;
  69. } else {
  70. ALOGW("Expected path %s with mode %o but found %o",
  71. path, mode, (sb.st_mode & ALL_PERMS));
  72. return 0;
  73. }
  74. }
  75. create:
  76. create_result = prepare_as_dir
  77. ? TEMP_FAILURE_RETRY(mkdir(path, mode))
  78. : TEMP_FAILURE_RETRY(open(path, O_CREAT | O_CLOEXEC | O_NOFOLLOW | O_RDONLY, 0644));
  79. if (create_result == -1) {
  80. if (errno != EEXIST) {
  81. ALOGE("Failed to %s(%s): %s",
  82. (prepare_as_dir ? "mkdir" : "open"), path, strerror(errno));
  83. return -1;
  84. }
  85. } else if (!prepare_as_dir) {
  86. // For regular files we need to make sure we close the descriptor
  87. if (close(create_result) == -1) {
  88. ALOGW("Failed to close file after create %s: %s", path, strerror(errno));
  89. }
  90. }
  91. fixup:
  92. if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) {
  93. ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno));
  94. return -1;
  95. }
  96. if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) {
  97. ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno));
  98. return -1;
  99. }
  100. return 0;
  101. }
  102. int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
  103. return fs_prepare_path_impl(path, mode, uid, gid, /*allow_fixup*/ 1, /*prepare_as_dir*/ 1);
  104. }
  105. int fs_prepare_dir_strict(const char* path, mode_t mode, uid_t uid, gid_t gid) {
  106. return fs_prepare_path_impl(path, mode, uid, gid, /*allow_fixup*/ 0, /*prepare_as_dir*/ 1);
  107. }
  108. int fs_prepare_file_strict(const char* path, mode_t mode, uid_t uid, gid_t gid) {
  109. return fs_prepare_path_impl(path, mode, uid, gid, /*allow_fixup*/ 0, /*prepare_as_dir*/ 0);
  110. }
  111. int fs_read_atomic_int(const char* path, int* out_value) {
  112. int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY));
  113. if (fd == -1) {
  114. ALOGE("Failed to read %s: %s", path, strerror(errno));
  115. return -1;
  116. }
  117. char buf[BUF_SIZE];
  118. if (TEMP_FAILURE_RETRY(read(fd, buf, BUF_SIZE)) == -1) {
  119. ALOGE("Failed to read %s: %s", path, strerror(errno));
  120. goto fail;
  121. }
  122. if (sscanf(buf, "%d", out_value) != 1) {
  123. ALOGE("Failed to parse %s: %s", path, strerror(errno));
  124. goto fail;
  125. }
  126. close(fd);
  127. return 0;
  128. fail:
  129. close(fd);
  130. *out_value = -1;
  131. return -1;
  132. }
  133. int fs_write_atomic_int(const char* path, int value) {
  134. char temp[PATH_MAX];
  135. if (snprintf(temp, PATH_MAX, "%s.XXXXXX", path) >= PATH_MAX) {
  136. ALOGE("Path too long");
  137. return -1;
  138. }
  139. int fd = TEMP_FAILURE_RETRY(mkstemp(temp));
  140. if (fd == -1) {
  141. ALOGE("Failed to open %s: %s", temp, strerror(errno));
  142. return -1;
  143. }
  144. char buf[BUF_SIZE];
  145. int len = snprintf(buf, BUF_SIZE, "%d", value) + 1;
  146. if (len > BUF_SIZE) {
  147. ALOGE("Value %d too large: %s", value, strerror(errno));
  148. goto fail;
  149. }
  150. if (TEMP_FAILURE_RETRY(write(fd, buf, len)) < len) {
  151. ALOGE("Failed to write %s: %s", temp, strerror(errno));
  152. goto fail;
  153. }
  154. if (close(fd) == -1) {
  155. ALOGE("Failed to close %s: %s", temp, strerror(errno));
  156. goto fail_closed;
  157. }
  158. if (rename(temp, path) == -1) {
  159. ALOGE("Failed to rename %s to %s: %s", temp, path, strerror(errno));
  160. goto fail_closed;
  161. }
  162. return 0;
  163. fail:
  164. close(fd);
  165. fail_closed:
  166. unlink(temp);
  167. return -1;
  168. }
  169. #ifndef __APPLE__
  170. int fs_mkdirs(const char* path, mode_t mode) {
  171. if (*path != '/') {
  172. ALOGE("Relative paths are not allowed: %s", path);
  173. return -EINVAL;
  174. }
  175. int fd = open("/", 0);
  176. if (fd == -1) {
  177. ALOGE("Failed to open(/): %s", strerror(errno));
  178. return -errno;
  179. }
  180. struct stat sb;
  181. int res = 0;
  182. char* buf = strdup(path);
  183. char* segment = buf + 1;
  184. char* p = segment;
  185. while (*p != '\0') {
  186. if (*p == '/') {
  187. *p = '\0';
  188. if (!strcmp(segment, "..") || !strcmp(segment, ".") || !strcmp(segment, "")) {
  189. ALOGE("Invalid path: %s", buf);
  190. res = -EINVAL;
  191. goto done_close;
  192. }
  193. if (fstatat(fd, segment, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
  194. if (errno == ENOENT) {
  195. /* Nothing there yet; let's create it! */
  196. if (mkdirat(fd, segment, mode) != 0) {
  197. if (errno == EEXIST) {
  198. /* We raced with someone; ignore */
  199. } else {
  200. ALOGE("Failed to mkdirat(%s): %s", buf, strerror(errno));
  201. res = -errno;
  202. goto done_close;
  203. }
  204. }
  205. } else {
  206. ALOGE("Failed to fstatat(%s): %s", buf, strerror(errno));
  207. res = -errno;
  208. goto done_close;
  209. }
  210. } else {
  211. if (S_ISLNK(sb.st_mode)) {
  212. ALOGE("Symbolic links are not allowed: %s", buf);
  213. res = -ELOOP;
  214. goto done_close;
  215. }
  216. if (!S_ISDIR(sb.st_mode)) {
  217. ALOGE("Existing segment not a directory: %s", buf);
  218. res = -ENOTDIR;
  219. goto done_close;
  220. }
  221. }
  222. /* Yay, segment is ready for us to step into */
  223. int next_fd;
  224. if ((next_fd = openat(fd, segment, O_NOFOLLOW | O_CLOEXEC)) == -1) {
  225. ALOGE("Failed to openat(%s): %s", buf, strerror(errno));
  226. res = -errno;
  227. goto done_close;
  228. }
  229. close(fd);
  230. fd = next_fd;
  231. *p = '/';
  232. segment = p + 1;
  233. }
  234. p++;
  235. }
  236. done_close:
  237. close(fd);
  238. free(buf);
  239. return res;
  240. }
  241. #endif