summaryrefslogtreecommitdiff
path: root/libmailutils/base/closefds.c
blob: 49c024fb1cbc59140b0c06f3fea135a39447ed39 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2019-2020 Free Software Foundation, Inc.

   This library is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <stdlib.h>
#include <unistd.h>
#include <mailutils/util.h>

#if defined (HAVE_FUNC_CLOSEFROM)
# include <unistd.h>

static int
close_fds_sys (int minfd)
{
  closefrom (minfd);
  return 0;
}

#elif defined (HAVE_FCNTL_CLOSEM)
# include <fcntl.h>

static int
close_fds_sys (int minfd)
{
  fcntl (minfd, F_CLOSEM, 0);
  return 0;
}

#elif defined (HAVE_LIBPROC_H) && defined (HAVE_FUNC_PROC_PIDINFO)
#include <libproc.h>

static int
close_fds_sys (int minfd)
{
  pid_t pid = getpid ();
  struct proc_fdinfo *fdinfo;
  int i, n, size;

  size = proc_pidinfo (pid, PROC_PIDLISTFDS, 0, NULL, 0);
  if (size == 0)
    return 0;
  else if (size < 0)
    return -1;

  fdinfo = calloc (size, sizeof (fdinfo[0]));
  if (!fdinfo)
    return -1;

  n = proc_pidinfo (pid, PROC_PIDLISTFDS, 0, fdinfo, size);
  if (n <= 0)
    {
      free (fdinfo);
      return -1;
    }

  n /= PROC_PIDLISTFD_SIZE;
  
  for (i = minfd; i < n; i++)
    {
      close (fdinfo_buf[i].proc_fd);
    }

  free (fdinfo);
  return 0;
}

#elif defined (HAVE_PROC_SELF_FD)
# include <sys/types.h>
# include <dirent.h>
# include <limits.h>

static int
close_fds_sys (int minfd)
{
  DIR *dir;
  struct dirent *ent;
  
  dir = opendir ("/proc/self/fd");
  if (!dir)
    return -1;
  while ((ent = readdir (dir)) != NULL)
    {
      long n;
      char *p;
      
      if (ent->d_name[0] == '.')
	continue;

      n = strtol (ent->d_name, &p, 10);
      if (n >= minfd && n < INT_MAX && *p == 0)
	close ((int) n);
    }
  closedir (dir);
  return 0;
}

#else
# define close_fds_sys(fd) (-1)
#endif

static int
close_fds_bruteforce (int minfd)
{
  int i, n = mu_getmaxfd ();

  for (i = minfd; i < n; i++)
    close (i);

  return 0;
}

void
mu_close_fds (int minfd)
{
  if (close_fds_sys (minfd))
    close_fds_bruteforce (minfd);
}

Return to:

Send suggestions and report system problems to the System administrator.