blob: bf15b4283cda822b484247ad480dededc5369974 [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 */
28
29// This file contains files implementation that can be shared between
30// platforms as long as the correct headers are included.
31#define _GNU_SOURCE 1 // for asprintf
32
33#include <ctype.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <limits.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/stat.h>
41#include <sys/types.h>
42#include <unistd.h>
43
44#include <cutils/files.h>
45
46#ifndef TEMP_FAILURE_RETRY // _WIN32 does not define
47#define TEMP_FAILURE_RETRY(exp) (exp)
48#endif
49
50int android_get_control_file(const char* path) {
51 if (!path) return -1;
52
53 char *key = NULL;
54 if (asprintf(&key, ANDROID_FILE_ENV_PREFIX "%s", path) < 0) return -1;
55 if (!key) return -1;
56
57 char *cp = key;
58 while (*cp) {
59 if (!isalnum(*cp)) *cp = '_';
60 ++cp;
61 }
62
63 const char* val = getenv(key);
64 free(key);
65 if (!val) return -1;
66
67 errno = 0;
68 long fd = strtol(val, NULL, 10);
69 if (errno) return -1;
70
71 // validity checking
72 if ((fd < 0) || (fd > INT_MAX)) return -1;
73#if defined(_SC_OPEN_MAX)
74 if (fd >= sysconf(_SC_OPEN_MAX)) return -1;
75#elif defined(OPEN_MAX)
76 if (fd >= OPEN_MAX) return -1;
77#elif defined(_POSIX_OPEN_MAX)
78 if (fd >= _POSIX_OPEN_MAX) return -1;
79#endif
80
81#if defined(F_GETFD)
82 if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD)) < 0) return -1;
83#elif defined(F_GETFL)
84 if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL)) < 0) return -1;
85#else
86 struct stat s;
87 if (TEMP_FAILURE_RETRY(fstat(fd, &s)) < 0) return -1;
88#endif
89
90#if defined(__linux__)
91 char *proc = NULL;
92 if (asprintf(&proc, "/proc/self/fd/%ld", fd) < 0) return -1;
93 if (!proc) return -1;
94
95 size_t len = strlen(path);
96 char *buf = static_cast<char *>(calloc(1, len + 2));
97 if (!buf) {
98 free(proc);
99 return -1;
100 }
101 ssize_t ret = TEMP_FAILURE_RETRY(readlink(proc, buf, len + 1));
102 free(proc);
103 int cmp = (len != static_cast<size_t>(ret)) || strcmp(buf, path);
104 free(buf);
105 if (ret < 0) return -1;
106 if (cmp != 0) return -1;
107#endif
108
109 // It is what we think it is
110 return static_cast<int>(fd);
111}