Blob Blame History Raw
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,