2

I'm writing some program that reads /etc/{passwd,group}.

I learned that any program should lock /etc/.pwd.lock by calling lckpwdf() before writing the above files so that write collisions can be avoided.

I guess reading operation also needs this lock so that program won't read out inconsistent data.

But I found I can't do the lock when reading /etc/passwd as non-root, because /etc/.pwd.lock is owned by root:root and has mode 0600.

Am I wrong? Doesn't reading need this lock?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
fpemud
  • 363
  • 3
  • 10

1 Answers1

2

On Linux, lckpwdf() is declared in <shadow.h> and should be used when accessing the shadow password database (/etc/shadow). It does not need to be called when reading /etc/passwd or /etc/group.

Since it's a nonstandard function, this is not necessarily the case on other UNIXes.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578