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