[mgetty] lockdev: fix device locking other tools
Michal Sekletar
msekleta at fedoraproject.org
Thu Jul 11 15:50:09 UTC 2013
commit 6f3650ead5f47ae260a976fb9bbba4b8725dca3c
Author: Michal Sekletar <msekleta at redhat.com>
Date: Wed Jul 10 19:05:28 2013 +0200
lockdev: fix device locking other tools
Module locks.c is compiled in not only into mgetty itself but to other
tools too, e.g vgetty. Initial version of patch adding support for
lockdev api covered device locking only for mgetty. At the time
neither me nor people who tested the patch discovered the bug,
because obviosly we didn't tested it with voice modems.
Resolves: #982711
mgetty-1.1.36-lockdev.patch | 118 +++++++++++++++++++++++++++----------------
mgetty.spec | 1 +
2 files changed, 75 insertions(+), 44 deletions(-)
---
diff --git a/mgetty-1.1.36-lockdev.patch b/mgetty-1.1.36-lockdev.patch
index 33a4932..45121d9 100644
--- a/mgetty-1.1.36-lockdev.patch
+++ b/mgetty-1.1.36-lockdev.patch
@@ -1,7 +1,7 @@
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 @@
++++ mgetty-1.1.36/locks.c 2013-07-10 18:56:36.060899913 +0200
+@@ -43,6 +43,10 @@
#define LCK_OPNFAIL -2
#endif
@@ -11,25 +11,16 @@ diff -up mgetty-1.1.36/locks.c.lockdev mgetty-1.1.36/locks.c
+
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;
+@@ -58,6 +62,7 @@ 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:
+@@ -148,8 +153,26 @@ again:
return(FAIL);
}
@@ -38,10 +29,10 @@ diff -up mgetty-1.1.36/locks.c.lockdev mgetty-1.1.36/locks.c
+#else
+ pid_t device_owner;
+
-+ device_owner = dev_lock(lockdev_device);
++ device_owner = dev_lock(lock);
+ if (device_owner < 0)
+ {
-+ lprintf(L_ERROR, "lock not made: dev_lock(%s) failed", lockdev_device);
++ lprintf(L_ERROR, "lock not made: dev_lock(%s) failed", lock);
+ return FAIL;
+ }
+
@@ -57,7 +48,7 @@ diff -up mgetty-1.1.36/locks.c.lockdev mgetty-1.1.36/locks.c
we_have_lock = TRUE;
return(SUCCESS);
}
-@@ -180,10 +208,13 @@ int makelock _P1( (device),
+@@ -180,8 +203,8 @@ int makelock _P1( (device),
*/
int steal_lock _P2((device, pid), char * device, int pid )
{
@@ -67,18 +58,23 @@ diff -up mgetty-1.1.36/locks.c.lockdev mgetty-1.1.36/locks.c
+
lprintf(L_NOISE, "steal_lock(%s) called", device);
+ if ( get_lock_name( lock, device ) == NULL )
+@@ -190,6 +213,9 @@ int steal_lock _P2((device, pid), char *
+ return FAIL;
+ }
+
+#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 *
++
+ is_pid = readlock(lock);
+
+ if ( is_pid != pid )
+@@ -208,6 +234,13 @@ int steal_lock _P2((device, pid), char *
}
retcode = lock_write_pid( fd );
+#else
-+ if (dev_relock(lockdev_device, pid) < 0)
++ if (dev_relock(lock, pid) < 0)
+ {
+ lprintf( L_ERROR, "can't steal lock: dev_relock(%s, %d) failed", device, pid);
+ retcode = FAIL;
@@ -87,25 +83,35 @@ diff -up mgetty-1.1.36/locks.c.lockdev mgetty-1.1.36/locks.c
if ( retcode == SUCCESS ) we_have_lock = TRUE;
return retcode;
-@@ -237,6 +275,7 @@ int makelock_file _P1( (file), char * fi
+@@ -237,7 +270,6 @@ int makelock_file _P1( (file), char * fi
int checklock _P1( (device), char * device)
{
int pid;
-+#ifndef HAVE_LOCKDEV
- struct stat st;
+- struct stat st;
char name[MAXLINE+1];
-@@ -270,7 +309,21 @@ int checklock _P1( (device), char * devi
+ if ( get_lock_name( name, device ) == NULL )
+@@ -246,6 +278,9 @@ int checklock _P1( (device), char * devi
+ return NO_LOCK;
+ }
+
++#ifndef HAVE_LOCKDEV
++ struct stat st;
++
+ if ((stat(name, &st) == FAIL) && errno == ENOENT)
+ {
+ lprintf(L_NOISE, "checklock: stat failed, no file");
+@@ -270,12 +305,27 @@ int checklock _P1( (device), char * devi
(void) unlink(name);
return NO_LOCK;
}
-
+#else
-+ pid = dev_testlock(lockdev_device);
++ pid = dev_testlock(name);
+
+ if (pid < 0)
+ {
-+ lprintf(L_ERROR, "checklock: can't check lockfile, dev_testlock(%s) failed", lockdev_device);
++ lprintf(L_ERROR, "checklock: can't check lockfile, dev_testlock(%s) failed", name);
+ return NO_LOCK;
+ }
+
@@ -118,7 +124,22 @@ diff -up mgetty-1.1.36/locks.c.lockdev mgetty-1.1.36/locks.c
lprintf(L_NOISE, "lockfile found, pid=%d", pid );
return pid;
-@@ -352,8 +405,13 @@ void rmlocks _P0(void)
+ }
+
++#ifndef HAVE_LOCKDEV
+ /*
+ * readlock() - read contents of lockfile
+ *
+@@ -342,6 +392,8 @@ static int lock_write_pid _P1((fd), int
+ close(fd);
+ return SUCCESS;
+ }
++
++#endif
+
+ /*
+ * rmlocks() - remove lockfile
+@@ -352,8 +404,13 @@ void rmlocks _P0(void)
if ( we_have_lock )
{
lprintf( L_NOISE, "removing lock file" );
@@ -126,29 +147,38 @@ diff -up mgetty-1.1.36/locks.c.lockdev mgetty-1.1.36/locks.c
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);
++ if (dev_unlock(lock, getpid()) < 0)
++ lprintf(L_ERROR, "error removing lock file, dev_unlock(%s) failed", lock);
+#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);
+@@ -364,7 +421,21 @@ void rmlocks _P0(void)
+ * determine full path + name of the lock file for a given device
+ */
+-#ifdef SVR4
+#ifdef HAVE_LOCKDEV
-+ strncpy(lockdev_device, devname, MAXLINE);
-+#endif
++/*
++ * lockdev API provides us with the way to lock device directly, thus lock name
++ * and device name are the same
++ */
++static char *get_lock_name _P2( (lock, device), char *lock, char *device )
++{
++ lprintf(L_NOISE, "get_lock_name(%s) called", device);
++
++ snprintf(lock, MAXLINE, "%s", device);
++
++ return lock;
++}
+
- /* Device ID = Device name without "/dev/", all '/' converted to '-' */
- DevID = mydup( Device );
- for ( i=0; DevID[i] != 0; i++ )
++#elif SVR4
+
+ /*
+ * get_lock_name() - create SVR4 lock file name (Bodo Bauer)
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
+--- mgetty-1.1.36/mgetty.h.lockdev 2013-07-10 18:54:27.284896229 +0200
++++ mgetty-1.1.36/mgetty.h 2013-07-10 18:54:27.303896229 +0200
@@ -247,6 +247,9 @@ int makelock_file _PROTO(( char * lockn
int checklock _PROTO((char * device));
void rmlocks _PROTO ((void));
diff --git a/mgetty.spec b/mgetty.spec
index 4ea1925..37e0264 100644
--- a/mgetty.spec
+++ b/mgetty.spec
@@ -367,6 +367,7 @@ fi
* Wed Jul 10 2013 Michal Sekletar <msekleta at redhat.com> - 1.1.36-22
- disable strict-aliasing optimizations
- fix dates in changelog
+- fix device locking in other tools than mgetty (#982711)
* Tue Apr 23 2013 Michal Sekletar <msekleta at redhat.com> - 1.1.36-21
- fix broken dependencies
More information about the scm-commits
mailing list