aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2015-01-29 12:35:26 +0100
committerKarel Zak <kzak@redhat.com>2015-01-29 12:35:26 +0100
commit1539750f2a82e7ff627893709ac70a0d241ec60c (patch)
treee5a12851f5ad459aa639eea60036ef42ad14957d
parent4631e25427df6f501dc2ae67002bd3841d2899b8 (diff)
downloadutil-linux-playground-1539750f2a82e7ff627893709ac70a0d241ec60c.tar.gz
lsblk: implement SOMEOK (=64) return code
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--misc-utils/lsblk.811
-rw-r--r--misc-utils/lsblk.c40
2 files changed, 38 insertions, 13 deletions
diff --git a/misc-utils/lsblk.8 b/misc-utils/lsblk.8
index bc7c3afc20..89e1ddd4c9 100644
--- a/misc-utils/lsblk.8
+++ b/misc-utils/lsblk.8
@@ -135,6 +135,17 @@ which is done by using
This sysfs block directory appeared in kernel 2.6.27 (October 2008).
In case of problems with a new enough kernel, check that CONFIG_SYSFS
was enabled at the time of the kernel build.
+
+.SH RETURN CODES
+.IP 0
+success
+.IP 1
+failure
+.IP 32
+not found all specified devices
+.IP 64
+some specified devices found, some not found
+
.SH AUTHORS
.nf
Milan Broz <mbroz@redhat.com>
diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c
index 8a38b9e996..031c241401 100644
--- a/misc-utils/lsblk.c
+++ b/misc-utils/lsblk.c
@@ -62,6 +62,10 @@
#include "mangle.h"
#include "optutils.h"
+
+#define LSBLK_EXIT_SOMEOK 64
+#define LSBLK_EXIT_ALLFAILED 32
+
/* column IDs */
enum {
COL_NAME = 0,
@@ -1338,7 +1342,7 @@ static int iterate_block_devices(void)
struct blkdev_cxt cxt = { 0 };
if (!(dir = opendir(_PATH_SYS_BLOCK)))
- return EXIT_FAILURE;
+ return -errno;
while ((d = xreaddir(dir))) {
if (set_cxt(&cxt, NULL, NULL, d->d_name))
@@ -1358,7 +1362,7 @@ static int iterate_block_devices(void)
closedir(dir);
- return EXIT_SUCCESS;
+ return 0;
}
static char *devno_to_sysfs_name(dev_t devno, char *devname, char *buf, size_t buf_size)
@@ -1385,19 +1389,18 @@ static int process_one_device(char *devname)
{
struct blkdev_cxt parent = { 0 }, cxt = { 0 };
struct stat st;
- char buf[PATH_MAX + 1], *name, *diskname = NULL;
+ char buf[PATH_MAX + 1], *name = NULL, *diskname = NULL;
dev_t disk = 0;
- int real_part = 0;
- int status = EXIT_FAILURE;
+ int real_part = 0, rc = -EINVAL;
if (stat(devname, &st) || !S_ISBLK(st.st_mode)) {
warnx(_("%s: not a block device"), devname);
- return EXIT_FAILURE;
+ goto leave;
}
if (!(name = devno_to_sysfs_name(st.st_rdev, devname, buf, PATH_MAX))) {
warn(_("%s: failed to get sysfs name"), devname);
- return EXIT_FAILURE;
+ goto leave;
}
if (!strncmp(name, "dm-", 3)) {
@@ -1406,7 +1409,7 @@ static int process_one_device(char *devname)
} else {
if (blkid_devno_to_wholedisk(st.st_rdev, buf, sizeof(buf), &disk)) {
warn(_("%s: failed to get whole-disk device number"), devname);
- return EXIT_FAILURE;
+ goto leave;
}
diskname = buf;
real_part = st.st_rdev != disk;
@@ -1434,7 +1437,7 @@ static int process_one_device(char *devname)
process_blkdev(&cxt, &parent, 1, NULL);
}
- status = EXIT_SUCCESS;
+ rc = 0;
leave:
free(name);
reset_blkdev_cxt(&cxt);
@@ -1442,7 +1445,7 @@ leave:
if (real_part)
reset_blkdev_cxt(&parent);
- return status;
+ return rc;
}
static void parse_excludes(const char *str0)
@@ -1791,9 +1794,20 @@ int main(int argc, char *argv[])
}
if (optind == argc)
- status = iterate_block_devices();
- else while (optind < argc)
- status = process_one_device(argv[optind++]);
+ status = iterate_block_devices() == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+ else {
+ int cnt = 0, cnt_err = 0;
+
+ while (optind < argc) {
+ if (process_one_device(argv[optind++]) != 0)
+ cnt_err++;
+ cnt++;
+ }
+ status = cnt == 0 ? EXIT_FAILURE : /* nothing */
+ cnt == cnt_err ? LSBLK_EXIT_ALLFAILED :/* all failed */
+ cnt_err ? LSBLK_EXIT_SOMEOK : /* some ok */
+ EXIT_SUCCESS; /* all success */
+ }
if (lsblk->sort_col)
scols_sort_table(lsblk->table, lsblk->sort_col);