[mgetty] mgetty: use liblockdev for device locking by default

Michal Sekletar msekleta at fedoraproject.org
Fri Apr 19 12:16:24 UTC 2013


commit 67aff5c15a789c8d6ee45d9cef1affd2e41b2de2
Author: Michal Sekletar <msekleta at redhat.com>
Date:   Thu Mar 28 12:50:49 2013 +0100

    mgetty: use liblockdev for device locking by default
    
    Former implementation of device locking was implemented directly
    by mgetty. Now we will rely on liblockdev to do device locking.
    
    Using liblockdev we solve issues described in RHBZ #754235, thus
    device lockfiles are created in /var/lock/lockdev and it makes
    mgetty interoperable with other software already migrated to
    liblockdev, e.g. minicom.

 mgetty-1.1.36-lockdev.patch |  161 +++++++++++++++++++++++++++++++++++++++++++
 mgetty.spec                 |    4 +-
 2 files changed, 164 insertions(+), 1 deletions(-)
---
diff --git a/mgetty-1.1.36-lockdev.patch b/mgetty-1.1.36-lockdev.patch
new file mode 100644
index 0000000..33a4932
--- /dev/null
+++ b/mgetty-1.1.36-lockdev.patch
@@ -0,0 +1,161 @@
+diff -up mgetty-1.1.36/locks.c.lockdev mgetty-1.1.36/locks.c
+--- mgetty-1.1.36/locks.c.lockdev	2006-09-26 00:32:08.000000000 +0200
++++ mgetty-1.1.36/locks.c	2013-04-18 15:33:49.224700113 +0200
+@@ -43,8 +43,16 @@
+ #define LCK_OPNFAIL  -2
+ #endif
+ 
++#ifdef HAVE_LOCKDEV
++#include <lockdev.h>
++#endif
++
+        char	lock[MAXLINE+1];	/* name of the lockfile */
+ 
++#ifdef HAVE_LOCKDEV
++char lockdev_device[MAXLINE + 1];
++#endif
++
+ static int readlock _PROTO(( char * name ));
+ static char *  get_lock_name _PROTO(( char * lock_name, char * device ));
+ static int lock_write_pid _PROTO(( int fd ));
+@@ -58,8 +66,10 @@ static int we_have_lock = FALSE;
+ 
+ int do_makelock _P0( void )
+ {
++#ifndef HAVE_LOCKDEV
+ 	int fd, pid;
+ 	char *temp, buf[MAXLINE+1];
++
+ #ifndef HAVE_MKSTEMP
+ 	int tries = 0;
+ #endif
+@@ -148,8 +158,26 @@ again:
+ 		return(FAIL);
+ 	}
+ 	
+-	lprintf(L_NOISE, "lock made");
+ 	(void) unlink(temp);
++#else
++        pid_t device_owner;
++
++        device_owner = dev_lock(lockdev_device);
++        if (device_owner < 0)
++        {
++                lprintf(L_ERROR, "lock not made: dev_lock(%s) failed", lockdev_device);
++                return FAIL;
++        }
++
++        if (device_owner > 0)
++        {
++                lprintf(L_MESG, "lock not made: lock file exists (pid=%d)", device_owner);
++                return FAIL;
++        }
++
++#endif
++
++        lprintf(L_NOISE, "lock made");
+ 	we_have_lock = TRUE;
+ 	return(SUCCESS);
+ }
+@@ -180,10 +208,13 @@ int makelock _P1( (device),
+  */
+ int steal_lock _P2((device, pid), char * device, int pid )
+ {
+-    int retcode, is_pid, fd;
+-    
++    int retcode = SUCCESS;
++
+     lprintf(L_NOISE, "steal_lock(%s) called", device);
+ 
++#ifndef HAVE_LOCKDEV
++    int is_pid, fd;
++    
+     if ( get_lock_name( lock, device ) == NULL )
+     {
+ 	lprintf( L_ERROR, "cannot get lock name" );
+@@ -208,6 +239,13 @@ int steal_lock _P2((device, pid), char *
+     }
+ 
+     retcode = lock_write_pid( fd );
++#else
++    if (dev_relock(lockdev_device, pid) < 0)
++    {
++            lprintf( L_ERROR, "can't steal lock: dev_relock(%s, %d) failed", device, pid);
++            retcode = FAIL;
++    }
++#endif
+ 
+     if ( retcode == SUCCESS ) we_have_lock = TRUE;
+     return retcode;
+@@ -237,6 +275,7 @@ int makelock_file _P1( (file), char * fi
+ int checklock _P1( (device), char * device)
+ {
+     int pid;
++#ifndef HAVE_LOCKDEV
+     struct stat st;
+     char name[MAXLINE+1];
+     
+@@ -270,7 +309,21 @@ int checklock _P1( (device), char * devi
+ 	(void) unlink(name);
+ 	return NO_LOCK;
+     }
+-    
++#else
++    pid = dev_testlock(lockdev_device);
++
++    if (pid < 0)
++    {
++            lprintf(L_ERROR, "checklock: can't check lockfile, dev_testlock(%s) failed", lockdev_device);
++            return NO_LOCK;
++    }
++
++    if (pid == 0)
++    {
++            lprintf(L_MESG, "checklock: device not locked");
++            return NO_LOCK;
++    }
++#endif
+     lprintf(L_NOISE, "lockfile found, pid=%d", pid );
+     
+     return pid;
+@@ -352,8 +405,13 @@ void rmlocks _P0(void)
+     if ( we_have_lock )
+     {
+ 	lprintf( L_NOISE, "removing lock file" );
++#ifndef HAVE_LOCKDEV
+ 	if ( unlink(lock) == -1 )
+ 	    lprintf( L_ERROR, "error removing lock file (huh?!)" );
++#else
++        if (dev_unlock(lockdev_device, getpid()) < 0)
++             lprintf(L_ERROR, "error removing lock file, dev_unlock(%s) failed", lockdev_device);
++#endif
+     }
+     /* mark lock file as 'not set' */
+     we_have_lock = FALSE;
+diff -up mgetty-1.1.36/mgetty.c.lockdev mgetty-1.1.36/mgetty.c
+--- mgetty-1.1.36/mgetty.c.lockdev	2013-04-18 15:33:49.210700109 +0200
++++ mgetty-1.1.36/mgetty.c	2013-04-18 15:33:49.224700113 +0200
+@@ -349,6 +349,10 @@ int main _P2((argc, argv), int argc, cha
+     /* need full name of the device */
+     sprintf( devname, "/dev/%s", Device);
+ 
++#ifdef HAVE_LOCKDEV
++    strncpy(lockdev_device, devname, MAXLINE);
++#endif 
++
+     /* Device ID = Device name without "/dev/", all '/' converted to '-' */
+     DevID = mydup( Device );
+     for ( i=0; DevID[i] != 0; i++ )
+diff -up mgetty-1.1.36/mgetty.h.lockdev mgetty-1.1.36/mgetty.h
+--- mgetty-1.1.36/mgetty.h.lockdev	2013-04-18 15:37:16.886757978 +0200
++++ mgetty-1.1.36/mgetty.h	2013-04-18 15:38:23.476777190 +0200
+@@ -247,6 +247,9 @@ int		makelock_file _PROTO(( char * lockn
+ int		checklock _PROTO((char * device));
+ void		rmlocks _PROTO ((void));
+ int		steal_lock _PROTO((char * device, int pid ));
++#ifdef HAVE_LOCKDEV
++extern char lockdev_device[MAXLINE + 1];
++#endif
+   
+ /* fax stuff */
+ void	faxrec _PROTO(( char * spool_dir, unsigned int switchbd,
diff --git a/mgetty.spec b/mgetty.spec
index 97b413b..26ce672 100644
--- a/mgetty.spec
+++ b/mgetty.spec
@@ -44,6 +44,7 @@ Patch25: mgetty-1.1.36-sd.patch
 # patch updates makefiles, it removes hardcoded -s parameter of /usr/bin/install
 # thus .debug files for all binaries will be generated properly
 Patch26: mgetty-1.1.36-makefiles.patch
+Patch27: mgetty-1.1.36-lockdev.patch
 
 License: GPLv2+
 Group: Applications/Communications
@@ -134,9 +135,10 @@ mv policy.h-dist policy.h
 %patch24 -p1 -b .man
 %patch25 -p1 -b .sd
 %patch26 -p1 -b .makefile
+%patch27 -p1 -b .lockdev
 
 %build
-%define makeflags CFLAGS="$RPM_OPT_FLAGS -Wall -DAUTO_PPP -D_FILE_OFFSET_BITS=64" prefix=%{_prefix} spool=%{_var}/spool BINDIR=%{_bindir} SBINDIR=%{_sbindir} LIBDIR=%{_libdir}/mgetty+sendfax HELPDIR=%{_libdir}/mgetty+sendfax CONFDIR=%{_sysconfdir}/mgetty+sendfax MANDIR=%{_mandir} MAN1DIR=%{_mandir}/man1 MAN4DIR=%{_mandir}/man4 MAN5DIR=%{_mandir}/man5 MAN8DIR=%{_mandir}/man8 INFODIR=%{_infodir} ECHO='"echo -e"' INSTALL=%{__install}
+%define makeflags CFLAGS="$RPM_OPT_FLAGS -Wall -DAUTO_PPP -D_FILE_OFFSET_BITS=64 -DHAVE_LOCKDEV" LIBS="-llockdev" prefix=%{_prefix} spool=%{_var}/spool BINDIR=%{_bindir} SBINDIR=%{_sbindir} LIBDIR=%{_libdir}/mgetty+sendfax HELPDIR=%{_libdir}/mgetty+sendfax CONFDIR=%{_sysconfdir}/mgetty+sendfax MANDIR=%{_mandir} MAN1DIR=%{_mandir}/man1 MAN4DIR=%{_mandir}/man4 MAN5DIR=%{_mandir}/man5 MAN8DIR=%{_mandir}/man8 INFODIR=%{_infodir} ECHO='"echo -e"' INSTALL=%{__install}
 make %{makeflags}
 make -C voice %{makeflags}
 make -C tools %{makeflags}


More information about the scm-commits mailing list