1// rlimit should be able to set file limits
2#include <stdlib.h>
3#include <string.h>
4#include <errno.h>
5#include <unistd.h>
6#include <sys/resource.h>
7#include "test.h"
8
9int main(void)
10{
11	static const long lim = 100;
12	static const int r = RLIMIT_NOFILE;
13	struct rlimit rl;
14	int fd, maxfd = 0;
15
16	rl.rlim_max = lim;
17	rl.rlim_cur = lim;
18	if (setrlimit(r, &rl))
19		t_error("setrlimit(%d, %ld) failed: %s\n", r, lim, strerror(errno));
20	if (getrlimit(r, &rl))
21		t_error("getrlimit(%d) failed: %s\n", r, strerror(errno));
22	if (rl.rlim_max != lim || rl.rlim_cur != lim)
23		t_error("getrlimit %d says cur=%ld,max=%ld after setting the limit to %ld\n", r, rl.rlim_cur, rl.rlim_max, lim);
24
25	while((fd=dup(1)) != -1)
26		if (fd > maxfd) maxfd = fd;
27	if (errno != EMFILE)
28		t_error("dup(1) failed with %s, wanted EMFILE\n", strerror(errno));
29	if (maxfd+1 > lim)
30		t_error("more fds are open than rlimit allows: fd=%d, limit=%d\n", maxfd, lim);
31
32	return t_status;
33}
34