You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2009, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <sys/types.h>
  28. #include <dirent.h>
  29. #include <errno.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "talk/base/linuxfdwalk.h"
  33. // Parses a file descriptor number in base 10, requiring the strict format used
  34. // in /proc/*/fd. Returns the value, or -1 if not a valid string.
  35. static int parse_fd(const char *s) {
  36. if (!*s) {
  37. // Empty string is invalid.
  38. return -1;
  39. }
  40. int val = 0;
  41. do {
  42. if (*s < '0' || *s > '9') {
  43. // Non-numeric characters anywhere are invalid.
  44. return -1;
  45. }
  46. int digit = *s++ - '0';
  47. val = val * 10 + digit;
  48. } while (*s);
  49. return val;
  50. }
  51. int fdwalk(void (*func)(void *, int), void *opaque) {
  52. DIR *dir = opendir("/proc/self/fd");
  53. if (!dir) {
  54. return -1;
  55. }
  56. int opendirfd = dirfd(dir);
  57. int parse_errors = 0;
  58. struct dirent *ent;
  59. // Have to clear errno to distinguish readdir() completion from failure.
  60. while (errno = 0, (ent = readdir(dir)) != NULL) {
  61. if (strcmp(ent->d_name, ".") == 0 ||
  62. strcmp(ent->d_name, "..") == 0) {
  63. continue;
  64. }
  65. // We avoid atoi or strtol because those are part of libc and they involve
  66. // locale stuff, which is probably not safe from a post-fork context in a
  67. // multi-threaded app.
  68. int fd = parse_fd(ent->d_name);
  69. if (fd < 0) {
  70. parse_errors = 1;
  71. continue;
  72. }
  73. if (fd != opendirfd) {
  74. (*func)(opaque, fd);
  75. }
  76. }
  77. int saved_errno = errno;
  78. if (closedir(dir) < 0) {
  79. if (!saved_errno) {
  80. // Return the closedir error.
  81. return -1;
  82. }
  83. // Else ignore it because we have a more relevant error to return.
  84. }
  85. if (saved_errno) {
  86. errno = saved_errno;
  87. return -1;
  88. } else if (parse_errors) {
  89. errno = EBADF;
  90. return -1;
  91. } else {
  92. return 0;
  93. }
  94. }