The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
| 17 | #include <stdarg.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <ctype.h> |
| 23 | #include <errno.h> |
Colin Cross | 504bc51 | 2010-04-13 19:35:09 -0700 | [diff] [blame] | 24 | #include <time.h> |
Nick Kralevich | ae76f6d | 2013-07-11 15:38:26 -0700 | [diff] [blame] | 25 | #include <ftw.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 27 | #include <selinux/label.h> |
Stephen Smalley | dbd37f2 | 2014-01-28 10:34:09 -0500 | [diff] [blame] | 28 | #include <selinux/android.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 29 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/un.h> |
| 34 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 35 | #include <base/file.h> |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 36 | #include <base/strings.h> |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 37 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | /* for ANDROID_SOCKET_* */ |
| 39 | #include <cutils/sockets.h> |
Andres Morales | db5f5d4 | 2015-05-08 08:30:33 -0700 | [diff] [blame] | 40 | #include <base/stringprintf.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | |
| 42 | #include <private/android_filesystem_config.h> |
| 43 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 44 | #include "init.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 45 | #include "log.h" |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 46 | #include "util.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | /* |
| 49 | * android_name_to_id - returns the integer uid/gid associated with the given |
Nick Kralevich | d2104df | 2015-06-18 17:46:54 -0700 | [diff] [blame] | 50 | * name, or UINT_MAX on error. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | */ |
| 52 | static unsigned int android_name_to_id(const char *name) |
| 53 | { |
Edwin Vane | de7f1ad | 2012-07-26 14:09:13 -0400 | [diff] [blame] | 54 | const struct android_id_info *info = android_ids; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | unsigned int n; |
| 56 | |
| 57 | for (n = 0; n < android_id_count; n++) { |
| 58 | if (!strcmp(info[n].name, name)) |
| 59 | return info[n].aid; |
| 60 | } |
| 61 | |
Nick Kralevich | d2104df | 2015-06-18 17:46:54 -0700 | [diff] [blame] | 62 | return UINT_MAX; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Nick Kralevich | d2104df | 2015-06-18 17:46:54 -0700 | [diff] [blame] | 65 | static unsigned int do_decode_uid(const char *s) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 | { |
| 67 | unsigned int v; |
| 68 | |
| 69 | if (!s || *s == '\0') |
Nick Kralevich | d2104df | 2015-06-18 17:46:54 -0700 | [diff] [blame] | 70 | return UINT_MAX; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | if (isalpha(s[0])) |
| 72 | return android_name_to_id(s); |
| 73 | |
| 74 | errno = 0; |
| 75 | v = (unsigned int) strtoul(s, 0, 0); |
| 76 | if (errno) |
Nick Kralevich | d2104df | 2015-06-18 17:46:54 -0700 | [diff] [blame] | 77 | return UINT_MAX; |
| 78 | return v; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * decode_uid - decodes and returns the given string, which can be either the |
| 83 | * numeric or name representation, into the integer uid or gid. Returns |
| 84 | * UINT_MAX on error. |
| 85 | */ |
| 86 | unsigned int decode_uid(const char *s) { |
| 87 | unsigned int v = do_decode_uid(s); |
| 88 | if (v == UINT_MAX) { |
| 89 | ERROR("decode_uid: Unable to find UID for '%s'. Returning UINT_MAX\n", s); |
| 90 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | return v; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR |
| 96 | * ("/dev/socket") as dictated in init.rc. This socket is inherited by the |
| 97 | * daemon. We communicate the file descriptor's value via the environment |
| 98 | * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo"). |
| 99 | */ |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 100 | int create_socket(const char *name, int type, mode_t perm, uid_t uid, |
| 101 | gid_t gid, const char *socketcon) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 102 | { |
| 103 | struct sockaddr_un addr; |
| 104 | int fd, ret; |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 105 | char *filecon; |
| 106 | |
| 107 | if (socketcon) |
| 108 | setsockcreatecon(socketcon); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | |
| 110 | fd = socket(PF_UNIX, type, 0); |
| 111 | if (fd < 0) { |
| 112 | ERROR("Failed to open socket '%s': %s\n", name, strerror(errno)); |
| 113 | return -1; |
| 114 | } |
| 115 | |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 116 | if (socketcon) |
| 117 | setsockcreatecon(NULL); |
| 118 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 119 | memset(&addr, 0 , sizeof(addr)); |
| 120 | addr.sun_family = AF_UNIX; |
| 121 | snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s", |
| 122 | name); |
| 123 | |
| 124 | ret = unlink(addr.sun_path); |
| 125 | if (ret != 0 && errno != ENOENT) { |
| 126 | ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno)); |
| 127 | goto out_close; |
| 128 | } |
| 129 | |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 130 | filecon = NULL; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 131 | if (sehandle) { |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 132 | ret = selabel_lookup(sehandle, &filecon, addr.sun_path, S_IFSOCK); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 133 | if (ret == 0) |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 134 | setfscreatecon(filecon); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 135 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 136 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr)); |
| 138 | if (ret) { |
| 139 | ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno)); |
| 140 | goto out_unlink; |
| 141 | } |
| 142 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 143 | setfscreatecon(NULL); |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 144 | freecon(filecon); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 145 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | chown(addr.sun_path, uid, gid); |
| 147 | chmod(addr.sun_path, perm); |
| 148 | |
| 149 | INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n", |
| 150 | addr.sun_path, perm, uid, gid); |
| 151 | |
| 152 | return fd; |
| 153 | |
| 154 | out_unlink: |
| 155 | unlink(addr.sun_path); |
| 156 | out_close: |
| 157 | close(fd); |
| 158 | return -1; |
| 159 | } |
| 160 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 161 | bool read_file(const char* path, std::string* content) { |
| 162 | content->clear(); |
| 163 | |
| 164 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC)); |
| 165 | if (fd == -1) { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | // For security reasons, disallow world-writable |
| 170 | // or group-writable files. |
Nick Kralevich | 38f368c | 2012-01-18 10:39:01 -0800 | [diff] [blame] | 171 | struct stat sb; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 172 | if (fstat(fd, &sb) == -1) { |
| 173 | ERROR("fstat failed for '%s': %s\n", path, strerror(errno)); |
| 174 | return false; |
Nick Kralevich | 38f368c | 2012-01-18 10:39:01 -0800 | [diff] [blame] | 175 | } |
| 176 | if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) { |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 177 | ERROR("skipping insecure file '%s'\n", path); |
| 178 | return false; |
Nick Kralevich | 38f368c | 2012-01-18 10:39:01 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 181 | bool okay = android::base::ReadFdToString(fd, content); |
Elliott Hughes | 9fc8343 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 182 | close(fd); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 183 | return okay; |
| 184 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 186 | int write_file(const char* path, const char* content) { |
| 187 | int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600)); |
| 188 | if (fd == -1) { |
Nick Kralevich | eedbe81 | 2015-04-25 14:10:03 -0700 | [diff] [blame] | 189 | NOTICE("write_file: Unable to open '%s': %s\n", path, strerror(errno)); |
| 190 | return -1; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 191 | } |
Nick Kralevich | eedbe81 | 2015-04-25 14:10:03 -0700 | [diff] [blame] | 192 | int result = android::base::WriteStringToFd(content, fd) ? 0 : -1; |
| 193 | if (result == -1) { |
| 194 | NOTICE("write_file: Unable to write to '%s': %s\n", path, strerror(errno)); |
| 195 | } |
Elliott Hughes | 9fc8343 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 196 | close(fd); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 197 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Colin Cross | f24ed8c | 2010-04-12 18:51:06 -0700 | [diff] [blame] | 200 | #define MAX_MTD_PARTITIONS 16 |
| 201 | |
| 202 | static struct { |
| 203 | char name[16]; |
| 204 | int number; |
| 205 | } mtd_part_map[MAX_MTD_PARTITIONS]; |
| 206 | |
| 207 | static int mtd_part_count = -1; |
| 208 | |
| 209 | static void find_mtd_partitions(void) |
| 210 | { |
| 211 | int fd; |
| 212 | char buf[1024]; |
| 213 | char *pmtdbufp; |
| 214 | ssize_t pmtdsize; |
| 215 | int r; |
| 216 | |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 217 | fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC); |
Colin Cross | f24ed8c | 2010-04-12 18:51:06 -0700 | [diff] [blame] | 218 | if (fd < 0) |
| 219 | return; |
| 220 | |
| 221 | buf[sizeof(buf) - 1] = '\0'; |
| 222 | pmtdsize = read(fd, buf, sizeof(buf) - 1); |
| 223 | pmtdbufp = buf; |
| 224 | while (pmtdsize > 0) { |
| 225 | int mtdnum, mtdsize, mtderasesize; |
| 226 | char mtdname[16]; |
| 227 | mtdname[0] = '\0'; |
| 228 | mtdnum = -1; |
| 229 | r = sscanf(pmtdbufp, "mtd%d: %x %x %15s", |
| 230 | &mtdnum, &mtdsize, &mtderasesize, mtdname); |
| 231 | if ((r == 4) && (mtdname[0] == '"')) { |
| 232 | char *x = strchr(mtdname + 1, '"'); |
| 233 | if (x) { |
| 234 | *x = 0; |
| 235 | } |
| 236 | INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1); |
| 237 | if (mtd_part_count < MAX_MTD_PARTITIONS) { |
| 238 | strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1); |
| 239 | mtd_part_map[mtd_part_count].number = mtdnum; |
| 240 | mtd_part_count++; |
| 241 | } else { |
| 242 | ERROR("too many mtd partitions\n"); |
| 243 | } |
| 244 | } |
| 245 | while (pmtdsize > 0 && *pmtdbufp != '\n') { |
| 246 | pmtdbufp++; |
| 247 | pmtdsize--; |
| 248 | } |
| 249 | if (pmtdsize > 0) { |
| 250 | pmtdbufp++; |
| 251 | pmtdsize--; |
| 252 | } |
| 253 | } |
| 254 | close(fd); |
| 255 | } |
| 256 | |
| 257 | int mtd_name_to_number(const char *name) |
| 258 | { |
| 259 | int n; |
| 260 | if (mtd_part_count < 0) { |
| 261 | mtd_part_count = 0; |
| 262 | find_mtd_partitions(); |
| 263 | } |
| 264 | for (n = 0; n < mtd_part_count; n++) { |
| 265 | if (!strcmp(name, mtd_part_map[n].name)) { |
| 266 | return mtd_part_map[n].number; |
| 267 | } |
| 268 | } |
| 269 | return -1; |
| 270 | } |
Colin Cross | 504bc51 | 2010-04-13 19:35:09 -0700 | [diff] [blame] | 271 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 272 | time_t gettime() { |
| 273 | timespec now; |
| 274 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 275 | return now.tv_sec; |
| 276 | } |
Colin Cross | 504bc51 | 2010-04-13 19:35:09 -0700 | [diff] [blame] | 277 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 278 | uint64_t gettime_ns() { |
| 279 | timespec now; |
| 280 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 281 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec; |
Colin Cross | 504bc51 | 2010-04-13 19:35:09 -0700 | [diff] [blame] | 282 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 283 | |
| 284 | int mkdir_recursive(const char *pathname, mode_t mode) |
| 285 | { |
| 286 | char buf[128]; |
| 287 | const char *slash; |
| 288 | const char *p = pathname; |
| 289 | int width; |
| 290 | int ret; |
| 291 | struct stat info; |
| 292 | |
| 293 | while ((slash = strchr(p, '/')) != NULL) { |
| 294 | width = slash - pathname; |
| 295 | p = slash + 1; |
| 296 | if (width < 0) |
| 297 | break; |
| 298 | if (width == 0) |
| 299 | continue; |
| 300 | if ((unsigned int)width > sizeof(buf) - 1) { |
| 301 | ERROR("path too long for mkdir_recursive\n"); |
| 302 | return -1; |
| 303 | } |
| 304 | memcpy(buf, pathname, width); |
| 305 | buf[width] = 0; |
| 306 | if (stat(buf, &info) != 0) { |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 307 | ret = make_dir(buf, mode); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 308 | if (ret && errno != EEXIST) |
| 309 | return ret; |
| 310 | } |
| 311 | } |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 312 | ret = make_dir(pathname, mode); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 313 | if (ret && errno != EEXIST) |
| 314 | return ret; |
| 315 | return 0; |
| 316 | } |
| 317 | |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 318 | /* |
| 319 | * replaces any unacceptable characters with '_', the |
| 320 | * length of the resulting string is equal to the input string |
| 321 | */ |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 322 | void sanitize(char *s) |
| 323 | { |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 324 | const char* accept = |
| 325 | "abcdefghijklmnopqrstuvwxyz" |
| 326 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 327 | "0123456789" |
| 328 | "_-."; |
| 329 | |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 330 | if (!s) |
| 331 | return; |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 332 | |
Christopher R. Palmer | 07f3fee | 2014-09-22 14:35:54 -0400 | [diff] [blame] | 333 | while (*s) { |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 334 | s += strspn(s, accept); |
Christopher R. Palmer | 07f3fee | 2014-09-22 14:35:54 -0400 | [diff] [blame] | 335 | if (*s) *s++ = '_'; |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 336 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 337 | } |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 338 | |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 339 | void make_link_init(const char *oldpath, const char *newpath) |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 340 | { |
| 341 | int ret; |
| 342 | char buf[256]; |
| 343 | char *slash; |
| 344 | int width; |
| 345 | |
| 346 | slash = strrchr(newpath, '/'); |
| 347 | if (!slash) |
| 348 | return; |
| 349 | width = slash - newpath; |
| 350 | if (width <= 0 || width > (int)sizeof(buf) - 1) |
| 351 | return; |
| 352 | memcpy(buf, newpath, width); |
| 353 | buf[width] = 0; |
| 354 | ret = mkdir_recursive(buf, 0755); |
| 355 | if (ret) |
| 356 | ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno); |
| 357 | |
| 358 | ret = symlink(oldpath, newpath); |
| 359 | if (ret && errno != EEXIST) |
| 360 | ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno); |
| 361 | } |
| 362 | |
| 363 | void remove_link(const char *oldpath, const char *newpath) |
| 364 | { |
| 365 | char path[256]; |
| 366 | ssize_t ret; |
| 367 | ret = readlink(newpath, path, sizeof(path) - 1); |
| 368 | if (ret <= 0) |
| 369 | return; |
| 370 | path[ret] = 0; |
| 371 | if (!strcmp(path, oldpath)) |
| 372 | unlink(newpath); |
| 373 | } |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 374 | |
| 375 | int wait_for_file(const char *filename, int timeout) |
| 376 | { |
| 377 | struct stat info; |
Thierry Strudel | 91cf41c | 2015-05-22 15:56:14 -0700 | [diff] [blame] | 378 | uint64_t timeout_time_ns = gettime_ns() + timeout * UINT64_C(1000000000); |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 379 | int ret = -1; |
| 380 | |
Thierry Strudel | 91cf41c | 2015-05-22 15:56:14 -0700 | [diff] [blame] | 381 | while (gettime_ns() < timeout_time_ns && ((ret = stat(filename, &info)) < 0)) |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 382 | usleep(10000); |
| 383 | |
| 384 | return ret; |
| 385 | } |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 386 | |
| 387 | void open_devnull_stdio(void) |
| 388 | { |
Nick Kralevich | e34577c | 2015-04-25 16:24:53 -0700 | [diff] [blame] | 389 | // Try to avoid the mknod() call if we can. Since SELinux makes |
| 390 | // a /dev/null replacement available for free, let's use it. |
| 391 | int fd = open("/sys/fs/selinux/null", O_RDWR); |
| 392 | if (fd == -1) { |
| 393 | // OOPS, /sys/fs/selinux/null isn't available, likely because |
| 394 | // /sys/fs/selinux isn't mounted. Fall back to mknod. |
| 395 | static const char *name = "/dev/__null__"; |
| 396 | if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) { |
| 397 | fd = open(name, O_RDWR); |
| 398 | unlink(name); |
| 399 | } |
| 400 | if (fd == -1) { |
| 401 | exit(1); |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Nick Kralevich | e34577c | 2015-04-25 16:24:53 -0700 | [diff] [blame] | 405 | dup2(fd, 0); |
| 406 | dup2(fd, 1); |
| 407 | dup2(fd, 2); |
| 408 | if (fd > 2) { |
| 409 | close(fd); |
| 410 | } |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 413 | void import_kernel_cmdline(bool in_qemu, |
| 414 | std::function<void(const std::string&, const std::string&, bool)> fn) { |
| 415 | std::string cmdline; |
| 416 | android::base::ReadFileToString("/proc/cmdline", &cmdline); |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 417 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 418 | for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) { |
| 419 | std::vector<std::string> pieces = android::base::Split(entry, "="); |
| 420 | if (pieces.size() == 2) { |
| 421 | fn(pieces[0], pieces[1], in_qemu); |
| 422 | } |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 425 | |
| 426 | int make_dir(const char *path, mode_t mode) |
| 427 | { |
| 428 | int rc; |
| 429 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 430 | char *secontext = NULL; |
| 431 | |
| 432 | if (sehandle) { |
| 433 | selabel_lookup(sehandle, &secontext, path, mode); |
| 434 | setfscreatecon(secontext); |
| 435 | } |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 436 | |
| 437 | rc = mkdir(path, mode); |
| 438 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 439 | if (secontext) { |
| 440 | int save_errno = errno; |
| 441 | freecon(secontext); |
| 442 | setfscreatecon(NULL); |
| 443 | errno = save_errno; |
| 444 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 445 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 446 | return rc; |
| 447 | } |
| 448 | |
Stephen Smalley | dbd37f2 | 2014-01-28 10:34:09 -0500 | [diff] [blame] | 449 | int restorecon(const char* pathname) |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 450 | { |
Stephen Smalley | 27a9365 | 2014-02-07 09:14:13 -0500 | [diff] [blame] | 451 | return selinux_android_restorecon(pathname, 0); |
Nick Kralevich | ae76f6d | 2013-07-11 15:38:26 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | int restorecon_recursive(const char* pathname) |
| 455 | { |
Stephen Smalley | 27a9365 | 2014-02-07 09:14:13 -0500 | [diff] [blame] | 456 | return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE); |
Nick Kralevich | ae76f6d | 2013-07-11 15:38:26 -0700 | [diff] [blame] | 457 | } |
Andres Morales | db5f5d4 | 2015-05-08 08:30:33 -0700 | [diff] [blame] | 458 | |
| 459 | /* |
| 460 | * Writes hex_len hex characters (1/2 byte) to hex from bytes. |
| 461 | */ |
| 462 | std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) { |
| 463 | std::string hex("0x"); |
| 464 | for (size_t i = 0; i < bytes_len; i++) |
| 465 | android::base::StringAppendF(&hex, "%02x", bytes[i]); |
| 466 | return hex; |
| 467 | } |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 468 | |
| 469 | /* |
| 470 | * Returns true is pathname is a directory |
| 471 | */ |
| 472 | bool is_dir(const char* pathname) { |
| 473 | struct stat info; |
| 474 | if (stat(pathname, &info) == -1) { |
| 475 | return false; |
| 476 | } |
| 477 | return S_ISDIR(info.st_mode); |
| 478 | } |