mirror of
https://github.com/systemd/systemd.git
synced 2024-12-26 03:22:00 +03:00
devnum-util: define helper macros for formatting devnum major/minor pairs
And port some parts over.
This commit is contained in:
parent
7176f06c9e
commit
ec61371fe6
@ -49,7 +49,7 @@ int device_path_make_major_minor(mode_t mode, dev_t devnum, char **ret) {
|
||||
else
|
||||
return -ENODEV;
|
||||
|
||||
if (asprintf(ret, "/dev/%s/%u:%u", t, major(devnum), minor(devnum)) < 0)
|
||||
if (asprintf(ret, "/dev/%s/" DEVNUM_FORMAT_STR, t, DEVNUM_FORMAT_VAL(devnum)) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
return 0;
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "stdio-util.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
|
||||
@ -35,3 +37,15 @@ static inline bool devnum_set_and_equal(dev_t a, dev_t b) {
|
||||
* know" and we'll return false */
|
||||
return a == b && a != 0;
|
||||
}
|
||||
|
||||
/* Maximum string length for a major:minor string. (Note that DECIMAL_STR_MAX includes space for a trailing NUL) */
|
||||
#define DEVNUM_STR_MAX (DECIMAL_STR_MAX(dev_t)-1+1+DECIMAL_STR_MAX(dev_t))
|
||||
|
||||
#define DEVNUM_FORMAT_STR "%u:%u"
|
||||
#define DEVNUM_FORMAT_VAL(d) major(d), minor(d)
|
||||
|
||||
static inline char *format_devnum(dev_t d, char buf[static DEVNUM_STR_MAX]) {
|
||||
return ASSERT_PTR(snprintf_ok(buf, DEVNUM_STR_MAX, DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(d)));
|
||||
}
|
||||
|
||||
#define FORMAT_DEVNUM(d) format_devnum((d), (char[DEVNUM_STR_MAX]) {})
|
||||
|
@ -1084,7 +1084,7 @@ static int set_bfq_weight(Unit *u, const char *controller, dev_t dev, uint64_t i
|
||||
bfq_weight = BFQ_WEIGHT(io_weight);
|
||||
|
||||
if (major(dev) > 0)
|
||||
xsprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), bfq_weight);
|
||||
xsprintf(buf, DEVNUM_FORMAT_STR " %" PRIu64 "\n", DEVNUM_FORMAT_VAL(dev), bfq_weight);
|
||||
else
|
||||
xsprintf(buf, "%" PRIu64 "\n", bfq_weight);
|
||||
|
||||
@ -1117,7 +1117,7 @@ static void cgroup_apply_io_device_weight(Unit *u, const char *dev_path, uint64_
|
||||
|
||||
r1 = set_bfq_weight(u, "io", dev, io_weight);
|
||||
|
||||
xsprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), io_weight);
|
||||
xsprintf(buf, DEVNUM_FORMAT_STR " %" PRIu64 "\n", DEVNUM_FORMAT_VAL(dev), io_weight);
|
||||
r2 = cg_set_attribute("io", u->cgroup_path, "io.weight", buf);
|
||||
|
||||
/* Look at the configured device, when both fail, prefer io.weight errno. */
|
||||
@ -1138,7 +1138,7 @@ static void cgroup_apply_blkio_device_weight(Unit *u, const char *dev_path, uint
|
||||
if (r < 0)
|
||||
return;
|
||||
|
||||
xsprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), blkio_weight);
|
||||
xsprintf(buf, DEVNUM_FORMAT_STR " %" PRIu64 "\n", DEVNUM_FORMAT_VAL(dev), blkio_weight);
|
||||
(void) set_attribute_and_warn(u, "blkio", "blkio.weight_device", buf);
|
||||
}
|
||||
|
||||
@ -1152,9 +1152,9 @@ static void cgroup_apply_io_device_latency(Unit *u, const char *dev_path, usec_t
|
||||
return;
|
||||
|
||||
if (target != USEC_INFINITY)
|
||||
xsprintf(buf, "%u:%u target=%" PRIu64 "\n", major(dev), minor(dev), target);
|
||||
xsprintf(buf, DEVNUM_FORMAT_STR " target=%" PRIu64 "\n", DEVNUM_FORMAT_VAL(dev), target);
|
||||
else
|
||||
xsprintf(buf, "%u:%u target=max\n", major(dev), minor(dev));
|
||||
xsprintf(buf, DEVNUM_FORMAT_STR " target=max\n", DEVNUM_FORMAT_VAL(dev));
|
||||
|
||||
(void) set_attribute_and_warn(u, "io", "io.latency", buf);
|
||||
}
|
||||
@ -1173,7 +1173,7 @@ static void cgroup_apply_io_device_limit(Unit *u, const char *dev_path, uint64_t
|
||||
else
|
||||
xsprintf(limit_bufs[type], "%s", limits[type] == CGROUP_LIMIT_MAX ? "max" : "0");
|
||||
|
||||
xsprintf(buf, "%u:%u rbps=%s wbps=%s riops=%s wiops=%s\n", major(dev), minor(dev),
|
||||
xsprintf(buf, DEVNUM_FORMAT_STR " rbps=%s wbps=%s riops=%s wiops=%s\n", DEVNUM_FORMAT_VAL(dev),
|
||||
limit_bufs[CGROUP_IO_RBPS_MAX], limit_bufs[CGROUP_IO_WBPS_MAX],
|
||||
limit_bufs[CGROUP_IO_RIOPS_MAX], limit_bufs[CGROUP_IO_WIOPS_MAX]);
|
||||
(void) set_attribute_and_warn(u, "io", "io.max", buf);
|
||||
@ -1186,10 +1186,10 @@ static void cgroup_apply_blkio_device_limit(Unit *u, const char *dev_path, uint6
|
||||
if (lookup_block_device(dev_path, &dev) < 0)
|
||||
return;
|
||||
|
||||
sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), rbps);
|
||||
sprintf(buf, DEVNUM_FORMAT_STR " %" PRIu64 "\n", DEVNUM_FORMAT_VAL(dev), rbps);
|
||||
(void) set_attribute_and_warn(u, "blkio", "blkio.throttle.read_bps_device", buf);
|
||||
|
||||
sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), wbps);
|
||||
sprintf(buf, DEVNUM_FORMAT_STR " %" PRIu64 "\n", DEVNUM_FORMAT_VAL(dev), wbps);
|
||||
(void) set_attribute_and_warn(u, "blkio", "blkio.throttle.write_bps_device", buf);
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include "base-filesystem.h"
|
||||
#include "chase-symlinks.h"
|
||||
#include "dev-setup.h"
|
||||
#include "devnum-util.h"
|
||||
#include "env-util.h"
|
||||
#include "escape.h"
|
||||
#include "extension-release.h"
|
||||
@ -885,10 +886,10 @@ add_symlink:
|
||||
return 0;
|
||||
|
||||
/* Create symlinks like /dev/char/1:9 → ../urandom */
|
||||
if (asprintf(&sl, "%s/dev/%s/%u:%u",
|
||||
if (asprintf(&sl, "%s/dev/%s/" DEVNUM_FORMAT_STR,
|
||||
temporary_mount,
|
||||
S_ISCHR(st.st_mode) ? "char" : "block",
|
||||
major(st.st_rdev), minor(st.st_rdev)) < 0)
|
||||
DEVNUM_FORMAT_VAL(st.st_rdev)) < 0)
|
||||
return log_oom();
|
||||
|
||||
(void) mkdir_parents(sl, 0755);
|
||||
|
@ -58,8 +58,8 @@ static int open_parent_block_device(dev_t devnum, int *ret_fd) {
|
||||
if (sd_device_get_devname(d, &name) < 0) {
|
||||
r = sd_device_get_syspath(d, &name);
|
||||
if (r < 0) {
|
||||
log_device_debug_errno(d, r, "Device %u:%u does not have a name, ignoring: %m",
|
||||
major(devnum), minor(devnum));
|
||||
log_device_debug_errno(d, r, "Device " DEVNUM_FORMAT_STR " does not have a name, ignoring: %m",
|
||||
DEVNUM_FORMAT_VAL(devnum));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "devnum-util.h"
|
||||
#include "fileio.h"
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
@ -12,7 +13,6 @@
|
||||
int main(int argc, char *argv[]) {
|
||||
struct stat st;
|
||||
const char *device;
|
||||
_cleanup_free_ char *major_minor = NULL;
|
||||
int r;
|
||||
|
||||
if (argc != 2) {
|
||||
@ -40,14 +40,9 @@ int main(int argc, char *argv[]) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (asprintf(&major_minor, "%d:%d", major(st.st_rdev), minor(st.st_rdev)) < 0) {
|
||||
log_oom();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
r = write_string_file("/sys/power/resume", major_minor, WRITE_STRING_FILE_DISABLE_BUFFER);
|
||||
r = write_string_file("/sys/power/resume", FORMAT_DEVNUM(st.st_rdev), WRITE_STRING_FILE_DISABLE_BUFFER);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to write '%s' to /sys/power/resume: %m", major_minor);
|
||||
log_error_errno(r, "Failed to write '" DEVNUM_FORMAT_STR "' to /sys/power/resume: %m", DEVNUM_FORMAT_VAL(st.st_rdev));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@ -58,6 +53,6 @@ int main(int argc, char *argv[]) {
|
||||
* no hibernation image).
|
||||
*/
|
||||
|
||||
log_info("Could not resume from '%s' (%s).", device, major_minor);
|
||||
log_info("Could not resume from '%s' (" DEVNUM_FORMAT_STR ").", device, DEVNUM_FORMAT_VAL(st.st_rdev));
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -1301,7 +1301,7 @@ int home_setup_luks(
|
||||
return log_error_errno(r, "Failed to stat block device %s: %m", n);
|
||||
assert(S_ISBLK(st.st_mode));
|
||||
|
||||
if (asprintf(&sysfs, "/sys/dev/block/%u:%u/partition", major(st.st_rdev), minor(st.st_rdev)) < 0)
|
||||
if (asprintf(&sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/partition", DEVNUM_FORMAT_VAL(st.st_rdev)) < 0)
|
||||
return log_oom();
|
||||
|
||||
if (access(sysfs, F_OK) < 0) {
|
||||
@ -1312,7 +1312,7 @@ int home_setup_luks(
|
||||
} else {
|
||||
_cleanup_free_ char *buffer = NULL;
|
||||
|
||||
if (asprintf(&sysfs, "/sys/dev/block/%u:%u/start", major(st.st_rdev), minor(st.st_rdev)) < 0)
|
||||
if (asprintf(&sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/start", DEVNUM_FORMAT_VAL(st.st_rdev)) < 0)
|
||||
return log_oom();
|
||||
|
||||
r = read_one_line_file(sysfs, &buffer);
|
||||
@ -2205,7 +2205,7 @@ int home_create_luks(
|
||||
if (!S_ISBLK(st.st_mode))
|
||||
return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Device is not a block device, refusing.");
|
||||
|
||||
if (asprintf(&sysfs, "/sys/dev/block/%u:%u/partition", major(st.st_rdev), minor(st.st_rdev)) < 0)
|
||||
if (asprintf(&sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/partition", DEVNUM_FORMAT_VAL(st.st_rdev)) < 0)
|
||||
return log_oom();
|
||||
if (access(sysfs, F_OK) < 0) {
|
||||
if (errno != ENOENT)
|
||||
|
@ -106,4 +106,19 @@ TEST(device_path_make_canonical) {
|
||||
}
|
||||
}
|
||||
|
||||
static void test_devnum_format_str_one(dev_t devnum, const char *s) {
|
||||
dev_t x;
|
||||
|
||||
assert_se(streq(FORMAT_DEVNUM(devnum), s));
|
||||
assert_se(parse_devnum(s, &x) >= 0);
|
||||
assert_se(x == devnum);
|
||||
}
|
||||
|
||||
TEST(devnum_format_str) {
|
||||
test_devnum_format_str_one(makedev(0, 0), "0:0");
|
||||
test_devnum_format_str_one(makedev(1, 2), "1:2");
|
||||
test_devnum_format_str_one(makedev(99, 100), "99:100");
|
||||
test_devnum_format_str_one(makedev(4095, 1048575), "4095:1048575");
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_INFO);
|
||||
|
Loading…
Reference in New Issue
Block a user