Remove signal handling from LUKS keyencryption and simplify code.
[platform/upstream/cryptsetup.git] / lib / utils_loop.c
index a1ed960..2fc46cd 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * loopback block device utilities
  *
- * Copyright (C) 2011, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2011-2012, 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
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <string.h>
 #include <limits.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <linux/loop.h>
 
 #include "utils_loop.h"
 
+#define LOOP_DEV_MAJOR 7
+
+#ifndef LO_FLAGS_AUTOCLEAR
+#define LO_FLAGS_AUTOCLEAR 4
+#endif
+
 #ifndef LOOP_CTL_GET_FREE
-#define LOOP_CTL_GET_FREE      0x4C82
+#define LOOP_CTL_GET_FREE 0x4C82
 #endif
 
-char *crypt_loop_get_device(void)
+static char *crypt_loop_get_device_old(void)
 {
        char dev[20];
        int i, loop_fd;
@@ -60,8 +67,6 @@ char *crypt_loop_get_device(void)
        return NULL;
 }
 
-/* loop-control not yet upstream */
-#if 0
 char *crypt_loop_get_device(void)
 {
        char dev[64];
@@ -70,7 +75,7 @@ char *crypt_loop_get_device(void)
 
        loop_fd = open("/dev/loop-control", O_RDONLY);
        if (loop_fd < 0)
-               return NULL;
+               return crypt_loop_get_device_old();
 
        i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
        if (i < 0) {
@@ -79,14 +84,14 @@ char *crypt_loop_get_device(void)
        }
        close(loop_fd);
 
-       snprintf(dev, "/dev/loop%d", i, sizeof(dev));
+       if (sprintf(dev, "/dev/loop%d", i) < 0)
+               return NULL;
 
        if (stat(dev, &st) || !S_ISBLK(st.st_mode))
                return NULL;
 
        return strdup(dev);
 }
-#endif
 
 int crypt_loop_attach(const char *loop, const char *file, int offset,
                      int autoclear, int *readonly)
@@ -95,7 +100,7 @@ int crypt_loop_attach(const char *loop, const char *file, int offset,
        int loop_fd = -1, file_fd = -1, r = 1;
 
        file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
-       if (file_fd < 0 && errno == EROFS && !*readonly) {
+       if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
                *readonly = 1;
                file_fd = open(file, O_RDONLY | O_EXCL);
        }