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 <string>
43 
44 #include <android-base/file.h>
45 #include <android-base/stringprintf.h>
46 
47 #include "android_get_control_env.h"
48 
__android_get_control_from_env(const char * prefix,const char * name)49 int __android_get_control_from_env(const char* prefix, 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     // Since we are inheriting an fd, it could legitimately exceed _SC_OPEN_MAX
71     if ((fd < 0) || (fd > INT_MAX)) return -1;
72 
73     // Still open?
74     if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD)) < 0) return -1;
75 
76     return static_cast<int>(fd);
77 }
78 
android_get_control_file(const char * path)79 int android_get_control_file(const char* path) {
80     std::string given_path;
81     if (!android::base::Realpath(path, &given_path)) return -1;
82 
83     // Try path, then realpath(path), as keys to get the fd from env.
84     auto fd = __android_get_control_from_env(ANDROID_FILE_ENV_PREFIX, path);
85     if (fd < 0) {
86         fd = __android_get_control_from_env(ANDROID_FILE_ENV_PREFIX, given_path.c_str());
87         if (fd < 0) return fd;
88     }
89 
90     // Find file path from /proc and make sure it is correct
91     auto proc = android::base::StringPrintf("/proc/self/fd/%d", fd);
92     std::string fd_path;
93     if (!android::base::Realpath(proc, &fd_path)) return -1;
94 
95     if (given_path != fd_path) return -1;
96     // It is what we think it is
97 
98     return fd;
99 }
100