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