Sun "lockf()" fails to provide locking.

Steve Jukoff jukoff at imram.net.com
Sat Mar 23 06:20:20 AEST 1991


I set a lock via lockf().  An immediately test fails to identify the lock.
Code is provided.  Is anyone else having this problem or did I do
something wrong?

System: Sun 4c (SparcStation 1+, O/S 4.1.1 + 
        "rpc.lockd jumbo patch" Patch-ID# 100075-06

Code follows...

---Cut Here------------------------------------------------------

/* lockf.c  -  Test lockf() function    */
/* Compile as: cc -o lockf lockf.c      */
/* open, "O_RDWR"                       */
#include <stdio.h>
#include <unistd.h> 
#include <sys/file.h> 
#include <errno.h> 
#include <sys/time.h> 

main(argc, argv)
    int     argc;
    char    *argv[]; 
{
    int     fd;             
    char    *fname = "/tmp/yy"; 
    extern  void    display_error();

    if ((fd = open (fname, O_RDWR)) < 0) {  
        perror ("open");
        (void) fprintf (stderr, "Unable to open file '%s'.\n", fname);
        exit(1);
    }

    lseek(fd, (off_t) 0, SEEK_SET);     /* rewind   */

    /* Lock from current loc to EOF - the whole file */
    if (lockf(fd, F_TLOCK, 0L) < 0) {   
        display_error(errno);
    } else {            
        fprintf(stdout, "I hope that LOCK IS ATTACHED.\n"); 
        lseek(fd, (off_t) 0, SEEK_SET);     /* rewind   */

        if (lockf(fd, F_TEST, 0L) < 0) {    /* test for lock    */
            display_error(errno); 
        } else {
            fprintf(stderr, 
                "lockf(F_TEST) should have returned EAGAIN but didn't.\n");
        }
    }
}

void
display_error(err_no)
    int err_no; 
{
    errno = err_no; 
    perror("lockf()");
    fprintf(stderr, "Errno = %d -- ", err_no); 

    switch (err_no) {
    case EBADF:     fprintf(stderr, "EBADF\n"); break; 
    case EAGAIN:    fprintf(stderr, "EAGAIN\n"); break; 
    case EINTR:     fprintf(stderr, "EINTR\n"); break; 
    case ENOLCK:    fprintf(stderr, "ENOLCK\n"); break; 
    default:        fprintf(stderr, "Undefined for lockf()\n");
    }
}



More information about the Comp.sys.sun mailing list