Finding out the file locker

Dave Decot decot at hpisod2.HP.COM
Fri Sep 28 11:41:24 AEST 1990


> I'm using HPUX 7.0 (SysV with quite a bit of BSD, for those who do not
> know it).
> How would you find out who has opened a file and has some lock on it ?
> There are routines which return what locks there are on a file,
> but I couldn't , even after diligent search of the the friendly
> man-pages, find any library function which would reveal this
> function.

The fcntl() function is used to lock, unlock, and test for data
locks on files.

While the information available only tells you the process ID of
the process that locked it, you can find out who owns that process
by looking through the output of ps(1).

This is from the online HP-UX 7.0 Reference Manual:


     FCNTL(2)						      FCNTL(2)

     ...

     DESCRIPTION
	  Fcntl provides for control over open files.  Fildes is an
	  open file descriptor.

	  The following are possible values of the cmd argument:

	  ...

	  F_GETLK	 Get the first lock that blocks the lock
			 described by the variable of type struct
			 flock pointed to by arg.  The information
			 retrieved overwrites the information passed
			 to fcntl in the flock structure.  If no lock
			 is found that would prevent this lock from
			 being created, the structure is passed back
			 unchanged, except that the lock type is set
			 to F_UNLCK.

	 ...

	  The structure flock describes the type (l_type), starting
	  offset (l_whence), relative offset (l_start), size (l_len),
	  and process ID (l_pid) of the segment of the file to be
	  affected.  The process ID field is only used with the
	  F_GETLK cmd to return the value of a block in lock.  Locks
	  can start and extend beyond the current end of a file, but
	  cannot be negative relative to the beginning of the file.  A
	  lock can be set to always extend to the end of file by
	  setting l_len to zero (0).  If such a lock also has l_start
	  set to zero (0), the whole file will be locked.  Changing or
	  unlocking a segment from the middle of a larger locked
	  segment leaves two smaller segments for either end.  Locking
	  a segment already locked by the calling process causes the
	  old lock type to be removed and the new lock type to take
	  effect.  All locks associated with a file for a given
	  process are removed when a file descriptor for that file is
	  closed by that process or the process holding that file
	  descriptor terminates.  Locks are not inherited by a child
	  process in a fork(2) system call.

	  ...

     ERRORS
	  Under the following conditions, the function fcntl fails and
	  sets the external variable errno accordingly:

	  ...
	  [EINVAL]	 Cmd is F_GETLK, F_SETLK, or F_SETLKW and
			 arg.lockdes or the data it points to is not
			 valid, or fildes refers to a file that does
			 not support locking.

     RETURN VALUE

     ...

	  F_GETLK	 Value other that -1.
     ...

	  Otherwise, a value of -1 is returned and errno is set to
	  indicate the error.
     ...

     STANDARDS CONFORMANCE
	  fcntl: SVID2, XPG2, XPG3, POSIX.1, FIPS 151-1

     Hewlett-Packard Company	   - 6 -  HP-UX Release 7.0: Sept 1989



More information about the Comp.unix.programmer mailing list