vircommand: Isolate FD dir parsing into a separate function

So far, virCommandMassCloseGetFDsLinux() opens "/proc/self/fd",
iterates over it marking opened FDs in @fds bitmap. Well, we can
do the same on other systems (with altered path), like MacOS or
FreeBSD. Therefore, isolate dir iteration into a separate
function that accepts dir path as an argument.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Michal Privoznik 2023-08-29 08:48:56 +02:00
parent dfe496ae33
commit 6ded014ba3

View File

@ -473,16 +473,12 @@ virExecCommon(virCommand *cmd, gid_t *groups, int ngroups)
} }
# ifdef __linux__ # ifdef __linux__
/* On Linux, we can utilize procfs and read the table of opened
* FDs and selectively close only those FDs we don't want to pass
* onto child process (well, the one we will exec soon since this
* is called from the child). */
static int static int
virCommandMassCloseGetFDsLinux(virBitmap *fds) virCommandMassCloseGetFDsDir(virBitmap *fds,
const char *dirName)
{ {
g_autoptr(DIR) dp = NULL; g_autoptr(DIR) dp = NULL;
struct dirent *entry; struct dirent *entry;
const char *dirName = "/proc/self/fd";
int rc; int rc;
if (virDirOpen(&dp, dirName) < 0) if (virDirOpen(&dp, dirName) < 0)
@ -506,16 +502,22 @@ virCommandMassCloseGetFDsLinux(virBitmap *fds)
return 0; return 0;
} }
# endif /* __linux__ */
# else /* !__linux__ */
static int static int
virCommandMassCloseGetFDsGeneric(virBitmap *fds) virCommandMassCloseGetFDs(virBitmap *fds)
{ {
# ifdef __linux__
/* On Linux, we can utilize procfs and read the table of opened
* FDs and selectively close only those FDs we don't want to pass
* onto child process (well, the one we will exec soon since this
* is called from the child). */
return virCommandMassCloseGetFDsDir(fds, "/proc/self/fd");
# else
virBitmapSetAll(fds); virBitmapSetAll(fds);
return 0; return 0;
# endif
} }
# endif /* !__linux__ */
static int static int
virCommandMassCloseFrom(virCommand *cmd, virCommandMassCloseFrom(virCommand *cmd,
@ -544,13 +546,8 @@ virCommandMassCloseFrom(virCommand *cmd,
fds = virBitmapNew(openmax); fds = virBitmapNew(openmax);
# ifdef __linux__ if (virCommandMassCloseGetFDs(fds) < 0)
if (virCommandMassCloseGetFDsLinux(fds) < 0)
return -1; return -1;
# else
if (virCommandMassCloseGetFDsGeneric(fds) < 0)
return -1;
# endif
lastfd = MAX(lastfd, childin); lastfd = MAX(lastfd, childin);
lastfd = MAX(lastfd, childout); lastfd = MAX(lastfd, childout);