702687f
diff --git a/locks.c b/locks.c
702687f
index 649ed4a..aa8898c 100644
702687f
--- a/locks.c
702687f
+++ b/locks.c
702687f
@@ -41,6 +41,10 @@
702687f
  
702687f
 #define LCK_NODEV    -1
67aff5c
 #define LCK_OPNFAIL  -2
67aff5c
+#endif
67aff5c
+
702687f
+#ifdef HAVE_LOCKDEV
702687f
+#include <lockdev.h>
702687f
 #endif
67aff5c
 
702687f
        char	lock[MAXLINE+1];	/* name of the lockfile */
6f3650e
@@ -58,6 +62,7 @@ static int we_have_lock = FALSE;
67aff5c
 
67aff5c
 int do_makelock _P0( void )
67aff5c
 {
67aff5c
+#ifndef HAVE_LOCKDEV
67aff5c
 	int fd, pid;
67aff5c
 	char *temp, buf[MAXLINE+1];
67aff5c
 #ifndef HAVE_MKSTEMP
6f3650e
@@ -148,8 +153,26 @@ again:
67aff5c
 		return(FAIL);
67aff5c
 	}
67aff5c
 	
67aff5c
-	lprintf(L_NOISE, "lock made");
67aff5c
 	(void) unlink(temp);
67aff5c
+#else
67aff5c
+        pid_t device_owner;
67aff5c
+
6f3650e
+        device_owner = dev_lock(lock);
67aff5c
+        if (device_owner < 0)
67aff5c
+        {
6f3650e
+                lprintf(L_ERROR, "lock not made: dev_lock(%s) failed", lock);
67aff5c
+                return FAIL;
67aff5c
+        }
67aff5c
+
67aff5c
+        if (device_owner > 0)
67aff5c
+        {
67aff5c
+                lprintf(L_MESG, "lock not made: lock file exists (pid=%d)", device_owner);
67aff5c
+                return FAIL;
67aff5c
+        }
67aff5c
+
67aff5c
+#endif
67aff5c
+
67aff5c
+        lprintf(L_NOISE, "lock made");
67aff5c
 	we_have_lock = TRUE;
67aff5c
 	return(SUCCESS);
67aff5c
 }
6f3650e
@@ -180,8 +203,8 @@ int makelock _P1( (device),
67aff5c
  */
67aff5c
 int steal_lock _P2((device, pid), char * device, int pid )
67aff5c
 {
67aff5c
-    int retcode, is_pid, fd;
67aff5c
-    
67aff5c
+    int retcode = SUCCESS;
67aff5c
+
67aff5c
     lprintf(L_NOISE, "steal_lock(%s) called", device);
67aff5c
 
6f3650e
     if ( get_lock_name( lock, device ) == NULL )
702687f
@@ -190,6 +213,9 @@ int steal_lock _P2((device, pid), char * device, int pid )
6f3650e
 	return FAIL;
6f3650e
     }
6f3650e
 
67aff5c
+#ifndef HAVE_LOCKDEV
67aff5c
+    int is_pid, fd;
6f3650e
+
6f3650e
     is_pid = readlock(lock);
6f3650e
 
6f3650e
     if ( is_pid != pid )
702687f
@@ -208,6 +234,13 @@ int steal_lock _P2((device, pid), char * device, int pid )
67aff5c
     }
67aff5c
 
67aff5c
     retcode = lock_write_pid( fd );
67aff5c
+#else
6f3650e
+    if (dev_relock(lock, pid) < 0)
67aff5c
+    {
67aff5c
+            lprintf( L_ERROR, "can't steal lock: dev_relock(%s, %d) failed", device, pid);
67aff5c
+            retcode = FAIL;
67aff5c
+    }
67aff5c
+#endif
67aff5c
 
67aff5c
     if ( retcode == SUCCESS ) we_have_lock = TRUE;
67aff5c
     return retcode;
702687f
@@ -237,7 +270,6 @@ int makelock_file _P1( (file), char * file )
67aff5c
 int checklock _P1( (device), char * device)
67aff5c
 {
67aff5c
     int pid;
6f3650e
-    struct stat st;
67aff5c
     char name[MAXLINE+1];
67aff5c
     
6f3650e
     if ( get_lock_name( name, device ) == NULL )
702687f
@@ -246,6 +278,9 @@ int checklock _P1( (device), char * device)
6f3650e
 	return NO_LOCK;
6f3650e
     }
6f3650e
 
