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