Skip to content

Instantly share code, notes, and snippets.

@hjr3
Last active December 12, 2015 07:58
Show Gist options
  • Save hjr3/4740788 to your computer and use it in GitHub Desktop.
Save hjr3/4740788 to your computer and use it in GitHub Desktop.
How does setrlimit work when the value is 0?
/*
*
* gcc -o rlimit rlimit.c
*/
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
int main(int argv, char *argc[])
{
struct rlimit r;
int rv;
r.rlim_max = r.rlim_cur = 0;
if (setrlimit(RLIMIT_NOFILE, &r) != 0) {
printf("setrlimit() failed with errno=%d, %s\n", errno, strerror(errno));
exit(1);
}
if (getrlimit(RLIMIT_NOFILE, &r) != 0) {
printf("getrlimit() failed with errno=%d, %s\n", errno, strerror(errno));
exit(1);
}
printf("The soft limit is %llu\n", r.rlim_cur);
printf("The hard limit is %llu\n", r.rlim_max);
if (open("./rlimit.c", O_RDONLY)) {
printf("open() failed with errno=%d, %s\n", errno, strerror(errno));
exit(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment