Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 28 | #include <ctype.h> |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <limits.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <unistd.h> |
| 38 | |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame^] | 39 | #include <cutils/android_get_control_file.h> |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 40 | |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame^] | 41 | #include "android_get_control_env.h" |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 42 | |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame^] | 43 | LIBCUTILS_HIDDEN int __android_get_control_from_env(const char* prefix, |
| 44 | const char* name) { |
| 45 | if (!prefix || !name) return -1; |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 46 | |
| 47 | char *key = NULL; |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame^] | 48 | if (asprintf(&key, "%s%s", prefix, name) < 0) return -1; |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 49 | if (!key) return -1; |
| 50 | |
| 51 | char *cp = key; |
| 52 | while (*cp) { |
| 53 | if (!isalnum(*cp)) *cp = '_'; |
| 54 | ++cp; |
| 55 | } |
| 56 | |
| 57 | const char* val = getenv(key); |
| 58 | free(key); |
| 59 | if (!val) return -1; |
| 60 | |
| 61 | errno = 0; |
| 62 | long fd = strtol(val, NULL, 10); |
| 63 | if (errno) return -1; |
| 64 | |
| 65 | // validity checking |
| 66 | if ((fd < 0) || (fd > INT_MAX)) return -1; |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame^] | 67 | |
| 68 | // Since we are inheriting an fd, it could legitimately exceed _SC_OPEN_MAX |
| 69 | |
| 70 | // Still open? |
| 71 | #if defined(F_GETFD) // Linux lowest overhead |
| 72 | if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD)) < 0) return -1; |
| 73 | #elif defined(F_GETFL) // Mac host lowest overhead |
| 74 | if (fcntl(fd, F_GETFL) < 0) return -1; |
| 75 | #else // Hail Mary pass |
| 76 | struct stat s; |
| 77 | if (fstat(fd, &s) < 0) return -1; |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 78 | #endif |
| 79 | |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame^] | 80 | return static_cast<int>(fd); |
| 81 | } |
| 82 | |
| 83 | int android_get_control_file(const char* path) { |
| 84 | int fd = __android_get_control_from_env(ANDROID_FILE_ENV_PREFIX, path); |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 85 | |
| 86 | #if defined(__linux__) |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame^] | 87 | // Find file path from /proc and make sure it is correct |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 88 | char *proc = NULL; |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame^] | 89 | if (asprintf(&proc, "/proc/self/fd/%d", fd) < 0) return -1; |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 90 | if (!proc) return -1; |
| 91 | |
| 92 | size_t len = strlen(path); |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame^] | 93 | // readlink() does not guarantee a nul byte, len+2 so we catch truncation. |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 94 | char *buf = static_cast<char *>(calloc(1, len + 2)); |
| 95 | if (!buf) { |
| 96 | free(proc); |
| 97 | return -1; |
| 98 | } |
| 99 | ssize_t ret = TEMP_FAILURE_RETRY(readlink(proc, buf, len + 1)); |
| 100 | free(proc); |
| 101 | int cmp = (len != static_cast<size_t>(ret)) || strcmp(buf, path); |
| 102 | free(buf); |
| 103 | if (ret < 0) return -1; |
| 104 | if (cmp != 0) return -1; |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame^] | 105 | // It is what we think it is |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 106 | #endif |
| 107 | |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame^] | 108 | return fd; |
Mark Salyzyn | 0b034d9 | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 109 | } |