Coverage Report

Created: 2025-08-26 06:15

/src/tmux/compat/closefrom.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2004-2005 Todd C. Miller <Todd.Miller@courtesan.com>
3
 *
4
 * Permission to use, copy, modify, and distribute this software for any
5
 * purpose with or without fee is hereby granted, provided that the above
6
 * copyright notice and this permission notice appear in all copies.
7
 *
8
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
 */
16
17
#ifndef HAVE_CLOSEFROM
18
19
#include <sys/types.h>
20
#include <sys/param.h>
21
#include <unistd.h>
22
#include <stdio.h>
23
#ifdef HAVE_FCNTL_H
24
# include <fcntl.h>
25
#endif
26
#include <limits.h>
27
#include <stdlib.h>
28
#include <stddef.h>
29
#include <string.h>
30
#include <unistd.h>
31
#ifdef HAVE_DIRENT_H
32
# include <dirent.h>
33
# define NAMLEN(dirent) strlen((dirent)->d_name)
34
#else
35
# define dirent direct
36
# define NAMLEN(dirent) (dirent)->d_namlen
37
# ifdef HAVE_SYS_NDIR_H
38
#  include <sys/ndir.h>
39
# endif
40
# ifdef HAVE_SYS_DIR_H
41
#  include <sys/dir.h>
42
# endif
43
# ifdef HAVE_NDIR_H
44
#  include <ndir.h>
45
# endif
46
#endif
47
#if defined(HAVE_LIBPROC_H)
48
# include <libproc.h>
49
#endif
50
51
#include "compat.h"
52
53
#ifndef OPEN_MAX
54
0
# define OPEN_MAX 256
55
#endif
56
57
#if 0
58
__unused static const char rcsid[] = "$Sudo: closefrom.c,v 1.11 2006/08/17 15:26:54 millert Exp $";
59
#endif /* lint */
60
61
#ifndef HAVE_FCNTL_CLOSEM
62
/*
63
 * Close all file descriptors greater than or equal to lowfd.
64
 */
65
static void
66
closefrom_fallback(int lowfd)
67
0
{
68
0
  long fd, maxfd;
69
70
  /*
71
   * Fall back on sysconf() or getdtablesize().  We avoid checking
72
   * resource limits since it is possible to open a file descriptor
73
   * and then drop the rlimit such that it is below the open fd.
74
   */
75
0
#ifdef HAVE_SYSCONF
76
0
  maxfd = sysconf(_SC_OPEN_MAX);
77
#else
78
  maxfd = getdtablesize();
79
#endif /* HAVE_SYSCONF */
80
0
  if (maxfd < 0)
81
0
    maxfd = OPEN_MAX;
82
83
0
  for (fd = lowfd; fd < maxfd; fd++)
84
0
    (void) close((int) fd);
85
0
}
86
#endif /* HAVE_FCNTL_CLOSEM */
87
88
#ifdef HAVE_FCNTL_CLOSEM
89
void
90
closefrom(int lowfd)
91
{
92
    (void) fcntl(lowfd, F_CLOSEM, 0);
93
}
94
#elif defined(HAVE_LIBPROC_H) && defined(HAVE_PROC_PIDINFO)
95
void
96
closefrom(int lowfd)
97
{
98
  int i, r, sz;
99
  pid_t pid = getpid();
100
  struct proc_fdinfo *fdinfo_buf = NULL;
101
102
  sz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0);
103
  if (sz == 0)
104
    return; /* no fds, really? */
105
  else if (sz == -1)
106
    goto fallback;
107
  if ((fdinfo_buf = malloc(sz)) == NULL)
108
    goto fallback;
109
  r = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, fdinfo_buf, sz);
110
  if (r < 0 || r > sz)
111
    goto fallback;
112
  for (i = 0; i < r / (int)PROC_PIDLISTFD_SIZE; i++) {
113
    if (fdinfo_buf[i].proc_fd >= lowfd)
114
      close(fdinfo_buf[i].proc_fd);
115
  }
116
  free(fdinfo_buf);
117
  return;
118
 fallback:
119
  free(fdinfo_buf);
120
  closefrom_fallback(lowfd);
121
  return;
122
}
123
#elif defined(HAVE_DIRFD) && defined(HAVE_PROC_PID)
124
void
125
closefrom(int lowfd)
126
0
{
127
0
    long fd;
128
0
    char fdpath[PATH_MAX], *endp;
129
0
    struct dirent *dent;
130
0
    DIR *dirp;
131
0
    int len;
132
133
    /* Check for a /proc/$$/fd directory. */
134
0
    len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid());
135
0
    if (len > 0 && (size_t)len < sizeof(fdpath) && (dirp = opendir(fdpath))) {
136
0
  while ((dent = readdir(dirp)) != NULL) {
137
0
      fd = strtol(dent->d_name, &endp, 10);
138
0
      if (dent->d_name != endp && *endp == '\0' &&
139
0
    fd >= 0 && fd < INT_MAX && fd >= lowfd && fd != dirfd(dirp))
140
0
    (void) close((int) fd);
141
0
  }
142
0
  (void) closedir(dirp);
143
0
  return;
144
0
    }
145
    /* /proc/$$/fd strategy failed, fall back to brute force closure */
146
0
    closefrom_fallback(lowfd);
147
0
}
148
#else
149
void
150
closefrom(int lowfd)
151
{
152
  closefrom_fallback(lowfd);
153
}
154
#endif /* !HAVE_FCNTL_CLOSEM */
155
#endif /* HAVE_CLOSEFROM */