]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/loop-util.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / shared / loop-util.c
index 37b8479f8811ba05c14d5398d87e92c7af7b6155..bf426eb8bca4d2d3a65e13f8d3186ec4b5685182 100644 (file)
@@ -1,22 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2016 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
 
 #include <errno.h>
 #include <fcntl.h>
@@ -27,6 +9,7 @@
 #include "alloc-util.h"
 #include "fd-util.h"
 #include "loop-util.h"
+#include "stat-util.h"
 
 int loop_device_make(int fd, int open_flags, LoopDevice **ret) {
         const struct loop_info64 info = {
@@ -37,7 +20,7 @@ int loop_device_make(int fd, int open_flags, LoopDevice **ret) {
         _cleanup_free_ char *loopdev = NULL;
         struct stat st;
         LoopDevice *d;
-        int nr;
+        int nr, r;
 
         assert(fd >= 0);
         assert(ret);
@@ -62,15 +45,16 @@ int loop_device_make(int fd, int open_flags, LoopDevice **ret) {
                 *d = (LoopDevice) {
                         .fd = copy,
                         .nr = -1,
+                        .relinquished = true, /* It's not allocated by us, don't destroy it when this object is freed */
                 };
 
                 *ret = d;
-
-                return 0;
+                return d->fd;
         }
 
-        if (!S_ISREG(st.st_mode))
-                return -EINVAL;
+        r = stat_verify_regular(&st);
+        if (r < 0)
+                return r;
 
         control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
         if (control < 0)
@@ -98,17 +82,13 @@ int loop_device_make(int fd, int open_flags, LoopDevice **ret) {
                 return -ENOMEM;
 
         *d = (LoopDevice) {
-                .fd = loop,
-                .node = loopdev,
+                .fd = TAKE_FD(loop),
+                .node = TAKE_PTR(loopdev),
                 .nr = nr,
         };
 
-        loop = -1;
-        loopdev = NULL;
-
         *ret = d;
-
-        return (*ret)->fd;
+        return d->fd;
 }
 
 int loop_device_make_by_path(const char *path, int open_flags, LoopDevice **ret) {