From owner-freebsd-threads@FreeBSD.ORG Sun Aug 15 19:58:07 2004 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 8CD3A16AF51; Sun, 15 Aug 2004 19:58:07 +0000 (GMT) Received: from blake.polstra.com (blake.polstra.com [64.81.189.66]) by mx1.FreeBSD.org (Postfix) with ESMTP id 1A44E43D4C; Sun, 15 Aug 2004 19:58:07 +0000 (GMT) (envelope-from jdp@polstra.com) Received: from strings.polstra.com (dsl081-189-067.sea1.dsl.speakeasy.net [64.81.189.67]) by blake.polstra.com (8.12.11/8.12.11) with ESMTP id i7FJw6O2068088 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 15 Aug 2004 12:58:06 -0700 (PDT) (envelope-from jdp@strings.polstra.com) Received: (from jdp@localhost) by strings.polstra.com (8.12.11/8.12.11/Submit) id i7FJw6nQ013644; Sun, 15 Aug 2004 12:58:06 -0700 (PDT) (envelope-from jdp) Message-ID: X-Mailer: XFMail 1.5.5 on FreeBSD X-Priority: 3 (Normal) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit MIME-Version: 1.0 In-Reply-To: Date: Sun, 15 Aug 2004 12:58:06 -0700 (PDT) From: John Polstra To: Robert Watson X-Bogosity: No, tests=bogofilter, spamicity=0.016632, version=0.14.5 cc: threads@freebsd.org Subject: RE: thread-unsafe syslog code in libc? X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Aug 2004 19:58:07 -0000 On 15-Aug-2004 Robert Watson wrote: > > I recently resolved a kernel race reported by Martin Blapp in which a file > descriptor had connect() called on it by one thread, and was > simultaenously close()'d by another. The bug resulted in a kernel crash, > which is certainly not the right response, and I'm working on a number of > aspects of that problem. However, this also speaks to a race in user > space. The socket in question was being connected to /var/run/log, so I > believe it was made from the libc syslog code. A glance at > src/lib/libc/gen/syslog.c suggests that things are indeed a bit > un-threadsafe, especially in vsyslog(), where things get connected and > disconnected a fair amount. I don't see any repeated connecting and disconnecting in vsyslog except under error conditions. Here's the relevant code, with my commentary non-indented: /* Get connected, output the message to the local logger. */ if (!opened) openlog(LogTag, LogStat | LOG_NDELAY, 0); connectlog(); Note that connectlog() is a no-op if we are already connected (the expected case after the first call). if (send(LogFile, tbuf, cnt, 0) >= 0) return; If the send succeeds (the normal case) we return here and don't disconnect. /* * If the send() failed, the odds are syslogd was restarted. * Make one (only) attempt to reconnect to /dev/log. */ disconnectlog(); connectlog(); if (send(LogFile, tbuf, cnt, 0) >= 0) return; The above is only to handle an unusual error case. There is some thread-unsafeness here, but it doesn't look like it would matter under normal conditions. John