[comp.unix.shell] file locking

jduarte@liege.ICS.UCI.EDU (J o s e D u a r t e) (06/21/91)

UNIX Gurus,

	I have a question for you! A while back I we discussed locking
parts of files using lockf,fcntl, and flock. I've implemented a routine
using lockf() to provide me with an advisory lock on a file which I
use to simulate exclusive access to a resource. It works OK with
any file that I use except when I try to lock a file in
/tmp , it returns with errno set to EINVAL (22).

Does anyone know why this happens? My file descriptor is open without
error :  mutex_file_handle = open(MUTEX_FILE,O_WRONLY|O_APPEND);



...and this is my locking routine. It just loops until the lock can be
set:

void seize_write_block(void)
{ long block_size = BLOCK_SIZE;
  int  result;

 loop:
    result = lockf(mutex_file_handle,F_TLOCK,block_size);
    if (result == SUCCESS)
        { return; }
    switch(errno)
      {
      case EBADF   : (void)fprintf(stderr,"EBADF\n"); break;
      case EINVAL  : (void)fprintf(stderr,"EINVAL\n"); break;
      case EDEADLK : (void)fprintf(stderr,"EDEADLK\n"); break;
      case EINTR   : /* No problem. It just got interrupted */
                     (void)fprintf(stderr,"EINTR\n"); break;
      case ENOLCK  : (void)fprintf(stderr,"ENOLCK\n"); break;
      case EACCES  : (void)fprintf(stderr,"EACCES\n"); break;
      default      : (void)fprintf(stderr,"DEFAULT. errno == %d\n",errno); break;
      }
    (void)fflush(stderr);
    return;
 goto loop;
}

The strange thing is that it returns errno==22 when I try to lock a file
in /tmp/any_file, but it works correctly on any file not in that
directory! I've made sure that the file_descriptor variable is valid (not -1), and
the rest of my code appears OK; so I'm baffled! My file attributes are set to
a+r,a+w for the file in any directory.


Any hints?


Thanks,

Jose' D.