2 * Copyright (C) 2004, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: resource.c,v 1.23 2009/02/13 23:48:14 tbox Exp $ */
22 #include <sys/types.h>
23 #include <sys/time.h> /* Required on some systems for <sys/resource.h>. */
24 #include <sys/resource.h>
26 #include <isc/platform.h>
27 #include <isc/resource.h>
28 #include <isc/result.h>
32 #include <linux/fs.h> /* To get the large NR_OPEN. */
35 #if defined(__hpux) && defined(HAVE_SYS_DYNTUNE_H)
36 #include <sys/dyntune.h>
39 #include "errno2result.h"
42 resource2rlim(isc_resource_t resource, int *rlim_resource) {
43 isc_result_t result = ISC_R_SUCCESS;
46 case isc_resource_coresize:
47 *rlim_resource = RLIMIT_CORE;
49 case isc_resource_cputime:
50 *rlim_resource = RLIMIT_CPU;
52 case isc_resource_datasize:
53 *rlim_resource = RLIMIT_DATA;
55 case isc_resource_filesize:
56 *rlim_resource = RLIMIT_FSIZE;
58 case isc_resource_lockedmemory:
60 *rlim_resource = RLIMIT_MEMLOCK;
62 result = ISC_R_NOTIMPLEMENTED;
65 case isc_resource_openfiles:
67 *rlim_resource = RLIMIT_NOFILE;
69 result = ISC_R_NOTIMPLEMENTED;
72 case isc_resource_processes:
74 *rlim_resource = RLIMIT_NPROC;
76 result = ISC_R_NOTIMPLEMENTED;
79 case isc_resource_residentsize:
81 *rlim_resource = RLIMIT_RSS;
83 result = ISC_R_NOTIMPLEMENTED;
86 case isc_resource_stacksize:
87 *rlim_resource = RLIMIT_STACK;
91 * This test is not very robust if isc_resource_t
92 * changes, but generates a clear assertion message.
94 REQUIRE(resource >= isc_resource_coresize &&
95 resource <= isc_resource_stacksize);
105 isc_resource_setlimit(isc_resource_t resource, isc_resourcevalue_t value) {
107 ISC_PLATFORM_RLIMITTYPE rlim_value;
112 result = resource2rlim(resource, &unixresource);
113 if (result != ISC_R_SUCCESS)
116 if (value == ISC_RESOURCE_UNLIMITED)
117 rlim_value = RLIM_INFINITY;
121 * isc_resourcevalue_t was chosen as an unsigned 64 bit
122 * integer so that it could contain the maximum range of
123 * reasonable values. Unfortunately, this exceeds the typical
124 * range on Unix systems. Ensure the range of
125 * ISC_PLATFORM_RLIMITTYPE is not overflowed.
127 isc_resourcevalue_t rlim_max;
128 isc_boolean_t rlim_t_is_signed =
129 ISC_TF(((double)(ISC_PLATFORM_RLIMITTYPE)-1) < 0);
131 if (rlim_t_is_signed)
132 rlim_max = ~((ISC_PLATFORM_RLIMITTYPE)1 <<
133 (sizeof(ISC_PLATFORM_RLIMITTYPE) * 8 - 1));
135 rlim_max = (ISC_PLATFORM_RLIMITTYPE)-1;
137 if (value > rlim_max)
143 rl.rlim_cur = rl.rlim_max = rlim_value;
144 unixresult = setrlimit(unixresource, &rl);
147 return (ISC_R_SUCCESS);
149 #if defined(OPEN_MAX) && defined(__APPLE__)
151 * The Darwin kernel doesn't accept RLIM_INFINITY for rlim_cur; the
152 * maximum possible value is OPEN_MAX. BIND8 used to use
153 * sysconf(_SC_OPEN_MAX) for such a case, but this value is much
154 * smaller than OPEN_MAX and is not really effective.
156 if (resource == isc_resource_openfiles && rlim_value == RLIM_INFINITY) {
157 rl.rlim_cur = OPEN_MAX;
158 unixresult = setrlimit(unixresource, &rl);
160 return (ISC_R_SUCCESS);
162 #elif defined(__linux__)
164 #define NR_OPEN (1024*1024)
168 * Some Linux kernels don't accept RLIM_INFINIT; the maximum
169 * possible value is the NR_OPEN defined in linux/fs.h.
171 if (resource == isc_resource_openfiles && rlim_value == RLIM_INFINITY) {
172 rl.rlim_cur = rl.rlim_max = NR_OPEN;
173 unixresult = setrlimit(unixresource, &rl);
175 return (ISC_R_SUCCESS);
177 #elif defined(__hpux) && defined(HAVE_SYS_DYNTUNE_H)
178 if (resource == isc_resource_openfiles && rlim_value == RLIM_INFINITY) {
180 if (gettune("maxfiles_lim", &maxfiles) == 0) {
181 rl.rlim_cur = rl.rlim_max = maxfiles;
182 unixresult = setrlimit(unixresource, &rl);
184 return (ISC_R_SUCCESS);
188 if (resource == isc_resource_openfiles && rlim_value == RLIM_INFINITY) {
189 if (getrlimit(unixresource, &rl) == 0) {
190 rl.rlim_cur = rl.rlim_max;
191 unixresult = setrlimit(unixresource, &rl);
193 return (ISC_R_SUCCESS);
196 return (isc__errno2result(errno));
200 isc_resource_getlimit(isc_resource_t resource, isc_resourcevalue_t *value) {
206 result = resource2rlim(resource, &unixresource);
207 if (result == ISC_R_SUCCESS) {
208 unixresult = getrlimit(unixresource, &rl);
209 INSIST(unixresult == 0);
210 *value = rl.rlim_max;
217 isc_resource_getcurlimit(isc_resource_t resource, isc_resourcevalue_t *value) {
223 result = resource2rlim(resource, &unixresource);
224 if (result == ISC_R_SUCCESS) {
225 unixresult = getrlimit(unixresource, &rl);
226 INSIST(unixresult == 0);
227 *value = rl.rlim_cur;