1570af302Sopenharmony_ci// rlimit should be able to set file limits
2570af302Sopenharmony_ci#include <stdlib.h>
3570af302Sopenharmony_ci#include <string.h>
4570af302Sopenharmony_ci#include <errno.h>
5570af302Sopenharmony_ci#include <unistd.h>
6570af302Sopenharmony_ci#include <sys/resource.h>
7570af302Sopenharmony_ci#include "test.h"
8570af302Sopenharmony_ci
9570af302Sopenharmony_ciint main(void)
10570af302Sopenharmony_ci{
11570af302Sopenharmony_ci	static const long lim = 100;
12570af302Sopenharmony_ci	static const int r = RLIMIT_NOFILE;
13570af302Sopenharmony_ci	struct rlimit rl;
14570af302Sopenharmony_ci	int fd, maxfd = 0;
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci	rl.rlim_max = lim;
17570af302Sopenharmony_ci	rl.rlim_cur = lim;
18570af302Sopenharmony_ci	if (setrlimit(r, &rl))
19570af302Sopenharmony_ci		t_error("setrlimit(%d, %ld) failed: %s\n", r, lim, strerror(errno));
20570af302Sopenharmony_ci	if (getrlimit(r, &rl))
21570af302Sopenharmony_ci		t_error("getrlimit(%d) failed: %s\n", r, strerror(errno));
22570af302Sopenharmony_ci	if (rl.rlim_max != lim || rl.rlim_cur != lim)
23570af302Sopenharmony_ci		t_error("getrlimit %d says cur=%ld,max=%ld after setting the limit to %ld\n", r, rl.rlim_cur, rl.rlim_max, lim);
24570af302Sopenharmony_ci
25570af302Sopenharmony_ci	while((fd=dup(1)) != -1)
26570af302Sopenharmony_ci		if (fd > maxfd) maxfd = fd;
27570af302Sopenharmony_ci	if (errno != EMFILE)
28570af302Sopenharmony_ci		t_error("dup(1) failed with %s, wanted EMFILE\n", strerror(errno));
29570af302Sopenharmony_ci	if (maxfd+1 > lim)
30570af302Sopenharmony_ci		t_error("more fds are open than rlimit allows: fd=%d, limit=%d\n", maxfd, lim);
31570af302Sopenharmony_ci
32570af302Sopenharmony_ci	return t_status;
33570af302Sopenharmony_ci}
34