2021-01-02 19:32:25 +00:00
|
|
|
/* Copyright (C) 1994-2021 Free Software Foundation, Inc.
|
1997-02-15 04:31:36 +00:00
|
|
|
This file is part of the GNU C Library.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
1997-02-15 04:31:36 +00:00
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
1997-02-15 04:31:36 +00:00
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 04:58:11 +00:00
|
|
|
Lesser General Public License for more details.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
#include <hurd.h>
|
|
|
|
#include <hurd/fd.h>
|
|
|
|
#include <hurd/resource.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "hurdmalloc.h" /* XXX */
|
|
|
|
|
|
|
|
/* Allocate a new file descriptor and return it, locked. The new
|
|
|
|
descriptor number will be no less than FIRST_FD. If the table is full,
|
|
|
|
set errno to EMFILE and return NULL. If FIRST_FD is negative or bigger
|
|
|
|
than the size of the table, set errno to EINVAL and return NULL. */
|
|
|
|
|
|
|
|
struct hurd_fd *
|
|
|
|
_hurd_alloc_fd (int *fd, int first_fd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
void *crit;
|
|
|
|
long int rlimit;
|
|
|
|
|
|
|
|
if (first_fd < 0)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
crit = _hurd_critical_section_lock ();
|
|
|
|
|
|
|
|
__mutex_lock (&_hurd_dtable_lock);
|
|
|
|
|
|
|
|
search:
|
|
|
|
for (i = first_fd; i < _hurd_dtablesize; ++i)
|
|
|
|
{
|
|
|
|
struct hurd_fd *d = _hurd_dtable[i];
|
|
|
|
if (d == NULL)
|
|
|
|
{
|
|
|
|
/* Allocate a new descriptor structure for this slot,
|
|
|
|
initializing its port cells to nil. The test below will catch
|
|
|
|
and return this descriptor cell after locking it. */
|
|
|
|
d = _hurd_new_fd (MACH_PORT_NULL, MACH_PORT_NULL);
|
|
|
|
if (d == NULL)
|
|
|
|
{
|
|
|
|
__mutex_unlock (&_hurd_dtable_lock);
|
|
|
|
_hurd_critical_section_unlock (crit);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
_hurd_dtable[i] = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
__spin_lock (&d->port.lock);
|
|
|
|
if (d->port.port == MACH_PORT_NULL)
|
|
|
|
{
|
|
|
|
__mutex_unlock (&_hurd_dtable_lock);
|
|
|
|
_hurd_critical_section_unlock (crit);
|
|
|
|
if (fd != NULL)
|
|
|
|
*fd = i;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
__spin_unlock (&d->port.lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
__mutex_lock (&_hurd_rlimit_lock);
|
|
|
|
rlimit = _hurd_rlimits[RLIMIT_OFILE].rlim_cur;
|
|
|
|
__mutex_unlock (&_hurd_rlimit_lock);
|
|
|
|
|
|
|
|
if (first_fd < rlimit)
|
|
|
|
{
|
|
|
|
/* The descriptor table is full. Check if we have reached the
|
|
|
|
resource limit, or only the allocated size. */
|
|
|
|
if (_hurd_dtablesize < rlimit)
|
|
|
|
{
|
|
|
|
/* Enlarge the table. */
|
|
|
|
int save = errno;
|
|
|
|
struct hurd_fd **new;
|
1995-05-12 00:50:29 +00:00
|
|
|
/* Try to double the table size, but don't exceed the limit,
|
|
|
|
and make sure it exceeds FIRST_FD. */
|
|
|
|
int size = _hurd_dtablesize * 2;
|
1995-02-18 01:27:10 +00:00
|
|
|
if (size > rlimit)
|
|
|
|
size = rlimit;
|
1995-05-12 00:50:29 +00:00
|
|
|
else if (size <= first_fd)
|
|
|
|
size = first_fd + 1;
|
|
|
|
|
1996-10-08 18:38:08 +00:00
|
|
|
if (size * sizeof (*_hurd_dtable) < size)
|
|
|
|
{
|
|
|
|
/* Integer overflow! */
|
|
|
|
errno = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
/* If we fail to allocate that, decrement the desired size
|
|
|
|
until we succeed in allocating it. */
|
|
|
|
do
|
|
|
|
new = realloc (_hurd_dtable, size * sizeof (*_hurd_dtable));
|
1995-05-12 00:50:29 +00:00
|
|
|
while (new == NULL && size-- > first_fd);
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
if (new != NULL)
|
|
|
|
{
|
|
|
|
/* We managed to allocate a new table. Now install it. */
|
|
|
|
errno = save;
|
1995-05-12 16:40:34 +00:00
|
|
|
if (first_fd < _hurd_dtablesize)
|
|
|
|
first_fd = _hurd_dtablesize;
|
1995-02-18 01:27:10 +00:00
|
|
|
/* Initialize the new slots. */
|
1995-05-12 16:40:34 +00:00
|
|
|
for (i = _hurd_dtablesize; i < size; ++i)
|
1995-02-18 01:27:10 +00:00
|
|
|
new[i] = NULL;
|
|
|
|
_hurd_dtablesize = size;
|
|
|
|
_hurd_dtable = new;
|
|
|
|
/* Go back to the loop to initialize the first new slot. */
|
|
|
|
goto search;
|
|
|
|
}
|
1996-10-08 18:38:08 +00:00
|
|
|
else
|
|
|
|
errno = ENOMEM;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
errno = EMFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
errno = EINVAL; /* Bogus FIRST_FD value. */
|
|
|
|
|
1996-10-08 18:38:08 +00:00
|
|
|
out:
|
1995-02-18 01:27:10 +00:00
|
|
|
__mutex_unlock (&_hurd_dtable_lock);
|
|
|
|
_hurd_critical_section_unlock (crit);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|