alloc-fd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Copyright (C) 1994-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library 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 GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <hurd.h>
  15. #include <hurd/fd.h>
  16. #include <hurd/resource.h>
  17. #include <stdlib.h>
  18. #include "hurdmalloc.h" /* XXX */
  19. /* Allocate a new file descriptor and return it, locked. The new
  20. descriptor number will be no less than FIRST_FD. If the table is full,
  21. set errno to EMFILE and return NULL. If FIRST_FD is negative or bigger
  22. than the size of the table, set errno to EINVAL and return NULL. */
  23. struct hurd_fd *
  24. _hurd_alloc_fd (int *fd, int first_fd)
  25. {
  26. int i;
  27. void *crit;
  28. rlim_t rlimit;
  29. if (first_fd < 0)
  30. return __hurd_fail (EINVAL), NULL;
  31. crit = _hurd_critical_section_lock ();
  32. __mutex_lock (&_hurd_dtable_lock);
  33. search:
  34. for (i = first_fd; i < _hurd_dtablesize; ++i)
  35. {
  36. struct hurd_fd *d = _hurd_dtable[i];
  37. if (d == NULL)
  38. {
  39. /* Allocate a new descriptor structure for this slot,
  40. initializing its port cells to nil. The test below will catch
  41. and return this descriptor cell after locking it. */
  42. d = _hurd_new_fd (MACH_PORT_NULL, MACH_PORT_NULL);
  43. if (d == NULL)
  44. {
  45. __mutex_unlock (&_hurd_dtable_lock);
  46. _hurd_critical_section_unlock (crit);
  47. return NULL;
  48. }
  49. _hurd_dtable[i] = d;
  50. }
  51. __spin_lock (&d->port.lock);
  52. if (d->port.port == MACH_PORT_NULL)
  53. {
  54. __mutex_unlock (&_hurd_dtable_lock);
  55. _hurd_critical_section_unlock (crit);
  56. if (fd != NULL)
  57. *fd = i;
  58. return d;
  59. }
  60. else
  61. __spin_unlock (&d->port.lock);
  62. }
  63. __mutex_lock (&_hurd_rlimit_lock);
  64. rlimit = _hurd_rlimits[RLIMIT_OFILE].rlim_cur;
  65. __mutex_unlock (&_hurd_rlimit_lock);
  66. if (first_fd < rlimit)
  67. {
  68. /* The descriptor table is full. Check if we have reached the
  69. resource limit, or only the allocated size. */
  70. if (_hurd_dtablesize < rlimit)
  71. {
  72. /* Enlarge the table. */
  73. int save = errno;
  74. struct hurd_fd **new;
  75. /* Try to double the table size, but don't exceed the limit,
  76. and make sure it exceeds FIRST_FD. */
  77. int size = _hurd_dtablesize * 2;
  78. if (size > rlimit)
  79. size = rlimit;
  80. else if (size <= first_fd)
  81. size = first_fd + 1;
  82. if (size * sizeof (*_hurd_dtable) < size)
  83. {
  84. /* Integer overflow! */
  85. __hurd_fail (ENOMEM);
  86. goto out;
  87. }
  88. /* If we fail to allocate that, decrement the desired size
  89. until we succeed in allocating it. */
  90. do
  91. new = realloc (_hurd_dtable, size * sizeof (*_hurd_dtable));
  92. while (new == NULL && size-- > first_fd);
  93. if (new != NULL)
  94. {
  95. /* We managed to allocate a new table. Now install it. */
  96. errno = save;
  97. if (first_fd < _hurd_dtablesize)
  98. first_fd = _hurd_dtablesize;
  99. /* Initialize the new slots. */
  100. for (i = _hurd_dtablesize; i < size; ++i)
  101. new[i] = NULL;
  102. _hurd_dtablesize = size;
  103. _hurd_dtable = new;
  104. /* Go back to the loop to initialize the first new slot. */
  105. goto search;
  106. }
  107. else
  108. __hurd_fail (ENOMEM);
  109. }
  110. else
  111. __hurd_fail (EMFILE);
  112. }
  113. else
  114. __hurd_fail (EINVAL); /* Bogus FIRST_FD value. */
  115. out:
  116. __mutex_unlock (&_hurd_dtable_lock);
  117. _hurd_critical_section_unlock (crit);
  118. return NULL;
  119. }