1 /* This file is part of GNU Pies
2    Copyright (C) 2015-2020 Sergey Poznyakoff
3 
4    GNU Pies is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Pies is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Pies.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include <config.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include "libpies.h"
21 
22 #if defined (HAVE_FUNC_CLOSEFROM)
23 # include <unistd.h>
24 
25 static int
close_fds_sys(int minfd)26 close_fds_sys (int minfd)
27 {
28   closefrom (minfd);
29   return 0;
30 }
31 
32 #elif defined (HAVE_FCNTL_CLOSEM)
33 # include <fcntl.h>
34 
35 static int
close_fds_sys(int minfd)36 close_fds_sys (int minfd)
37 {
38   fcntl (minfd, F_CLOSEM, 0);
39   return 0;
40 }
41 
42 #elif defined (HAVE_LIBPROC_H) && defined (HAVE_FUNC_PROC_PIDINFO)
43 #include <libproc.h>
44 
45 static int
close_fds_sys(int minfd)46 close_fds_sys (int minfd)
47 {
48   pid_t pid = getpid ();
49   struct proc_fdinfo *fdinfo;
50   int i, n, size;
51 
52   size = proc_pidinfo (pid, PROC_PIDLISTFDS, 0, NULL, 0);
53   if (size == 0)
54     return 0;
55   else if (size < 0)
56     return -1;
57 
58   fdinfo = calloc (size, sizeof (fdinfo[0]));
59   if (!fdinfo)
60     return -1;
61 
62   n = proc_pidinfo (pid, PROC_PIDLISTFDS, 0, fdinfo, size);
63   if (n <= 0)
64     {
65       free (fdinfo);
66       return -1;
67     }
68 
69   n /= PROC_PIDLISTFD_SIZE;
70 
71   for (i = 0; i < n; i++)
72     {
73       if (fdinfo_buf[i].proc_fd >= minfd)
74 	close (fdinfo_buf[i].proc_fd);
75     }
76 
77   free (fdinfo);
78   return 0;
79 }
80 
81 #elif defined (HAVE_PROC_SELF_FD)
82 # include <sys/types.h>
83 # include <dirent.h>
84 # include <limits.h>
85 
86 static int
close_fds_sys(int minfd)87 close_fds_sys (int minfd)
88 {
89   DIR *dir;
90   struct dirent *ent;
91 
92   dir = opendir ("/proc/self/fd");
93   if (!dir)
94     return -1;
95   while ((ent = readdir (dir)) != NULL)
96     {
97       long n;
98       char *p;
99 
100       if (ent->d_name[0] == '.')
101 	continue;
102 
103       n = strtol (ent->d_name, &p, 10);
104       if (n >= minfd && n < INT_MAX && *p == 0)
105 	close ((int) n);
106     }
107   closedir (dir);
108   return 0;
109 }
110 
111 #else
112 # define close_fds_sys(fd) (-1)
113 #endif
114 
115 static int
close_fds_bruteforce(int minfd)116 close_fds_bruteforce (int minfd)
117 {
118   int i, n = getmaxfd ();
119 
120   for (i = minfd; i < n; i++)
121     close (i);
122 
123   return 0;
124 }
125 
126 void
pies_close_fds(int minfd)127 pies_close_fds (int minfd)
128 {
129   if (close_fds_sys (minfd))
130     close_fds_bruteforce (minfd);
131 }
132