bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new module 'strerror_r-posix' (was: Re: strerror thread-safety?)


From: Bruno Haible
Subject: new module 'strerror_r-posix' (was: Re: strerror thread-safety?)
Date: Tue, 9 Nov 2010 03:09:51 +0100
User-agent: KMail/1.9.9

Eric Blake wrote:
> strerror_r has its own set of portability problems, since the glibc
> signature disagrees with the POSIX signature.  It also has not yet been
> ported to gnulib (in either the POSIX or glibc variant).  That might be
> a useful project to tackle first.

Here's a draft proposal: A new module 'strerror_r-posix'. I wouldn't want
to call it 'strerror_r' because that would be misunderstandable.


2010-11-09  Bruno Haible  <address@hidden>

        New module 'strerror_r-posix'.
        * lib/string.in.h (strerror_r): New declaration.
        * lib/strerror_r.c: New file.
        * m4/strerror_r.m4: New file.
        * m4/string_h.m4 (gl_HEADER_STRING_H_BODY): Check for the declaration
        of strerror_r.
        (gl_HEADER_STRING_H_DEFAULTS): Initialize GNULIB_STRERROR_R,
        HAVE_DECL_STRERROR_R, REPLACE_STRERROR_R.
        * modules/strerror_r-posix: New file.
        * modules/string (Makefile.am): Substitute GNULIB_STRERROR_R,
        HAVE_DECL_STRERROR_R, REPLACE_STRERROR_R.
        * doc/posix-functions/strerror_r.texi: Mention the new module and the
        portability problems.

        Tests for module 'strerror_r-posix'.
        * modules/strerror_r-posix-tests: New file.
        * tests/test-strerror_r.c: New file.
        * tests/test-string-c++.cc: Check the signature of strerror_r.

--- lib/string.in.h.orig        Tue Nov  9 02:53:13 2010
+++ lib/string.in.h     Tue Nov  9 01:20:43 2010
@@ -902,6 +902,33 @@
                  "use gnulib module strerror to guarantee non-NULL result");
 #endif
 
+/* Map any int, typically from errno, into an error message.  Multithread-safe.
+   Uses the POSIX declaration, not the glibc declaration.  */
+#if @GNULIB_STRERROR_R@
+# if @REPLACE_STRERROR_R@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef strerror_r
+#   define strerror_r rpl_strerror_r
+#  endif
+_GL_FUNCDECL_RPL (strerror_r, int, (int errnum, char *buf, size_t buflen)
+                                   _GL_ARG_NONNULL ((2)));
+_GL_CXXALIAS_RPL (strerror_r, int, (int errnum, char *buf, size_t buflen));
+# else
+#  if address@hidden@
+_GL_FUNCDECL_SYS (strerror_r, int, (int errnum, char *buf, size_t buflen)
+                                   _GL_ARG_NONNULL ((2)));
+#  endif
+_GL_CXXALIAS_SYS (strerror_r, int, (int errnum, char *buf, size_t buflen));
+# endif
+_GL_CXXALIASWARN (strerror_r);
+#elif defined GNULIB_POSIXCHECK
+# undef strerror_r
+# if HAVE_RAW_DECL_STRERROR_R
+_GL_WARN_ON_USE (strerror_r, "strerror_r is unportable - "
+                 "use gnulib module strerror_r-posix for portability");
+# endif
+#endif
+
 #if @GNULIB_STRSIGNAL@
 # if @REPLACE_STRSIGNAL@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
============================== lib/strerror_r.c ==============================
/* strerror_r.c --- POSIX compatible system error routine

   Copyright (C) 2010 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <address@hidden>, 2010.  */

#include <config.h>

/* Specification.  */
#include <string.h>

#include <errno.h>

#if __GLIBC__ >= 2 && HAVE___XPG_STRERROR_R && 0 /* glibc >= 2.3.4 */

int
strerror_r (int errnum, char *buf, size_t buflen)
{
  extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen);

  int ret = __xpg_strerror_r (errnum, buf, buflen);
  return (ret < 0 ? errno : 0);
}

#else

# include "glthread/lock.h"

/* Use strerror(), with locking.  */

/* This lock protects the buffer returned by strerror().  We assume that
   no other uses of strerror() exist in the program.  */
gl_lock_define_initialized(static, strerror_lock)

