From 289aff86231153670a1de4ee9772f726f0a7e846 Mon Sep 17 00:00:00 2001 From: Sergey Naumov Date: Mon, 6 Sep 2010 13:35:58 +0200 Subject: blockdev: new applet function old new delta blockdev_main - 287 +287 bdcommands - 160 +160 packed_usage 27203 27298 +95 applet_names 2297 2306 +9 applet_main 1352 1356 +4 applet_nameofs 676 678 +2 applet_install_loc 169 170 +1 ------------------------------------------------------------------------------ (add/remove: 3/0 grow/shrink: 5/0 up/down: 558/0) Total: 558 bytes Signed-off-by: Sergey Naumov Signed-off-by: Denys Vlasenko --- util-linux/blockdev.c | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 util-linux/blockdev.c (limited to 'util-linux/blockdev.c') diff --git a/util-linux/blockdev.c b/util-linux/blockdev.c new file mode 100644 index 000000000..eb5914b58 --- /dev/null +++ b/util-linux/blockdev.c @@ -0,0 +1,180 @@ +/* + * blockdev implementation for busybox + * + * Copyright (C) 2010 Sergey Naumov + * + * Licensed under GPLv2, see file License in this tarball for details. + */ + +//applet:IF_BLOCKDEV(APPLET(blockdev, _BB_DIR_SBIN, _BB_SUID_DROP)) + +//kbuild:lib-$(CONFIG_BLOCKDEV) += blockdev.o + +//config:config BLOCKDEV +//config: bool "blockdev" +//config: default y +//config: help +//config: Performs some ioctls with block devices. + +//usage:#define blockdev_trivial_usage +//usage: "OPTION [OPTARG] DEVICE" +//usage:#define blockdev_full_usage "\n\n" +//usage: "Options:" +//usage: "\n --setro Set ro" +//usage: "\n --setrw Set rw" +//usage: "\n --getro Get ro" +//usage: "\n --getss Get sector size" +//usage: "\n --getbsz Get block size" +//usage: "\n --setbsz BYTES Set block size" +//usage: "\n --getsize Get device size in 512-byte sectors" +//usage: "\n --getsize64 Get device size in bytes" +//usage: "\n --flushbufs Flush buffers" +//usage: "\n --rereadpt Reread partition table" + + +#include "libbb.h" +#include + +enum { + ARG_NONE = 0, + ARG_INT = 1, + ARG_ULONG = 2, + ARG_ULLONG = 3, + ARG_MASK = 3, + + FL_USRARG = 4, /* argument is provided by user */ + FL_NORESULT = 8, +}; + +struct bdc { + uint32_t ioc; /* ioctl code */ + const char name[sizeof("flushbufs")]; /* "--setfoo" wothout "--" */ + uint8_t flags; + int8_t argval; /* default argument value */ +}; + +static const struct bdc bdcommands[] = { + { + .ioc = BLKROSET, + .name = "setro", + .flags = ARG_INT + FL_NORESULT, + .argval = 1, + },{ + .ioc = BLKROSET, + .name = "setrw", + .flags = ARG_INT + FL_NORESULT, + .argval = 0, + },{ + .ioc = BLKROGET, + .name = "getro", + .flags = ARG_INT, + .argval = -1, + },{ + .ioc = BLKSSZGET, + .name = "getss", + .flags = ARG_INT, + .argval = -1, + },{ + .ioc = BLKBSZGET, + .name = "getbsz", + .flags = ARG_INT, + .argval = -1, + },{ + .ioc = BLKBSZSET, + .name = "setbsz", + .flags = ARG_INT + FL_NORESULT + FL_USRARG, + .argval = 0, + },{ + .ioc = BLKGETSIZE, + .name = "getsize", + .flags = ARG_ULONG, + .argval = -1, + },{ + .ioc = BLKGETSIZE64, + .name = "getsize64", + .flags = ARG_ULLONG, + .argval = -1, + },{ + .ioc = BLKFLSBUF, + .name = "flushbufs", + .flags = ARG_NONE + FL_NORESULT, + .argval = 0, + },{ + .ioc = BLKRRPART, + .name = "rereadpt", + .flags = ARG_NONE + FL_NORESULT, + .argval = 0, + } +}; + +static const struct bdc *find_cmd(const char *s) +{ + int j; + if (*s++ == '-') + if (*s++ == '-') + for (j = 0; j < ARRAY_SIZE(bdcommands); j++) + if (strcmp(s, bdcommands[j].name) == 0) + return &bdcommands[j]; + bb_show_usage(); +} + +int blockdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int blockdev_main(int argc, char **argv) +{ + const struct bdc *bdcmd; + void *ioctl_ptr; + int fd; + int iarg; + unsigned long lu; + unsigned long long llu; + + if ((unsigned)(argc - 3) > 1) /* must have 2 or 3 args */ + bb_show_usage(); + + bdcmd = find_cmd(*++argv); + + llu = (int)bdcmd->argval; + if (bdcmd->flags & FL_USRARG) + llu = xatoi_positive(*++argv); + lu = llu; + iarg = llu; + + if (!*++argv) + bb_show_usage(); + fd = xopen(*argv, O_RDONLY); + + ioctl_ptr = NULL; + switch (bdcmd->flags & ARG_MASK) { + case ARG_INT: + ioctl_ptr = &iarg; + break; + case ARG_ULONG: + ioctl_ptr = &lu; + break; + case ARG_ULLONG: + ioctl_ptr = &llu; + break; + } + + if (ioctl(fd, bdcmd->ioc, ioctl_ptr) == -1) + bb_simple_perror_msg_and_die(*argv); + + switch (bdcmd->flags & (ARG_MASK+FL_NORESULT)) { + case ARG_INT: + /* Smaller code when we use long long + * (gcc tail-merges printf call) + */ + printf("%lld\n", (long long)iarg); + break; + case ARG_ULONG: + llu = lu; + /* FALLTHROUGH */ + case ARG_ULLONG: + printf("%llu\n", llu); + break; + } + + if (ENABLE_FEATURE_CLEAN_UP) + close(fd); + return EXIT_SUCCESS; +} -- cgit v1.2.3-55-g6feb From b4e6b419340452d2e0fedc9f0f88c5fd102982de Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 6 Sep 2010 14:15:46 +0200 Subject: tweak blockdev's help text Signed-off-by: Denys Vlasenko --- util-linux/blockdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util-linux/blockdev.c') diff --git a/util-linux/blockdev.c b/util-linux/blockdev.c index eb5914b58..fea30b009 100644 --- a/util-linux/blockdev.c +++ b/util-linux/blockdev.c @@ -17,7 +17,7 @@ //config: Performs some ioctls with block devices. //usage:#define blockdev_trivial_usage -//usage: "OPTION [OPTARG] DEVICE" +//usage: "OPTION BLOCKDEV" //usage:#define blockdev_full_usage "\n\n" //usage: "Options:" //usage: "\n --setro Set ro" -- cgit v1.2.3-55-g6feb From 264bdadb2c0406474a4e874a3b16cb57661e6b3a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 6 Sep 2010 15:34:15 +0200 Subject: blockdev: -2 bytes Signed-off-by: Denys Vlasenko --- util-linux/blockdev.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'util-linux/blockdev.c') diff --git a/util-linux/blockdev.c b/util-linux/blockdev.c index fea30b009..2b48c28c9 100644 --- a/util-linux/blockdev.c +++ b/util-linux/blockdev.c @@ -109,12 +109,15 @@ static const struct bdc bdcommands[] = { static const struct bdc *find_cmd(const char *s) { - int j; - if (*s++ == '-') - if (*s++ == '-') - for (j = 0; j < ARRAY_SIZE(bdcommands); j++) - if (strcmp(s, bdcommands[j].name) == 0) - return &bdcommands[j]; + const struct bdc *bdcmd = bdcommands; + if (s[0] == '-' && s[1] == '-') { + s += 2; + do { + if (strcmp(s, bdcmd->name) == 0) + return bdcmd; + bdcmd++; + } while (bdcmd != bdcommands + ARRAY_SIZE(bdcommands)); + } bb_show_usage(); } -- cgit v1.2.3-55-g6feb From 3d4a8f864693e6a079a78db2290708554b130cb8 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 6 Sep 2010 16:22:25 +0200 Subject: made 3 license strings to follow the usual form ("or later" bit is not changed!) Signed-off-by: Denys Vlasenko --- archival/dpkg.c | 2 +- networking/slattach.c | 2 +- util-linux/blockdev.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'util-linux/blockdev.c') diff --git a/archival/dpkg.c b/archival/dpkg.c index 219512b9a..b36c26198 100644 --- a/archival/dpkg.c +++ b/archival/dpkg.c @@ -12,7 +12,7 @@ * * started life as a busybox implementation of udpkg * - * licensed under gplv2 or later, see file license in this tarball for details. + * Licensed under GPLv2 or later, see file LICENSE in this source tree. */ /* diff --git a/networking/slattach.c b/networking/slattach.c index 12a3067de..921ec552d 100644 --- a/networking/slattach.c +++ b/networking/slattach.c @@ -4,7 +4,7 @@ * * Author: Ignacio Garcia Perez (iggarpe at gmail dot com) * - * License: GPLv2 or later, see LICENSE file in this tarball. + * Licensed under GPLv2 or later, see file LICENSE in this source tree. * * There are some differences from the standard net-tools slattach: * diff --git a/util-linux/blockdev.c b/util-linux/blockdev.c index 2b48c28c9..9b5d86d00 100644 --- a/util-linux/blockdev.c +++ b/util-linux/blockdev.c @@ -3,7 +3,7 @@ * * Copyright (C) 2010 Sergey Naumov * - * Licensed under GPLv2, see file License in this tarball for details. + * Licensed under GPLv2, see file LICENSE in this source tree. */ //applet:IF_BLOCKDEV(APPLET(blockdev, _BB_DIR_SBIN, _BB_SUID_DROP)) -- cgit v1.2.3-55-g6feb From a35dfe62004abaa116d01155605e3f3df379688c Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 6 Sep 2010 16:31:18 +0200 Subject: blockdev: make "blockdev --rereadpt /dev/sda EXTRA_ARG" complain. +6 bytes. Signed-off-by: Denys Vlasenko --- util-linux/blockdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util-linux/blockdev.c') diff --git a/util-linux/blockdev.c b/util-linux/blockdev.c index 9b5d86d00..a780d2238 100644 --- a/util-linux/blockdev.c +++ b/util-linux/blockdev.c @@ -142,7 +142,7 @@ int blockdev_main(int argc, char **argv) lu = llu; iarg = llu; - if (!*++argv) + if (!*++argv || argv[1]) bb_show_usage(); fd = xopen(*argv, O_RDONLY); -- cgit v1.2.3-55-g6feb From 571c5ab768075c98da547e627b457d9188dca350 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 6 Sep 2010 17:37:52 +0200 Subject: blockdev: code shrink function old new delta blockdev_main 292 255 -37 Signed-off-by: Denys Vlasenko --- util-linux/blockdev.c | 56 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 22 deletions(-) (limited to 'util-linux/blockdev.c') diff --git a/util-linux/blockdev.c b/util-linux/blockdev.c index a780d2238..29c288a15 100644 --- a/util-linux/blockdev.c +++ b/util-linux/blockdev.c @@ -3,7 +3,7 @@ * * Copyright (C) 2010 Sergey Naumov * - * Licensed under GPLv2, see file LICENSE in this source tree. + * Licensed under GPLv2, see file License in this tarball for details. */ //applet:IF_BLOCKDEV(APPLET(blockdev, _BB_DIR_SBIN, _BB_SUID_DROP)) @@ -39,7 +39,8 @@ enum { ARG_NONE = 0, ARG_INT = 1, ARG_ULONG = 2, - ARG_ULLONG = 3, + /* Yes, BLKGETSIZE64 takes pointer to uint64_t, not ullong! */ + ARG_U64 = 3, ARG_MASK = 3, FL_USRARG = 4, /* argument is provided by user */ @@ -92,7 +93,7 @@ static const struct bdc bdcommands[] = { },{ .ioc = BLKGETSIZE64, .name = "getsize64", - .flags = ARG_ULLONG, + .flags = ARG_U64, .argval = -1, },{ .ioc = BLKFLSBUF, @@ -125,55 +126,66 @@ int blockdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int blockdev_main(int argc, char **argv) { const struct bdc *bdcmd; - void *ioctl_ptr; int fd; - int iarg; - unsigned long lu; - unsigned long long llu; + uint64_t u64; + union { + int i; + unsigned long lu; + uint64_t u64; + } ioctl_val_on_stack; if ((unsigned)(argc - 3) > 1) /* must have 2 or 3 args */ bb_show_usage(); bdcmd = find_cmd(*++argv); - llu = (int)bdcmd->argval; + u64 = (int)bdcmd->argval; if (bdcmd->flags & FL_USRARG) - llu = xatoi_positive(*++argv); - lu = llu; - iarg = llu; + u64 = xatoi_positive(*++argv); if (!*++argv || argv[1]) bb_show_usage(); fd = xopen(*argv, O_RDONLY); - ioctl_ptr = NULL; + ioctl_val_on_stack.u64 = u64; +#if BB_BIG_ENDIAN + /* Store data properly wrt data size. + * (1) It's no-op for little-endian. + * (2) it's no-op for 0 and -1. Only --setro uses arg != 0 and != -1, + * and it is ARG_INT. --setbsz USER_VAL is also ARG_INT. + * Thus, we don't need to handle ARG_ULONG. + */ switch (bdcmd->flags & ARG_MASK) { case ARG_INT: - ioctl_ptr = &iarg; + ioctl_val_on_stack.i = (int)u64; break; +# if 0 /* unused */ case ARG_ULONG: - ioctl_ptr = &lu; - break; - case ARG_ULLONG: - ioctl_ptr = &llu; + ioctl_val_on_stack.lu = (unsigned long)u64; break; +# endif } +#endif - if (ioctl(fd, bdcmd->ioc, ioctl_ptr) == -1) + if (ioctl(fd, bdcmd->ioc, &ioctl_val_on_stack.u64) == -1) bb_simple_perror_msg_and_die(*argv); + /* Fetch it into register(s) */ + u64 = ioctl_val_on_stack.u64; + + /* Zero- or one-extend the value if needed, then print */ switch (bdcmd->flags & (ARG_MASK+FL_NORESULT)) { case ARG_INT: /* Smaller code when we use long long * (gcc tail-merges printf call) */ - printf("%lld\n", (long long)iarg); + printf("%lld\n", (long long)(int)u64); break; case ARG_ULONG: - llu = lu; + u64 = (unsigned long)u64; /* FALLTHROUGH */ - case ARG_ULLONG: - printf("%llu\n", llu); + case ARG_U64: + printf("%llu\n", (unsigned long long)u64); break; } -- cgit v1.2.3-55-g6feb From f38b912fcfbb9a6289a161242c7e9ce2eae8cc0d Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 6 Sep 2010 17:41:46 +0200 Subject: Again fix license line format Signed-off-by: Denys Vlasenko --- util-linux/blockdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util-linux/blockdev.c') diff --git a/util-linux/blockdev.c b/util-linux/blockdev.c index 29c288a15..c61116df9 100644 --- a/util-linux/blockdev.c +++ b/util-linux/blockdev.c @@ -3,7 +3,7 @@ * * Copyright (C) 2010 Sergey Naumov * - * Licensed under GPLv2, see file License in this tarball for details. + * Licensed under GPLv2, see file LICENSE in this source tree. */ //applet:IF_BLOCKDEV(APPLET(blockdev, _BB_DIR_SBIN, _BB_SUID_DROP)) -- cgit v1.2.3-55-g6feb