[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-gnulib] Re: Bug in getloadavg
From: |
Paul Eggert |
Subject: |
Re: [Bug-gnulib] Re: Bug in getloadavg |
Date: |
Mon, 29 Mar 2004 16:01:31 -0800 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) |
Dave Love <address@hidden> writes:
> I was merging the gnulib version, which is what has the bug. It
> should do this (following the example in libc.info):
Thanks for reporting this. I've installed the following patch to
gnulib. This also merges a recent fix to getloadavg in coreutils.
2004-03-29 Paul Eggert <address@hidden>
Merge changes to getloadavg.c from coreutils and Emacs.
* modules/getloadvg: Depend on cloexec and xalloc.
* 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, m4/cloexec.m4, modules/cloexec:
New files.
Index: lib/getloadavg.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getloadavg.c,v
retrieving revision 1.20
diff -p -u -r1.20 getloadavg.c
--- lib/getloadavg.c 13 Sep 2003 22:13:49 -0000 1.20
+++ lib/getloadavg.c 29 Mar 2004 23:54:05 -0000
@@ -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 address@hidden
@@ -111,9 +111,12 @@ extern int errno;
# include <locale.h>
#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 */
Index: modules/getloadavg
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/getloadavg,v
retrieving revision 1.3
diff -p -u -r1.3 getloadavg
--- modules/getloadavg 20 Jan 2003 10:02:37 -0000 1.3
+++ modules/getloadavg 29 Mar 2004 23:54:06 -0000
@@ -6,6 +6,8 @@ lib/getloadavg.c
m4/getloadavg.m4
Depends-on:
+cloexec
+xalloc
configure.ac:
gl_FUNC_GETLOADAVG
--- /dev/null Tue Mar 18 13:55:57 2003
+++ lib/cloexec.c Thu Mar 18 12:05:05 2004
@@ -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 <config.h>
+#endif
+
+#include "cloexec.h"
+
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#if HAVE_FCNTL_H
+# include <fcntl.h>
+#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
+}
--- /dev/null Tue Mar 18 13:55:57 2003
+++ lib/cloexec.h Thu Mar 18 12:04:56 2004
@@ -0,0 +1,2 @@
+#include <stdbool.h>
+bool set_cloexec_flag (int desc, bool value);
--- /dev/null Tue Mar 18 13:55:57 2003
+++ m4/cloexec.m4 Thu Mar 18 12:07:07 2004
@@ -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)
+])
--- /dev/null Tue Mar 18 13:55:57 2003
+++ modules/cloexec Thu Mar 18 12:08:47 2004
@@ -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