FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Redhat > Red Hat Install

 
 
LinkBack Thread Tools
 
Old 11-30-2007, 12:09 AM
Rick Stevens
 
Default File locking program

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, "
Usage:
"); /* Nope, spew usage message
*/
fprintf(stderr, " %s [-w] /path/to/file

", 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
", 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
", fname);
close(fd);
exit(errno);
}

printf("
Acquiring %s lock on %s
", 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:
%s
", 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
");
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...

");
exit(0);
}
------------------------------- CUT HERE -----------------------------

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

_______________________________________________
Redhat-install-list mailing list
Redhat-install-list@redhat.com
https://www.redhat.com/mailman/listinfo/redhat-install-list
To Unsubscribe Go To ABOVE URL or send a message to:
redhat-install-list-request@redhat.com
Subject: unsubscribe
 

Thread Tools




All times are GMT. The time now is 01:27 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org