int
strerror_r (int errnum, char *buf, size_t buflen)
{
  gl_lock_lock (strerror_lock);

  {
    char *errmsg = strerror (errnum);
    size_t len = strlen (errmsg);
    int ret;

    if (len < buflen)
      {
        memcpy (buf, errmsg, len + 1);
        ret = 0;
      }
    else
      ret = ERANGE;

    gl_lock_unlock (strerror_lock);

    return ret;
  }
}

#endif
============================== m4/strerror_r.m4 ==============================
# strerror_r.m4 serial 1
dnl Copyright (C) 2002, 2007-2010 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_FUNC_STRERROR_R],
[
  AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
  AC_REQUIRE([gl_HEADER_ERRNO_H])

  dnl Persuade Solaris <string.h> to declare strerror_r().
  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])

  dnl Some systems don't declare strerror_r() if _THREAD_SAFE and _REENTRANT
  dnl are not defined.
  AC_CHECK_DECLS_ONCE([strerror_r])
  if test $ac_cv_have_decl_strerror_r = no; then
    HAVE_DECL_STRERROR_R=0
  fi

  AC_CHECK_FUNCS([strerror_r])
  if test $ac_cv_func_strerror_r = yes; then
    if test -z "$ERRNO_H"; then
      AC_CACHE_CHECK([for strerror_r with POSIX signature],
        [gl_cv_func_strerror_r_posix_signature],
        [AC_COMPILE_IFELSE(
           [AC_LANG_PROGRAM(
              [[#include <string.h>
                int strerror_r (int, char *, size_t);
              ]],
              [[return strerror (0);]])],
           [gl_cv_func_strerror_r_posix_signature=yes],
           [gl_cv_func_strerror_r_posix_signature=no])
        ])
      if test $gl_cv_func_strerror_r_posix_signature != yes; then
        dnl glibc >= 2.3.4 has a function __xpg_strerror_r.
        AC_CHECK_FUNCS([__xpg_strerror_r])
        dnl The system's strerror() has a wrong signature. Replace it.
        REPLACE_STRERROR_R=1
      fi
    else
      dnl The system's strerror_r() cannot know about the new errno values we
      dnl add to <errno.h>. Replace it.
      REPLACE_STRERROR_R=1
    fi
  fi
  if test $HAVE_DECL_STRERROR_R = 0 || test $REPLACE_STRERROR_R = 1; then
    AC_LIBOBJ([strerror_r])
    gl_PREREQ_STRERROR_R
  fi
])

# Prerequisites of lib/strerror_r.c.
AC_DEFUN([gl_PREREQ_STRERROR_R], [
  :
])
--- m4/string_h.m4.orig Tue Nov  9 02:53:13 2010
+++ m4/string_h.m4      Tue Nov  9 01:20:59 2010
@@ -5,7 +5,7 @@
 # gives unlimited permission to copy and/or distribute it,
 # with or without modifications, as long as this notice is preserved.
 
-# serial 17
+# serial 18
 
 # Written by Paul Eggert.
 
