android_get_control_file.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <cutils/android_get_control_file.h>
  29. #include <ctype.h>
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <limits.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <sys/stat.h>
  37. #include <sys/types.h>
  38. #include <unistd.h>
  39. #include <string>
  40. #include <android-base/file.h>
  41. #include <android-base/stringprintf.h>
  42. #include "android_get_control_env.h"
  43. int __android_get_control_from_env(const char* prefix, const char* name) {
  44. if (!prefix || !name) return -1;
  45. char *key = NULL;
  46. if (asprintf(&key, "%s%s", prefix, name) < 0) return -1;
  47. if (!key) return -1;
  48. char *cp = key;
  49. while (*cp) {
  50. if (!isalnum(*cp)) *cp = '_';
  51. ++cp;
  52. }
  53. const char* val = getenv(key);
  54. free(key);
  55. if (!val) return -1;
  56. errno = 0;
  57. long fd = strtol(val, NULL, 10);
  58. if (errno) return -1;
  59. // Since we are inheriting an fd, it could legitimately exceed _SC_OPEN_MAX
  60. if ((fd < 0) || (fd > INT_MAX)) return -1;
  61. // Still open?
  62. if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD)) < 0) return -1;
  63. return static_cast<int>(fd);
  64. }
  65. int android_get_control_file(const char* path) {
  66. std::string given_path;
  67. if (!android::base::Realpath(path, &given_path)) return -1;
  68. // Try path, then realpath(path), as keys to get the fd from env.
  69. auto fd = __android_get_control_from_env(ANDROID_FILE_ENV_PREFIX, path);
  70. if (fd < 0) {
  71. fd = __android_get_control_from_env(ANDROID_FILE_ENV_PREFIX, given_path.c_str());
  72. if (fd < 0) return fd;
  73. }
  74. // Find file path from /proc and make sure it is correct
  75. auto proc = android::base::StringPrintf("/proc/self/fd/%d", fd);
  76. std::string fd_path;
  77. if (!android::base::Realpath(proc, &fd_path)) return -1;
  78. if (given_path != fd_path) return -1;
  79. // It is what we think it is
  80. return fd;
  81. }