[comp.sys.sgi] Silly error with flock

rlb@lavalite.asd.sgi.com (Robert Brown) (01/10/91)

On the bottom of my copy of the flock manual page is the following:

     Unlike BSD flock(2), attempts to acquire an exclusive lock on an fd
     opened for reading but not writing will fail with EBADF, as will attempts
     to acquire a shared lock on a write-only fd.

Also, your test

      if ( lock <= 0) perror("open");

should be

      if ( lock < 0) perror("open");

but that's unrelated to the condition you asked about

Hope that helps!

Bob Brown
------------------------------------------------------------------
Silicon Graphics, Inc.  Advanced Systems Division

krogh@talon.ncsa.uiuc.edu (Mike Krogh) (01/11/91)

Make sure that 'lockd' and 'statd' are running on your machine (see 
'man lockd') for info.

There also exists a more serious error with file locking, which may affect
your Email (and possibly other things).

If you set a lock on a file that exists on an NFS partition and that file
goes to zero in size, then the file will appear to contain garbage from 
then on.  To solve the problem, you must either delete the file or reboot
the machine.  The file will appear to be correct on other machines, so the
file is not actually corrupted, just its appearance.

I first encountered this bug after deleting my new mail in 'mbox', which
is on our server.  'mbox' then appears to be corrupted from then on.  The
temporary solution has been to read my mail on a Sun workstation.

In article <1991Jan9.030948.18665@ccu1.aukuni.ac.nz>, russell@ccu1.aukuni.ac.nz (Russell J Fulton;ccc032u) writes:
> I am trying to use flock but I always get an "Bad file number" error.
> Here is my test program:
> 
> #include <sys/types.h>
> #include <sys/file.h>
> #include <stdio.h>
> 
> main() {
>     int lock;
> 
>     lock = open("test.lock",O_RDONLY); /* test.lock does exist */
>     if ( lock <= 0) perror("open");
> 
>     if(flock(lock,LOCK_EX) == -1) perror("flock");
> 
>     getchar();
>     flock(lock,LOCK_UN);
>     close (lock);
> }
> 
> Always prints "flock: Bad file number"!
> 
> Any ideas?? It must be something so obvious that I have totally missed it!
> 
> Cheers, Russell.
> -- 
> Russell Fulton, Computer Center, University of Auckland, New Zealand.
> <rj_fulton@aukuni.ac.nz>

jesse@camelot.sgi.com (Jesse Rendleman) (01/11/91)

In article <1991Jan9.030948.18665@ccu1.aukuni.ac.nz>, russell@ccu1.aukuni.ac.nz (Russell J Fulton;ccc032u) writes:
> I am trying to use flock but I always get an "Bad file number" error.
> Here is my test program:
> 
> #include <sys/types.h>
> #include <sys/file.h>
> #include <stdio.h>
> 
> main() {
>     int lock;
> 
>     lock = open("test.lock",O_RDONLY); /* test.lock does exist */
>     if ( lock <= 0) perror("open");
> 
>     if(flock(lock,LOCK_EX) == -1) perror("flock");
> 
>     getchar();
>     flock(lock,LOCK_UN);
>     close (lock);
> }
> 
> Always prints "flock: Bad file number"!
> 
> Any ideas?? It must be something so obvious that I have totally missed it!

from the flock(3b) man page...

     Unlike BSD flock(2), attempts to acquire an exclusive lock on an fd
     opened for reading but not writing will fail with EBADF, as will attempts
     to acquire a shared lock on a write-only fd.