From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Kevin Ryde Newsgroups: gmane.lisp.guile.devel Subject: strerror thread safety Date: Wed, 28 Jul 2004 09:02:47 +1000 Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Message-ID: <87hdrt12aw.fsf@zip.com.au> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: sea.gmane.org 1090969417 1731 80.91.224.253 (27 Jul 2004 23:03:37 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 27 Jul 2004 23:03:37 +0000 (UTC) Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Wed Jul 28 01:03:28 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1Bpaym-0008HB-00 for ; Wed, 28 Jul 2004 01:03:28 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1Bpb1s-0001vf-TQ for guile-devel@m.gmane.org; Tue, 27 Jul 2004 19:06:40 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1Bpb1k-0001vZ-Et for guile-devel@gnu.org; Tue, 27 Jul 2004 19:06:32 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1Bpb1j-0001vN-H5 for guile-devel@gnu.org; Tue, 27 Jul 2004 19:06:31 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1Bpb1j-0001vK-D7 for guile-devel@gnu.org; Tue, 27 Jul 2004 19:06:31 -0400 Original-Received: from [61.8.0.85] (helo=mailout2.pacific.net.au) by monty-python.gnu.org with esmtp (Exim 4.34) id 1BpayI-0007a9-G3 for guile-devel@gnu.org; Tue, 27 Jul 2004 19:02:59 -0400 Original-Received: from mailproxy1.pacific.net.au (mailproxy1.pacific.net.au [61.8.0.86]) by mailout2.pacific.net.au (8.12.3/8.12.3/Debian-6.6) with ESMTP id i6RN2vje023656 for ; Wed, 28 Jul 2004 09:02:57 +1000 Original-Received: from localhost (ppp20F1.dyn.pacific.net.au [61.8.32.241]) by mailproxy1.pacific.net.au (8.12.3/8.12.3/Debian-6.6) with ESMTP id i6RN2tvd026110 for ; Wed, 28 Jul 2004 09:02:56 +1000 Original-Received: from gg by localhost with local (Exim 3.36 #1 (Debian)) id 1Bpay7-0000xe-00; Wed, 28 Jul 2004 09:02:47 +1000 Original-To: guile-devel@gnu.org Mail-Copies-To: never User-Agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3 (gnu/linux) X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.lisp.guile.devel:3899 X-Report-Spam: http://spam.gmane.org/gmane.lisp.guile.devel:3899 --=-=-= * error.c (scm_strerror): Use scm_i_misc_mutex around strerror since it's not thread safe. (scm_syserror): Use scm_strerror rather than SCM_I_STRERROR, to take advantage of this. * fports.c (scm_open_file): Use scm_strerror likewise. * filesys.c (scm_stat, scm_lstat): Ditto. A program provoking the problem (in glibc at least), (use-modules (ice-9 threads)) (define e1 9999) (define s1 (strerror e1)) (define e2 7777) (define s2 (strerror e2)) (begin-thread (while #t (display "-") (let ((s (strerror e1))) (if (not (string=? s1 s)) (begin (format #t "oops, wrong s1, got ~s want ~s\n" s s1) (primitive-exit 1)))))) (while #t (display ".") (let ((s (strerror e2))) (if (not (string=? s2 s)) (begin (format #t "oops, wrong s2, got ~s want ~s\n" s s2) (primitive-exit 1))))) --=-=-= Content-Disposition: attachment; filename=error.c.strerror-mutex.diff Index: error.c =================================================================== RCS file: /cvsroot/guile/guile/guile-core/libguile/error.c,v retrieving revision 1.75 diff -u -u -r1.75 error.c --- error.c 23 Jul 2004 15:43:01 -0000 1.75 +++ error.c 27 Jul 2004 23:01:30 -0000 @@ -1,4 +1,4 @@ -/* Copyright (C) 1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2004 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -26,6 +26,7 @@ #include #include "libguile/_scm.h" +#include "libguile/dynwind.h" #include "libguile/pairs.h" #include "libguile/strings.h" #include "libguile/throw.h" @@ -142,13 +143,34 @@ # define SCM_I_ERRNO() errno #endif /* __MINGW32__ */ +/* strerror may not be thread safe, for instance in glibc (version 2.3.2) an + error number not among the known values results in a string like "Unknown + error 9999" formed in a static buffer, which will be overwritten by a + similar call in another thread. A test program running two threads with + different unknown error numbers can trip this problem fairly quickly. + + Some systems don't do what glibc does, instead just giving a single + "Unknown error" for unrecognised numbers. It doesn't seem worth trying + to tell if that's the case, a mutex is reasonably fast, and in any case + strerror isn't needed very often. */ + SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0, (SCM err), "Return the Unix error message corresponding to @var{err}, which\n" "must be an integer value.") #define FUNC_NAME s_scm_strerror { - return scm_makfrom0str (SCM_I_STRERROR (scm_to_int (err))); + SCM ret; + scm_frame_begin (0); + scm_frame_unwind_handler ((void(*)(void*)) scm_mutex_unlock, + &scm_i_misc_mutex, + SCM_F_WIND_EXPLICITLY); + scm_mutex_lock (&scm_i_misc_mutex); + + ret = scm_makfrom0str (SCM_I_STRERROR (scm_to_int (err))); + + scm_frame_end (); + return ret; } #undef FUNC_NAME @@ -156,13 +178,12 @@ void scm_syserror (const char *subr) { - int save_errno = SCM_I_ERRNO (); - + SCM err = scm_from_int (SCM_I_ERRNO ()); scm_error (scm_system_error_key, subr, "~A", - scm_cons (scm_makfrom0str (SCM_I_STRERROR (save_errno)), SCM_EOL), - scm_cons (scm_from_int (save_errno), SCM_EOL)); + scm_cons (scm_strerror (err), SCM_EOL), + scm_cons (err, SCM_EOL)); } void --=-=-= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Guile-devel mailing list Guile-devel@gnu.org http://lists.gnu.org/mailman/listinfo/guile-devel --=-=-=--