isfcbwatchfd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
  24. /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
  25. /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
  26. /*%% (c) Copyright 1993, 1994 Novell, Inc. */
  27. /*%% $XConsortium: isfcbwatchfd.c /main/3 1995/10/23 11:38:51 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isfcbwatchfd.c
  33. *
  34. * Description:
  35. * Watch the limit on UNIX file descriptors used by the FCB module.
  36. */
  37. #if defined(_AIX)
  38. #define _POSIX_SYSCONF
  39. #endif
  40. #if defined(_AIX)
  41. #define _POSIX_SYSCONF
  42. #endif
  43. #include "isam_impl.h"
  44. #include <sys/time.h>
  45. #include <sys/resource.h>
  46. #ifdef _POSIX_SYSCONF
  47. #include <unistd.h>
  48. #endif /* _POSIX_SYSCONF */
  49. static int _limit = MAXFCB_UNIXFD; /* Imposed limit */
  50. static int _in_use = 0; /* Current number of
  51. * open file descriptors
  52. */
  53. /*
  54. * _watchfd_incr(n)
  55. *
  56. * Register that n additional UNIX file descriptors were open.
  57. *
  58. * Return the new number of open file descriptors.
  59. */
  60. int
  61. _watchfd_incr(int n)
  62. {
  63. _in_use += n;
  64. assert(_in_use <= _limit);
  65. return (_in_use);
  66. }
  67. /*
  68. * _watch_decr(n)
  69. *
  70. * Register that n open file descriptors were closed.
  71. *
  72. * Return the new number of open file descriptors.
  73. */
  74. int
  75. _watchfd_decr(int n)
  76. {
  77. _in_use -= n;
  78. assert(_in_use >= 0);
  79. return (_in_use);
  80. }
  81. /*
  82. * _watch_check()
  83. *
  84. * Return number of fd that are still available.
  85. */
  86. int
  87. _watchfd_check(void)
  88. {
  89. return (_limit - _in_use);
  90. }
  91. /*
  92. * _watchfd_max_set(n)
  93. *
  94. * Set the maximum number of UNIX fds that may be comsumed by ISAM files.
  95. */
  96. int
  97. _watchfd_max_set(int n)
  98. {
  99. int oldlimit = _limit;
  100. #ifdef _POSIX_SYSCONF
  101. if (n < 3 || n > sysconf(_SC_OPEN_MAX)) {
  102. #else
  103. int dtab_size = 0;
  104. struct rlimit rl;
  105. if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
  106. dtab_size = rl.rlim_cur;
  107. if (n < 3 || n > dtab_size) {
  108. #endif
  109. _setiserrno2(EBADARG, '9', '0');
  110. return (ISERROR);
  111. }
  112. _limit = n;
  113. return (oldlimit);
  114. }
  115. /*
  116. * _watchfd_max_get()
  117. *
  118. * Get the maximum number of UNIX fds that may be comsumed by ISAM files.
  119. */
  120. int
  121. _watchfd_max_get(void)
  122. {
  123. return (_limit);
  124. }