Back to index

glibc  2.9
alloc-fd.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <hurd.h>
00020 #include <hurd/fd.h>
00021 #include <hurd/resource.h>
00022 #include <stdlib.h>
00023 #include "hurdmalloc.h"            /* XXX */
00024 
00025 /* Allocate a new file descriptor and return it, locked.  The new
00026    descriptor number will be no less than FIRST_FD.  If the table is full,
00027    set errno to EMFILE and return NULL.  If FIRST_FD is negative or bigger
00028    than the size of the table, set errno to EINVAL and return NULL.  */
00029 
00030 struct hurd_fd *
00031 _hurd_alloc_fd (int *fd, int first_fd)
00032 {
00033   int i;
00034   void *crit;
00035   long int rlimit;
00036 
00037   if (first_fd < 0)
00038     {
00039       errno = EINVAL;
00040       return NULL;
00041     }
00042 
00043   crit = _hurd_critical_section_lock ();
00044 
00045   __mutex_lock (&_hurd_dtable_lock);
00046 
00047  search:
00048   for (i = first_fd; i < _hurd_dtablesize; ++i)
00049     {
00050       struct hurd_fd *d = _hurd_dtable[i];
00051       if (d == NULL)
00052        {
00053          /* Allocate a new descriptor structure for this slot,
00054             initializing its port cells to nil.  The test below will catch
00055             and return this descriptor cell after locking it.  */
00056          d = _hurd_new_fd (MACH_PORT_NULL, MACH_PORT_NULL);
00057          if (d == NULL)
00058            {
00059              __mutex_unlock (&_hurd_dtable_lock);
00060              _hurd_critical_section_unlock (crit);
00061              return NULL;
00062            }
00063          _hurd_dtable[i] = d;
00064        }
00065 
00066       __spin_lock (&d->port.lock);
00067       if (d->port.port == MACH_PORT_NULL)
00068        {
00069          __mutex_unlock (&_hurd_dtable_lock);
00070          _hurd_critical_section_unlock (crit);
00071          if (fd != NULL)
00072            *fd = i;
00073          return d;
00074        }
00075       else
00076        __spin_unlock (&d->port.lock);
00077     }
00078 
00079   __mutex_lock (&_hurd_rlimit_lock);
00080   rlimit = _hurd_rlimits[RLIMIT_OFILE].rlim_cur;
00081   __mutex_unlock (&_hurd_rlimit_lock);
00082 
00083   if (first_fd < rlimit)
00084     {
00085       /* The descriptor table is full.  Check if we have reached the
00086         resource limit, or only the allocated size.  */
00087       if (_hurd_dtablesize < rlimit)
00088        {
00089          /* Enlarge the table.  */
00090          int save = errno;
00091          struct hurd_fd **new;
00092          /* Try to double the table size, but don't exceed the limit,
00093             and make sure it exceeds FIRST_FD.  */
00094          int size = _hurd_dtablesize * 2;
00095          if (size > rlimit)
00096            size = rlimit;
00097          else if (size <= first_fd)
00098            size = first_fd + 1;
00099 
00100          if (size * sizeof (*_hurd_dtable) < size)
00101            {
00102              /* Integer overflow! */
00103              errno = ENOMEM;
00104              goto out;
00105            }
00106 
00107          /* If we fail to allocate that, decrement the desired size
00108             until we succeed in allocating it.  */
00109          do
00110            new = realloc (_hurd_dtable, size * sizeof (*_hurd_dtable));
00111          while (new == NULL && size-- > first_fd);
00112 
00113          if (new != NULL)
00114            {
00115              /* We managed to allocate a new table.  Now install it.  */
00116              errno = save;
00117              if (first_fd < _hurd_dtablesize)
00118               first_fd = _hurd_dtablesize;
00119              /* Initialize the new slots.  */
00120              for (i = _hurd_dtablesize; i < size; ++i)
00121               new[i] = NULL;
00122              _hurd_dtablesize = size;
00123              _hurd_dtable = new;
00124              /* Go back to the loop to initialize the first new slot.  */
00125              goto search;
00126            }
00127          else
00128            errno = ENOMEM;
00129        }
00130       else
00131        errno = EMFILE;
00132     }
00133   else
00134     errno = EINVAL;         /* Bogus FIRST_FD value.  */
00135 
00136  out:
00137   __mutex_unlock (&_hurd_dtable_lock);
00138   _hurd_critical_section_unlock (crit);
00139 
00140   return NULL;
00141 }