From owner-freebsd-hackers Tue Aug 10 6:29:39 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from tornado.cisco.com (tornado.cisco.com [171.69.104.22]) by hub.freebsd.org (Postfix) with ESMTP id D3DD515444 for ; Tue, 10 Aug 1999 06:29:32 -0700 (PDT) (envelope-from bmcgover@bmcgover-pc.cisco.com) Received: from bmcgover-pc.cisco.com (bmcgover-pc.cisco.com [171.69.104.147]) by tornado.cisco.com (8.8.5-Cisco.1/8.6.5) with ESMTP id JAA22653; Tue, 10 Aug 1999 09:26:44 -0400 (EDT) Received: from bmcgover-pc.cisco.com (localhost.pa.dtd.cisco.com [127.0.0.1]) by bmcgover-pc.cisco.com (8.9.3/8.9.3) with ESMTP id JAA01963; Tue, 10 Aug 1999 09:26:44 -0400 (EDT) (envelope-from bmcgover@bmcgover-pc.cisco.com) Message-Id: <199908101326.JAA01963@bmcgover-pc.cisco.com> To: ognir@humboldt1.com Cc: dan@trinsec.com, hackers@FreeBSD.ORG Subject: Re: gethostbyaddr() and threads. In-reply-to: Your message of "Mon, 09 Aug 1999 14:21:38 PDT." <199908092121.OAA21266@home.humboldt1.com> Date: Tue, 10 Aug 1999 09:26:44 -0400 From: Brian McGovern Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG >> gethostbyaddr... actually, most of the gethostby* functions... are not >> thread safe. They all use a static buffer in the library. >> >> Therefore, with threads, if you don't take precautions, I'd expect your >> results to be odd. >> -Brian >> >Couldn't this be easily fixed? I haven't looked at the source yet, but I >believe you could replaced the static buffers with a dynamically-allocated list >of buffers, with one for each thread using the gethost functions. Or perhaps >you could just eliminate all the static stuff altogether? -Joe You could, but thats not what was asked. Nor is the solution portable. Posix warns that these functions are not thread safe. Realistically, the portable solution would be to mutex lock (or equiv.) around the calls, so that only one thread can call the function(s) at a time, and be sure to copy the results out of the temporary buffers before releasing the locks. (see note on "if you don't take precautions") An even more interesting solution may be to write some wrappers that do the lock, call the function, and store the result in a thread-specific area, then unlock again. -Brian To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message