File: rlimit_nofile.c

package info (click to toggle)
valgrind 1:3.12.0~svn20160714-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 120,428 kB
  • ctags: 70,855
  • sloc: ansic: 674,645; exp: 26,134; xml: 21,574; asm: 7,570; cpp: 7,567; makefile: 7,380; sh: 6,188; perl: 5,855; haskell: 195
file content (108 lines) | stat: -rw-r--r-- 2,479 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <unistd.h>
#include "fdleak.h"

int main(int argc, char **argv)
{
   struct rlimit oldrlim;
   struct rlimit newrlim;
   int fd;

   CLOSE_INHERITED_FDS;

   if (getrlimit(RLIMIT_NOFILE, &oldrlim) < 0)
   {
      perror("getrlimit");
      exit(1);
   }

   newrlim.rlim_cur = oldrlim.rlim_max+1;
   newrlim.rlim_max = oldrlim.rlim_max;
   if (setrlimit(RLIMIT_NOFILE, &newrlim) == -1)
   {
      if (errno != EINVAL) {
         fprintf(stderr, "setrlimit exceeding hardlimit must set errno=EINVAL\n");
         exit(1);
      }
   }
   else
   {
        fprintf(stderr, "setrlimit exceeding hardlimit must return -1\n");
        exit(1);
   }

   newrlim.rlim_cur = oldrlim.rlim_max;
   newrlim.rlim_max = oldrlim.rlim_max+1;
   if (setrlimit(RLIMIT_NOFILE, &newrlim) == -1)
   {
      if (errno != EPERM) {
         fprintf(stderr, "setrlimit changing hardlimit must set errno=EPERM\n");
         exit(1);
      }
   }
   else
   {
        fprintf(stderr, "setrlimit changing hardlimit must return -1\n");
        exit(1);
   }

   newrlim.rlim_cur = oldrlim.rlim_cur / 2;
   newrlim.rlim_max = oldrlim.rlim_max;
     
   if (setrlimit(RLIMIT_NOFILE, &newrlim) < 0)
   {
      perror("setrlimit");
      exit(1);
   }
     
   if (getrlimit(RLIMIT_NOFILE, &newrlim) < 0)
   {
      perror("getrlimit");
      exit(1);
   }

   if (newrlim.rlim_cur != oldrlim.rlim_cur / 2)
   {
      fprintf(stderr, "rlim_cur is %llu (should be %llu)\n",
              (unsigned long long)newrlim.rlim_cur,
              (unsigned long long)oldrlim.rlim_cur / 2);
   }

   if (newrlim.rlim_max != oldrlim.rlim_max)
   {
      fprintf(stderr, "rlim_max is %llu (should be %llu)\n",
              (unsigned long long)newrlim.rlim_max,
              (unsigned long long)oldrlim.rlim_max);
   }

   newrlim.rlim_cur -= 3; /* allow for stdin, stdout and stderr */

   while (newrlim.rlim_cur-- > 0)
   {
      if (open("/dev/null", O_RDONLY) < 0)
      {
         perror("open");
      }
   }

   if ((fd = open("/dev/null", O_RDONLY)) >= 0)
   {
      fprintf(stderr, "open succeeded with fd %d - it should have failed!\n", fd);
   }
   else if (errno != EMFILE)
   {
      perror("open");
   }

   if (setrlimit(RLIMIT_NOFILE, NULL) != -1  || errno != EFAULT)
   {
      fprintf(stderr, "setrlimit non addressable arg2 must set errno=EFAULT\n");
      exit(1);
   }
   
   exit(0);
}