[comp.unix.programmer] Checking if a process opened a file

it1@Isis.MsState.Edu (Tim Tsai) (04/02/91)

  Is it possible to check if a process (possibly from another machine
via NFS) opened a file for reading/writing?  I've mucked with the
select() call, but the call always returns with the value 1.  What is
an "exceptional condition"?  Here's a sketch of what I've attempted
(with error checking, etc removed):

    infile = open ( filename, O_RDONLY );
    FD_ZERO ( &fdset );
    FD_SET ( infile, &fdset );
    for (;;)
    {
        result = select ( ulimit(), 0, 0, &fdset, 0 );
        printf ("exceptional condition on %s, result = %d\n", filename,
            result);
    }

  BTW, I know about named pipes.  I'm interested in regular files.

  This is on a Sun4, SunOS 4.1.1..  Thanks in advance for any help..

  PS:  I also tried poll(), with timeout set to -1 and events set to
  POLLPRI..  Same result.

--
  Never argue with a fool, people might not know the difference.
                                                       <Murphy's Law>

subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (04/02/91)

In article <it1.670574750@Isis.MsState.Edu> it1@Isis.MsState.Edu (Tim Tsai) writes:
>  Is it possible to check if a process (possibly from another machine
>via NFS) opened a file for reading/writing?  I've mucked with the
>select() call, but the call always returns with the value 1.  What is
>an "exceptional condition"?  Here's a sketch of what I've attempted
>(with error checking, etc removed):
>
>    infile = open ( filename, O_RDONLY );
>    FD_ZERO ( &fdset );
>    FD_SET ( infile, &fdset );
>    for (;;)
>    {
>        result = select ( ulimit(), 0, 0, &fdset, 0 );
>        printf ("exceptional condition on %s, result = %d\n", filename,
>            result);
>    }

select() is used generally to determine whether a file descriptor is ready
for reading from, or writing to. In the case of a "normal" file (i.e not a
socket or tty or something), reads and writes generally do not have a
chance of blocking indefinitely, like they might on a tty, because there's 
no chance of 'more' information appearing in the file. An example of an 
"exceptional" condition would be out of band data on a socket. Again, 
something you are not really interested in here.

There is a command ofiles to determine who has a given file open, but I'm
not sure that this works across NFS -- your best bet might be to either set up
some communication with any other process that wants to open a file (via
sockets) or establish your own protocol for determining that another process
wants to access this file.

			-Kartik
--
internet# find . -name core -exec cat {} \; |& tee /dev/tty*
subbarao@phoenix.Princeton.EDU -| Internet
kartik@silvertone.Princeton.EDU (NeXT mail)  
SUBBARAO@PUCC.BITNET			          - Bitnet

torek@elf.ee.lbl.gov (Chris Torek) (04/06/91)

In article <it1.670574750@Isis.MsState.Edu> it1@Isis.MsState.Edu (Tim Tsai)
writes:
>  Is it possible to check if a process (possibly from another machine
>via NFS) opened a file for reading/writing?

No.

>What is an "exceptional condition"?

Anything and nothing---the select system call simply calls some
associated function (fp->f_ops->fo_select) asking `do you want to
report an exceptional condition'.  Ordinary files invariably answer
`no'.  Device files (`character special') simply pass the question
on to the device driver; sockets answer `yes' if the socket is marked
as having `out of band' data (which again is up to lower level code).
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek@ee.lbl.gov

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (04/07/91)

In article <7794@idunno.Princeton.EDU> subbarao@phoenix.Princeton.EDU (Kartik Subbarao) writes:
> There is a command ofiles to determine who has a given file open, but I'm
> not sure that this works across NFS

The version from comp.sources.unix volume 18 does not support NFS, but
there are other versions of ofiles running around that do. Sometime in
the near future I will make my kstuff package available. Included in
kstuff is pf, a more portable, hopefully easier-to-use, integrated
version of ofiles/fstat; pf works over NFS, though there's no way to
tell that a process on some other machine has a file open.

---Dan