openat-proc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Create /proc/self/fd-related names for subfiles of open directories.
  2. Copyright (C) 2006, 2009-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. #include <config.h>
  15. #include "openat-priv.h"
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #ifdef __KLIBC__
  24. # include <InnoTekLIBC/backend.h>
  25. #endif
  26. #include "intprops.h"
  27. /* Set BUF to the name of the subfile of the directory identified by
  28. FD, where the subfile is named FILE. If successful, return BUF if
  29. the result fits in BUF, dynamically allocated memory otherwise.
  30. Return NULL (setting errno) on error. */
  31. char *
  32. openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file)
  33. {
  34. char *result = buf;
  35. int dirlen;
  36. /* Make sure the caller gets ENOENT when appropriate. */
  37. if (!*file)
  38. {
  39. buf[0] = '\0';
  40. return buf;
  41. }
  42. #ifndef __KLIBC__
  43. # define PROC_SELF_FD_FORMAT "/proc/self/fd/%d/"
  44. {
  45. enum {
  46. PROC_SELF_FD_DIR_SIZE_BOUND
  47. = (sizeof PROC_SELF_FD_FORMAT - (sizeof "%d" - 1)
  48. + INT_STRLEN_BOUND (int))
  49. };
  50. static int proc_status = 0;
  51. if (! proc_status)
  52. {
  53. /* Set PROC_STATUS to a positive value if /proc/self/fd is
  54. reliable, and a negative value otherwise. Solaris 10
  55. /proc/self/fd mishandles "..", and any file name might expand
  56. to ".." after symbolic link expansion, so avoid /proc/self/fd
  57. if it mishandles "..". Solaris 10 has openat, but this
  58. problem is exhibited on code that built on Solaris 8 and
  59. running on Solaris 10. */
  60. int proc_self_fd =
  61. open ("/proc/self/fd",
  62. O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK | O_CLOEXEC);
  63. if (proc_self_fd < 0)
  64. proc_status = -1;
  65. else
  66. {
  67. /* Detect whether /proc/self/fd/%i/../fd exists, where %i is the
  68. number of a file descriptor open on /proc/self/fd. On Linux,
  69. that name resolves to /proc/self/fd, which was opened above.
  70. However, on Solaris, it may resolve to /proc/self/fd/fd, which
  71. cannot exist, since all names in /proc/self/fd are numeric. */
  72. char dotdot_buf[PROC_SELF_FD_DIR_SIZE_BOUND + sizeof "../fd" - 1];
  73. sprintf (dotdot_buf, PROC_SELF_FD_FORMAT "../fd", proc_self_fd);
  74. proc_status = access (dotdot_buf, F_OK) ? -1 : 1;
  75. close (proc_self_fd);
  76. }
  77. }
  78. if (proc_status < 0)
  79. return NULL;
  80. else
  81. {
  82. size_t bufsize = PROC_SELF_FD_DIR_SIZE_BOUND + strlen (file);
  83. if (OPENAT_BUFFER_SIZE < bufsize)
  84. {
  85. result = malloc (bufsize);
  86. if (! result)
  87. return NULL;
  88. }
  89. dirlen = sprintf (result, PROC_SELF_FD_FORMAT, fd);
  90. }
  91. }
  92. #else
  93. /* OS/2 kLIBC provides a function to retrieve a path from a fd. */
  94. {
  95. char dir[_MAX_PATH];
  96. size_t bufsize;
  97. if (__libc_Back_ioFHToPath (fd, dir, sizeof dir))
  98. return NULL;
  99. dirlen = strlen (dir);
  100. bufsize = dirlen + 1 + strlen (file) + 1; /* 1 for '/', 1 for null */
  101. if (OPENAT_BUFFER_SIZE < bufsize)
  102. {
  103. result = malloc (bufsize);
  104. if (! result)
  105. return NULL;
  106. }
  107. strcpy (result, dir);
  108. result[dirlen++] = '/';
  109. }
  110. #endif
  111. strcpy (result + dirlen, file);
  112. return result;
  113. }