|
|||||||||||
|
File locking program
From: Rick Stevens <rstevens(at)internap.com>
Date: Thu Nov 29 2007 - 20:09:41 EST
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.
extern int errno;
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
*/
if (argc <= 1) { /* No arguments?
*/ }
if (argc > 2) { /* Did we get > 2 arguments?
*/
} else { /* No, so...
*/ } if ((fd = open(fname, O_RDWR)) < 0) {
/* Can we open the file?
}
if (fstat(fd, &statinfo) < 0) { /* Can we stat the file?
x = errno; /* Keep errno for later
}
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?
}
(void)signal(SIGHUP, siggrab); /* Catch CTRL-C and its kin
(void)signal(SIGINT, siggrab);
for (;;) /* For ever and ever...
*/ } 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@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 Received on Thu Nov 29 20:10:35 2007 This archive was generated by hypermail 2.1.8 : Fri May 30 2008 - 14:35:45 EDT |
||||||||||
|
|||||||||||