diff -drup magicdev-1.1.4.orig/daemon.c magicdev-1.1.4/daemon.c
--- magicdev-1.1.4.orig/daemon.c	2003-01-10 21:25:38.000000000 +0700
+++ magicdev-1.1.4/daemon.c	2003-08-14 20:01:40.000000000 +0700
@@ -23,7 +23,7 @@
 
 #include <gdk/gdkx.h>
 
-#undef MD_DEBUG
+#define MD_DEBUG
 #define MNTTYPE_ISO9660 "iso9660"
 #define MNTOPT_USER "user"
 #define MNTOPT_USERS "users"
@@ -36,13 +36,15 @@ typedef struct _DevInfo DevInfo;
 typedef struct _AppInfo AppInfo;
 
 typedef enum {
-        STATE_ACTIVE=0,
-        STATE_INACTIVE=1,
-        STATE_EMPTY=2,
-        STATE_LAST=3
+	STATE_BUSY=0,
+        STATE_ACTIVE=1,
+        STATE_INACTIVE=2,
+        STATE_EMPTY=3,
+        STATE_LAST=4
 } DevState;
 
 const char * const state_names[] = {
+	"BUSY",
         "ACTIVE",
         "INACTIVE",
         "EMPTY"
@@ -125,6 +127,8 @@ static gboolean app_device_is_mounted   
 static gboolean app_device_is_writer    (DevInfo *dev);
 static void     app_device_mount        (DevInfo *dev,
                                          AppInfo *ai);
+static void     app_device_umount        (DevInfo *dev,
+                                         AppInfo *ai);
 static void     app_device_run_cdplayer (DevInfo *dev,
                                          AppInfo *ai);
 static void     app_device_run_cdburner (DevInfo *dev,
@@ -133,6 +137,10 @@ static void     app_device_run_dvdplayer
 					 AppInfo *ai);
 static gboolean app_device_autorun      (DevInfo *dev,
                                          AppInfo *ai);
+static gboolean app_device_lock          (DevInfo *dev,
+                                         AppInfo *ai);
+static gboolean app_device_unlock        (DevInfo *dev,
+                                         AppInfo *ai);
 static void     app_device_ask_mixed    (DevInfo *dev,
                                          AppInfo *ai);
 
@@ -537,6 +545,7 @@ static gboolean
 mntent_has_fs_type (struct mntent *ent,
                     const gchar   *fstype)
 {
+	
         char **fstypes = g_strsplit (ent->mnt_type, ",", -1);
         int i;
         gboolean result = TRUE;
@@ -902,9 +911,12 @@ app_device_cdrom_set_state (DevInfo *dev
 
 end:
         if (dev->state != STATE_EMPTY) {
-                if (app_device_is_mounted (dev))
+                if (app_device_is_mounted (dev)) {
                         dev->state = STATE_ACTIVE;
-                else
+			#define CDROM_MOUNT_STATUS 0x5332
+			if (ioctl(dev->fd, CDROM_MOUNT_STATUS) > 1)
+				dev->state = STATE_BUSY;
+		} else
                         dev->state = STATE_INACTIVE;
         }
         
@@ -1009,11 +1021,60 @@ app_device_deactivate (DevInfo *dev,
                        AppInfo *ai)
 {
         /* Nothing to do here... */
-        dev->did_mount = FALSE;
+        /*dev->did_mount = FALSE;*/
+	if (app_device_is_mounted(dev))
+		app_device_umount(dev, ai);
 
         return TRUE;
 }
 
+static gboolean
+app_device_lock (DevInfo *dev,
+		 AppInfo *ai)
+{
+	g_message("locking");
+        switch (dev->type) {
+        case DEV_CDROM:
+		if (dev->fd < 0) {
+	        	dev->fd = open (dev->fsname, O_RDONLY);
+		}			
+		if (dev->fd >= 0) {
+			ioctl (dev->fd, CDROM_LOCKDOOR, 1);
+			close(dev->fd);
+			dev->fd = -1;
+		}
+                break;
+        default:
+                break;
+        }
+        return TRUE;
+}	
+
+static gboolean
+app_device_unlock (DevInfo *dev,
+		 AppInfo *ai)
+{
+	g_message("unlocking");
+        switch (dev->type) {
+	case DEV_CDROM:
+		if (dev->fd < 0) {
+	        	dev->fd = open (dev->fsname, O_RDONLY);
+		}
+		if (dev->fd >= 0) {
+			/* say to the kernel -- I'll manage locking myself */
+		        ioctl (dev->fd, CDROM_CLEAR_OPTIONS, CDO_LOCK);
+			/* then unlock the door */
+			ioctl (dev->fd, CDROM_LOCKDOOR, 0); 
+			close(dev->fd);
+			dev->fd = -1;
+		}
+                break;
+        default:
+                break;
+        }
+        return TRUE;
+}	
+
 typedef gboolean (*ChangeFunc) (DevInfo *dev, AppInfo *ai);
 
 static void
@@ -1022,11 +1083,12 @@ app_device_check_change (DevInfo *dev,
 {
         /* What functions to run for particular state transitions */
         static ChangeFunc state_transitions[STATE_LAST][STATE_LAST] = {
-                /************  from: ACTIVE                 INACTIVE                 EMPTY */
-                /* to */
-                /* ACTIVE */   {     NULL,                  app_device_autorun,      app_device_autorun  },
-                /* INACTIVE */ {     app_device_deactivate, NULL              ,      app_device_activate },
-                /* EMPTY */    {     app_device_deactivate, NULL              ,      NULL                }
+/************  from: BUSY,		    ACTIVE		     INACTIVE                 EMPTY */
+/* to */
+/* BUSY */     {     NULL,		    app_device_lock,	   app_device_lock,	    app_device_lock },
+/* ACTIVE */   {     app_device_unlock,	    NULL,                  app_device_autorun,      app_device_autorun  },
+/* INACTIVE */ {     app_device_deactivate, app_device_deactivate, NULL              ,      app_device_activate },
+/* EMPTY */    {     app_device_deactivate, app_device_deactivate, NULL              ,      NULL                }
         };
 
 
@@ -1193,6 +1255,24 @@ app_device_mount (DevInfo *dev,
                 gnome_execute_async (g_get_home_dir (), 2, argv);
                 dev->did_mount = TRUE;
         }
+	app_device_unlock(dev, ai); 
+	/* because we will manage to lock the drive only 
+	   when it is really in-use (busy) */
+}
+
+static void
+app_device_umount (DevInfo *dev,
+                  AppInfo *ai)
+{
+        char *argv[3];
+        
+        if (ai->do_automount == TRUE) {
+                argv[0] = "/bin/umount";
+                argv[1] = dev->fspath;
+                argv[2] = NULL;
+                gnome_execute_async (g_get_home_dir (), 2, argv);
+                dev->did_mount = FALSE;
+        }
 }
 
 static gboolean
@@ -1308,4 +1388,3 @@ app_device_is_writer (DevInfo *dev)
         else
                 return (drivetype & BURNER) != 0;
 }
-
