INSTALL: regenerate
[glibc.git] / sysdeps / mach / hurd / close_range.c
blob3e9ec64df7cb4c9eb6e71957c3e390d5691d24fd
1 /* Copyright (C) 2021-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
18 #include <unistd.h>
19 #include <hurd.h>
20 #include <hurd/fd.h>
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)
29 int i;
31 if (first > last)
32 return __hurd_fail (EINVAL);
33 if (flags & ~CLOSE_RANGE_CLOEXEC)
34 return __hurd_fail (EINVAL);
36 HURD_CRITICAL_BEGIN;
37 __mutex_lock (&_hurd_dtable_lock);
39 for (i = first; i <= last && i < _hurd_dtablesize; i++)
41 struct hurd_fd *fd = _hurd_dtable[i];
43 if (fd == NULL || fd->port.port == MACH_PORT_NULL)
44 continue;
46 __spin_lock (&fd->port.lock);
48 if (flags & CLOSE_RANGE_CLOEXEC)
49 fd->flags |= FD_CLOEXEC;
50 else
52 _hurd_port_set (&fd->ctty, MACH_PORT_NULL);
53 _hurd_port_locked_set (&fd->port, MACH_PORT_NULL);
56 __spin_unlock (&fd->port.lock);
59 __mutex_unlock (&_hurd_dtable_lock);
60 HURD_CRITICAL_END;
62 return 0;
65 libc_hidden_def (__close_range)
66 weak_alias (__close_range, close_range)