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 */ 28 29 #include <cutils/android_get_control_file.h> 30 31 #include <ctype.h> 32 #include <errno.h> 33 #include <fcntl.h> 34 #include <limits.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <sys/stat.h> 39 #include <sys/types.h> 40 #include <unistd.h> 41 42 #include "android_get_control_env.h" 43 44 #ifndef TEMP_FAILURE_RETRY 45 #define TEMP_FAILURE_RETRY(exp) (exp) // KISS implementation 46 #endif 47 48 LIBCUTILS_HIDDEN int __android_get_control_from_env(const char* prefix, 49 const char* name) { 50 if (!prefix || !name) return -1; 51 52 char *key = NULL; 53 if (asprintf(&key, "%s%s", prefix, name) < 0) return -1; 54 if (!key) return -1; 55 56 char *cp = key; 57 while (*cp) { 58 if (!isalnum(*cp)) *cp = '_'; 59 ++cp; 60 } 61 62 const char* val = getenv(key); 63 free(key); 64 if (!val) return -1; 65 66 errno = 0; 67 long fd = strtol(val, NULL, 10); 68 if (errno) return -1; 69 70 // validity checking 71 if ((fd < 0) || (fd > INT_MAX)) return -1; 72 73 // Since we are inheriting an fd, it could legitimately exceed _SC_OPEN_MAX 74 75 // Still open? 76 #if defined(F_GETFD) // Lowest overhead 77 if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD)) < 0) return -1; 78 #elif defined(F_GETFL) // Alternate lowest overhead 79 if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL)) < 0) return -1; 80 #else // Hail Mary pass 81 struct stat s; 82 if (TEMP_FAILURE_RETRY(fstat(fd, &s)) < 0) return -1; 83 #endif 84 85 return static_cast<int>(fd); 86 } 87 88 int android_get_control_file(const char* path) { 89 int fd = __android_get_control_from_env(ANDROID_FILE_ENV_PREFIX, path); 90 91 #if defined(__linux__) 92 // Find file path from /proc and make sure it is correct 93 char *proc = NULL; 94 if (asprintf(&proc, "/proc/self/fd/%d", fd) < 0) return -1; 95 if (!proc) return -1; 96 97 size_t len = strlen(path); 98 // readlink() does not guarantee a nul byte, len+2 so we catch truncation. 99 char *buf = static_cast<char *>(calloc(1, len + 2)); 100 if (!buf) { 101 free(proc); 102 return -1; 103 } 104 ssize_t ret = TEMP_FAILURE_RETRY(readlink(proc, buf, len + 1)); 105 free(proc); 106 int cmp = (len != static_cast<size_t>(ret)) || strcmp(buf, path); 107 free(buf); 108 if (ret < 0) return -1; 109 if (cmp != 0) return -1; 110 // It is what we think it is 111 #endif 112 113 return fd; 114 } 115