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