Sync usage with man page.
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / open_limit.c
blob1e7f5a5c4622fdacf6ce14e3002f241f268bc761
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* open_limit 3
6 /* SUMMARY
7 /* set/get open file limit
8 /* SYNOPSIS
9 /* #include <iostuff.h>
11 /* int open_limit(int limit)
12 /* DESCRIPTION
13 /* The \fIopen_limit\fR() routine attempts to change the maximum
14 /* number of open files to the specified limit. Specify a null
15 /* argument to effect no change. The result is the actual open file
16 /* limit for the current process. The number can be smaller or larger
17 /* than the requested limit.
18 /* DIAGNOSTICS
19 /* open_limit() returns -1 in case of problems. The errno
20 /* variable gives hints about the nature of the problem.
21 /* LICENSE
22 /* .ad
23 /* .fi
24 /* The Secure Mailer license must be distributed with this software.
25 /* AUTHOR(S)
26 /* Wietse Venema
27 /* IBM T.J. Watson Research
28 /* P.O. Box 704
29 /* Yorktown Heights, NY 10598, USA
30 /*--*/
32 /* System libraries. */
34 #include "sys_defs.h"
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 #include <errno.h>
39 /* Application-specific. */
41 #include "iostuff.h"
44 * 44BSD compatibility.
46 #ifndef RLIMIT_NOFILE
47 #ifdef RLIMIT_OFILE
48 #define RLIMIT_NOFILE RLIMIT_OFILE
49 #endif
50 #endif
52 /* open_limit - set/query file descriptor limit */
54 int open_limit(int limit)
56 #ifdef RLIMIT_NOFILE
57 struct rlimit rl;
58 #endif
60 if (limit < 0) {
61 errno = EINVAL;
62 return (-1);
64 #ifdef RLIMIT_NOFILE
65 if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
66 return (-1);
67 if (limit > 0) {
68 if (limit > rl.rlim_max)
69 rl.rlim_cur = rl.rlim_max;
70 else
71 rl.rlim_cur = limit;
72 if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
73 return (-1);
75 return (rl.rlim_cur);
76 #endif
78 #ifndef RLIMIT_NOFILE
79 return (getdtablesize());
80 #endif