@@ -28,8 +28,8 @@
   gl_WARN_ON_USE_PREPARE([[#include <string.h>
     ]],
     [memmem mempcpy memrchr rawmemchr stpcpy stpncpy strchrnul strdup
-     strncat strndup strnlen strpbrk strsep strcasestr strtok_r strsignal
-     strverscmp])
+     strncat strndup strnlen strpbrk strsep strcasestr strtok_r strerror_r
+     strsignal strverscmp])
 ])
 
 AC_DEFUN([gl_STRING_MODULE_INDICATOR],
@@ -75,6 +75,7 @@
   GNULIB_MBSSEP=0;      AC_SUBST([GNULIB_MBSSEP])
   GNULIB_MBSTOK_R=0;    AC_SUBST([GNULIB_MBSTOK_R])
   GNULIB_STRERROR=0;    AC_SUBST([GNULIB_STRERROR])
+  GNULIB_STRERROR_R=0;  AC_SUBST([GNULIB_STRERROR_R])
   GNULIB_STRSIGNAL=0;   AC_SUBST([GNULIB_STRSIGNAL])
   GNULIB_STRVERSCMP=0;  AC_SUBST([GNULIB_STRVERSCMP])
   HAVE_MBSLEN=0;        AC_SUBST([HAVE_MBSLEN])
@@ -94,6 +95,7 @@
   HAVE_STRSEP=1;                AC_SUBST([HAVE_STRSEP])
   HAVE_STRCASESTR=1;            AC_SUBST([HAVE_STRCASESTR])
   HAVE_DECL_STRTOK_R=1;         AC_SUBST([HAVE_DECL_STRTOK_R])
+  HAVE_DECL_STRERROR_R=1;       AC_SUBST([HAVE_DECL_STRERROR_R])
   HAVE_DECL_STRSIGNAL=1;        AC_SUBST([HAVE_DECL_STRSIGNAL])
   HAVE_STRVERSCMP=1;            AC_SUBST([HAVE_STRVERSCMP])
   REPLACE_MEMCHR=0;             AC_SUBST([REPLACE_MEMCHR])
@@ -103,6 +105,7 @@
   REPLACE_STRSTR=0;             AC_SUBST([REPLACE_STRSTR])
   REPLACE_STRCASESTR=0;         AC_SUBST([REPLACE_STRCASESTR])
   REPLACE_STRERROR=0;           AC_SUBST([REPLACE_STRERROR])
+  REPLACE_STRERROR_R=0;         AC_SUBST([REPLACE_STRERROR_R])
   REPLACE_STRNCAT=0;            AC_SUBST([REPLACE_STRNCAT])
   REPLACE_STRNDUP=0;            AC_SUBST([REPLACE_STRNDUP])
   REPLACE_STRNLEN=0;            AC_SUBST([REPLACE_STRNLEN])
========================== modules/strerror_r-posix ==========================
Description:
strerror_r() function: get string describing error code.

Files:
lib/strerror_r.c
m4/strerror_r.m4

Depends-on:
string
errno
extensions
lock

configure.ac:
gl_FUNC_STRERROR_R
gl_STRING_MODULE_INDICATOR([strerror_r])

Makefile.am:

Include:
<string.h>

License:
LGPL

Maintainer:
Bruno Haible
--- modules/string.orig Tue Nov  9 02:53:13 2010
+++ modules/string      Tue Nov  9 01:21:10 2010
@@ -60,6 +60,7 @@
              -e 's|@''GNULIB_STRCASESTR''@|$(GNULIB_STRCASESTR)|g' \
              -e 's|@''GNULIB_STRTOK_R''@|$(GNULIB_STRTOK_R)|g' \
              -e 's|@''GNULIB_STRERROR''@|$(GNULIB_STRERROR)|g' \
+             -e 's|@''GNULIB_STRERROR_R''@|$(GNULIB_STRERROR_R)|g' \
              -e 's|@''GNULIB_STRSIGNAL''@|$(GNULIB_STRSIGNAL)|g' \
              -e 's|@''GNULIB_STRVERSCMP''@|$(GNULIB_STRVERSCMP)|g' \
              < $(srcdir)/string.in.h | \
@@ -79,6 +80,7 @@
              -e 's|@''HAVE_STRSEP''@|$(HAVE_STRSEP)|g' \
              -e 's|@''HAVE_STRCASESTR''@|$(HAVE_STRCASESTR)|g' \
              -e 's|@''HAVE_DECL_STRTOK_R''@|$(HAVE_DECL_STRTOK_R)|g' \
+             -e 's|@''HAVE_DECL_STRERROR_R''@|$(HAVE_DECL_STRERROR_R)|g' \
              -e 's|@''HAVE_DECL_STRSIGNAL''@|$(HAVE_DECL_STRSIGNAL)|g' \
              -e 's|@''HAVE_STRVERSCMP''@|$(HAVE_STRVERSCMP)|g' \
              -e 's|@''REPLACE_STPNCPY''@|$(REPLACE_STPNCPY)|g' \
@@ -88,6 +90,7 @@
              -e 's|@''REPLACE_STRDUP''@|$(REPLACE_STRDUP)|g' \
              -e 's|@''REPLACE_STRSTR''@|$(REPLACE_STRSTR)|g' \
              -e 's|@''REPLACE_STRERROR''@|$(REPLACE_STRERROR)|g' \
+             -e 's|@''REPLACE_STRERROR_R''@|$(REPLACE_STRERROR_R)|g' \
              -e 's|@''REPLACE_STRNCAT''@|$(REPLACE_STRNCAT)|g' \
              -e 's|@''REPLACE_STRNDUP''@|$(REPLACE_STRNDUP)|g' \
              -e 's|@''REPLACE_STRNLEN''@|$(REPLACE_STRNLEN)|g' \
--- doc/posix-functions/strerror_r.texi.orig    Tue Nov  9 02:53:13 2010
+++ doc/posix-functions/strerror_r.texi Tue Nov  9 01:19:22 2010
@@ -4,14 +4,10 @@
 
 POSIX specification:@* 
@url{http://www.opengroup.org/onlinepubs/9699919799/functions/strerror_r.html}
 
-Gnulib module: ---
+Gnulib module: strerror_r-posix
 
 Portability problems fixed by Gnulib:
 @itemize
address@hidden itemize
-
-Portability problems not fixed by Gnulib:
address@hidden
 @item
 This function is missing on some platforms:
 NetBSD 3.0, HP-UX 11, IRIX 6.5, Solaris 9, mingw.
@@ -24,4 +20,16 @@
 @smallexample
 char *s = strerror_r (err, buf, buflen);
 @end smallexample
address@hidden
+The third argument is of type @code{int} instead of @code{size_t} on some
+platforms:
+AIX 5.1, HP-UX 11, OSF/1 5.1.
address@hidden
+This function does not support the error values that are specified by POSIX
+but not defined by the system, on some platforms:
+OpenBSD 4.0, OSF/1 5.1, NonStop Kernel, Cygwin 1.5.x.
address@hidden itemize
+
+Portability problems not fixed by Gnulib:
address@hidden
 @end itemize


======================= modules/strerror_r-posix-tests =======================
Files:
tests/test-strerror_r.c
tests/signature.h
tests/macros.h

Depends-on:

configure.ac:

Makefile.am:
TESTS += test-strerror_r
check_PROGRAMS += test-strerror_r
=========================== tests/test-strerror_r.c ===========================
/* Test of strerror_r() function.
   Copyright (C) 2007-2010 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 3, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */

#include <config.h>

#include <string.h>

#include "signature.h"
SIGNATURE_CHECK (strerror_r, int, (int, char *, size_t));

#include <errno.h>

#include "macros.h"

int
main (void)
{
  char buf[100];
  int ret;

  /* Test results with valid errnum and enough room.  */

  buf[0] = '\0';
  ASSERT (strerror_r (EACCES, buf, sizeof (buf)) == 0);
  ASSERT (buf[0] != '\0');

  buf[0] = '\0';
  ASSERT (strerror_r (ETIMEDOUT, buf, sizeof (buf)) == 0);
  ASSERT (buf[0] != '\0');

  buf[0] = '\0';
  ASSERT (strerror_r (EOVERFLOW, buf, sizeof (buf)) == 0);
  ASSERT (buf[0] != '\0');

  /* Test results with out-of-range errnum and enough room.  */

  buf[0] = '\0';
  ret = strerror_r (0, buf, sizeof (buf));
  ASSERT (ret == 0 || ret == EINVAL);
  if (ret == 0)
    ASSERT (buf[0] != '\0');

  buf[0] = '\0';
  ret = strerror_r (-3, buf, sizeof (buf));
  ASSERT (ret == 0 || ret == EINVAL);
  if (ret == 0)
    ASSERT (buf[0] != '\0');

  /* Test results with a too small buffer.  */

  ASSERT (strerror_r (EACCES, buf, sizeof (buf)) == 0);
  {
    size_t len = strlen (buf);
    size_t i;

    for (i = 0; i <= len; i++)
      {
        strcpy (buf, "BADFACE");
        ASSERT (strerror_r (EACCES, buf, i) == ERANGE);
        ASSERT (strcmp (buf, "BADFACE") == 0);
      }
  }

  return 0;
}
--- tests/test-string-c++.cc.orig       Tue Nov  9 02:53:13 2010
+++ tests/test-string-c++.cc    Tue Nov  9 01:00:32 2010
@@ -126,6 +126,10 @@
 SIGNATURE_CHECK (GNULIB_NAMESPACE::strerror, char *, (int));
 #endif
 
+#if GNULIB_TEST_STRERROR_R
+SIGNATURE_CHECK (GNULIB_NAMESPACE::strerror_r, int, (int, char *, size_t));
+#endif
+
 #if GNULIB_TEST_STRSIGNAL
 SIGNATURE_CHECK (GNULIB_NAMESPACE::strsignal, char *, (int));
 #endif



reply via email to

[Prev in Thread] Current Thread [Next in Thread]