--- lockdev-1.0.0/src/sample.c.rh Wed Dec 1 06:50:09 1999 +++ lockdev-1.0.0/src/sample.c Tue Aug 14 14:27:39 2001 @@ -3,27 +3,26 @@ void usage() { - fprintf( stderr, "Usage: sample [-lurd] \n" ); - exit( -1 ); + fprintf(stderr, "Usage: %s [-lud] \n", "lockdev"); + exit(-1); } int debug; int -main( int argc, char *argv[] ) +main(int argc, char *argv[]) { int i, chld; - char *p, *dev, ch; + char *p = NULL, *dev = NULL, ch; ch = '\0'; - for( i = argc - 1; i > 0; i-- ) { + for(i = argc - 1; i > 0; i--) { p = argv[i]; - if( *p == '-' ) { - switch( *++p ) { + if (*p == '-') { + switch(*++p) { case 'l': - case 'u': - case 'r': ch = *p; break; + case 'u': ch = *p; break; case 'd': debug = atoi(++p); break; @@ -32,31 +31,28 @@ } else dev = p; } - fprintf( stderr, "option %c, device %s\n", ch, dev ); i = 0; - switch( ch ) { + (void) dev_setpid(getppid()); + switch(ch) { case 'l': - i = dev_lock( dev); + i = dev_lock(dev); break; case 'u': - i = dev_unlock( dev, 0); - break; - case 'r': - dev_lock( dev); - if(( chld = fork()) == 0 ) { - sleep(5); - } - else { - sleep( 1); - if (( i = dev_relock( dev, chld)) < 0 ) { - fprintf( stderr, "Relock failed in parent.\n" ); - } - } + i = dev_unlock(dev, 0); break; default: - i = dev_testlock( dev); + if (dev) + i = dev_testlock(dev); break; } - exit( i); -} + /* + * Exit dev_lock dev_unlock dev_testlock + * 0 OK OK not locked + * 1 locked other locked other locked + * 255 fail fail fail + */ + if (i < 0) i = 255; + else if (i > 0) i = 1; + exit(i); +} --- lockdev-1.0.0/src/lockdev.c.rh Wed Dec 1 06:43:47 1999 +++ lockdev-1.0.0/src/lockdev.c Tue Aug 14 14:25:55 2001 @@ -176,6 +176,19 @@ liblockdev_debug = 0; } +static pid_t dev_pid = 0; + +pid_t dev_getpid(void) +{ + return (dev_pid ? dev_pid : getpid()); +} + +pid_t dev_setpid(pid_t newpid) +{ + pid_t oldpid = dev_pid; + dev_pid = newpid; + return oldpid; +} /* * for internal use * @@ -429,7 +442,7 @@ * maybe also this sprintf should be added to the * conditional part, as the others */ - sprintf( tpname, "%s/.%d", LOCK_PATH, (int)getpid()); + sprintf( tpname, "%s/.%d", LOCK_PATH, (int)dev_getpid()); unlink( tpname); /* in case there was */ rename( lockname, tpname); if ( ! (fd=fopen( tpname, "r")) ) { @@ -586,7 +599,7 @@ #endif /* DEBUG */ _debug( 3, "dev_lock(%s)\n", devname); if (oldmask == -1 ) - oldmask = umask( 0); /* give full permissions to files created */ + oldmask = umask( 002); /* apply o-w to files created */ if ( ! (p=_dl_check_devname( devname)) ) close_n_return( -1); strcpy( device, DEV_PATH); @@ -599,9 +612,12 @@ if ( stat( device, &statbuf) == -1 ) { close_n_return( -1); } + if ( access( device, W_OK ) == -1 ) { + close_n_return( -1); + } /* now get our own pid */ - our_pid = getpid(); + our_pid = dev_getpid(); _debug( 2, "dev_lock() our own pid = %d\n", (int)our_pid); /* We will use this algorithm: @@ -742,7 +758,7 @@ #endif /* DEBUG */ _debug( 3, "dev_relock(%s, %d)\n", devname, (int)old_pid); if (oldmask == -1 ) - oldmask = umask( 0); /* give full permissions to files created */ + oldmask = umask( 002); /* apply o-w to files created */ if ( ! (p=_dl_check_devname( devname)) ) close_n_return( -1); strcpy( device, DEV_PATH); @@ -755,9 +771,12 @@ if ( stat( device, &statbuf) == -1 ) { close_n_return( -1); } + if ( access( device, W_OK ) == -1 ) { + close_n_return( -1); + } /* now get our own pid */ - our_pid = getpid(); + our_pid = dev_getpid(); _debug( 2, "dev_relock() our own pid = %d\n", (int)our_pid); /* first check for the FSSTND-1.2 lock, get the pid of the @@ -828,7 +847,7 @@ #endif /* DEBUG */ _debug( 3, "dev_unlock(%s, %d)\n", devname, (int)pid); if (oldmask == -1 ) - oldmask = umask( 0); /* give full permissions to files created */ + oldmask = umask( 002); /* apply o-w to files created */ if ( ! (p=_dl_check_devname( devname)) ) close_n_return( -1); strcpy( device, DEV_PATH); @@ -841,6 +860,9 @@ if ( stat( device, &statbuf) == -1 ) { close_n_return( -1); } + if ( access( device, W_OK ) == -1 ) { + close_n_return( -1); + } /* first remove the FSSTND-1.2 lock, get the pid of the * owner of the lock and test for its existence; in case, --- lockdev-1.0.0/src/lockdev.h.rh Wed Dec 1 06:39:42 1999 +++ lockdev-1.0.0/src/lockdev.h Tue Aug 14 14:19:58 2001 @@ -60,6 +60,9 @@ void liblockdev_incr_debug __P(()); void liblockdev_reset_debug __P(()); +pid_t dev_getpid __P(( void )); +pid_t dev_setpid __P(( pid_t pid )); + pid_t dev_testlock __P(( const char * devname)); pid_t dev_lock __P(( const char * devname)); --- lockdev-1.0.0/src/baudboy.h.rh Tue Aug 14 14:19:58 2001 +++ lockdev-1.0.0/src/baudboy.h Tue Aug 14 14:21:37 2001 @@ -0,0 +1,83 @@ +#ifndef _BADUBOY_H_ +#define _BAUDBOY_H_ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define LOCKDEV_PATH "/usr/sbin/lockdev" + +static inline int doit(const char * argv[]) +{ + pid_t child; + int status; + void (*osig) (int) = signal(SIGCHLD, SIG_IGN); + int rc; + + if (!(child = fork())) { + int fd; + /* these have to be open to something */ + if ((fd = open("/dev/null", 2)) < 0) + exit(-1); + dup2(fd, 0); + dup2(fd, 1); + dup2(fd, 2); + close(fd); + execv(argv[0], (char *const *)argv); + exit(-1); + } + + rc = (int) waitpid(child, &status, 0); + signal(SIGCHLD, osig); + if (rc == child && WIFEXITED(status)) { + rc = WEXITSTATUS(status); + if (rc == 255) rc = -1; + } else + rc = -1; + + return rc; + +} + +static inline int ttylock(const char * devname) +{ + const char * argv[] = { LOCKDEV_PATH, "-l", NULL, NULL}; + argv[2] = devname; + return doit(argv); +} + +static inline int ttyunlock(const char * devname) +{ + const char * argv[] = { LOCKDEV_PATH, "-u", NULL, NULL}; + argv[2] = devname; + return doit(argv); +} + +static inline int ttylocked(const char * devname) +{ + const char * argv[] = { LOCKDEV_PATH, NULL, NULL}; + argv[1] = devname; + return doit(argv); +} + +static inline int ttywait(const char * devname) +{ + int rc; + while((rc = ttylocked(devname)) == 0) + sleep(1); + return rc; +} + +#ifdef __cplusplus +}; +#endif + +#endif /* _BAUDBOY_H_ */ --- lockdev-1.0.0/Makefile.rh Wed Dec 1 05:56:22 1999 +++ lockdev-1.0.0/Makefile Tue Aug 14 14:19:58 2001 @@ -6,6 +6,8 @@ objs = src/lockdev.o +lockdev = src/sample.c + VER = $(shell expr `pwd` : '.*-\([0-9.]*\)') MVER = ${shell expr `pwd` : '.*-\([0-9]*\).[0-9]*'} @@ -18,17 +20,18 @@ basedir = /usr/local srcdir=. +sbindir = ${basedir}/sbin libdir = ${basedir}/lib incdir = ${basedir}/include mandir = ${basedir}/share/man CC = gcc -LCFLAGS = -g -O2 -fPIC -Wall -pipe -D_REENTRANT -CFLAGS = -g +CFLAGS = -g -O2 -Wall -pipe +LCFLAGS = ${CFLAGS} -fPIC -D_REENTRANT LDLIBS = -llockdev .PHONY: shared static perl-lib -ALL: shared static perl-lib +ALL: shared static lockdev perl-lib static ${static}: ${objs} $(AR) $(ARFLAGS) ${static} $^ @@ -36,6 +39,11 @@ shared ${shared}: ${objs} ${CC} ${LCFLAGS} -shared -Wl,-soname,${soname} $^ -lc -o ${shared} +lockdev.o: ${lockdev} + ${CC} ${CFLAGS} -I./src -o $@ -c $^ + +lockdev: lockdev.o ${static} + ${CC} -o $@ $^ perl-lib: static cd LockDev && perl Makefile.PL INSTALLDIRS=perl @@ -51,6 +59,7 @@ install -m755 -d ${incdir} install -m644 src/lockdev.h ${incdir} install -m644 src/ttylock.h ${incdir} + install -m644 src/baudboy.h ${incdir} install_debug: ${static} ${shared} install -m755 -d ${libdir}/debug @@ -68,7 +77,9 @@ install_run: ${shared} install -m755 -d ${libdir} - install -m644 ${shared} ${libdir} + install -m755 ${shared} ${libdir} + install -m755 -d ${sbindir} + install -m755 lockdev ${sbindir} .PHONY: clean distclean perl-clean mostyclean perl-clean: clean