Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close non-standard file descriptors #34

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions obs-command-source.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
#ifdef _WIN32
#include <Windows.h>
#else
#ifdef __APPLE__
#include <libproc.h>
#else // Linux
#define __USE_GNU // close_range
#endif
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
Expand Down Expand Up @@ -86,6 +91,23 @@ static void setenv_int(const char *name, int val)

#ifndef _WIN32

#if defined(__APPLE__)
static void closefrom(int lower)
{
#define NFDS 128
struct proc_fdinfo fds[NFDS];
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 >= NFDS);
}
#endif

static void fork_exec(const char *cmd, struct command_source *s, pid_t *pid_sig)
{
obs_source_t *current_src = obs_frontend_get_current_scene();
Expand All @@ -102,6 +124,12 @@ static void fork_exec(const char *cmd, struct command_source *s, pid_t *pid_sig)
setenv_if("OBS_SOURCE_NAME", obs_source_get_name(s->self));
setenv_int("OBS_TRANSITION_DURATION", obs_frontend_get_transition_duration());

#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__)
closefrom(3);
#else // Linux
close_range(3, 65535, 0);
#endif

execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
_exit(1);
}
Expand Down
Loading