blob: 496fbbfe655bbd943ecab4ac753a21fbc48e0220 [file] [log] [blame]
Mark Salyzyn0b034d92016-10-27 08:04:48 -07001/*
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 Salyzyn0b034d92016-10-27 08:04:48 -070028#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 Salyzyn52bd37e2016-11-07 09:39:30 -080039#include <cutils/android_get_control_file.h>
Mark Salyzyn0b034d92016-10-27 08:04:48 -070040
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080041#include "android_get_control_env.h"
Mark Salyzyn0b034d92016-10-27 08:04:48 -070042
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080043LIBCUTILS_HIDDEN int __android_get_control_from_env(const char* prefix,
44 const char* name) {
45 if (!prefix || !name) return -1;
Mark Salyzyn0b034d92016-10-27 08:04:48 -070046
47 char *key = NULL;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080048 if (asprintf(&key, "%s%s", prefix, name) < 0) return -1;
Mark Salyzyn0b034d92016-10-27 08:04:48 -070049 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 Salyzyn52bd37e2016-11-07 09:39:30 -080067
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 Salyzyn0b034d92016-10-27 08:04:48 -070078#endif
79
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080080 return static_cast<int>(fd);
81}
82
83int android_get_control_file(const char* path) {
84 int fd = __android_get_control_from_env(ANDROID_FILE_ENV_PREFIX, path);
Mark Salyzyn0b034d92016-10-27 08:04:48 -070085
86#if defined(__linux__)
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080087 // Find file path from /proc and make sure it is correct
Mark Salyzyn0b034d92016-10-27 08:04:48 -070088 char *proc = NULL;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080089 if (asprintf(&proc, "/proc/self/fd/%d", fd) < 0) return -1;
Mark Salyzyn0b034d92016-10-27 08:04:48 -070090 if (!proc) return -1;
91
92 size_t len = strlen(path);
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080093 // readlink() does not guarantee a nul byte, len+2 so we catch truncation.
Mark Salyzyn0b034d92016-10-27 08:04:48 -070094 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 Salyzyn52bd37e2016-11-07 09:39:30 -0800105 // It is what we think it is
Mark Salyzyn0b034d92016-10-27 08:04:48 -0700106#endif
107
Mark Salyzyn52bd37e2016-11-07 09:39:30 -0800108 return fd;
Mark Salyzyn0b034d92016-10-27 08:04:48 -0700109}