[Lguest] [PATCH 2.6.23] lguest: LCA08 lguest tutorial - patch for ioctl and kill-via-ioctl (M-hioctl and S-hkill)

Mark Wallis lguest at markwallis.id.au
Fri Feb 1 02:04:01 EST 2008


ioctl handler for /dev/guest with support for killing the guest via  
ioctl.

Signed-off-by: Mark Wallis <lguest at markwallis.id.au>

--- Documentation/lguest/lguest.c.orig	2008-01-31 23:57:37.000000000  
+1100
+++ Documentation/lguest/lguest.c	2008-02-01 00:01:33.000000000 +1100
@@ -753,7 +753,7 @@
  				/* Just in case waker is blocked in BREAK, send
  				 * unbreak now. */
  				write(fd, args, sizeof(args));
-				exit(2);
+				ioctl(fd, LGUEST_IOCTL_LGKILL, -1);
  			}
  			abort->count = 0;
  		}
@@ -1326,7 +1326,7 @@
  	for (;;) {
  		u32 args[] = { LHREQ_BREAK, 0 };
  		unsigned long arr[2];
-		int readval;
+		int readval,writeval;

  		/* We read from the /dev/lguest device to run the Guest. */
  		readval = read(lguest_fd, arr, sizeof(arr));
@@ -1352,7 +1352,15 @@
  		/* Service input, then unset the BREAK which releases
  		 * the Waker. */
  		handle_input(lguest_fd, device_list);
-		if (write(lguest_fd, args, sizeof(args)) < 0)
+		writeval = write(lguest_fd, args, sizeof(args));
+
+		if (errno == ENOENT)
+		{
+                        char reason[1024] = { 0 };
+			read(lguest_fd, reason, sizeof(reason)-1);
+			errx(1, "%s", reason);
+		}
+		else if (writeval < 0)
  			err(1, "Resetting break");
  	}
  }
@@ -1507,6 +1595,12 @@
  	 * run the Guest until it tries to output something. */
  	waker_fd = setup_waker(lguest_fd, &device_list);

+	/* Test the ioctl handler to ensure it is alive */
+	if (ioctl(lguest_fd, -1, -1) != 0 && errno == ENOTTY)
+		printf("IOCTL handler test successful\n");
+	else
+		printf("IOCTL handler test failed\n");
+
  	/* Finally, run the Guest.  This doesn't return. */
  	run_guest(lguest_fd, &device_list);
  }
--- include/linux/lguest_launcher.h.orig	2008-02-01 01:31:50.000000000  
+1100
+++ include/linux/lguest_launcher.h		2008-02-01 00:37:16.000000000 +1100
@@ -9,6 +9,9 @@
  /* How many devices?  Assume each one wants up to two dma arrays per  
device. */
  #define LGUEST_MAX_DEVICES (LGUEST_MAX_DMA/2)

+/* lguest ioctl definitions */
+#define LGUEST_IOCTL_LGKILL 0x7601
+
  /*D:200
   * Lguest I/O
   *
--- drivers/lguest/lguest_user.c.orig	2008-01-31 23:05:09.000000000  
+1100
+++ drivers/lguest/lguest_user.c	2008-02-01 01:33:47.000000000 +1100
@@ -104,6 +104,27 @@
  	return 0;
  }

+static long lg_ioctl(struct file *file, unsigned int ioctl, unsigned  
long arg)
+{
+	struct lguest *lg = file->private_data;
+	char *reason;
+	int r = -ENOTTY;
+
+	switch (ioctl) {
+	case LGUEST_IOCTL_LGKILL: {
+		reason = kmalloc(18, GFP_KERNEL);
+		memcpy(reason, "killed by launcher", 18);
+		lg->dead = reason;
+		r = 0;
+		break;
+	}
+	default:
+		r = -ENOTTY;
+	}
+
+	return r;
+}
+
  /*L:040 Once our Guest is initialized, the Launcher makes it run by  
reading
   * from /dev/lguest. */
  static ssize_t read(struct file *file, char __user *user, size_t  
size,loff_t*o)
@@ -361,6 +382,8 @@
  	.release = close,
  	.write	 = write,
  	.read	 = read,
+	.unlocked_ioctl   = lg_ioctl,
+	.compat_ioctl     = lg_ioctl,
  };

  /* This is a textbook example of a "misc" character device.   
Populate a "struct




More information about the Lguest mailing list