How do I lock the passwd file?

Tony Facca fsfacca at AVELON.LERC.NASA.GOV
Thu Jan 10 01:27:27 AEST 1991


> 
> These scripts (obviously) need to modify the password file /etc/passwd. The
> way our system will operate it is quite possible (likely ??) that two or more
> people may attempt modification at the same time with erroneous results.
> 
> The man page for passwd(4) suggests that adequate locking MUST be performed
> but gives no hint of how. There is a file /etc/.pwd.lock which is presumably
> used by the passwd command.
> 

The man page is probably suggesting that YOU enforce a locking scheme if you
plan to modify the passwd file.  

A method which works well for us is:

    if [lock file exists] then
	exit
    else
	[create the lock file]
	[make a backup copy of the passwd file]
	[copy the password file to a temp file]
	[make modifications to the temp file]
	[disable interrupts]
	[replace the password file with the temp file]
	[remove the lock file]
	[enable interrupts]
    endif


In C-Shell code:

  set lockfile = /etc/passwd.lock
  set tempfile = /etc/passwd.temp
  set savefile = /etc/passwd.save

  if (-e $lockfile) then
      echo "Try again later"
      exit
  else
      cp /dev/null $lockfile
      cp /etc/passwd $tempfile
      cp /etc/passwd $savefile

	   [ code to modify the temp file ]

      # this is the critical section

      onintr -
      mv $tempfile /etc/passwd		
      # you may want to check $status here 
      rm $lockfile
      onintr

  endif 


The same thing can be done using the Bourne Shell, I don't know about perl.
Be careful that you set the proper umask before you start and that the file
modes are read-only.

Good Luck!
--
-----------------------------------------------------------------------------
Tony Facca   |   fsfacca at avelon.lerc.nasa.gov      |     phone: 216-433-8318
-----------------------------------------------------------------------------
      You are at Witt's end.  Passages lead off in *all* directions.



More information about the Comp.sys.sgi mailing list