trying to learn systems programming, fear I have not understood and thus messed up

Fabian Keil freebsd-listen at fabiankeil.de
Sat Oct 22 17:51:32 UTC 2011


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

> On Sat, 22 Oct 2011 16:45:08 +0200
> Fabian Keil <freebsd-listen at fabiankeil.de> wrote:
> 
> > "Christopher J. Ruwe" <cjr at cruwe.de> wrote:
> > 
> > > On Fri, 21 Oct 2011 18:53:33 +0200
> > > "Christopher J. Ruwe" <cjr at 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.
> > 
> > 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.
> > 
> > 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 = GETPWNAM(...)
[...]
trgpwd = GETPWNAM(...)
pwd->pw_uid = (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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20111022/84f59bd1/signature.pgp


More information about the freebsd-questions mailing list