]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: split out dev_t related calls into new devno-util.[ch]
authorLennart Poettering <lennart@poettering.net>
Wed, 13 Apr 2022 13:38:21 +0000 (15:38 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 13 Apr 2022 14:26:31 +0000 (16:26 +0200)
No actual code changes, just splitting out of some dev_t handling
related calls from stat-util.[ch], they are quite a number already, and
deserve their own module now I think.

Also, try to settle on the name "devnum" as the name for the concept,
instead of "devno" or "dev" or "devid". "devnum" is the name exported in
udev APIs, hence probably best to stick to that. (this just renames a
few symbols to "devum", local variables are left untouched, to make the
patch not too invasive)

No actual code changes.

39 files changed:
src/basic/devnum-util.c [new file with mode: 0644]
src/basic/devnum-util.h [new file with mode: 0644]
src/basic/meson.build
src/basic/parse-util.c
src/basic/parse-util.h
src/basic/stat-util.c
src/basic/stat-util.h
src/basic/terminal-util.c
src/boot/bless-boot.c
src/boot/bootctl.c
src/core/bpf-devices.c
src/core/cgroup.c
src/gpt-auto-generator/gpt-auto-generator.c
src/home/homed-manager.c
src/home/homework-luks.c
src/home/homework-quota.c
src/libsystemd/sd-device/sd-device.c
src/login/logind-session-dbus.c
src/login/logind-session.c
src/nspawn/nspawn-oci.c
src/partition/growfs.c
src/partition/repart.c
src/shared/blockdev-util.c
src/shared/bootspec.c
src/shared/find-esp.c
src/shared/loop-util.c
src/shared/quota-util.c
src/shared/quota-util.h
src/shared/sleep-config.c
src/sysext/sysext.c
src/sysupdate/sysupdate-resource.c
src/test/meson.build
src/test/test-devnum-util.c [new file with mode: 0644]
src/test/test-parse-util.c
src/test/test-stat-util.c
src/tmpfiles/tmpfiles.c
src/udev/udev-node.c
src/udev/udevadm-lock.c
src/volatile-root/volatile-root.c

diff --git a/src/basic/devnum-util.c b/src/basic/devnum-util.c
new file mode 100644 (file)
index 0000000..d13a87d
--- /dev/null
@@ -0,0 +1,133 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <string.h>
+#include <sys/stat.h>
+
+#include "chase-symlinks.h"
+#include "devnum-util.h"
+#include "parse-util.h"
+#include "path-util.h"
+#include "string-util.h"
+
+int parse_devnum(const char *s, dev_t *ret) {
+        const char *major;
+        unsigned x, y;
+        size_t n;
+        int r;
+
+        n = strspn(s, DIGITS);
+        if (n == 0)
+                return -EINVAL;
+        if (s[n] != ':')
+                return -EINVAL;
+
+        major = strndupa_safe(s, n);
+        r = safe_atou(major, &x);
+        if (r < 0)
+                return r;
+
+        r = safe_atou(s + n + 1, &y);
+        if (r < 0)
+                return r;
+
+        if (!DEVICE_MAJOR_VALID(x) || !DEVICE_MINOR_VALID(y))
+                return -ERANGE;
+
+        *ret = makedev(x, y);
+        return 0;
+}
+
+int device_path_make_major_minor(mode_t mode, dev_t devnum, char **ret) {
+        const char *t;
+
+        /* Generates the /dev/{char|block}/MAJOR:MINOR path for a dev_t */
+
+        if (S_ISCHR(mode))
+                t = "char";
+        else if (S_ISBLK(mode))
+                t = "block";
+        else
+                return -ENODEV;
+
+        if (asprintf(ret, "/dev/%s/%u:%u", t, major(devnum), minor(devnum)) < 0)
+                return -ENOMEM;
+
+        return 0;
+}
+
+int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret) {
+        _cleanup_free_ char *p = NULL;
+        int r;
+
+        /* Finds the canonical path for a device, i.e. resolves the /dev/{char|block}/MAJOR:MINOR path to the end. */
+
+        assert(ret);
+
+        if (major(devnum) == 0 && minor(devnum) == 0) {
+                char *s;
+
+                /* A special hack to make sure our 'inaccessible' device nodes work. They won't have symlinks in
+                 * /dev/block/ and /dev/char/, hence we handle them specially here. */
+
+                if (S_ISCHR(mode))
+                        s = strdup("/run/systemd/inaccessible/chr");
+                else if (S_ISBLK(mode))
+                        s = strdup("/run/systemd/inaccessible/blk");
+                else
+                        return -ENODEV;
+
+                if (!s)
+                        return -ENOMEM;
+
+                *ret = s;
+                return 0;
+        }
+
+        r = device_path_make_major_minor(mode, devnum, &p);
+        if (r < 0)
+                return r;
+
+        return chase_symlinks(p, NULL, 0, ret, NULL);
+}
+
+int device_path_parse_major_minor(const char *path, mode_t *ret_mode, dev_t *ret_devnum) {
+        mode_t mode;
+        dev_t devnum;
+        int r;
+
+        /* Tries to extract the major/minor directly from the device path if we can. Handles /dev/block/ and /dev/char/
+         * paths, as well out synthetic inaccessible device nodes. Never goes to disk. Returns -ENODEV if the device
+         * path cannot be parsed like this.  */
+
+        if (path_equal(path, "/run/systemd/inaccessible/chr")) {
+                mode = S_IFCHR;
+                devnum = makedev(0, 0);
+        } else if (path_equal(path, "/run/systemd/inaccessible/blk")) {
+                mode = S_IFBLK;
+                devnum = makedev(0, 0);
+        } else {
+                const char *w;
+
+                w = path_startswith(path, "/dev/block/");
+                if (w)
+                        mode = S_IFBLK;
+                else {
+                        w = path_startswith(path, "/dev/char/");
+                        if (!w)
+                                return -ENODEV;
+
+                        mode = S_IFCHR;
+                }
+
+                r = parse_devnum(w, &devnum);
+                if (r < 0)
+                        return r;
+        }
+
+        if (ret_mode)
+                *ret_mode = mode;
+        if (ret_devnum)
+                *ret_devnum = devnum;
+
+        return 0;
+}
diff --git a/src/basic/devnum-util.h b/src/basic/devnum-util.h
new file mode 100644 (file)
index 0000000..9632e99
--- /dev/null
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <inttypes.h>
+#include <stdbool.h>
+#include <sys/types.h>
+
+int parse_devnum(const char *s, dev_t *ret);
+
+/* glibc and the Linux kernel have different ideas about the major/minor size. These calls will check whether the
+ * specified major is valid by the Linux kernel's standards, not by glibc's. Linux has 20bits of minor, and 12 bits of
+ * major space. See MINORBITS in linux/kdev_t.h in the kernel sources. (If you wonder why we define _y here, instead of
+ * comparing directly >= 0: it's to trick out -Wtype-limits, which would otherwise complain if the type is unsigned, as
+ * such a test would be pointless in such a case.) */
+
+#define DEVICE_MAJOR_VALID(x)                                           \
+        ({                                                              \
+                typeof(x) _x = (x), _y = 0;                             \
+                _x >= _y && _x < (UINT32_C(1) << 12);                   \
+                                                                        \
+        })
+
+#define DEVICE_MINOR_VALID(x)                                           \
+        ({                                                              \
+                typeof(x) _x = (x), _y = 0;                             \
+                _x >= _y && _x < (UINT32_C(1) << 20);                   \
+        })
+
+int device_path_make_major_minor(mode_t mode, dev_t devnum, char **ret);
+int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret);
+int device_path_parse_major_minor(const char *path, mode_t *ret_mode, dev_t *ret_devnum);
+
+static inline bool devnum_set_and_equal(dev_t a, dev_t b) {
+        /* Returns true if a and b definitely refer to the same device. If either is zero, this means "don't
+         * know" and we'll return false */
+        return a == b && a != 0;
+}
index 92a4df20177608f16d2775c60153448aab005a9a..501fcf147faff11328d614e46254df0952568734 100644 (file)
@@ -32,6 +32,8 @@ basic_sources = files(
         'conf-files.c',
         'conf-files.h',
         'def.h',
+        'devnum-util.c',
+        'devnum-util.h',
         'dirent-util.c',
         'dirent-util.h',
         'dns-def.h',
index 222b2304cd05d353b346564507293d297b8001e0..35fbb5ec6adc7b4988aec5546c8f0352dee51f9e 100644 (file)
@@ -692,34 +692,6 @@ int parse_ip_prefix_length(const char *s, int *ret) {
         return 0;
 }
 
-int parse_dev(const char *s, dev_t *ret) {
-        const char *major;
-        unsigned x, y;
-        size_t n;
-        int r;
-
-        n = strspn(s, DIGITS);
-        if (n == 0)
-                return -EINVAL;
-        if (s[n] != ':')
-                return -EINVAL;
-
-        major = strndupa_safe(s, n);
-        r = safe_atou(major, &x);
-        if (r < 0)
-                return r;
-
-        r = safe_atou(s + n + 1, &y);
-        if (r < 0)
-                return r;
-
-        if (!DEVICE_MAJOR_VALID(x) || !DEVICE_MINOR_VALID(y))
-                return -ERANGE;
-
-        *ret = makedev(x, y);
-        return 0;
-}
-
 int parse_oom_score_adjust(const char *s, int *ret) {
         int r, v;
 
index 8273124626f2082f766687afee444ad5a82a6972..f2222dcffb09d86356345e772ae54fbfb90be26d 100644 (file)
@@ -12,7 +12,6 @@
 typedef unsigned long loadavg_t;
 
 int parse_boolean(const char *v) _pure_;
-int parse_dev(const char *s, dev_t *ret);
 int parse_pid(const char *s, pid_t* ret_pid);
 int parse_mode(const char *s, mode_t *ret);
 int parse_ifindex(const char *s);
index b25cabc6b48bc7597b4fa8036cbed2f10259fd2c..c7293db6989b04de9d41ebd5675ea6daf8c1396d 100644 (file)
@@ -314,101 +314,6 @@ int fd_verify_directory(int fd) {
         return stat_verify_directory(&st);
 }
 
-int device_path_make_major_minor(mode_t mode, dev_t devno, char **ret) {
-        const char *t;
-
-        /* Generates the /dev/{char|block}/MAJOR:MINOR path for a dev_t */
-
-        if (S_ISCHR(mode))
-                t = "char";
-        else if (S_ISBLK(mode))
-                t = "block";
-        else
-                return -ENODEV;
-
-        if (asprintf(ret, "/dev/%s/%u:%u", t, major(devno), minor(devno)) < 0)
-                return -ENOMEM;
-
-        return 0;
-}
-
-int device_path_make_canonical(mode_t mode, dev_t devno, char **ret) {
-        _cleanup_free_ char *p = NULL;
-        int r;
-
-        /* Finds the canonical path for a device, i.e. resolves the /dev/{char|block}/MAJOR:MINOR path to the end. */
-
-        assert(ret);
-
-        if (major(devno) == 0 && minor(devno) == 0) {
-                char *s;
-
-                /* A special hack to make sure our 'inaccessible' device nodes work. They won't have symlinks in
-                 * /dev/block/ and /dev/char/, hence we handle them specially here. */
-
-                if (S_ISCHR(mode))
-                        s = strdup("/run/systemd/inaccessible/chr");
-                else if (S_ISBLK(mode))
-                        s = strdup("/run/systemd/inaccessible/blk");
-                else
-                        return -ENODEV;
-
-                if (!s)
-                        return -ENOMEM;
-
-                *ret = s;
-                return 0;
-        }
-
-        r = device_path_make_major_minor(mode, devno, &p);
-        if (r < 0)
-                return r;
-
-        return chase_symlinks(p, NULL, 0, ret, NULL);
-}
-
-int device_path_parse_major_minor(const char *path, mode_t *ret_mode, dev_t *ret_devno) {
-        mode_t mode;
-        dev_t devno;
-        int r;
-
-        /* Tries to extract the major/minor directly from the device path if we can. Handles /dev/block/ and /dev/char/
-         * paths, as well out synthetic inaccessible device nodes. Never goes to disk. Returns -ENODEV if the device
-         * path cannot be parsed like this.  */
-
-        if (path_equal(path, "/run/systemd/inaccessible/chr")) {
-                mode = S_IFCHR;
-                devno = makedev(0, 0);
-        } else if (path_equal(path, "/run/systemd/inaccessible/blk")) {
-                mode = S_IFBLK;
-                devno = makedev(0, 0);
-        } else {
-                const char *w;
-
-                w = path_startswith(path, "/dev/block/");
-                if (w)
-                        mode = S_IFBLK;
-                else {
-                        w = path_startswith(path, "/dev/char/");
-                        if (!w)
-                                return -ENODEV;
-
-                        mode = S_IFCHR;
-                }
-
-                r = parse_dev(w, &devno);
-                if (r < 0)
-                        return r;
-        }
-
-        if (ret_mode)
-                *ret_mode = mode;
-        if (ret_devno)
-                *ret_devno = devno;
-
-        return 0;
-}
-
 int proc_mounted(void) {
         int r;
 
index 37513a43e77b7b0e4f8b5fb95905a2e1d684fe87..4483ceb7de7665f7f751e4ca046b47082d9ea18b 100644 (file)
@@ -71,29 +71,6 @@ int fd_verify_regular(int fd);
 int stat_verify_directory(const struct stat *st);
 int fd_verify_directory(int fd);
 
-/* glibc and the Linux kernel have different ideas about the major/minor size. These calls will check whether the
- * specified major is valid by the Linux kernel's standards, not by glibc's. Linux has 20bits of minor, and 12 bits of
- * major space. See MINORBITS in linux/kdev_t.h in the kernel sources. (If you wonder why we define _y here, instead of
- * comparing directly >= 0: it's to trick out -Wtype-limits, which would otherwise complain if the type is unsigned, as
- * such a test would be pointless in such a case.) */
-
-#define DEVICE_MAJOR_VALID(x)                                           \
-        ({                                                              \
-                typeof(x) _x = (x), _y = 0;                             \
-                _x >= _y && _x < (UINT32_C(1) << 12);                   \
-                                                                        \
-        })
-
-#define DEVICE_MINOR_VALID(x)                                           \
-        ({                                                              \
-                typeof(x) _x = (x), _y = 0;                             \
-                _x >= _y && _x < (UINT32_C(1) << 20);                   \
-        })
-
-int device_path_make_major_minor(mode_t mode, dev_t devno, char **ret);
-int device_path_make_canonical(mode_t mode, dev_t devno, char **ret);
-int device_path_parse_major_minor(const char *path, mode_t *ret_mode, dev_t *ret_devno);
-
 int proc_mounted(void);
 
 bool stat_inode_same(const struct stat *a, const struct stat *b);
@@ -119,9 +96,3 @@ int statx_fallback(int dfd, const char *path, int flags, unsigned mask, struct s
                 struct new_statx nsx;           \
         } var
 #endif
-
-static inline bool devid_set_and_equal(dev_t a, dev_t b) {
-        /* Returns true if a and b definitely refer to the same device. If either is zero, this means "don't
-         * know" and we'll return false */
-        return a == b && a != 0;
-}
index 6119f21c1b117f33a0c26658a5bcef8fd503c4e7..7cf74e97d0a7f5f15460c179d0feaf25079a11a2 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "alloc-util.h"
 #include "def.h"
+#include "devnum-util.h"
 #include "env-util.h"
 #include "fd-util.h"
 #include "fileio.h"
index c8bc205d1b03dae8aae232f65c5f01b2f081eb5f..d9c901d73bb2cc8eae76c949a948415725f85108 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "alloc-util.h"
 #include "bootspec.h"
+#include "devnum-util.h"
 #include "efi-api.h"
 #include "efi-loader.h"
 #include "efivars.h"
@@ -16,7 +17,6 @@
 #include "parse-util.h"
 #include "path-util.h"
 #include "pretty-print.h"
-#include "stat-util.h"
 #include "sync-util.h"
 #include "terminal-util.h"
 #include "util.h"
@@ -121,7 +121,7 @@ static int acquire_path(void) {
                                        "Couldn't find $BOOT partition. It is recommended to mount it to /boot.\n"
                                        "Alternatively, use --path= to specify path to mount point.");
 
-        if (esp_path && xbootldr_path && !devid_set_and_equal(esp_devid, xbootldr_devid)) /* in case the two paths refer to the same inode, suppress one */
+        if (esp_path && xbootldr_path && !devnum_set_and_equal(esp_devid, xbootldr_devid)) /* in case the two paths refer to the same inode, suppress one */
                 a = strv_new(esp_path, xbootldr_path);
         else if (esp_path)
                 a = strv_new(esp_path);
index a393370f8d413021b5e2122ac2480458fd6e0f25..eac071bcc637bf5f371971dd2c4646138c43126d 100644 (file)
@@ -16,6 +16,7 @@
 #include "blkid-util.h"
 #include "bootspec.h"
 #include "copy.h"
+#include "devnum-util.h"
 #include "dirent-util.h"
 #include "efi-api.h"
 #include "efi-loader.h"
@@ -595,7 +596,7 @@ static int boot_config_load_and_select(
 
         /* If XBOOTLDR and ESP actually refer to the same block device, suppress XBOOTLDR, since it would
          * find the same entries twice. */
-        bool same = esp_path && xbootldr_path && devid_set_and_equal(esp_devid, xbootldr_devid);
+        bool same = esp_path && xbootldr_path && devnum_set_and_equal(esp_devid, xbootldr_devid);
 
         r = boot_config_load(config, esp_path, same ? NULL : xbootldr_path);
         if (r < 0)
index 46996d28f4e7054384d98b5ab8e4ff62501e353f..3af9e78a1e32f5e9f2987d115da3002cb0b77d92 100644 (file)
@@ -5,12 +5,12 @@
 
 #include "bpf-devices.h"
 #include "bpf-program.h"
+#include "devnum-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "nulstr-util.h"
 #include "parse-util.h"
 #include "path-util.h"
-#include "stat-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
 
index 2e2dfcb2aab2953c4ef216003de570ebdcdaf94a..d3b617dc1a39aa61ce0a350d7e65439ec31e15d2 100644 (file)
@@ -16,6 +16,7 @@
 #include "cgroup-setup.h"
 #include "cgroup-util.h"
 #include "cgroup.h"
+#include "devnum-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "in-addr-prefix-util.h"
@@ -31,7 +32,6 @@
 #include "procfs-util.h"
 #include "restrict-ifaces.h"
 #include "special.h"
-#include "stat-util.h"
 #include "stdio-util.h"
 #include "string-table.h"
 #include "string-util.h"
index 4731687b7f8f4da172325f52fad83bd13ed15a6b..8b35b2364d33a236be86f0b98f86043d5647ea16 100644 (file)
@@ -12,6 +12,7 @@
 #include "blockdev-util.h"
 #include "btrfs-util.h"
 #include "device-util.h"
+#include "devnum-util.h"
 #include "dirent-util.h"
 #include "dissect-image.h"
 #include "dropin.h"
index c2b9eb64f9bd0d23b6475e1d688842c74ef8ae0e..a0253c34c1d3a605edd335ecc537301206071164 100644 (file)
@@ -527,7 +527,7 @@ static int search_quota(uid_t uid, const char *exclude_quota_path) {
 
                 previous_devno = st.st_dev;
 
-                r = quotactl_devno(QCMD_FIXED(Q_GETQUOTA, USRQUOTA), st.st_dev, uid, &req);
+                r = quotactl_devnum(QCMD_FIXED(Q_GETQUOTA, USRQUOTA), st.st_dev, uid, &req);
                 if (r < 0) {
                         if (ERRNO_IS_NOT_SUPPORTED(r))
                                 log_debug_errno(r, "No UID quota support on %s, ignoring.", where);
index 5416d12fcf984e5456701794adfff80fb0958f7d..9cfb0e7ae6118531d5676a5c2a54ce8a84fa828f 100644 (file)
@@ -19,6 +19,7 @@
 #include "blockdev-util.h"
 #include "btrfs-util.h"
 #include "chattr-util.h"
+#include "devnum-util.h"
 #include "dm-util.h"
 #include "env-util.h"
 #include "errno-util.h"
@@ -46,7 +47,6 @@
 #include "process-util.h"
 #include "random-util.h"
 #include "resize-fs.h"
-#include "stat-util.h"
 #include "strv.h"
 #include "sync-util.h"
 #include "tmpfile-util.h"
index 7001870dfb85ac9d1b9b1b9202b804336e988665..574d1556af18ea03298d4032ee61df99255894c9 100644 (file)
@@ -54,7 +54,7 @@ int home_update_quota_classic(UserRecord *h, const char *path) {
         if (devno == 0)
                 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system %s not backed by a block device.", path);
 
-        r = quotactl_devno(QCMD_FIXED(Q_GETQUOTA, USRQUOTA), devno, h->uid, &req);
+        r = quotactl_devnum(QCMD_FIXED(Q_GETQUOTA, USRQUOTA), devno, h->uid, &req);
         if (r < 0) {
                 if (ERRNO_IS_NOT_SUPPORTED(r))
                         return log_error_errno(r, "No UID quota support on %s.", path);
@@ -74,7 +74,7 @@ int home_update_quota_classic(UserRecord *h, const char *path) {
         req.dqb_valid = QIF_BLIMITS;
         req.dqb_bsoftlimit = req.dqb_bhardlimit = h->disk_size / QIF_DQBLKSIZE;
 
-        r = quotactl_devno(QCMD_FIXED(Q_SETQUOTA, USRQUOTA), devno, h->uid, &req);
+        r = quotactl_devnum(QCMD_FIXED(Q_SETQUOTA, USRQUOTA), devno, h->uid, &req);
         if (r < 0) {
                 if (r == -ESRCH)
                         return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), "UID quota not available on %s.", path);
index d31526fc222071e3429c3b0f8eb64c8b3458726d..22f16b9edc10193fc9b9380b969ddad4a4d0d577 100644 (file)
@@ -12,6 +12,7 @@
 #include "device-internal.h"
 #include "device-private.h"
 #include "device-util.h"
+#include "devnum-util.h"
 #include "dirent-util.h"
 #include "fd-util.h"
 #include "fileio.h"
@@ -25,7 +26,6 @@
 #include "path-util.h"
 #include "set.h"
 #include "socket-util.h"
-#include "stat-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
@@ -786,7 +786,7 @@ _public_ int sd_device_new_from_device_id(sd_device **ret, const char *id) {
                 if (isempty(id))
                         return -EINVAL;
 
-                r = parse_dev(id + 1, &devt);
+                r = parse_devnum(id + 1, &devt);
                 if (r < 0)
                         return r;
 
index ff4cd0a631bf9fc19302f17011244ae1497d8932..7d46759822bf83b7baa8fdf8d8f09ff0d8ec9f90 100644 (file)
@@ -8,6 +8,7 @@
 #include "bus-label.h"
 #include "bus-polkit.h"
 #include "bus-util.h"
+#include "devnum-util.h"
 #include "fd-util.h"
 #include "logind-brightness.h"
 #include "logind-dbus.h"
@@ -21,7 +22,6 @@
 #include "missing_capability.h"
 #include "path-util.h"
 #include "signal-util.h"
-#include "stat-util.h"
 #include "strv.h"
 #include "user-util.h"
 #include "util.h"
index eef48c252753bd2d5c8ed7b81421df59366b9f0d..4995e5885aae24c9aace1254759f3534d28e557d 100644 (file)
@@ -15,6 +15,7 @@
 #include "audit-util.h"
 #include "bus-error.h"
 #include "bus-util.h"
+#include "devnum-util.h"
 #include "env-file.h"
 #include "escape.h"
 #include "fd-util.h"
@@ -377,7 +378,7 @@ static int session_load_devices(Session *s, const char *devices) {
                         break;
                 }
 
-                k = parse_dev(word, &dev);
+                k = parse_devnum(word, &dev);
                 if (k < 0) {
                         r = k;
                         continue;
index 44564ba61959e2f25d0280cc916222ba97fb6032..86c014d25e7aae82d6e29e4ee434300139870feb 100644 (file)
@@ -8,6 +8,7 @@
 #include "bus-util.h"
 #include "cap-list.h"
 #include "cpu-set-util.h"
+#include "devnum-util.h"
 #include "env-util.h"
 #include "format-util.h"
 #include "fs-util.h"
@@ -20,7 +21,6 @@
 #if HAVE_SECCOMP
 #include "seccomp-util.h"
 #endif
-#include "stat-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
index 0d1da2773f065815b91f2b83fc509fc1a53eb8fc..31dcf0ffdf14f47c3f00bc85d816789e66994c32 100644 (file)
@@ -14,6 +14,7 @@
 #include "btrfs-util.h"
 #include "cryptsetup-util.h"
 #include "device-nodes.h"
+#include "devnum-util.h"
 #include "dissect-image.h"
 #include "escape.h"
 #include "fd-util.h"
index 34aa88198b78637adeb1385db382c6b2e72a18f1..051242e836c5e5a575c361b8bf7090371dfcfd91 100644 (file)
@@ -24,6 +24,7 @@
 #include "conf-parser.h"
 #include "cryptsetup-util.h"
 #include "def.h"
+#include "devnum-util.h"
 #include "dirent-util.h"
 #include "efivars.h"
 #include "errno-util.h"
@@ -55,7 +56,6 @@
 #include "resize-fs.h"
 #include "sort-util.h"
 #include "specifier.h"
-#include "stat-util.h"
 #include "stdio-util.h"
 #include "string-table.h"
 #include "string-util.h"
@@ -3885,7 +3885,7 @@ static int resolve_copy_blocks_auto(
                         if (r < 0)
                                 return log_error_errno(r, "Failed to read %s: %m", q);
 
-                        r = parse_dev(t, &sl);
+                        r = parse_devnum(t, &sl);
                         if (r < 0) {
                                 log_debug_errno(r, "Failed to parse %s, ignoring: %m", q);
                                 continue;
index a0c60be26e95ed398882410f16cd4d0083ee12ea..c3b90bb22734a664526f38a58ac6ef723a1336f9 100644 (file)
@@ -6,12 +6,12 @@
 #include "alloc-util.h"
 #include "blockdev-util.h"
 #include "btrfs-util.h"
+#include "devnum-util.h"
 #include "dirent-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "missing_magic.h"
 #include "parse-util.h"
-#include "stat-util.h"
 
 int block_get_whole_disk(dev_t d, dev_t *ret) {
         char p[SYS_BLOCK_PATH_MAX("/partition")];
@@ -44,7 +44,7 @@ int block_get_whole_disk(dev_t d, dev_t *ret) {
         if (r < 0)
                 return r;
 
-        r = parse_dev(s, &devt);
+        r = parse_devnum(s, &devt);
         if (r < 0)
                 return r;
 
@@ -170,7 +170,7 @@ int block_get_originating(dev_t dt, dev_t *ret) {
         if (r < 0)
                 return r;
 
-        r = parse_dev(t, &devt);
+        r = parse_devnum(t, &devt);
         if (r < 0)
                 return -EINVAL;
 
index 7016f3840e138defd3a2423553779ab38dbdb78a..91cb605fb1ce011aaa81bf4aa9a584d84a096e45 100644 (file)
@@ -5,6 +5,7 @@
 #include "bootspec.h"
 #include "bootspec-fundamental.h"
 #include "conf-files.h"
+#include "devnum-util.h"
 #include "dirent-util.h"
 #include "efi-loader.h"
 #include "env-file.h"
@@ -15,7 +16,6 @@
 #include "pe-header.h"
 #include "recurse-dir.h"
 #include "sort-util.h"
-#include "stat-util.h"
 #include "strv.h"
 #include "unaligned.h"
 
@@ -918,7 +918,7 @@ int boot_config_load_auto(
                 return r; /* It's fine if the XBOOTLDR partition doesn't exist, hence we ignore ENOKEY here */
 
         /* If both paths actually refer to the same inode, suppress the xbootldr path */
-        if (esp_where && xbootldr_where && devid_set_and_equal(esp_devid, xbootldr_devid))
+        if (esp_where && xbootldr_where && devnum_set_and_equal(esp_devid, xbootldr_devid))
                 xbootldr_where = mfree(xbootldr_where);
 
         return boot_config_load(config, esp_where, xbootldr_where);
index fca24329d4dcbdf93c812fc093072a540f3708c3..75e639dd99f5cf1693ed1997eeb1d633a4aa13a8 100644 (file)
@@ -1,11 +1,13 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <linux/magic.h>
+#include <sys/vfs.h>
 
 #include "sd-device.h"
 
 #include "alloc-util.h"
 #include "blkid-util.h"
+#include "devnum-util.h"
 #include "env-util.h"
 #include "errno-util.h"
 #include "find-esp.h"
index 39a9a766c010c52d240deaffec0d8996a799542b..530688fc97f0255eddc942b77e8793f4a4190142 100644 (file)
@@ -18,6 +18,7 @@
 #include "alloc-util.h"
 #include "blockdev-util.h"
 #include "device-util.h"
+#include "devnum-util.h"
 #include "env-util.h"
 #include "errno-util.h"
 #include "fd-util.h"
@@ -876,7 +877,7 @@ static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) {
         r = read_one_line_file(sysfs, &buffer);
         if (r < 0)
                 return r;
-        r = parse_dev(buffer, &devno);
+        r = parse_devnum(buffer, &devno);
         if (r < 0)
                 return r;
 
index fbf8ee50644cf91b2b23fec18fa6ff2248eb5f25..7aacad12048279335a9ef53a4fd4f3ad92c2af26 100644 (file)
@@ -1,20 +1,21 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <sys/quota.h>
+#include <sys/stat.h>
 
 #include "alloc-util.h"
 #include "blockdev-util.h"
+#include "devnum-util.h"
 #include "quota-util.h"
-#include "stat-util.h"
 
-int quotactl_devno(int cmd, dev_t devno, int id, void *addr) {
+int quotactl_devnum(int cmd, dev_t devnum, int id, void *addr) {
         _cleanup_free_ char *devnode = NULL;
         int r;
 
         /* Like quotactl() but takes a dev_t instead of a path to a device node, and fixes caddr_t → void*,
          * like we should, today */
 
-        r = device_path_make_major_minor(S_IFBLK, devno, &devnode);
+        r = device_path_make_major_minor(S_IFBLK, devnum, &devnode);
         if (r < 0)
                 return r;
 
@@ -37,5 +38,5 @@ int quotactl_path(int cmd, const char *path, int id, void *addr) {
         if (devno == 0) /* Doesn't have a block device */
                 return -ENODEV;
 
-        return quotactl_devno(cmd, devno, id, addr);
+        return quotactl_devnum(cmd, devno, id, addr);
 }
index a61bdcbae60ebffdd37714bf1dffcf84cbac99c1..fc395221ae68e448b4bfbe0303d0d1251adf7012 100644 (file)
@@ -15,5 +15,5 @@ static inline int QCMD_FIXED(uint32_t cmd, uint32_t type) {
         return (int) QCMD(cmd, type);
 }
 
-int quotactl_devno(int cmd, dev_t devno, int id, void *addr);
+int quotactl_devnum(int cmd, dev_t devnum, int id, void *addr);
 int quotactl_path(int cmd, const char *path, int id, void *addr);
index 8ec3d09a58314428a7e4531ae6e2d65613ae4772..a56f2ff618271cb3f6a507c6861553cb60792ef0 100644 (file)
 #include "btrfs-util.h"
 #include "conf-parser.h"
 #include "def.h"
+#include "devnum-util.h"
 #include "env-util.h"
 #include "errno-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "log.h"
 #include "macro.h"
-#include "parse-util.h"
 #include "path-util.h"
 #include "sleep-config.h"
 #include "stat-util.h"
@@ -274,7 +274,7 @@ static int read_resume_files(dev_t *ret_resume, uint64_t *ret_resume_offset) {
         if (r < 0)
                 return log_debug_errno(r, "Error reading /sys/power/resume: %m");
 
-        r = parse_dev(resume_str, &resume);
+        r = parse_devnum(resume_str, &resume);
         if (r < 0)
                 return log_debug_errno(r, "Error parsing /sys/power/resume device: %s: %m", resume_str);
 
index ccc0bd2687e43043bb40df7de6feabc2c27d8c88..20fbb916e9b48c22772f6f2fcf715f98b872fe30 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "capability-util.h"
 #include "chase-symlinks.h"
+#include "devnum-util.h"
 #include "discover-image.h"
 #include "dissect-image.h"
 #include "env-util.h"
@@ -31,7 +32,6 @@
 #include "pretty-print.h"
 #include "process-util.h"
 #include "sort-util.h"
-#include "stat-util.h"
 #include "terminal-util.h"
 #include "user-util.h"
 #include "verbs.h"
@@ -84,7 +84,7 @@ static int is_our_mount_point(const char *p) {
         if (r < 0)
                 return log_error_errno(r, "Failed to determine whether hierarchy '%s' contains '.systemd-sysext/dev': %m", p);
 
-        r = parse_dev(buf, &dev);
+        r = parse_devnum(buf, &dev);
         if (r < 0)
                 return log_error_errno(r, "Failed to parse device major/minor stored in '.systemd-sysext/dev' file on '%s': %m", p);
 
index 3df34cf7fb2db565709bd3266a9a306c1590ea08..edc524cc7596ae8593f0296a63e356cdcda45ab7 100644 (file)
@@ -7,6 +7,7 @@
 #include "alloc-util.h"
 #include "blockdev-util.h"
 #include "chase-symlinks.h"
+#include "devnum-util.h"
 #include "dirent-util.h"
 #include "env-util.h"
 #include "fd-util.h"
@@ -18,7 +19,6 @@
 #include "macro.h"
 #include "process-util.h"
 #include "sort-util.h"
-#include "stat-util.h"
 #include "string-table.h"
 #include "sysupdate-cache.h"
 #include "sysupdate-instance.h"
index 6e7fe1a3bb0649353ba7137cdf7d0f904bb520c5..c47dd71d197cb81c5dc02141905c7ad21102569a 100644 (file)
@@ -244,6 +244,8 @@ tests += [
 
         [files('test-stat-util.c')],
 
+        [files('test-devnum-util.c')],
+
         [files('test-os-util.c')],
 
         [files('test-libcrypt-util.c'),
diff --git a/src/test/test-devnum-util.c b/src/test/test-devnum-util.c
new file mode 100644 (file)
index 0000000..9cb9a8e
--- /dev/null
@@ -0,0 +1,109 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <sys/stat.h>
+
+#include "devnum-util.h"
+#include "path-util.h"
+#include "stat-util.h"
+#include "tests.h"
+
+TEST(parse_devnum) {
+        dev_t dev;
+
+        assert_se(parse_devnum("", &dev) == -EINVAL);
+        assert_se(parse_devnum("junk", &dev) == -EINVAL);
+        assert_se(parse_devnum("0", &dev) == -EINVAL);
+        assert_se(parse_devnum("5", &dev) == -EINVAL);
+        assert_se(parse_devnum("5:", &dev) == -EINVAL);
+        assert_se(parse_devnum(":5", &dev) == -EINVAL);
+        assert_se(parse_devnum("-1:-1", &dev) == -EINVAL);
+#if SIZEOF_DEV_T < 8
+        assert_se(parse_devnum("4294967295:4294967295", &dev) == -EINVAL);
+#endif
+        assert_se(parse_devnum("8:11", &dev) >= 0 && major(dev) == 8 && minor(dev) == 11);
+        assert_se(parse_devnum("0:0", &dev) >= 0 && major(dev) == 0 && minor(dev) == 0);
+}
+
+TEST(device_major_minor_valid) {
+        /* on glibc dev_t is 64bit, even though in the kernel it is only 32bit */
+        assert_cc(sizeof(dev_t) == sizeof(uint64_t));
+
+        assert_se(DEVICE_MAJOR_VALID(0U));
+        assert_se(DEVICE_MINOR_VALID(0U));
+
+        assert_se(DEVICE_MAJOR_VALID(1U));
+        assert_se(DEVICE_MINOR_VALID(1U));
+
+        assert_se(!DEVICE_MAJOR_VALID(-1U));
+        assert_se(!DEVICE_MINOR_VALID(-1U));
+
+        assert_se(DEVICE_MAJOR_VALID(1U << 10));
+        assert_se(DEVICE_MINOR_VALID(1U << 10));
+
+        assert_se(DEVICE_MAJOR_VALID((1U << 12) - 1));
+        assert_se(DEVICE_MINOR_VALID((1U << 20) - 1));
+
+        assert_se(!DEVICE_MAJOR_VALID((1U << 12)));
+        assert_se(!DEVICE_MINOR_VALID((1U << 20)));
+
+        assert_se(!DEVICE_MAJOR_VALID(1U << 25));
+        assert_se(!DEVICE_MINOR_VALID(1U << 25));
+
+        assert_se(!DEVICE_MAJOR_VALID(UINT32_MAX));
+        assert_se(!DEVICE_MINOR_VALID(UINT32_MAX));
+
+        assert_se(!DEVICE_MAJOR_VALID(UINT64_MAX));
+        assert_se(!DEVICE_MINOR_VALID(UINT64_MAX));
+
+        assert_se(DEVICE_MAJOR_VALID(major(0)));
+        assert_se(DEVICE_MINOR_VALID(minor(0)));
+}
+
+static void test_device_path_make_canonical_one(const char *path) {
+        _cleanup_free_ char *resolved = NULL, *raw = NULL;
+        struct stat st;
+        dev_t devno;
+        mode_t mode;
+        int r;
+
+        log_debug("> %s", path);
+
+        if (stat(path, &st) < 0) {
+                assert_se(errno == ENOENT);
+                log_notice("Path %s not found, skipping test", path);
+                return;
+        }
+
+        r = device_path_make_canonical(st.st_mode, st.st_rdev, &resolved);
+        if (r == -ENOENT) {
+                /* maybe /dev/char/x:y and /dev/block/x:y are missing in this test environment, because we
+                 * run in a container or so? */
+                log_notice("Device %s cannot be resolved, skipping test", path);
+                return;
+        }
+
+        assert_se(r >= 0);
+        assert_se(path_equal(path, resolved));
+
+        assert_se(device_path_make_major_minor(st.st_mode, st.st_rdev, &raw) >= 0);
+        assert_se(device_path_parse_major_minor(raw, &mode, &devno) >= 0);
+
+        assert_se(st.st_rdev == devno);
+        assert_se((st.st_mode & S_IFMT) == (mode & S_IFMT));
+}
+
+TEST(device_path_make_canonical) {
+        test_device_path_make_canonical_one("/dev/null");
+        test_device_path_make_canonical_one("/dev/zero");
+        test_device_path_make_canonical_one("/dev/full");
+        test_device_path_make_canonical_one("/dev/random");
+        test_device_path_make_canonical_one("/dev/urandom");
+        test_device_path_make_canonical_one("/dev/tty");
+
+        if (is_device_node("/run/systemd/inaccessible/blk") > 0) {
+                test_device_path_make_canonical_one("/run/systemd/inaccessible/chr");
+                test_device_path_make_canonical_one("/run/systemd/inaccessible/blk");
+        }
+}
+
+DEFINE_TEST_MAIN(LOG_INFO);
index daa547c793b9d510a7a096b5fe549bfc4c2ddd34..388d0fe3f7ef7c120289e8c4c02ed80432ef716e 100644 (file)
@@ -812,23 +812,6 @@ TEST(parse_nice) {
         assert_se(parse_nice("+20", &n) == -ERANGE);
 }
 
-TEST(parse_dev) {
-        dev_t dev;
-
-        assert_se(parse_dev("", &dev) == -EINVAL);
-        assert_se(parse_dev("junk", &dev) == -EINVAL);
-        assert_se(parse_dev("0", &dev) == -EINVAL);
-        assert_se(parse_dev("5", &dev) == -EINVAL);
-        assert_se(parse_dev("5:", &dev) == -EINVAL);
-        assert_se(parse_dev(":5", &dev) == -EINVAL);
-        assert_se(parse_dev("-1:-1", &dev) == -EINVAL);
-#if SIZEOF_DEV_T < 8
-        assert_se(parse_dev("4294967295:4294967295", &dev) == -EINVAL);
-#endif
-        assert_se(parse_dev("8:11", &dev) >= 0 && major(dev) == 8 && minor(dev) == 11);
-        assert_se(parse_dev("0:0", &dev) >= 0 && major(dev) == 0 && minor(dev) == 0);
-}
-
 TEST(parse_errno) {
         assert_se(parse_errno("EILSEQ") == EILSEQ);
         assert_se(parse_errno("EINVAL") == EINVAL);
index 9975a1848d254f1c4efbe11b95e84b00a38f0c7d..c5afde02d0c642df504699fb75ee03eb4d5a205a 100644 (file)
@@ -149,88 +149,6 @@ TEST(fd_is_ns) {
         assert_se(IN_SET(fd_is_ns(fd, CLONE_NEWNET), 1, -EUCLEAN));
 }
 
-TEST(device_major_minor_valid) {
-        /* on glibc dev_t is 64bit, even though in the kernel it is only 32bit */
-        assert_cc(sizeof(dev_t) == sizeof(uint64_t));
-
-        assert_se(DEVICE_MAJOR_VALID(0U));
-        assert_se(DEVICE_MINOR_VALID(0U));
-
-        assert_se(DEVICE_MAJOR_VALID(1U));
-        assert_se(DEVICE_MINOR_VALID(1U));
-
-        assert_se(!DEVICE_MAJOR_VALID(-1U));
-        assert_se(!DEVICE_MINOR_VALID(-1U));
-
-        assert_se(DEVICE_MAJOR_VALID(1U << 10));
-        assert_se(DEVICE_MINOR_VALID(1U << 10));
-
-        assert_se(DEVICE_MAJOR_VALID((1U << 12) - 1));
-        assert_se(DEVICE_MINOR_VALID((1U << 20) - 1));
-
-        assert_se(!DEVICE_MAJOR_VALID((1U << 12)));
-        assert_se(!DEVICE_MINOR_VALID((1U << 20)));
-
-        assert_se(!DEVICE_MAJOR_VALID(1U << 25));
-        assert_se(!DEVICE_MINOR_VALID(1U << 25));
-
-        assert_se(!DEVICE_MAJOR_VALID(UINT32_MAX));
-        assert_se(!DEVICE_MINOR_VALID(UINT32_MAX));
-
-        assert_se(!DEVICE_MAJOR_VALID(UINT64_MAX));
-        assert_se(!DEVICE_MINOR_VALID(UINT64_MAX));
-
-        assert_se(DEVICE_MAJOR_VALID(major(0)));
-        assert_se(DEVICE_MINOR_VALID(minor(0)));
-}
-
-static void test_device_path_make_canonical_one(const char *path) {
-        _cleanup_free_ char *resolved = NULL, *raw = NULL;
-        struct stat st;
-        dev_t devno;
-        mode_t mode;
-        int r;
-
-        log_debug("> %s", path);
-
-        if (stat(path, &st) < 0) {
-                assert_se(errno == ENOENT);
-                log_notice("Path %s not found, skipping test", path);
-                return;
-        }
-
-        r = device_path_make_canonical(st.st_mode, st.st_rdev, &resolved);
-        if (r == -ENOENT) {
-                /* maybe /dev/char/x:y and /dev/block/x:y are missing in this test environment, because we
-                 * run in a container or so? */
-                log_notice("Device %s cannot be resolved, skipping test", path);
-                return;
-        }
-
-        assert_se(r >= 0);
-        assert_se(path_equal(path, resolved));
-
-        assert_se(device_path_make_major_minor(st.st_mode, st.st_rdev, &raw) >= 0);
-        assert_se(device_path_parse_major_minor(raw, &mode, &devno) >= 0);
-
-        assert_se(st.st_rdev == devno);
-        assert_se((st.st_mode & S_IFMT) == (mode & S_IFMT));
-}
-
-TEST(device_path_make_canonical) {
-        test_device_path_make_canonical_one("/dev/null");
-        test_device_path_make_canonical_one("/dev/zero");
-        test_device_path_make_canonical_one("/dev/full");
-        test_device_path_make_canonical_one("/dev/random");
-        test_device_path_make_canonical_one("/dev/urandom");
-        test_device_path_make_canonical_one("/dev/tty");
-
-        if (is_device_node("/run/systemd/inaccessible/blk") > 0) {
-                test_device_path_make_canonical_one("/run/systemd/inaccessible/chr");
-                test_device_path_make_canonical_one("/run/systemd/inaccessible/blk");
-        }
-}
-
 TEST(dir_is_empty) {
         _cleanup_(rm_rf_physical_and_freep) char *empty_dir = NULL;
         _cleanup_free_ char *j = NULL, *jj = NULL;
index 4d9907e4245586e02e0b3ca712fb22d46552b237..0842d67d8557914cbf92ae2a5f44b126a1f6f9a7 100644 (file)
@@ -26,6 +26,7 @@
 #include "conf-files.h"
 #include "copy.h"
 #include "def.h"
+#include "devnum-util.h"
 #include "dirent-util.h"
 #include "dissect-image.h"
 #include "env-util.h"
@@ -58,7 +59,6 @@
 #include "set.h"
 #include "sort-util.h"
 #include "specifier.h"
-#include "stat-util.h"
 #include "stdio-util.h"
 #include "string-table.h"
 #include "string-util.h"
@@ -3084,7 +3084,7 @@ static int parse_line(
                         return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Device file requires argument.");
                 }
 
-                r = parse_dev(i.argument, &i.major_minor);
+                r = parse_devnum(i.argument, &i.major_minor);
                 if (r < 0) {
                         *invalid_config = true;
                         return log_syntax(NULL, LOG_ERR, fname, line, r, "Can't parse device file major/minor '%s'.", i.argument);
index deacbc31c56ab301d1f18625c8d27a49803591f0..c9f58e8c29bf29e6abab5e8d64cef7f6de5acdb7 100644 (file)
@@ -12,6 +12,7 @@
 #include "alloc-util.h"
 #include "device-private.h"
 #include "device-util.h"
+#include "devnum-util.h"
 #include "dirent-util.h"
 #include "escape.h"
 #include "fd-util.h"
index 951711f1203d0bcee4a2a368ae0667bfcd38759b..32935e8aa41ab250d4064ce4c5f4578e80c1a13b 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "blockdev-util.h"
 #include "btrfs-util.h"
+#include "devnum-util.h"
 #include "fd-util.h"
 #include "fdset.h"
 #include "main-func.h"
@@ -16,7 +17,6 @@
 #include "process-util.h"
 #include "signal-util.h"
 #include "sort-util.h"
-#include "stat-util.h"
 #include "strv.h"
 #include "time-util.h"
 #include "udevadm.h"
index 90065b410b075ae403247f7e6aed7a44948eeff9..aa16582d9e4f3d0cf239b30410c257e9bdacb0af 100644 (file)
@@ -5,13 +5,13 @@
 #include "alloc-util.h"
 #include "blockdev-util.h"
 #include "chase-symlinks.h"
+#include "devnum-util.h"
 #include "escape.h"
 #include "main-func.h"
 #include "mkdir.h"
 #include "mount-util.h"
 #include "mountpoint-util.h"
 #include "path-util.h"
-#include "stat-util.h"
 #include "string-util.h"
 #include "volatile-util.h"