1 | /* Copyright (C) 2021-2024 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 <unistd.h> |
19 | #include <hurd.h> |
20 | #include <hurd/fd.h> |
21 | |
22 | /* Close the file descriptors from FIRST up to LAST, inclusive. |
23 | If CLOSE_RANGE_CLOEXEC is set in FLAGS, set the FD_CLOEXEC flag |
24 | instead of closing. */ |
25 | int |
26 | __close_range (unsigned int first, unsigned int last, |
27 | int flags) |
28 | { |
29 | int i; |
30 | |
31 | if (first > last) |
32 | return __hurd_fail (EINVAL); |
33 | if (flags & ~CLOSE_RANGE_CLOEXEC) |
34 | return __hurd_fail (EINVAL); |
35 | |
36 | HURD_CRITICAL_BEGIN; |
37 | __mutex_lock (&_hurd_dtable_lock); |
38 | |
39 | for (i = first; i <= last && i < _hurd_dtablesize; i++) |
40 | { |
41 | struct hurd_fd *fd = _hurd_dtable[i]; |
42 | |
43 | if (fd == NULL || fd->port.port == MACH_PORT_NULL) |
44 | continue; |
45 | |
46 | __spin_lock (&fd->port.lock); |
47 | |
48 | if (flags & CLOSE_RANGE_CLOEXEC) |
49 | fd->flags |= FD_CLOEXEC; |
50 | else |
51 | { |
52 | _hurd_port_set (&fd->ctty, MACH_PORT_NULL); |
53 | _hurd_port_locked_set (&fd->port, MACH_PORT_NULL); |
54 | } |
55 | |
56 | __spin_unlock (&fd->port.lock); |
57 | } |
58 | |
59 | __mutex_unlock (&_hurd_dtable_lock); |
60 | HURD_CRITICAL_END; |
61 | |
62 | return 0; |
63 | } |
64 | |
65 | libc_hidden_def (__close_range) |
66 | weak_alias (__close_range, close_range) |
67 | |