File locking program

Rick Stevens rstevens at internap.com
Fri Nov 30 01:09:41 UTC 2007


Here's the source of a locking program you can use to lock files for
the NFS test.  Build by saving the source somewhere and

	gcc -o setalock setalock.c

Running it as

	./setalock -w /path/to/test/file

will acquire a WRITE lock on the entire file.  Omitting the "-w":

	./setalock /path/to/test/file

will acquire a read lock on the entire file.  In either case, hit
"CTRL-C" to stop the program and release the lock.

------------------------------- CUT HERE -----------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include <string.h>

extern int      errno;
struct flock    flck = {0};
struct stat     statinfo = {0};
int             fd;

void            siggrab(int);

int main(int argc, char *argv[]) {

    int                 x;
    char                *fname;
    char                type[10];

    flck.l_whence = SEEK_SET;           /* Lock from beginning
*/
    flck.l_start = 0;                   /* Start at 0
*/

    if (argc <= 1) {                    /* No arguments?
*/
        fprintf(stderr, "\nUsage:\n");  /* Nope, spew usage message
*/
        fprintf(stderr, "\t%s [-w] /path/to/file\n\n", argv[0]);
        exit(EINVAL);                   /* Exit
*/
    }

    if (argc > 2) {                     /* Did we get > 2 arguments?
*/
        strcpy(type, "write");          /* Yes, select write lock
*/
        flck.l_type = F_WRLCK;          /* Set lock type
*/
        fname = strdup(argv[2]);        /* Grab filename
*/

    } else {                            /* No, so...
*/
        strcpy(type, "read");           /* ...select read lock
*/
        flck.l_type = F_RDLCK;          /* Set read-only lock
*/
        fname = strdup(argv[1]);        /* Grab filename
*/
    }

    if ((fd = open(fname, O_RDWR)) < 0) {
                                        /* Can we open the file?
*/
        x = errno;                      /* Nope, save errno
*/
        fprintf(stderr, "Unable to open %s read/write\n", fname);
        exit(x);
    }

    if (fstat(fd, &statinfo) < 0) {     /* Can we stat the file?
*/
        x = errno;                      /* Keep errno for later
*/
        fprintf(stderr, "Unable to get file size on %s\n", fname);
        close(fd);
        exit(errno);
    }

    printf("\nAcquiring %s lock on %s\n", type, fname);
    flck.l_len = statinfo.st_size;      /* Lock WHOLE file
*/
    if (fcntl(fd, F_SETLK, &flck) < 0) {
                                        /* Could we lock it?
*/
        x = errno;                      /* Noope, grab errno
*/
        fprintf(stderr, "Unable to lock %s:\n\t%s\n", fname,
strerror(x));
        close(fd);
        exit(x);
    }

    (void)signal(SIGHUP, siggrab);      /* Catch CTRL-C and its kin
*/
    (void)signal(SIGINT, siggrab);
    (void)signal(SIGABRT, siggrab);
    printf("Lock acquired.  Press \"CTRL-C\" to release lock\n");
    for (;;)                            /* For ever and ever...
*/
        sleep(10);                      /* ...sleep 10 seconds
*/

}

void siggrab(int signal) {

    flck.l_type = F_UNLCK;              /* Select "unlock"
*/
    fcntl(fd, F_SETLK, &flck);          /* Unlock the file
*/
    close(fd);                          /* Close the file
*/
    printf("Lock released.  Exiting...\n\n");
    exit(0);
}
------------------------------- CUT HERE -----------------------------

----------------------------------------------------------------------
- Rick Stevens, Principal Engineer             rstevens at internap.com -
- CDN Systems, Internap, Inc.                http://www.internap.com -
-                                                                    -
-          When all else fails, try reading the instructions.        -
----------------------------------------------------------------------




More information about the Redhat-install-list mailing list