[hackers] [skvm] When a drive is cold ejected, make sure to umount the device || Dimitrios Papastamos

From: <hg_AT_suckless.org>
Date: Wed, 13 May 2009 18:56:14 +0000 (UTC)

changeset: 11:86c788e5d212
tag: tip
user: Dimitrios Papastamos <stateless [at] archlinux.us>
date: Wed May 13 19:56:07 2009 -0700
files: README skvm.c
description:
When a drive is cold ejected, make sure to umount the device
and remove the mount point.

diff -r 99dfa9d53535 -r 86c788e5d212 README
--- a/README Sat May 09 16:16:33 2009 -0700
+++ b/README Wed May 13 19:56:07 2009 -0700
@@ -21,3 +21,7 @@
 * sudo /etc/rc.d/skvm start
 * sudo /etc/rc.d/skvm restart
 * sudo /etc/rc.d/skvm stop
+
+If you want to unmount the drives, you will have to be root. If you
+cold eject a drive, skvm will unmount it and delete the mountpoint.
+That is probably not a good idea.
diff -r 99dfa9d53535 -r 86c788e5d212 skvm.c
--- a/skvm.c Sat May 09 16:16:33 2009 -0700
+++ b/skvm.c Wed May 13 19:56:07 2009 -0700
@@ -30,7 +30,13 @@
  */
 
 /* feel free to alter these to your liking */
+
+/* NOTE: you may want to use the sync mount option if you plan
+ * to cold eject your usb drives. Even though the side-effects
+ * are probably not worth it.
+ */
 #define MOUNT_CMD_PATH "/bin/mount"
+#define UMOUNT_CMD_PATH "/bin/umount"
 #define DEFAULT_MNT_OPTIONS "noexec,nosuid"
 #define BASE_MNT_DIR "/media/"
 
@@ -49,6 +55,18 @@
       p1->volume = p2->volume; \
       p1->drive = p2->drive; \
    } while (0)
+#define EXEC_CMD(args) \
+ g_spawn_sync( \
+ "/", \
+ (args), \
+ (gchar **)NULL, \
+ 0, \
+ (GSpawnChildSetupFunc)NULL, \
+ (gpointer)NULL, \
+ (gchar **)NULL, \
+ (gchar **)NULL, \
+ (gint *)NULL, \
+ (GError **)NULL)
 
 /* ============================
  * = Structs
@@ -121,6 +139,7 @@
 static void consider_fstab(struct device_t *device);
 static void do_args(struct device_t *device);
 static int do_mount(struct device_t *device);
+static int do_umount(const struct device_t *device);
 
 /* general function helpers */
 static inline int file_exists(const char *path);
@@ -316,6 +335,9 @@
       if (!strcmp(did, iter->did)) {
          mountp = iter->mountp;
          if (!file_exists(mountp)) {
+ if (!is_mounted(iter->dev))
+ if (do_umount(iter))
+ printf("FUCK ME\n");
             if (rmdir(mountp) < 0)
                syslog(LOG_WARNING, "%s:%d: %s", __FILE__, __LINE__,
                       strerror(errno));
@@ -568,6 +590,7 @@
    endmntent(fp);
 }
 
+/* TODO: might actually remove this one */
 static void
 do_args(struct device_t *device) {
    int i = 0;
@@ -582,7 +605,7 @@
    device->args[i++] = (!device->opt) ? DEFAULT_MNT_OPTIONS : device->opt;
    device->args[i++] = device->dev;
    device->args[i++] = device->mountp;
- device->args[i] = NULL;
+ device->args[i] = (char *)NULL;
 }
 
 static int
@@ -591,17 +614,18 @@
       return -1;
 
    do_args(device);
- return (g_spawn_sync(
- "/",
- device->args,
- (gchar **)NULL,
- 0,
- (GSpawnChildSetupFunc)NULL,
- (gpointer)NULL,
- (gchar **)NULL,
- (gchar **)NULL,
- (gint *)NULL,
- (GError **)NULL) ? 0 : -1);
+ return EXEC_CMD(device->args) ? 0 : -1;
+}
+
+static int
+do_umount(const struct device_t *device) {
+ if (!device)
+ return -1;
+
+ return EXEC_CMD(
+ ((char *[]) {
+ UMOUNT_CMD_PATH, device->dev, (char *)NULL
+ })) ? 0 : -1;
 }
 
 static inline int
Received on Wed May 13 2009 - 18:56:14 UTC

This archive was generated by hypermail 2.2.0 : Wed May 13 2009 - 19:00:06 UTC