[PATCH 3/4] Add doPwUmount() and mountCommandWrapper() to isys (#493333)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Turn doPwMount() in to a mount command wrapper.  We're already calling
mount(8) and capturing error messages so we can pass them back up to our
Python code, so the same for unmounting filesystems.
---
 isys/imount.c |  127 +++++++++++++++++++++++++++++++++++++--------------------
 isys/imount.h |    7 +++-
 2 files changed, 89 insertions(+), 45 deletions(-)

diff --git a/isys/imount.c b/isys/imount.c
index fd51fde..1579ed5 100644
--- a/isys/imount.c
+++ b/isys/imount.c
@@ -1,7 +1,7 @@
 /*
  * imount.c
  *
- * Copyright (C) 2007, 2008 Red Hat, Inc.  All rights reserved.
+ * Copyright (C) 2007, 2008, 2009  Red Hat, Inc.  All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -68,44 +68,58 @@ static int readFD(int fd, char **buf) {
     return filesize;
 }
 
-int doPwMount(char *dev, char *where, char *fs, char *options, char **err) {
+int mountCommandWrapper(int mode, char *dev, char *where, char *fs,
+                        char *options, char **err) {
     int rc, child, status, pipefd[2];
-    char *opts = NULL, *device;
+    char *opts = NULL, *device = NULL, *cmd = NULL;
     int programLogFD;
 
-    if (mkdirChain(where))
-        return IMOUNT_ERR_ERRNO;
+    if (mode == IMOUNT_MODE_MOUNT) {
+        cmd = "/bin/mount";
+    } else if (mode == IMOUNT_MODE_UMOUNT) {
+        cmd = "/bin/umount";
+    } else {
+        return IMOUNT_ERR_MODE;
+    }
 
-    if (strstr(fs, "nfs")) {
-        if (options) {
-            if (asprintf(&opts, "%s,nolock", options) == -1) {
-                fprintf(stderr, "%s: %d: %s\n", __func__, __LINE__,
-                        strerror(errno));
-                fflush(stderr);
-                abort();
+    if (mode == IMOUNT_MODE_MOUNT) {
+        if (mkdirChain(where))
+            return IMOUNT_ERR_ERRNO;
+
+        if (strstr(fs, "nfs")) {
+            if (options) {
+                if (asprintf(&opts, "%s,nolock", options) == -1) {
+                    fprintf(stderr, "%s: %d: %s\n", __func__, __LINE__,
+                            strerror(errno));
+                    fflush(stderr);
+                    abort();
+                }
+            } else {
+                opts = strdup("nolock");
             }
+
+            device = strdup(dev);
         } else {
-            opts = strdup("nolock");
-        }
-        device = strdup(dev);
-    } else {
-        if ((options && strstr(options, "bind") == NULL) && 
-            strncmp(dev, "LABEL=", 6) && strncmp(dev, "UUID=", 5) &&
-            *dev != '/') {
-           if (asprintf(&device, "/dev/%s", dev) == -1) {
-               fprintf(stderr, "%s: %d: %s\n", __func__, __LINE__,
-                       strerror(errno));
-               fflush(stderr);
-               abort();
-           }
-        } else {
-           device = strdup(dev);
+            if ((options && strstr(options, "bind") == NULL) && 
+                strncmp(dev, "LABEL=", 6) && strncmp(dev, "UUID=", 5) &&
+                *dev != '/') {
+               if (asprintf(&device, "/dev/%s", dev) == -1) {
+                   fprintf(stderr, "%s: %d: %s\n", __func__, __LINE__,
+                           strerror(errno));
+                   fflush(stderr);
+                   abort();
+               }
+            } else {
+               device = strdup(dev);
+            }
+
+            if (options)
+                opts = strdup(options);
         }
-        if (options)
-            opts = strdup(options);
     }
 
-    programLogFD = open("/tmp/program.log", O_APPEND|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+    programLogFD = open("/tmp/program.log",
+                        O_APPEND|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
 
     if (pipe(pipefd))
         return IMOUNT_ERR_ERRNO;
@@ -116,8 +130,9 @@ int doPwMount(char *dev, char *where, char *fs, char *options, char **err) {
         close(pipefd[0]);
 
         /* Close stdin entirely, redirect stdout to /tmp/program.log, and
-         * redirect stderr to a pipe so we can put error messages into exceptions.
-         * We'll only use these messages should mount also return an error code.
+         * redirect stderr to a pipe so we can put error messages into
+         * exceptions.  We'll only use these messages should mount also
+         * return an error code.
          */
         fd = open("/dev/tty5", O_RDONLY);
         close(STDIN_FILENO);
@@ -129,17 +144,25 @@ int doPwMount(char *dev, char *where, char *fs, char *options, char **err) {
 
         dup2(pipefd[1], STDERR_FILENO);
 
-        if (opts) {
-            fprintf(stdout, "Running... /bin/mount -n -t %s -o %s %s %s\n",
-                    fs, opts, device, where);
-            rc = execl("/bin/mount",
-                       "/bin/mount", "-n", "-t", fs, "-o", opts, device, where, NULL);
+        if (mode == IMOUNT_MODE_MOUNT) {
+            if (opts) {
+                fprintf(stdout, "Running... %s -n -t %s -o %s %s %s\n",
+                        cmd, fs, opts, device, where);
+                rc = execl(cmd, cmd,
+                           "-n", "-t", fs, "-o", opts, device, where, NULL);
+                exit(1);
+            } else {
+                fprintf(stdout, "Running... %s -n -t %s %s %s\n",
+                        cmd, fs, device, where);
+                rc = execl(cmd, cmd, "-n", "-t", fs, device, where, NULL);
+                exit(1);
+            }
+        } else if (mode == IMOUNT_MODE_UMOUNT) {
+            fprintf(stdout, "Running... %s %s\n", cmd, where);
+            rc = execl(cmd, cmd, where, NULL);
             exit(1);
-        }
-        else {
-            fprintf(stdout, "Running... /bin/mount -n -t %s %s %s\n",
-                    fs, device, where);
-            rc = execl("/bin/mount", "/bin/mount", "-n", "-t", fs, device, where, NULL);
+        } else {
+            fprintf(stdout, "Running... Unknown imount mode: %d\n", mode);
             exit(1);
         }
     }
@@ -158,14 +181,30 @@ int doPwMount(char *dev, char *where, char *fs, char *options, char **err) {
 
     close(programLogFD);
 
-    free(opts);
-    free(device);
+    if (opts) {
+        free(opts);
+    }
+
+    if (device) {
+        free(device);
+    }
+
     if (!WIFEXITED(status) || (WIFEXITED(status) && WEXITSTATUS(status)))
        return IMOUNT_ERR_OTHER;
 
     return 0;
 }
 
+int doPwMount(char *dev, char *where, char *fs, char *options, char **err) {
+    return mountCommandWrapper(IMOUNT_MODE_MOUNT,
+                               dev, where, fs, options, err);
+}
+
+int doPwUmount(char *where, char **err) {
+    return mountCommandWrapper(IMOUNT_MODE_UMOUNT,
+                               NULL, where, NULL, NULL, err);
+}
+
 int mkdirChain(char * origChain) {
     char * chain;
     char * chptr;
diff --git a/isys/imount.h b/isys/imount.h
index 9f9c425..95cca38 100644
--- a/isys/imount.h
+++ b/isys/imount.h
@@ -1,7 +1,7 @@
 /*
  * imount.h
  *
- * Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
+ * Copyright (C) 2007, 2008, 2009  Red Hat, Inc.  All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -22,6 +22,7 @@
 
 #define IMOUNT_ERR_ERRNO	1
 #define IMOUNT_ERR_OTHER	2
+#define IMOUNT_ERR_MODE		3
 
 #include <sys/mount.h>		/* for umount() */
 
@@ -29,7 +30,11 @@
 #define IMOUNT_BIND    2
 #define IMOUNT_REMOUNT 4
 
+#define IMOUNT_MODE_MOUNT  1
+#define IMOUNT_MODE_UMOUNT 2
+
 int doPwMount(char *dev, char *where, char *fs, char *options, char **err);
+int doPwUmount(char *where, char **err);
 int mkdirChain(char * origChain);
 
 #endif
-- 
1.6.2.2

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux