home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / hurd / setdtsz.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-29  |  2.5 KB  |  89 lines

  1. /* Copyright (C) 1991, 1992 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 Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. 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. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <hurd.h>
  20.  
  21. /* XXX this should be done by setrlimit (RLIM_OFILES, size). */
  22.  
  23. /* Change the size of the descriptor table.
  24.    You cannot shrink the table if any of the descriptors
  25.    which would be removed are being used.  */
  26.  
  27. int
  28. setdtablesize (size_t size)
  29. {
  30.   int i;
  31.   struct _hurd_fd *table;
  32.  
  33.   __mutex_lock (&_hurd_dtable_lock);
  34.  
  35.   /* Check that no descriptors which are in use are going to disappear.  */
  36.   for (i = size; i < _hurd_dtable.size; ++i)
  37.     {
  38.       struct _hurd_fd *const d = &_hurd_dtable.d[i];
  39.  
  40.       __spin_lock (&d->port.lock);
  41.       if (d->port.port != MACH_PORT_NULL)
  42.     {
  43.       __spin_unlock (&d->port.lock);
  44.       __mutex_unlock (&_hurd_dtable_lock);
  45.       errno = EBUSY;
  46.       return -1;
  47.     }
  48.     }
  49.  
  50.   /* Resize the table.  */
  51.  
  52.   if (_hurd_dtable_user_dealloc == NULL)
  53.     /* Noone is using the table.  We can relocate it.  */
  54.     table = realloc (_hurd_dtable.d, size * sizeof (*table));
  55.   else
  56.     /* Someone else is using the table.
  57.        We must make a new copy, and let them free the old one.  */
  58.     table = malloc (size * sizeof (*table));
  59.  
  60.   if (table == NULL)
  61.     {
  62.       __mutex_unlock (&_hurd_dtable_lock);
  63.       return -1;
  64.     }
  65.  
  66.   if (_hurd_dtable_user_dealloc != NULL)
  67.     {
  68.       *_hurd_dtable_user_dealloc = 1;
  69.       _hurd_dtable_user_dealloc = NULL;
  70.  
  71.       memcpy (table, _hurd_dtable.d, _hurd_dtable.size * sizeof (table[0]));
  72.     }
  73.  
  74.   /* If the table grew, initialize the new slots.  */
  75.   for (i = _hurd_dtable.size; i < size; ++i)
  76.     {
  77.       table[i].flags = 0;
  78.       _hurd_port_init (&table[i].port, MACH_PORT_NULL);
  79.       _hurd_port_init (&table[i].ctty, MACH_PORT_NULL);
  80.     }
  81.  
  82.   _hurd_dtable.size = size;
  83.   _hurd_dtable.d = table;
  84.  
  85.   __mutex_unlock (&_hurd_dtable_lock);
  86.  
  87.   return 0;
  88. }
  89.