changeset 4979:6b9af4133e6f

Merge changes to getloadavg.c from coreutils and Emacs; this adds a new module cloexec.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 29 Mar 2004 23:57:36 +0000
parents 2293cc28332b
children 4870b9a8dd19
files ChangeLog lib/ChangeLog lib/cloexec.c lib/cloexec.h lib/getloadavg.c m4/ChangeLog m4/cloexec.m4 modules/cloexec modules/getloadavg
diffstat 9 files changed, 134 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,8 @@
-2004-03-09  Paul Eggert  <eggert@cs.ucla.edu>
+2004-03-29  Paul Eggert  <eggert@twinsun.com>
+
+	* modules/getloadvg: Depend on cloexec and xalloc.
+
+2004-03-09  Paul Eggert  <eggert@twinsun.com>
 
 	* modules/getopt: Add lib/getopt_int.h.
 
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,4 +1,15 @@
-2004-03-18  Paul Eggert  <eggert@cs.ucla.edu>
+2004-03-29  Paul Eggert  <eggert@twinsun.com>
+
+	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  <eggert@twinsun.com>
 
 	* getopt.h: Sync with libc CVS.
 
@@ -9,7 +20,7 @@
 	not on all platforms that have <wchar.h>.
 	* mbswidth.c: Include <stdio.h> and <time.h> before <wchar.h>.
 
-2004-03-09  Paul Eggert  <eggert@cs.ucla.edu>
+2004-03-09  Paul Eggert  <eggert@twinsun.com>
 
 	* argp-parse.c, getopt.c, getopt.h, getopt1.c:
 	Sync with libc CVS.
new file mode 100644
--- /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 <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
+}
new file mode 100644
--- /dev/null
+++ b/lib/cloexec.h
@@ -0,0 +1,2 @@
+#include <stdbool.h>
+bool set_cloexec_flag (int desc, bool value);
--- 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 @@
 # 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 @@
   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 @@
     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 @@
 	{
 	  /* 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 */
--- a/m4/ChangeLog
+++ b/m4/ChangeLog
@@ -1,3 +1,7 @@
+2004-03-29  Paul Eggert  <eggert@twinsun.com>
+
+	* cloexec.m4: New file.
+
 2004-03-18  Paul Eggert  <eggert@twinsun.com>
             Bruno Haible  <bruno@clisp.org>
 
new file mode 100644
--- /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)
+])
new file mode 100644
--- /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
--- a/modules/getloadavg
+++ b/modules/getloadavg
@@ -6,6 +6,8 @@
 m4/getloadavg.m4
 
 Depends-on:
+cloexec
+xalloc
 
 configure.ac:
 gl_FUNC_GETLOADAVG