Make sure SDL file descriptors don't leak into child processes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
diff --git a/src/audio/SDL_audiodev.c b/src/audio/SDL_audiodev.c
index dc1daee..46530e2 100644
--- a/src/audio/SDL_audiodev.c
+++ b/src/audio/SDL_audiodev.c
@@ -51,7 +51,7 @@ test_device(const int iscapture, const char *fname, int flags, int (*test) (int
{
struct stat sb;
if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
- const int audio_fd = open(fname, flags, 0);
+ const int audio_fd = open(fname, flags | O_CLOEXEC, 0);
if (audio_fd >= 0) {
const int okay = test(audio_fd);
close(audio_fd);
diff --git a/src/audio/dsp/SDL_dspaudio.c b/src/audio/dsp/SDL_dspaudio.c
index 2124dcc..f2efaf3 100644
--- a/src/audio/dsp/SDL_dspaudio.c
+++ b/src/audio/dsp/SDL_dspaudio.c
@@ -103,7 +103,7 @@ DSP_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
SDL_zerop(this->hidden);
/* Open the audio device */
- this->hidden->audio_fd = open(devname, flags, 0);
+ this->hidden->audio_fd = open(devname, flags | O_CLOEXEC, 0);
if (this->hidden->audio_fd < 0) {
return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
}
diff --git a/src/audio/netbsd/SDL_netbsdaudio.c b/src/audio/netbsd/SDL_netbsdaudio.c
index 32817ab..38b1502 100644
--- a/src/audio/netbsd/SDL_netbsdaudio.c
+++ b/src/audio/netbsd/SDL_netbsdaudio.c
@@ -226,7 +226,7 @@ NETBSDAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
SDL_zerop(this->hidden);
/* Open the audio device */
- this->hidden->audio_fd = open(devname, iscapture ? O_RDONLY : O_WRONLY);
+ this->hidden->audio_fd = open(devname, (iscapture ? O_RDONLY : O_WRONLY) | O_CLOEXEC);
if (this->hidden->audio_fd < 0) {
return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
}
diff --git a/src/core/freebsd/SDL_evdev_kbd_freebsd.c b/src/core/freebsd/SDL_evdev_kbd_freebsd.c
index 48fab58..2491035 100644
--- a/src/core/freebsd/SDL_evdev_kbd_freebsd.c
+++ b/src/core/freebsd/SDL_evdev_kbd_freebsd.c
@@ -238,7 +238,7 @@ SDL_EVDEV_kbd_init(void)
kbd->npadch = -1;
/* This might fail if we're not connected to a tty (e.g. on the Steam Link) */
- kbd->keyboard_fd = kbd->console_fd = open("/dev/tty", O_RDONLY);
+ kbd->keyboard_fd = kbd->console_fd = open("/dev/tty", O_RDONLY | O_CLOEXEC);
kbd->shift_state = 0;
@@ -274,7 +274,7 @@ SDL_EVDEV_kbd_init(void)
*/
ioctl(kbd->console_fd, CONS_RELKBD, 1ul);
asprintf(&devicePath, "/dev/kbd%d", kbd->kbInfo->kb_index);
- kbd->keyboard_fd = open(devicePath, O_WRONLY);
+ kbd->keyboard_fd = open(devicePath, O_WRONLY | O_CLOEXEC);
if (kbd->keyboard_fd == -1)
{
// Give keyboard back.
diff --git a/src/core/linux/SDL_evdev.c b/src/core/linux/SDL_evdev.c
index bd4fcf6..3635920 100644
--- a/src/core/linux/SDL_evdev.c
+++ b/src/core/linux/SDL_evdev.c
@@ -725,7 +725,7 @@ SDL_EVDEV_device_added(const char *dev_path, int udev_class)
return SDL_OutOfMemory();
}
- item->fd = open(dev_path, O_RDONLY | O_NONBLOCK);
+ item->fd = open(dev_path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (item->fd < 0) {
SDL_free(item);
return SDL_SetError("Unable to open %s", dev_path);
diff --git a/src/core/linux/SDL_evdev_kbd.c b/src/core/linux/SDL_evdev_kbd.c
index 506e24d..4ea6b14 100644
--- a/src/core/linux/SDL_evdev_kbd.c
+++ b/src/core/linux/SDL_evdev_kbd.c
@@ -354,7 +354,7 @@ SDL_EVDEV_kbd_init(void)
kbd->npadch = -1;
/* This might fail if we're not connected to a tty (e.g. on the Steam Link) */
- kbd->console_fd = open("/dev/tty", O_RDONLY);
+ kbd->console_fd = open("/dev/tty", O_RDONLY | O_CLOEXEC);
if (ioctl(kbd->console_fd, TIOCLINUX, shift_state) == 0) {
kbd->shift_state = *shift_state;
diff --git a/src/core/openbsd/SDL_wscons_kbd.c b/src/core/openbsd/SDL_wscons_kbd.c
index 8d62a6c..a507b6f 100644
--- a/src/core/openbsd/SDL_wscons_kbd.c
+++ b/src/core/openbsd/SDL_wscons_kbd.c
@@ -416,7 +416,7 @@ static SDL_WSCONS_input_data* SDL_WSCONS_Init_Keyboard(const char* dev)
if (!input) {
return input;
}
- input->fd = open(dev,O_RDWR | O_NONBLOCK);
+ input->fd = open(dev,O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (input->fd == -1) {
free(input);
input = NULL;
diff --git a/src/core/openbsd/SDL_wscons_mouse.c b/src/core/openbsd/SDL_wscons_mouse.c
index da8e9e2..7251050 100644
--- a/src/core/openbsd/SDL_wscons_mouse.c
+++ b/src/core/openbsd/SDL_wscons_mouse.c
@@ -43,7 +43,7 @@ SDL_WSCONS_mouse_input_data* SDL_WSCONS_Init_Mouse()
SDL_WSCONS_mouse_input_data* mouseInputData = SDL_calloc(1, sizeof(SDL_WSCONS_mouse_input_data));
if (!mouseInputData) return NULL;
- mouseInputData->fd = open("/dev/wsmouse",O_RDWR | O_NONBLOCK);
+ mouseInputData->fd = open("/dev/wsmouse",O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (mouseInputData->fd == -1) {free(mouseInputData); return NULL; }
#ifdef WSMOUSEIO_SETMODE
ioctl(mouseInputData->fd, WSMOUSEIO_SETMODE, WSMOUSE_COMPAT);
diff --git a/src/cpuinfo/SDL_cpuinfo.c b/src/cpuinfo/SDL_cpuinfo.c
index ded6cd5..0205729 100644
--- a/src/cpuinfo/SDL_cpuinfo.c
+++ b/src/cpuinfo/SDL_cpuinfo.c
@@ -361,7 +361,7 @@ CPU_haveARMSIMD(void)
int arm_simd = 0;
int fd;
- fd = open("/proc/self/auxv", O_RDONLY);
+ fd = open("/proc/self/auxv", O_RDONLY | O_CLOEXEC);
if (fd >= 0)
{
Elf32_auxv_t aux;
@@ -417,7 +417,7 @@ readProcAuxvForNeon(void)
int neon = 0;
int fd;
- fd = open("/proc/self/auxv", O_RDONLY);
+ fd = open("/proc/self/auxv", O_RDONLY | O_CLOEXEC);
if (fd >= 0)
{
Elf32_auxv_t aux;
diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c
index 515aa9d..b183ca7 100644
--- a/src/haptic/linux/SDL_syshaptic.c
+++ b/src/haptic/linux/SDL_syshaptic.c
@@ -260,7 +260,7 @@ MaybeAddDevice(const char *path)
}
/* try to open */
- fd = open(path, O_RDWR, 0);
+ fd = open(path, O_RDWR | O_CLOEXEC, 0);
if (fd < 0) {
return -1;
}
@@ -375,7 +375,7 @@ SDL_SYS_HapticName(int index)
item = HapticByDevIndex(index);
/* Open the haptic device. */
name = NULL;
- fd = open(item->fname, O_RDONLY, 0);
+ fd = open(item->fname, O_RDONLY | O_CLOEXEC, 0);
if (fd >= 0) {
@@ -453,7 +453,7 @@ SDL_SYS_HapticOpen(SDL_Haptic * haptic)
item = HapticByDevIndex(haptic->index);
/* Open the character device */
- fd = open(item->fname, O_RDWR, 0);
+ fd = open(item->fname, O_RDWR | O_CLOEXEC, 0);
if (fd < 0) {
return SDL_SetError("Haptic: Unable to open %s: %s",
item->fname, strerror(errno));
@@ -483,7 +483,7 @@ SDL_SYS_HapticMouse(void)
for (item = SDL_hapticlist; item; item = item->next) {
/* Open the device. */
- fd = open(item->fname, O_RDWR, 0);
+ fd = open(item->fname, O_RDWR | O_CLOEXEC, 0);
if (fd < 0) {
return SDL_SetError("Haptic: Unable to open %s: %s",
item->fname, strerror(errno));
@@ -570,7 +570,7 @@ SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
return SDL_SetError("Haptic: Joystick doesn't have Haptic capabilities");
}
- fd = open(joystick->hwdata->fname, O_RDWR, 0);
+ fd = open(joystick->hwdata->fname, O_RDWR | O_CLOEXEC, 0);
if (fd < 0) {
return SDL_SetError("Haptic: Unable to open %s: %s",
joystick->hwdata->fname, strerror(errno));
diff --git a/src/hidapi/linux/hid.c b/src/hidapi/linux/hid.c
index b954415..418dc0a 100644
--- a/src/hidapi/linux/hid.c
+++ b/src/hidapi/linux/hid.c
@@ -716,7 +716,7 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path, int bExclusive)
dev = new_hid_device();
/* OPEN HERE */
- dev->device_handle = open(path, O_RDWR);
+ dev->device_handle = open(path, O_RDWR | O_CLOEXEC);
/* If we have a good handle, return it. */
if (dev->device_handle >= 0) {
diff --git a/src/joystick/bsd/SDL_bsdjoystick.c b/src/joystick/bsd/SDL_bsdjoystick.c
index 8735b4d..e0bf2a1 100644
--- a/src/joystick/bsd/SDL_bsdjoystick.c
+++ b/src/joystick/bsd/SDL_bsdjoystick.c
@@ -240,7 +240,7 @@ BSD_JoystickInit(void)
}
for (i = 0; i < MAX_JOY_JOYS; i++) {
SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
- fd = open(s, O_RDONLY);
+ fd = open(s, O_RDONLY | O_CLOEXEC);
if (fd != -1) {
joynames[numjoysticks++] = SDL_strdup(s);
close(fd);
@@ -357,7 +357,7 @@ BSD_JoystickOpen(SDL_Joystick *joy, int device_index)
int fd;
int i;
- fd = open(path, O_RDONLY);
+ fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd == -1) {
return SDL_SetError("%s: %s", path, strerror(errno));
}
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index a3936e6..88aea57 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -302,7 +302,7 @@ MaybeAddDevice(const char *path)
}
}
- fd = open(path, O_RDONLY, 0);
+ fd = open(path, O_RDONLY | O_CLOEXEC, 0);
if (fd < 0) {
return -1;
}
@@ -963,7 +963,7 @@ PrepareJoystickHwdata(SDL_Joystick *joystick, SDL_joylist_item *item)
&joystick->naxes,
&joystick->nhats);
} else {
- const int fd = open(item->path, O_RDWR, 0);
+ const int fd = open(item->path, O_RDWR | O_CLOEXEC, 0);
if (fd < 0) {
return SDL_SetError("Unable to open %s", item->path);
}
diff --git a/src/power/haiku/SDL_syspower.c b/src/power/haiku/SDL_syspower.c
index cb47aa9..87167b9 100644
--- a/src/power/haiku/SDL_syspower.c
+++ b/src/power/haiku/SDL_syspower.c
@@ -45,7 +45,7 @@
SDL_bool
SDL_GetPowerInfo_Haiku(SDL_PowerState * state, int *seconds, int *percent)
{
- const int fd = open("/dev/misc/apm", O_RDONLY);
+ const int fd = open("/dev/misc/apm", O_RDONLY | O_CLOEXEC);
SDL_bool need_details = SDL_FALSE;
uint16 regs[6];
uint8 ac_status;
diff --git a/src/power/linux/SDL_syspower.c b/src/power/linux/SDL_syspower.c
index fa2aaee..dee691d 100644
--- a/src/power/linux/SDL_syspower.c
+++ b/src/power/linux/SDL_syspower.c
@@ -51,7 +51,7 @@ open_power_file(const char *base, const char *node, const char *key)
}
snprintf(path, pathlen, "%s/%s/%s", base, node, key);
- return open(path, O_RDONLY);
+ return open(path, O_RDONLY | O_CLOEXEC);
}
@@ -330,7 +330,7 @@ SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
int battery_flag = 0;
int battery_percent = 0;
int battery_time = 0;
- const int fd = open(proc_apm_path, O_RDONLY);
+ const int fd = open(proc_apm_path, O_RDONLY | O_CLOEXEC);
char buf[128];
char *ptr = &buf[0];
char *str = NULL;