--- hydra/src/boa.c 2002/09/30 18:18:52 1.16 +++ hydra/src/boa.c 2002/10/02 19:26:15 1.17 @@ -22,7 +22,7 @@ * */ -/* $Id: boa.c,v 1.16 2002/09/30 18:18:52 nmav Exp $*/ +/* $Id: boa.c,v 1.17 2002/10/02 19:26:15 nmav Exp $*/ #include "boa.h" #include "ssl.h" @@ -44,6 +44,7 @@ static void fixup_server_root(void); static socket_type create_server_socket(int port, int); void hic_init(void); +static void initialize_rlimits(); static void drop_privs(void); static server_params *smp_init(socket_type server_s[2]); static void create_server_names( void); @@ -153,17 +154,7 @@ initialize_mmap(); - if (max_connections < 1) { - struct rlimit rl; - - /* has not been set explicitly */ - c = getrlimit(RLIMIT_NOFILE, &rl); - if (c < 0) { - perror("getrlimit"); - exit(1); - } - max_connections = rl.rlim_cur; - } + initialize_rlimits(); /* background ourself */ if (do_fork) { @@ -536,3 +527,55 @@ strcat( boa_tls_version, "\r\n"); } } + +#ifdef HAVE_GETRLIMIT + +#ifndef RLIMIT_NOFILE +# define RLIMIT_NOFILE RLIMIT_OFILE +#endif + +#ifndef LONG_MAX +# define LONG_MAX 2147483647L +#endif + +#define SET_MAX_CON( lim) \ + if (lim == RLIM_INFINITY) { \ + max_connections = LONG_MAX; \ + } else { \ + max_connections = lim; \ + } + +static void initialize_rlimits( ) +{ +int c; +struct rlimit rl; + + if (max_connections > 0 && max_connections < 1) { + /* has not been set explicitly */ + c = getrlimit(RLIMIT_NOFILE, &rl); + if (c < 0) { + perror("getrlimit"); + exit(1); + } + SET_MAX_CON( rl.rlim_cur); +#ifdef HAVE_SETRLIMIT + if (rl.rlim_max > rl.rlim_cur) { + rl.rlim_cur = rl.rlim_max; + c = setrlimit(RLIMIT_NOFILE, &rl); + if (c < 0) { + perror("setrlimit:"); + } + + SET_MAX_CON( rl.rlim_max); + + } +#endif + } +} + +#else /* rlimits are not present */ +static void initialize_rlimits( ) +{ + return; +} +#endif