tests: close inherited descriptors before exec

This commit is contained in:
2026-08-14 02:49:57 +09:00
parent f395f29918
commit d7115c5eb5
2 changed files with 20 additions and 2 deletions

View File

@@ -3,6 +3,7 @@
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h>
#include <poll.h> #include <poll.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
@@ -228,7 +229,8 @@ static int
startdaemon(Daemon *d, char *program, char *mapdir) startdaemon(Daemon *d, char *program, char *mapdir)
{ {
char missing[384]; char missing[384];
int notifyfd, watch, errpipe[2]; int fd, notifyfd, watch, errpipe[2];
long maxfd;
pid_t pid; pid_t pid;
memset(d, 0, sizeof *d); memset(d, 0, sizeof *d);
@@ -281,6 +283,13 @@ startdaemon(Daemon *d, char *program, char *mapdir)
dup2(errpipe[1], STDERR_FILENO) < 0) dup2(errpipe[1], STDERR_FILENO) < 0)
_exit(126); _exit(126);
close(errpipe[1]); close(errpipe[1]);
if(close_range(3, UINT_MAX, 0) < 0){
maxfd = sysconf(_SC_OPEN_MAX);
if(maxfd < 0)
maxfd = 1024;
for(fd = 3; fd < maxfd; fd++)
close(fd);
}
if(setenv("XDG_RUNTIME_DIR", d->runtime, 1) < 0 || if(setenv("XDG_RUNTIME_DIR", d->runtime, 1) < 0 ||
setenv("XDG_CONFIG_HOME", d->config, 1) < 0 || setenv("XDG_CONFIG_HOME", d->config, 1) < 0 ||
setenv("HOME", d->home, 1) < 0 || unsetenv("DISPLAY") < 0 || setenv("HOME", d->home, 1) < 0 || unsetenv("DISPLAY") < 0 ||

View File

@@ -2,6 +2,7 @@
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h>
#include <poll.h> #include <poll.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
@@ -174,7 +175,8 @@ static int
startdaemon(Daemon *d, char *program, char *mapdir) startdaemon(Daemon *d, char *program, char *mapdir)
{ {
char missing[384]; char missing[384];
int notifyfd, watch, errpipe[2]; int fd, notifyfd, watch, errpipe[2];
long maxfd;
pid_t pid; pid_t pid;
memset(d, 0, sizeof *d); memset(d, 0, sizeof *d);
@@ -228,6 +230,13 @@ startdaemon(Daemon *d, char *program, char *mapdir)
dup2(errpipe[1], STDERR_FILENO) < 0) dup2(errpipe[1], STDERR_FILENO) < 0)
_exit(126); _exit(126);
close(errpipe[1]); close(errpipe[1]);
if(close_range(3, UINT_MAX, 0) < 0){
maxfd = sysconf(_SC_OPEN_MAX);
if(maxfd < 0)
maxfd = 1024;
for(fd = 3; fd < maxfd; fd++)
close(fd);
}
if(setenv("XDG_RUNTIME_DIR", d->runtime, 1) < 0 || if(setenv("XDG_RUNTIME_DIR", d->runtime, 1) < 0 ||
setenv("XDG_CONFIG_HOME", d->config, 1) < 0 || setenv("XDG_CONFIG_HOME", d->config, 1) < 0 ||
setenv("HOME", d->home, 1) < 0 || unsetenv("DISPLAY") < 0 || setenv("HOME", d->home, 1) < 0 || unsetenv("DISPLAY") < 0 ||