Include 'sys/sysmacros.h' for GCC-9
[platform/upstream/cryptsetup.git] / lib / utils_loop.c
index 123154b..640d95b 100644 (file)
@@ -1,11 +1,13 @@
 /*
  * loopback block device utilities
  *
- * Copyright (C) 2011, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2011-2012, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2012, Milan Broz
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -14,7 +16,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>
+#ifdef HAVE_SYS_SYSMACROS_H
+#include <sys/sysmacros.h>     /* for major, minor */
+#endif
 
 #include "utils_loop.h"
 
@@ -43,13 +49,10 @@ static char *crypt_loop_get_device_old(void)
 {
        char dev[20];
        int i, loop_fd;
-       struct stat st;
        struct loop_info64 lo64 = {0};
 
        for (i = 0; i < 256; i++) {
                sprintf(dev, "/dev/loop%d", i);
-               if (stat(dev, &st) || !S_ISBLK(st.st_mode))
-                       return NULL;
 
                loop_fd = open(dev, O_RDONLY);
                if (loop_fd < 0)
@@ -99,7 +102,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);
        }