Commit 30688fed authored by Per Cederqvist's avatar Per Cederqvist
Browse files

(go_daemon): Close everything below fd_ceiling instead of using

	MAX_NO_OF_CONNECTIONS + PROTECTED_FDS.
(initialize): Use getrlimit/setrlimit if USING_RLIMIT_NOFILE is
	defined.  Check that getrlimit after setrlimit returns a sane
	value.  Don't use sysconf(_SC_OPEN_MAX), since getrlimit
	provides a better value.  Set fd_ceiling instead of
	MAX_NO_OF_CONNECTIONS.  Make it possible to lower the ceiling
	even when we don't have a working setrlimit.  Removed the
	final sanity checks; the world ain't sane.
parent 6a618f57
/*
* $Id: ramkomd.c,v 0.106 2002/10/26 11:59:03 ceder Exp $
* $Id: ramkomd.c,v 0.107 2002/10/26 22:46:34 ceder Exp $
* Copyright (C) 1991-1999, 2001-2002 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
......@@ -342,7 +342,7 @@ go_daemon(void)
}
/* Close all file descriptors */
for (fd = 0; fd < MAX_NO_OF_CONNECTIONS + PROTECTED_FDS; fd++)
for (fd = 0; fd < fd_ceiling; fd++)
close(fd);
if (open("/dev/null", O_RDONLY) != 0
||open("/dev/null", O_WRONLY) != 1
......@@ -359,7 +359,7 @@ go_daemon(void)
static void
initialize(const char *config_file)
{
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
#ifdef USING_RLIMIT_NOFILE
struct rlimit rlim;
#endif
......@@ -382,7 +382,7 @@ initialize(const char *config_file)
}
#endif
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
#ifdef USING_RLIMIT_NOFILE
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
{
perror("getrlimit(RLIMIT_NOFILE) failed");
......@@ -407,39 +407,40 @@ initialize(const char *config_file)
perror("setrlimit failed");
exit(1);
}
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
{
perror("getrlimit(RLIMIT_NOFILE) failed");
exit(1);
}
if (rlim.rlim_cur != (rlim_t)param.no_files)
restart_kom("getrlimit after setrlimit returns %ld, not %ld.\n",
(long)rlim.rlim_cur, (long)param.no_files);
}
#else
if (param.no_files != -1)
fd_ceiling = rlim.rlim_cur;
#elif HAVE_GETDTABLESIZE
if (param.no_files == -1)
fd_ceiling = getdtablesize();
else if (param.no_files >= getdtablesize())
{
fprintf(stderr,
"can't alter number of open files in your environment.\n");
fprintf(stderr, "getdtablesize() indicates that at most %ld files"
" can be used.\n", (long)getdtablesize());
exit(1);
}
#endif
/* Find out how many connections we can handle. */
#if defined (HAVE_SYSCONF)
MAX_NO_OF_CONNECTIONS = sysconf(_SC_OPEN_MAX) - PROTECTED_FDS;
else
fd_ceiling = param.no_files;
#elif defined(OPEN_MAX)
if (param.no_files == -1)
fd_ceiling = OPEN_MAX;
else if (param.no_files >= OPEN_MAX)
{
fprintf(stderr, "OPEN_MAX indicates that at most %ld files"
" can be used.\n", (long)OPEN_MAX);
exit(1);
}
else
fd_ceiling = param.no_files;
#else
# ifdef HAVE_GETDTABLESIZE
MAX_NO_OF_CONNECTIONS = getdtablesize() - PROTECTED_FDS;
# else
/* If we don't have getdtablesize or sysconf, MAX_NO_OF_CONNECTIONS is
set at compile time. */
# endif
#endif
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
/* Check that getrlimit and sysconf/getdtablesize/the compiled default
makes sense. */
if (rlim.rlim_cur > (rlim_t)(MAX_NO_OF_CONNECTIONS + PROTECTED_FDS))
kom_log("WARNING: getrlimit indicates that %ld files can be open, "
"but MAX_NO_OF_CONNECTIONS is only %ld\n",
(long)rlim.rlim_cur, (long)MAX_NO_OF_CONNECTIONS);
else if (rlim.rlim_cur < (rlim_t)(MAX_NO_OF_CONNECTIONS + PROTECTED_FDS))
kom_log("warning: getrlimit indicates that only %ld files can be open, "
"but MAX_NO_OF_CONNECTIONS is %ld (this should be harmless)\n",
(long)rlim.rlim_cur, (long)MAX_NO_OF_CONNECTIONS);
# error Do not know how to find maximum number of open files.
#endif
go_daemon();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment