Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 22 Oct 2011 19:49:29 +0200
From:      Fabian Keil <freebsd-listen@fabiankeil.de>
To:        "Christopher J. Ruwe" <cjr@cruwe.de>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: trying to learn systems programming, fear I have not understood and thus messed up
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>

next in thread | previous in thread | raw e-mail | index | archive | help
--Sig_/DXxJGEUjKUd1p0n3jJl2hW7
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: quoted-printable

"Christopher J. Ruwe" <cjr@cruwe.de> wrote:

> On Sat, 22 Oct 2011 16:45:08 +0200
> Fabian Keil <freebsd-listen@fabiankeil.de> wrote:
>=20
> > "Christopher J. Ruwe" <cjr@cruwe.de> wrote:
> >=20
> > > On Fri, 21 Oct 2011 18:53:33 +0200
> > > "Christopher J. Ruwe" <cjr@cruwe.de> 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--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20111022194929.434452ec>