common: drop closefrom() shim, cockpithacks

We have a cockpithacks.h file which is meant to collect various
workarounds/polyfills that we have for older operating systems.  This
currently only contains a single polyfill: for closefrom(), which we can
replace with the now-widely-available close_range().  Make that change,
and drop the files.
This commit is contained in:
Allison Karlitskaya 2026-01-29 10:38:53 +01:00 committed by Martin Pitt
parent 570661c606
commit f558d6baab
9 changed files with 5 additions and 156 deletions

View file

@ -38,7 +38,7 @@ AC_PROG_RANLIB
# This is required to find the correct `ar` for cross-compiling
AC_CHECK_TOOL(AR, ar)
AC_CHECK_FUNCS(closefrom pidfd_getpid)
AC_CHECK_FUNCS(pidfd_getpid)
AM_SILENT_RULES([yes])

View file

@ -16,7 +16,6 @@ libcockpit_common_nodeps_a_SOURCES = \
src/common/cockpitfdpassing.h \
src/common/cockpitframe.c \
src/common/cockpitframe.h \
src/common/cockpithacks.h \
src/common/cockpithex.c \
src/common/cockpithex.h \
src/common/cockpitjsonprint.c \
@ -50,7 +49,6 @@ libcockpit_common_a_LIBS = \
libcockpit_common_a_SOURCES = \
src/common/cockpitchannel.c \
src/common/cockpitchannel.h \
src/common/cockpitclosefrom.c \
src/common/cockpitcontrolmessages.c \
src/common/cockpitcontrolmessages.h \
src/common/cockpiterror.c \

View file

@ -1,85 +0,0 @@
/*
* This file is part of Cockpit.
*
* Copyright (C) 2021 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "cockpithacks.h"
#ifndef HAVE_CLOSEFROM
#include <assert.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/resource.h>
void
cockpit_closefrom (int lowfd)
{
DIR *d;
if ((d = opendir ("/proc/self/fd")))
{
struct dirent *de;
while ((de = readdir (d))) {
char *e;
errno = 0;
long l = strtol (de->d_name, &e, 10);
if (errno != 0 || !e || *e)
continue;
int fd = (int) l;
if ((long) fd != l)
continue;
if (fd == dirfd (d))
continue;
/* don't bother about error checking; On Linux, EINTR still closes the fd, and with EBADF we already have a closed fd */
if (fd >= lowfd)
close (fd);
}
closedir (d);
}
else
{
/* If /proc is not mounted or not accessible we fall back to the old
* rlimit trick */
struct rlimit rl;
int open_max;
if (getrlimit (RLIMIT_NOFILE, &rl) == 0 && rl.rlim_max != RLIM_INFINITY)
open_max = rl.rlim_max;
else
open_max = sysconf (_SC_OPEN_MAX);
for (int fd = lowfd; fd < open_max; fd++)
close (fd);
}
}
#endif

View file

@ -19,15 +19,8 @@
#pragma once
/* See the comment in cockpithacks.h for an explanation of why this file
* exists.
*
* This file is intended to be included from code which is built against
* GLib. For the "pure C" parts of cockpit, include cockpithacks.h
* directly.
*/
/* Workarounds for older versions of GLib. */
#include "cockpithacks.h"
#include <glib.h>
/* g_debug() defaults to writing its output to stdout, which doesn't

View file

@ -1,52 +0,0 @@
/*
* This file is part of Cockpit.
*
* Copyright (C) 2020 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
/* Here's where we try to implement "temporary" workaround code:
* anything that might be labelled TODO, FIXME, HACK, XXX, etc. Having
* the workarounds in one place gives at least two advantages:
*
* - common workarounds made in several places can be given a single
* function/macro/etc. defined in this file, giving a common place
* to document the workaround, links to related bug reports, etc.
* This also makes removing the workaround easier: remove it from
* this file and the compiler will help us find all the places where
* we were using it.
*
* For this reason, we even add small functions here for totally
* trivial stuff.
*
* - having all of our hacks in a central place gives us one place to
* periodically check, allowing us to remove hacks that are not
* longer relevant (because the bug got fixed, etc).
*
* This file is intended to be included from code which isn't using
* GLib, and therefore must only contain "pure libc" C. For code that
* depends on GLib (and friends), there's cockpithacks-glib.h.
*/
#ifndef PACKAGE_VERSION
#error config.h should be included from the top of every .c file
#endif
#ifndef HAVE_CLOSEFROM
#define closefrom(lowfd) cockpit_closefrom(lowfd)
void cockpit_closefrom (int lowfd);
#endif

View file

@ -22,7 +22,6 @@
#include "cockpitpipe.h"
#include "cockpitflow.h"
#include "cockpithacks.h"
#include "cockpitunicode.h"
#include <glib-unix.h>
@ -1407,7 +1406,7 @@ cockpit_pipe_pty (const gchar **argv,
pid = forkpty (&fd, NULL, NULL, &winsz);
if (pid == 0)
{
closefrom (3);
close_range (3, ~0U, 0);
if (directory)
{

View file

@ -11,8 +11,6 @@ cockpit_session_LDADD = \
$(NULL)
cockpit_session_SOURCES = \
src/common/cockpitclosefrom.c \
src/common/cockpithacks.h \
src/session/client-certificate.h \
src/session/client-certificate.c \
src/session/client-certificate.h \

View file

@ -23,7 +23,6 @@
#include "common/cockpitframe.h"
#include "common/cockpitjsonprint.h"
#include "common/cockpithacks.h"
#include "common/cockpitmemory.h"
#include <fcntl.h>
@ -572,7 +571,7 @@ fd_remap (const int *remap_fds,
abort_with_message ("dup2(%d, %d) failed: %m", fds[i], i);
/* close everything else */
closefrom (n_remap_fds);
close_range (n_remap_fds, ~0U, 0);
}
int

View file

@ -28,7 +28,6 @@
#include "common/cockpitauthorize.h"
#include "common/cockpitconf.h"
#include "common/cockpiterror.h"
#include "common/cockpithacks.h"
#include "common/cockpithex.h"
#include "common/cockpitjson.h"
#include "common/cockpitmemory.h"
@ -469,7 +468,7 @@ session_child_setup (gpointer data)
close (child->io);
closefrom (3);
close_range (3, ~0U, 0);
}
static CockpitPipe *