1 /* $NetBSD: open_limit.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* open_limit 3 6 /* SUMMARY 7 /* set/get open file limit 8 /* SYNOPSIS 9 /* #include <iostuff.h> 10 /* 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 /*--*/ 31 32 /* System libraries. */ 33 34 #include "sys_defs.h" 35 #include <sys/time.h> 36 #include <sys/resource.h> 37 #include <errno.h> 38 39 /* Application-specific. */ 40 41 #include "iostuff.h" 42 43 /* 44 * 44BSD compatibility. 45 */ 46 #ifndef RLIMIT_NOFILE 47 #ifdef RLIMIT_OFILE 48 #define RLIMIT_NOFILE RLIMIT_OFILE 49 #endif 50 #endif 51 52 /* open_limit - set/query file descriptor limit */ 53 54 int open_limit(int limit) 55 { 56 #ifdef RLIMIT_NOFILE 57 struct rlimit rl; 58 #endif 59 60 if (limit < 0) { 61 errno = EINVAL; 62 return (-1); 63 } 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); 74 } 75 return (rl.rlim_cur); 76 #endif 77 78 #ifndef RLIMIT_NOFILE 79 return (getdtablesize()); 80 #endif 81 } 82 83