blob: 780d9f136cbcfc6cf30f35408c29b3d5451d3223 [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 Salyzyn6b652162016-11-17 08:05:10 -080043#ifndef TEMP_FAILURE_RETRY
44#define TEMP_FAILURE_RETRY(exp) (exp) // KISS implementation
45#endif
46
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080047LIBCUTILS_HIDDEN int __android_get_control_from_env(const char* prefix,
48 const char* name) {
49 if (!prefix || !name) return -1;
Mark Salyzyn0b034d92016-10-27 08:04:48 -070050
51 char *key = NULL;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080052 if (asprintf(&key, "%s%s", prefix, name) < 0) return -1;
Mark Salyzyn0b034d92016-10-27 08:04:48 -070053 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;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080071
72 // Since we are inheriting an fd, it could legitimately exceed _SC_OPEN_MAX
73
74 // Still open?
Mark Salyzyn6b652162016-11-17 08:05:10 -080075#if defined(F_GETFD) // Lowest overhead
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080076 if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD)) < 0) return -1;
Mark Salyzyn6b652162016-11-17 08:05:10 -080077#elif defined(F_GETFL) // Alternate lowest overhead
78 if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL)) < 0) return -1;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080079#else // Hail Mary pass
80 struct stat s;
Mark Salyzyn6b652162016-11-17 08:05:10 -080081 if (TEMP_FAILURE_RETRY(fstat(fd, &s)) < 0) return -1;
Mark Salyzyn0b034d92016-10-27 08:04:48 -070082#endif
83
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080084 return static_cast<int>(fd);
85}
86
87int android_get_control_file(const char* path) {
88 int fd = __android_get_control_from_env(ANDROID_FILE_ENV_PREFIX, path);
Mark Salyzyn0b034d92016-10-27 08:04:48 -070089
90#if defined(__linux__)
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080091 // Find file path from /proc and make sure it is correct
Mark Salyzyn0b034d92016-10-27 08:04:48 -070092 char *proc = NULL;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080093 if (asprintf(&proc, "/proc/self/fd/%d", fd) < 0) return -1;
Mark Salyzyn0b034d92016-10-27 08:04:48 -070094 if (!proc) return -1;
95
96 size_t len = strlen(path);
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080097 // readlink() does not guarantee a nul byte, len+2 so we catch truncation.
Mark Salyzyn0b034d92016-10-27 08:04:48 -070098 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;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -0800109 // It is what we think it is
Mark Salyzyn0b034d92016-10-27 08:04:48 -0700110#endif
111
Mark Salyzyn52bd37e2016-11-07 09:39:30 -0800112 return fd;
Mark Salyzyn0b034d92016-10-27 08:04:48 -0700113}