From owner-freebsd-questions@FreeBSD.ORG Sat Oct 22 17:51:32 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id A68CA106566C for ; Sat, 22 Oct 2011 17:51:32 +0000 (UTC) (envelope-from freebsd-listen@fabiankeil.de) Received: from smtprelay04.ispgateway.de (smtprelay04.ispgateway.de [80.67.31.32]) by mx1.freebsd.org (Postfix) with ESMTP id 3A7228FC12 for ; Sat, 22 Oct 2011 17:51:31 +0000 (UTC) Received: from [87.78.63.222] (helo=fabiankeil.de) by smtprelay04.ispgateway.de with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.68) (envelope-from ) id 1RHfjG-0005qL-7j; Sat, 22 Oct 2011 19:51:30 +0200 Date: Sat, 22 Oct 2011 19:49:29 +0200 From: Fabian Keil To: "Christopher J. Ruwe" Message-ID: <20111022194929.434452ec@fabiankeil.de> In-Reply-To: <20111022185415.25c76aa1@dijkstra> References: <20111021185333.7c197a2a@dijkstra> <20111022160453.35dac1f3@dijkstra> <20111022164508.0dcca5aa@fabiankeil.de> <20111022185415.25c76aa1@dijkstra> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=PGP-SHA1; boundary="Sig_/DXxJGEUjKUd1p0n3jJl2hW7"; protocol="application/pgp-signature" X-Df-Sender: Nzc1MDY3 Cc: freebsd-questions@freebsd.org Subject: Re: trying to learn systems programming, fear I have not understood and thus messed up X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 22 Oct 2011 17:51:32 -0000 --Sig_/DXxJGEUjKUd1p0n3jJl2hW7 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable "Christopher J. Ruwe" wrote: > On Sat, 22 Oct 2011 16:45:08 +0200 > Fabian Keil wrote: >=20 > > "Christopher J. Ruwe" wrote: > >=20 > > > On Fri, 21 Oct 2011 18:53:33 +0200 > > > "Christopher J. Ruwe" wrote: > > > As I stil do not know why the latter variant of my code worked and > > > the former does not, I would still appreciate any comment or > > > explanation which would help me understanding GETPWNAM and getpwnam. > >=20 > > I'm not familiar with the code you're working with, > > but according to the man page getpwnam() isn't thread > > safe so you probably shouldn't mess with the returned > > pointer in the first place and only treat the one > > returned by the last call as valid. > >=20 > > Did you try using getpwnam_r() instead? > You are quite right, GETPWNAM() is a macro to getpwnam(), which is not > thread safe. GETPWNAM() is called throughout the code of pw and pw > itself is not threaded, so it should not matter whether the functions > called are thread-safe or not. I am not completely sure on my last > statement though. Do you have other experience regarding this topic? My point is that if getpwnam() isn't thread safe because separate calls are using the same static buffer to return their result (which I didn't verify), and you do something like: pwd =3D GETPWNAM(...) [...] trgpwd =3D GETPWNAM(...) pwd->pw_uid =3D (uid_t) (trgpwd->pw_uid); the second getpwnam() call is going to reuse the memory pointed to by pwd. While I assume your intention was to only change pwd->pw_uid, the second getpwnam() call already had the side effect of overwriting all the other members as well. This would happen in a single-threaded application as well. Fabian --Sig_/DXxJGEUjKUd1p0n3jJl2hW7 Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.18 (FreeBSD) iEYEARECAAYFAk6jAi0ACgkQBYqIVf93VJ1OygCgsEpm+nGFV2SD6L/KMsxekYAa OfMAn25GgK2I6J66cXUR4yBR4cTHlOSu =hYPn -----END PGP SIGNATURE----- --Sig_/DXxJGEUjKUd1p0n3jJl2hW7--