diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-06-28 20:57:15 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-11-29 20:02:39 +0100 |
commit | fb2430c6e56e31c1b4220719640fc544e289214b (patch) | |
tree | eaa692de8a44fa29e65378575595d7c043f373be /src/basic | |
parent | Merge pull request #10987 from poettering/index-md-work-around (diff) | |
download | systemd-fb2430c6e56e31c1b4220719640fc544e289214b.tar.xz systemd-fb2430c6e56e31c1b4220719640fc544e289214b.zip |
stat-util: add macros for checking whether major and minor values are in range
As it turns out glibc and the Linux kernel have different ideas about
the size of dev_t and how many bits exist for the major and the minor.
When validating major/minor numbers we should check against the kernel's
actual sizes, hence add macros for this.
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/stat-util.h | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/basic/stat-util.h b/src/basic/stat-util.h index 84400a6083..fe4a4bb717 100644 --- a/src/basic/stat-util.h +++ b/src/basic/stat-util.h @@ -62,3 +62,22 @@ 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); \ + }) |