Skip to content

Commit

Permalink
Close non-standard file descriptors
Browse files Browse the repository at this point in the history
The obs-ffmpeg-mux process cannot be terminated while the process forked
by this plugin is running, which causes audio subsystem of OBS stalls.
The obs-ffmpeg-mux process is terminated when an EOF appears on the
pipe. However, since the forked process holds the file descriptor of the
write-end of the pipe, the EOF won't arrive to the obs-ffmpeg-mux
process.
  • Loading branch information
norihiro committed Apr 7, 2024
1 parent 64eb927 commit e4a3748
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/capdev-nix.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#ifdef __APPLE__
#include <libproc.h>
#else
#define _GNU_SOURCE // close_range
#endif
#include <unistd.h>
#include <sys/wait.h>
#include <inttypes.h>
Expand All @@ -17,6 +22,22 @@

#define K_OFFSET_DECAY (256 * 16)

#if defined(__APPLE__)
static void closefrom(int lower)
{
struct proc_fdinfo fds[128];
pid_t pid = getpid();
int ret;
do {
ret = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, fds, sizeof(fds)) / sizeof(*fds);
for (int i = 0; i < ret; i++) {
if (fds[i].proc_fd >= lower)
close(fds[i].proc_fd);
}
} while (ret >= sizeof(fds) / sizeof(*fds));
}
#endif

static pid_t thread_start_proc(const char *name, int *fd_req, int *fd_data)
{
int pipe_req[2] = {-1, -1};
Expand Down Expand Up @@ -62,6 +83,11 @@ static pid_t thread_start_proc(const char *name, int *fd_req, int *fd_data)
dup2(pipe_data[1], 1);
close(pipe_data[0]);
close(pipe_data[1]);
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__)
closefrom(3);
#else // Linux
close_range(3, 65535, 0);
#endif
if (execlp(proc_path, PROC_4219, name, NULL) < 0) {
fprintf(stderr, "Error: failed to exec \"%s\"\n", proc_path);
close(0);
Expand Down

0 comments on commit e4a3748

Please sign in to comment.