From f68c57a7466a299f333352b6d6c958a3a36c0e3c Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 29 Mar 2004 23:57:36 +0000 Subject: [PATCH] Merge changes to getloadavg.c from coreutils and Emacs; this adds a new module cloexec. --- ChangeLog | 6 ++++- lib/ChangeLog | 15 +++++++++-- lib/cloexec.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ lib/cloexec.h | 2 ++ lib/getloadavg.c | 19 +++++++------- m4/ChangeLog | 4 +++ m4/cloexec.m4 | 13 ++++++++++ modules/cloexec | 22 ++++++++++++++++ modules/getloadavg | 2 ++ 9 files changed, 134 insertions(+), 12 deletions(-) create mode 100644 lib/cloexec.c create mode 100644 lib/cloexec.h create mode 100644 m4/cloexec.m4 create mode 100644 modules/cloexec diff --git a/ChangeLog b/ChangeLog index 0a80d7f771..67003cb6f6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,8 @@ -2004-03-09 Paul Eggert +2004-03-29 Paul Eggert + + * modules/getloadvg: Depend on cloexec and xalloc. + +2004-03-09 Paul Eggert * modules/getopt: Add lib/getopt_int.h. diff --git a/lib/ChangeLog b/lib/ChangeLog index 986440e424..76d685ee4a 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,4 +1,15 @@ -2004-03-18 Paul Eggert +2004-03-29 Paul Eggert + + Merge changes to getloadavg.c from coreutils and Emacs. + + * lib/getloadavg.c [!defined HAVE_SETLOCALE] (setlocale): + Define to an expression, not to the empty string. + Include cloexec.h and xalloc.h. + (getloadavg): Restore LC_NUMERIC locale after setting it temporarily. + Use set_cloexec_flag rather than rolling our own. + * lib/cloexec.c, lib/cloexec.h: New files. + +2004-03-18 Paul Eggert * getopt.h: Sync with libc CVS. @@ -9,7 +20,7 @@ not on all platforms that have . * mbswidth.c: Include and before . -2004-03-09 Paul Eggert +2004-03-09 Paul Eggert * argp-parse.c, getopt.c, getopt.h, getopt1.c: Sync with libc CVS. diff --git a/lib/cloexec.c b/lib/cloexec.c new file mode 100644 index 0000000000..20f30db45d --- /dev/null +++ b/lib/cloexec.c @@ -0,0 +1,63 @@ +/* closexec.c - set or clear the close-on-exec descriptor flag + Copyright (C) 1991, 2004 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + The code is taken from glibc/manual/llio.texi */ + +#if HAVE_CONFIG_H +# include +#endif + +#include "cloexec.h" + +#if HAVE_UNISTD_H +# include +#endif + +#if HAVE_FCNTL_H +# include +#endif + +#ifndef FD_CLOEXEC +# define FD_CLOEXEC 1 +#endif + +/* Set the `FD_CLOEXEC' flag of DESC if VALUE is true, + or clear the flag if VALUE is false. + Return true on success, or false on error with `errno' set. */ + +bool +set_cloexec_flag (int desc, bool value) +{ +#if defined F_GETFD && defined F_SETFD + + int flags = fcntl (desc, F_GETFD, 0); + int newflags; + + if (flags < 0) + return false; + + newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC); + + return (flags == newflags + || fcntl (desc, F_SETFD, newflags) != -1); + +#else + + return true; + +#endif +} diff --git a/lib/cloexec.h b/lib/cloexec.h new file mode 100644 index 0000000000..ecad8d712f --- /dev/null +++ b/lib/cloexec.h @@ -0,0 +1,2 @@ +#include +bool set_cloexec_flag (int desc, bool value); diff --git a/lib/getloadavg.c b/lib/getloadavg.c index 6ac0a8c5d2..dce4117f08 100644 --- a/lib/getloadavg.c +++ b/lib/getloadavg.c @@ -1,7 +1,7 @@ /* Get the system load averages. Copyright (C) 1985, 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994, - 1995, 1997, 1999, 2000, 2003 Free Software Foundation, Inc. + 1995, 1997, 1999, 2000, 2003, 2004 Free Software Foundation, Inc. NOTE: The canonical source of this file is maintained with gnulib. Bugs can be reported to bug-gnulib@gnu.org. @@ -111,9 +111,12 @@ extern int errno; # include #endif #ifndef HAVE_SETLOCALE -# define setlocale(Category, Locale) /* empty */ +# define setlocale(Category, Locale) ((char *) NULL) #endif +#include "cloexec.h" +#include "xalloc.h" + #ifndef HAVE_GETLOADAVG /* The existing Emacs configuration files define a macro called @@ -592,6 +595,7 @@ getloadavg (double loadavg[], int nelem) char ldavgbuf[40]; double load_ave[3]; int fd, count; + char *old_locale; fd = open (LINUX_LDAV_FILE, O_RDONLY); if (fd == -1) @@ -602,10 +606,12 @@ getloadavg (double loadavg[], int nelem) return -1; /* The following sscanf must use the C locale. */ + old_locale = xstrdup (setlocale (LC_NUMERIC, NULL)); setlocale (LC_NUMERIC, "C"); count = sscanf (ldavgbuf, "%lf %lf %lf", &load_ave[0], &load_ave[1], &load_ave[2]); - setlocale (LC_NUMERIC, ""); + setlocale (LC_NUMERIC, old_locale); + free (old_locale); if (count < 1) return -1; @@ -924,12 +930,7 @@ getloadavg (double loadavg[], int nelem) { /* Set the channel to close on exec, so it does not litter any child's descriptor table. */ -# ifdef F_SETFD -# ifndef FD_CLOEXEC -# define FD_CLOEXEC 1 -# endif - (void) fcntl (channel, F_SETFD, FD_CLOEXEC); -# endif + set_cloexec_flag (channel, true); getloadavg_initialized = 1; } # else /* SUNOS_5 */ diff --git a/m4/ChangeLog b/m4/ChangeLog index 8abf642bef..f1c91cfcdb 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,7 @@ +2004-03-29 Paul Eggert + + * cloexec.m4: New file. + 2004-03-18 Paul Eggert Bruno Haible diff --git a/m4/cloexec.m4 b/m4/cloexec.m4 new file mode 100644 index 0000000000..06b36d5e2d --- /dev/null +++ b/m4/cloexec.m4 @@ -0,0 +1,13 @@ +# cloexec.m4 serial 1 +dnl Copyright (C) 2004 Free Software Foundation, Inc. +dnl This file is free software, distributed under the terms of the GNU +dnl General Public License. As a special exception to the GNU General +dnl Public License, this file may be distributed as part of a program +dnl that contains a configuration script generated by Autoconf, under +dnl the same distribution terms as the rest of that program. + +AC_DEFUN([gl_CLOEXEC], +[ + dnl Prerequisites of lib/cloexec.c. + AC_CHECK_HEADERS_ONCE(fcntl.h unistd.h) +]) diff --git a/modules/cloexec b/modules/cloexec new file mode 100644 index 0000000000..9dc2c1a3f7 --- /dev/null +++ b/modules/cloexec @@ -0,0 +1,22 @@ +Description: +Set or clear the close-on-exec descriptor flag. + +Files: +lib/cloexec.c +lib/cloexec.h +m4/cloexec.m4 + +Depends-on: +stdbool + +configure.ac: +gl_CLOEXEC + +Makefile.am: +lib_SOURCES += cloexec.c cloexec.h + +Include: +"cloexec.h" + +Maintainer: +Jim Meyering diff --git a/modules/getloadavg b/modules/getloadavg index 3a41e33523..c3fd60a1aa 100644 --- a/modules/getloadavg +++ b/modules/getloadavg @@ -6,6 +6,8 @@ lib/getloadavg.c m4/getloadavg.m4 Depends-on: +cloexec +xalloc configure.ac: gl_FUNC_GETLOADAVG -- 2.30.2