|
Packit |
6c4009 |
/* Copyright (C) 1994-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <hurd.h>
|
|
Packit |
6c4009 |
#include <hurd/fd.h>
|
|
Packit |
6c4009 |
#include <hurd/resource.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include "hurdmalloc.h" /* XXX */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Allocate a new file descriptor and return it, locked. The new
|
|
Packit |
6c4009 |
descriptor number will be no less than FIRST_FD. If the table is full,
|
|
Packit |
6c4009 |
set errno to EMFILE and return NULL. If FIRST_FD is negative or bigger
|
|
Packit |
6c4009 |
than the size of the table, set errno to EINVAL and return NULL. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct hurd_fd *
|
|
Packit |
6c4009 |
_hurd_alloc_fd (int *fd, int first_fd)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int i;
|
|
Packit |
6c4009 |
void *crit;
|
|
Packit |
6c4009 |
long int rlimit;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (first_fd < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
errno = EINVAL;
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
crit = _hurd_critical_section_lock ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
__mutex_lock (&_hurd_dtable_lock);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
search:
|
|
Packit |
6c4009 |
for (i = first_fd; i < _hurd_dtablesize; ++i)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct hurd_fd *d = _hurd_dtable[i];
|
|
Packit |
6c4009 |
if (d == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Allocate a new descriptor structure for this slot,
|
|
Packit |
6c4009 |
initializing its port cells to nil. The test below will catch
|
|
Packit |
6c4009 |
and return this descriptor cell after locking it. */
|
|
Packit |
6c4009 |
d = _hurd_new_fd (MACH_PORT_NULL, MACH_PORT_NULL);
|
|
Packit |
6c4009 |
if (d == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
__mutex_unlock (&_hurd_dtable_lock);
|
|
Packit |
6c4009 |
_hurd_critical_section_unlock (crit);
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
_hurd_dtable[i] = d;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
__spin_lock (&d->port.lock);
|
|
Packit |
6c4009 |
if (d->port.port == MACH_PORT_NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
__mutex_unlock (&_hurd_dtable_lock);
|
|
Packit |
6c4009 |
_hurd_critical_section_unlock (crit);
|
|
Packit |
6c4009 |
if (fd != NULL)
|
|
Packit |
6c4009 |
*fd = i;
|
|
Packit |
6c4009 |
return d;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
__spin_unlock (&d->port.lock);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
__mutex_lock (&_hurd_rlimit_lock);
|
|
Packit |
6c4009 |
rlimit = _hurd_rlimits[RLIMIT_OFILE].rlim_cur;
|
|
Packit |
6c4009 |
__mutex_unlock (&_hurd_rlimit_lock);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (first_fd < rlimit)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* The descriptor table is full. Check if we have reached the
|
|
Packit |
6c4009 |
resource limit, or only the allocated size. */
|
|
Packit |
6c4009 |
if (_hurd_dtablesize < rlimit)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Enlarge the table. */
|
|
Packit |
6c4009 |
int save = errno;
|
|
Packit |
6c4009 |
struct hurd_fd **new;
|
|
Packit |
6c4009 |
/* Try to double the table size, but don't exceed the limit,
|
|
Packit |
6c4009 |
and make sure it exceeds FIRST_FD. */
|
|
Packit |
6c4009 |
int size = _hurd_dtablesize * 2;
|
|
Packit |
6c4009 |
if (size > rlimit)
|
|
Packit |
6c4009 |
size = rlimit;
|
|
Packit |
6c4009 |
else if (size <= first_fd)
|
|
Packit |
6c4009 |
size = first_fd + 1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (size * sizeof (*_hurd_dtable) < size)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Integer overflow! */
|
|
Packit |
6c4009 |
errno = ENOMEM;
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* If we fail to allocate that, decrement the desired size
|
|
Packit |
6c4009 |
until we succeed in allocating it. */
|
|
Packit |
6c4009 |
do
|
|
Packit |
6c4009 |
new = realloc (_hurd_dtable, size * sizeof (*_hurd_dtable));
|
|
Packit |
6c4009 |
while (new == NULL && size-- > first_fd);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (new != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* We managed to allocate a new table. Now install it. */
|
|
Packit |
6c4009 |
errno = save;
|
|
Packit |
6c4009 |
if (first_fd < _hurd_dtablesize)
|
|
Packit |
6c4009 |
first_fd = _hurd_dtablesize;
|
|
Packit |
6c4009 |
/* Initialize the new slots. */
|
|
Packit |
6c4009 |
for (i = _hurd_dtablesize; i < size; ++i)
|
|
Packit |
6c4009 |
new[i] = NULL;
|
|
Packit |
6c4009 |
_hurd_dtablesize = size;
|
|
Packit |
6c4009 |
_hurd_dtable = new;
|
|
Packit |
6c4009 |
/* Go back to the loop to initialize the first new slot. */
|
|
Packit |
6c4009 |
goto search;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
errno = ENOMEM;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
errno = EMFILE;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
errno = EINVAL; /* Bogus FIRST_FD value. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
out:
|
|
Packit |
6c4009 |
__mutex_unlock (&_hurd_dtable_lock);
|
|
Packit |
6c4009 |
_hurd_critical_section_unlock (crit);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
}
|