openat-proc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Create /proc/self/fd-related names for subfiles of open directories.
  2. Copyright (C) 2006, 2009-2015 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 <http://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. #include "intprops.h"
  24. #define PROC_SELF_FD_FORMAT "/proc/self/fd/%d/%s"
  25. #define PROC_SELF_FD_NAME_SIZE_BOUND(len) \
  26. (sizeof PROC_SELF_FD_FORMAT - sizeof "%d%s" \
  27. + INT_STRLEN_BOUND (int) + (len) + 1)
  28. /* Set BUF to the expansion of PROC_SELF_FD_FORMAT, using FD and FILE
  29. respectively for %d and %s. If successful, return BUF if the
  30. result fits in BUF, dynamically allocated memory otherwise. But
  31. return NULL if /proc is not reliable, either because the operating
  32. system support is lacking or because memory is low. */
  33. char *
  34. openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file)
  35. {
  36. static int proc_status = 0;
  37. /* Make sure the caller gets ENOENT when appropriate. */
  38. if (!*file)
  39. {
  40. buf[0] = '\0';
  41. return buf;
  42. }
  43. if (! proc_status)
  44. {
  45. /* Set PROC_STATUS to a positive value if /proc/self/fd is
  46. reliable, and a negative value otherwise. Solaris 10
  47. /proc/self/fd mishandles "..", and any file name might expand
  48. to ".." after symbolic link expansion, so avoid /proc/self/fd
  49. if it mishandles "..". Solaris 10 has openat, but this
  50. problem is exhibited on code that built on Solaris 8 and
  51. running on Solaris 10. */
  52. int proc_self_fd = open ("/proc/self/fd",
  53. O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK);
  54. if (proc_self_fd < 0)
  55. proc_status = -1;
  56. else
  57. {
  58. /* Detect whether /proc/self/fd/%i/../fd exists, where %i is the
  59. number of a file descriptor open on /proc/self/fd. On Linux,
  60. that name resolves to /proc/self/fd, which was opened above.
  61. However, on Solaris, it may resolve to /proc/self/fd/fd, which
  62. cannot exist, since all names in /proc/self/fd are numeric. */
  63. char dotdot_buf[PROC_SELF_FD_NAME_SIZE_BOUND (sizeof "../fd" - 1)];
  64. sprintf (dotdot_buf, PROC_SELF_FD_FORMAT, proc_self_fd, "../fd");
  65. proc_status = access (dotdot_buf, F_OK) ? -1 : 1;
  66. close (proc_self_fd);
  67. }
  68. }
  69. if (proc_status < 0)
  70. return NULL;
  71. else
  72. {
  73. size_t bufsize = PROC_SELF_FD_NAME_SIZE_BOUND (strlen (file));
  74. char *result = buf;
  75. if (OPENAT_BUFFER_SIZE < bufsize)
  76. {
  77. result = malloc (bufsize);
  78. if (! result)
  79. return NULL;
  80. }
  81. sprintf (result, PROC_SELF_FD_FORMAT, fd, file);
  82. return result;
  83. }
  84. }