6f3650e
+#ifndef HAVE_LOCKDEV
6f3650e
+    struct stat st;
6f3650e
+
6f3650e
     if ((stat(name, &st) == FAIL) && errno == ENOENT)
6f3650e
     {
6f3650e
 	lprintf(L_NOISE, "checklock: stat failed, no file");
702687f
@@ -270,12 +305,27 @@ int checklock _P1( (device), char * device)
67aff5c
 	(void) unlink(name);
67aff5c
 	return NO_LOCK;
67aff5c
     }
67aff5c
-    
67aff5c
+#else
6f3650e
+    pid = dev_testlock(name);
67aff5c
+
67aff5c
+    if (pid < 0)
67aff5c
+    {
6f3650e
+            lprintf(L_ERROR, "checklock: can't check lockfile, dev_testlock(%s) failed", name);
67aff5c
+            return NO_LOCK;
67aff5c
+    }
67aff5c
+
67aff5c
+    if (pid == 0)
67aff5c
+    {
67aff5c
+            lprintf(L_MESG, "checklock: device not locked");
67aff5c
+            return NO_LOCK;
67aff5c
+    }
67aff5c
+#endif
67aff5c
     lprintf(L_NOISE, "lockfile found, pid=%d", pid );
67aff5c
     
67aff5c
     return pid;
6f3650e
 }
6f3650e
 
6f3650e
+#ifndef HAVE_LOCKDEV
6f3650e
 /*
6f3650e
  *	readlock() - read contents of lockfile
6f3650e
  *
702687f
@@ -342,6 +392,8 @@ static int lock_write_pid _P1((fd), int fd)
6f3650e
     close(fd);
6f3650e
     return SUCCESS;
6f3650e
 }
6f3650e
+
6f3650e
+#endif
6f3650e
 	
6f3650e
 /*
6f3650e
  *	rmlocks() - remove lockfile
6f3650e
@@ -352,8 +404,13 @@ void rmlocks _P0(void)
67aff5c
     if ( we_have_lock )
67aff5c
     {
67aff5c
 	lprintf( L_NOISE, "removing lock file" );
67aff5c
+#ifndef HAVE_LOCKDEV
67aff5c
 	if ( unlink(lock) == -1 )
67aff5c
 	    lprintf( L_ERROR, "error removing lock file (huh?!)" );
67aff5c
+#else
6f3650e
+        if (dev_unlock(lock, getpid()) < 0)
6f3650e
+             lprintf(L_ERROR, "error removing lock file, dev_unlock(%s) failed", lock);
67aff5c
+#endif
67aff5c
     }
67aff5c
     /* mark lock file as 'not set' */
67aff5c
     we_have_lock = FALSE;
6f3650e
@@ -364,7 +421,21 @@ void rmlocks _P0(void)
6f3650e
  * determine full path + name of the lock file for a given device
6f3650e
  */
67aff5c
 
6f3650e
-#ifdef SVR4
67aff5c
+#ifdef HAVE_LOCKDEV
6f3650e
+/*
6f3650e
+ * lockdev API provides us with the way to lock device directly, thus lock name
6f3650e
+ * and device name are the same
6f3650e
+ */
6f3650e
+static char *get_lock_name _P2( (lock, device), char *lock, char *device )
6f3650e
+{
6f3650e
+        lprintf(L_NOISE, "get_lock_name(%s) called", device);
6f3650e
+
6f3650e
+        snprintf(lock, MAXLINE, "%s", device);
6f3650e
+        
6f3650e
+        return lock;
6f3650e
+}
67aff5c
+
6f3650e
+#elif SVR4
6f3650e
 
6f3650e
 /*
6f3650e
  * get_lock_name() - create SVR4 lock file name (Bodo Bauer)
702687f
diff --git a/mgetty.h b/mgetty.h
702687f
index 64053fd..c6ddd58 100644
702687f
--- a/mgetty.h
702687f
+++ b/mgetty.h
702687f
@@ -277,6 +277,9 @@ int		makelock_file _PROTO(( char * lockname ));
67aff5c
 int		checklock _PROTO((char * device));
67aff5c
 void		rmlocks _PROTO ((void));
67aff5c
 int		steal_lock _PROTO((char * device, int pid ));
67aff5c
+#ifdef HAVE_LOCKDEV
67aff5c
+extern char lockdev_device[MAXLINE + 1];
67aff5c
+#endif
67aff5c
   
67aff5c
 /* fax stuff */
67aff5c
 void	faxrec _PROTO(( char * spool_dir, unsigned int switchbd,