Mailing List
Home
Forum Home
Linux - General Red Hat Linux discussion list
Installation - Getting started with Red Hat Linux
Enterprise Linux 3 - Discussion of Red Hat Enterprise Linux 3 (Taroon)
Red Hat Linux 9 - Discussion of Red Hat Linux 9 (Shrike)
Red Hat Linux 7.2 - Discussion of Red Hat Linux 7.2 (Enigma)
Red Hat Linux 7.3 - Discussion of Red Hat Linux 7.3 (Valhalla)
Apache Web Server
Oracle database, Microsoft SQL server ...
Subjects
Subject: application/x mplayer2 plugin
RPM error: db4 error(16) from dbenv >remove: Device or resource
   busy
Command stream end of file while reading
Subject: X Windows problem (xauth)
Subject: Upgrading openoffice 1 1 rpm
Subject: FTP: connection refused
Subject: FTP: connection refused
mount: /dev/cdrom: is not a valid block device
Dell Precision 650, RedHat 9, no sound
how to trace the cause resulting in the crash of bind server
Virus on the list
UNINSTALL RPM MYSQL
usb pen drives: mounting as a user
Subject: broadcom network interface
make mrproper
Couldn 't open PID file /var/run/named/named pid Permission denied
sendmail configuration on redhat
kernel 2 6 and /dev/sound/mixer not found
Subject: Promise 378 controller
Subject: Problem using up2date
mrtg step by step howto/configuration for a newbie?
Compiling and Installing Kernel 2 6
Can 't locate module ppp0, can 't locate module ppp compress 21
Subject: Lotus Notes under Wine
HOW I CAN MAKE BOOTABLE FLOPPY DISKET
/etc/security/limits conf question
Intel E/1000 driver
rpm database corrupt
Command stream end of file while reading
qla2300 modules
 
File locking program

File locking program

2007-11-29       - By Rick Stevens

 Back
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@(protected) -
- CDN Systems, Internap, Inc.                http://www.internap.com -
-                                                                    -
-          When all else fails, try reading the instructions.        -
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- --

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