safe_closefrom(), safe_fdwalk_set_cloexec() as public API
gspawn.c
has what I believe is, by now, quite a good implementation of async-signal-safe wrappers for closefrom()
and close_range()
.
Lower-level software like Flatpak would benefit from being able to call into these, rather than having to reimplement them. With newer GLib it could share the code that GLib already needs, and with older GLib, libglnx could have a backport.
gnome-control-center, libvirt and NetworkManager seem to be examples of GLib-based code that also reimplements this.
The API I would suggest is to put these in glib-unix.h
:
/* Returns -1 with errno set on error, 0 on success */
int g_closefrom (int lowfd);
int g_fdwalk_set_cloexec (int lowfd);
Merge request to follow. I realise this is probably too late for 2.80.
Edited by Simon McVittie