alloc-fd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright (C) 1994-2019 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. <http://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. long int rlimit;
  29. if (first_fd < 0)
  30. {
  31. errno = EINVAL;
  32. return NULL;
  33. }
  34. crit = _hurd_critical_section_lock ();
  35. __mutex_lock (&_hurd_dtable_lock);
  36. search:
  37. for (i = first_fd; i < _hurd_dtablesize; ++i)
  38. {
  39. struct hurd_fd *d = _hurd_dtable[i];
  40. if (d == NULL)
  41. {
  42. /* Allocate a new descriptor structure for this slot,
  43. initializing its port cells to nil. The test below will catch
  44. and return this descriptor cell after locking it. */
  45. d = _hurd_new_fd (MACH_PORT_NULL, MACH_PORT_NULL);
  46. if (d == NULL)
  47. {
  48. __mutex_unlock (&_hurd_dtable_lock);
  49. _hurd_critical_section_unlock (crit);
  50. return NULL;
  51. }
  52. _hurd_dtable[i] = d;
  53. }
  54. __spin_lock (&d->port.lock);
  55. if (d->port.port == MACH_PORT_NULL)
  56. {
  57. __mutex_unlock (&_hurd_dtable_lock);
  58. _hurd_critical_section_unlock (crit);
  59. if (fd != NULL)
  60. *fd = i;
  61. return d;
  62. }
  63. else
  64. __spin_unlock (&d->port.lock);
  65. }
  66. __mutex_lock (&_hurd_rlimit_lock);
  67. rlimit = _hurd_rlimits[RLIMIT_OFILE].rlim_cur;
  68. __mutex_unlock (&_hurd_rlimit_lock);
  69. if (first_fd < rlimit)
  70. {
  71. /* The descriptor table is full. Check if we have reached the
  72. resource limit, or only the allocated size. */
  73. if (_hurd_dtablesize < rlimit)
  74. {
  75. /* Enlarge the table. */
  76. int save = errno;
  77. struct hurd_fd **new;
  78. /* Try to double the table size, but don't exceed the limit,
  79. and make sure it exceeds FIRST_FD. */
  80. int size = _hurd_dtablesize * 2;
  81. if (size > rlimit)
  82. size = rlimit;
  83. else if (size <= first_fd)
  84. size = first_fd + 1;
  85. if (size * sizeof (*_hurd_dtable) < size)
  86. {
  87. /* Integer overflow! */
  88. errno = ENOMEM;
  89. goto out;
  90. }
  91. /* If we fail to allocate that, decrement the desired size
  92. until we succeed in allocating it. */
  93. do
  94. new = realloc (_hurd_dtable, size * sizeof (*_hurd_dtable));
  95. while (new == NULL && size-- > first_fd);
  96. if (new != NULL)
  97. {
  98. /* We managed to allocate a new table. Now install it. */
  99. errno = save;
  100. if (first_fd < _hurd_dtablesize)
  101. first_fd = _hurd_dtablesize;
  102. /* Initialize the new slots. */
  103. for (i = _hurd_dtablesize; i < size; ++i)
  104. new[i] = NULL;
  105. _hurd_dtablesize = size;
  106. _hurd_dtable = new;
  107. /* Go back to the loop to initialize the first new slot. */
  108. goto search;
  109. }
  110. else
  111. errno = ENOMEM;
  112. }
  113. else
  114. errno = EMFILE;
  115. }
  116. else
  117. errno = EINVAL; /* Bogus FIRST_FD value. */
  118. out:
  119. __mutex_unlock (&_hurd_dtable_lock);
  120. _hurd_critical_section_unlock (crit);
  121. return NULL;
  122. }