1 /*
2  * Copyright (C) 2004, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000, 2001  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17 
18 /* $Id: resource.c,v 1.10 2008/07/11 23:47:09 tbox Exp $ */
19 
20 #include <config.h>
21 
22 #include <stdio.h>
23 
24 #include <isc/platform.h>
25 #include <isc/resource.h>
26 #include <isc/result.h>
27 #include <isc/util.h>
28 
29 #include "errno2result.h"
30 
31 /*
32  * Windows limits the maximum number of open files to 2048
33  */
34 
35 #define WIN32_MAX_OPEN_FILES	2048
36 
37 isc_result_t
isc_resource_setlimit(isc_resource_t resource,isc_resourcevalue_t value)38 isc_resource_setlimit(isc_resource_t resource, isc_resourcevalue_t value) {
39 	isc_resourcevalue_t rlim_value;
40 	int wresult;
41 
42 	if (resource != isc_resource_openfiles)
43 		return (ISC_R_NOTIMPLEMENTED);
44 
45 
46 	if (value == ISC_RESOURCE_UNLIMITED)
47 		rlim_value = WIN32_MAX_OPEN_FILES;
48 	else
49 		rlim_value = min(value, WIN32_MAX_OPEN_FILES);
50 
51 	wresult = _setmaxstdio((int) rlim_value);
52 
53 	if (wresult > 0)
54 		return (ISC_R_SUCCESS);
55 	else
56 		return (isc__errno2result(errno));
57 }
58 
59 isc_result_t
isc_resource_getlimit(isc_resource_t resource,isc_resourcevalue_t * value)60 isc_resource_getlimit(isc_resource_t resource, isc_resourcevalue_t *value) {
61 
62 	if (resource != isc_resource_openfiles)
63 		return (ISC_R_NOTIMPLEMENTED);
64 
65 	*value = WIN32_MAX_OPEN_FILES;
66 	return (ISC_R_SUCCESS);
67 }
68 
69 isc_result_t
isc_resource_getcurlimit(isc_resource_t resource,isc_resourcevalue_t * value)70 isc_resource_getcurlimit(isc_resource_t resource, isc_resourcevalue_t *value) {
71 	return (isc_resource_getlimit(resource, value));
72 }
73