1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-13 10:02:38 +00:00

Change the closeallfds() routine to use closefrom() when it is

available (closefrom() was added to FreeBSD in 8.0-release).
The selection is made at compile-time, as I still compile a
FreeBSD-based version of lpr&friends on other platforms.

While testing I out that (at least on my system) lpd has been
closing 11095 fd's, when there are only 6 fd's open.  The old
code took 120 times more clocktime than calling closefrom().
(although that was still less than 2/1000-ths of a second!)

Reviewed by:	jilles
MFC after:	2 weeks
This commit is contained in:
Garance A Drosehn 2013-05-27 22:19:01 +00:00
parent 24f3b0bcd0
commit 137076c5f7
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=251044
2 changed files with 29 additions and 8 deletions

View File

@ -757,16 +757,22 @@ fatal(const struct printer *pp, const char *msg, ...)
/*
* Close all file descriptors from START on up.
* This is a horrific kluge, since getdtablesize() might return
* ``infinity'', in which case we will be spending a long time
* closing ``files'' which were never open. Perhaps it would
* be better to close the first N fds, for some small value of N.
*/
void
closeallfds(int start)
{
int stop = getdtablesize();
for (; start < stop; start++)
close(start);
int stop;
if (USE_CLOSEFROM) /* The faster, modern solution */
closefrom(start);
else {
/* This older logic can be pretty awful on some OS's. The
* getdtablesize() might return ``infinity'', and then this
* will waste a lot of time closing file descriptors which
* had never been open()-ed. */
stop = getdtablesize();
for (; start < stop; start++)
close(start);
}
}

View File

@ -1,6 +1,6 @@
/*-
* ------+---------+---------+---------+---------+---------+---------+---------*
* Copyright (c) 2003 - Garance Alistair Drosehn <gad@FreeBSD.org>.
* Copyright (c) 2003,2013 - Garance Alistair Drosehn <gad@FreeBSD.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -55,6 +55,21 @@
# include <sys/cdefs.h>
#endif
/*
* FreeBSD added a closefrom() routine in release 8.0. When compiling
* `lpr' on other platforms you might want to include bsd-closefrom.c
* from the portable-openssh project.
*/
#ifndef USE_CLOSEFROM
# if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
# define USE_CLOSEFROM 1
# endif
#endif
/* The macro USE_CLOSEFROM must be defined with a value of 0 or 1. */
#ifndef USE_CLOSEFROM
# define USE_CLOSEFROM 0
#endif
/*
* __unused is a compiler-specific trick which can be used to avoid
* warnings about a variable which is defined but never referenced.