xref: /netbsd/external/bsd/ntp/dist/lib/isc/win32/resource.c (revision 6550d01e)
1 /*	$NetBSD: resource.c,v 1.1.1.1 2009/12/13 16:54:44 kardel Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000, 2001  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: resource.c,v 1.10 2008/07/11 23:47:09 tbox Exp */
21 
22 #include <config.h>
23 
24 #include <stdio.h>
25 
26 #include <isc/platform.h>
27 #include <isc/resource.h>
28 #include <isc/result.h>
29 #include <isc/util.h>
30 
31 #include "errno2result.h"
32 
33 /*
34  * Windows limits the maximum number of open files to 2048
35  */
36 
37 #define WIN32_MAX_OPEN_FILES	2048
38 
39 isc_result_t
40 isc_resource_setlimit(isc_resource_t resource, isc_resourcevalue_t value) {
41 	isc_resourcevalue_t rlim_value;
42 	int wresult;
43 
44 	if (resource != isc_resource_openfiles)
45 		return (ISC_R_NOTIMPLEMENTED);
46 
47 
48 	if (value == ISC_RESOURCE_UNLIMITED)
49 		rlim_value = WIN32_MAX_OPEN_FILES;
50 	else
51 		rlim_value = min(value, WIN32_MAX_OPEN_FILES);
52 
53 	wresult = _setmaxstdio((int) rlim_value);
54 
55 	if (wresult > 0)
56 		return (ISC_R_SUCCESS);
57 	else
58 		return (isc__errno2result(errno));
59 }
60 
61 isc_result_t
62 isc_resource_getlimit(isc_resource_t resource, isc_resourcevalue_t *value) {
63 
64 	if (resource != isc_resource_openfiles)
65 		return (ISC_R_NOTIMPLEMENTED);
66 
67 	*value = WIN32_MAX_OPEN_FILES;
68 	return (ISC_R_SUCCESS);
69 }
70 
71 isc_result_t
72 isc_resource_getcurlimit(isc_resource_t resource, isc_resourcevalue_t *value) {
73 	return (isc_resource_getlimit(resource, value));
74 }
75