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