Commit 69872ed5 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Lower the limit using setrlimit and test the limits afterwards as

well.  Report an error if the assumptions made by lyskomd do not hold.
parent de53dc41
......@@ -24,6 +24,7 @@
/*
* Test various ways of obtaining the number of available file descriptors.
* Return failure if the assumptions used by lyskomd do not hold.
*/
#include <config.h>
......@@ -57,30 +58,48 @@
typedef int rlim_t;
#endif
int
main(void)
#define LOW_LIMIT (32)
struct data {
long getdtable;
long openmax;
long rlimmax;
long rlimcur;
long sysconfmax;
long max_seen;
};
struct all_data {
struct data pre;
struct data post;
};
static void
find_limits(const char *s,
struct data *p)
{
long getdtable = 0;
long openmax = 0;
long rlimmax = 0;
long sysconfmax = 0;
long max_seen = 0;
long fd;
long max_seen = 0;
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
struct rlimit rlim;
#endif
#if HAVE_GETDTABLESIZE
getdtable = getdtablesize();
printf("getdtablesize: %ld\n", getdtable);
p->getdtable = getdtablesize();
printf("%sgetdtablesize: %ld\n", s, p->getdtable);
#else
printf("%sgetdtablesize: n/a\n", s);
p->getdtablesize = 0;
#endif
#ifdef OPEN_MAX
openmax = OPEN_MAX;
printf("OPEN_MAX: %ld\n", openmax);
p->openmax = OPEN_MAX;
printf("%sOPEN_MAX: %ld\n", s, p->openmax);
#else
printf("OPEN_MAX: n/a\n");
printf("%sOPEN_MAX: n/a\n", s);
p->openmax = 0;
#endif
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
......@@ -90,19 +109,23 @@ main(void)
exit(1);
}
rlimmax = rlim.rlim_max;
printf("getrlimit-current: %ld\n", (long)rlim.rlim_cur);
printf("getrlimit-max: %ld\n", rlimmax);
p->rlimmax = rlim.rlim_max;
p->rlimcur = rlim.rlim_cur;
printf("%sgetrlimit-current: %ld\n", s, p->rlimcur);
printf("%sgetrlimit-max: %ld\n", s, p->rlimmax);
#else
printf("getrlimit-current: n/a\n");
printf("getrlimit-max: n/a\n");
printf("%sgetrlimit-current: n/a\n", s);
printf("%sgetrlimit-max: n/a\n", s);
p->rlimmax = 0;
p->rlimcur = 0;
#endif
#if defined (HAVE_SYSCONF)
sysconfmax = sysconf(_SC_OPEN_MAX);
printf("sysconf: %ld\n", sysconfmax);
p->sysconfmax = sysconf(_SC_OPEN_MAX);
printf("%ssysconf: %ld\n", s, p->sysconfmax);
#else
printf("sysconf: n/a\n");
printf("%ssysconf: n/a\n", s);
p->sysconfmax = 0;
#endif
while(1)
......@@ -114,6 +137,81 @@ main(void)
max_seen = fd + 1;
}
printf("open: %ld\n", max_seen);
return 0;
printf("%sopen: %ld\n", s, max_seen);
p->max_seen = max_seen;
for (fd = 10; fd < max_seen; fd++)
close(fd);
}
int
main(void)
{
int errs = 0;
struct all_data s;
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
struct rlimit rlim;
#endif
find_limits("pre-", &s.pre);
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
{
perror("ERROR: testfd: post-setrlimit getrlimit failed");
exit(1);
}
rlim.rlim_cur = LOW_LIMIT;
if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
{
perror("ERROR: testfd: post-setrlimit getrlimit failed");
exit(1);
}
#endif
find_limits("post-", &s.post);
if (s.post.max_seen != 0 && s.post.max_seen != LOW_LIMIT)
{
fprintf(stderr, "ERROR: open-post not equal to LOW_LIMIT\n");
errs++;
}
if (s.post.sysconfmax != 0)
{
if (s.post.sysconfmax != s.post.max_seen)
{
fprintf(stderr, "ERROR: open-post not equal to sysconf-post\n");
errs++;
}
if (s.post.getdtable != 0 && s.post.getdtable != s.post.max_seen)
{
fprintf(stderr,
"WARNING: open-post not equal to getdtable-post\n");
}
}
else if (s.post.getdtable != 0)
{
if (s.post.getdtable != s.post.max_seen)
{
fprintf(stderr, "ERROR: open-post not equal to getdtable-post\n");
errs++;
}
}
else if (s.pre.openmax != 0)
{
if (s.pre.openmax != s.post.openmax)
{
fprintf(stderr, "ERROR: openmax is mutable\n");
errs++;
}
if (s.post.openmax != s.post.max_seen)
{
fprintf(stderr, "ERROR: open-post not equal to openmax\n");
errs++;
}
}
exit(!!errs);
}
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