Merge branch 'valgrind-fd-fun' into 'main'

gspawn: Ignore invalid FDs when using safe_fdwalk()

See merge request GNOME/glib!3076
This commit is contained in:
Simon McVittie 2022-11-23 17:34:05 +00:00
commit 78d0ac4237

View File

@ -1343,21 +1343,6 @@ dupfd_cloexec (int old_fd, int new_fd_min)
return fd;
}
/* fdwalk()-compatible callback to close a valid fd.
* It is an error to pass an invalid fd (causing EBADF) to this function.
*
* This function is called between fork() and exec() and hence must be
* async-signal-safe (see signal-safety(7)).
*/
G_GNUC_UNUSED static int
close_func (void *data, int fd)
{
if (fd >= GPOINTER_TO_INT (data))
g_close (fd, NULL);
return 0;
}
/* fdwalk()-compatible callback to close a fd for non-compliant
* implementations of fdwalk() that potentially pass already
* closed fds.
@ -1427,6 +1412,8 @@ filename_to_fd (const char *p)
}
#endif
static int safe_fdwalk_with_invalid_fds (int (*cb)(void *data, int fd), void *data);
/* This function is called between fork() and exec() and hence must be
* async-signal-safe (see signal-safety(7)). */
static int
@ -1508,8 +1495,7 @@ safe_fdwalk (int (*cb)(void *data, int fd), void *data)
return res;
#endif
errno = ENOSYS;
return -1;
return safe_fdwalk_with_invalid_fds (cb, data);
#endif
}
@ -1620,9 +1606,6 @@ safe_fdwalk_set_cloexec (int lowfd)
ret = safe_fdwalk (set_cloexec, GINT_TO_POINTER (lowfd));
if (ret < 0 && errno == ENOSYS)
ret = safe_fdwalk_with_invalid_fds (set_cloexec, GINT_TO_POINTER (lowfd));
return ret;
}
@ -1678,10 +1661,7 @@ safe_closefrom (int lowfd)
if (ret == 0 || errno != ENOSYS)
return ret;
#endif /* HAVE_CLOSE_RANGE */
ret = safe_fdwalk (close_func, GINT_TO_POINTER (lowfd));
if (ret < 0 && errno == ENOSYS)
ret = safe_fdwalk_with_invalid_fds (close_func_with_invalid_fds, GINT_TO_POINTER (lowfd));
ret = safe_fdwalk (close_func_with_invalid_fds, GINT_TO_POINTER (lowfd));
return ret;
#endif