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