Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * lsscsi implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2017 Markus Gothe <nietzsche@lysator.liu.se> |
| 6 | * |
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 8 | */ |
| 9 | //config:config LSSCSI |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "lsscsi (2.5 kb)" |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 11 | //config: default y |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: lsscsi is a utility for displaying information about SCSI buses in the |
| 14 | //config: system and devices connected to them. |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 15 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | //config: This version uses sysfs (/sys/bus/scsi/devices) only. |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 17 | |
Denys Vlasenko | 3239ab8 | 2017-08-05 23:28:19 +0200 | [diff] [blame] | 18 | //applet:IF_LSSCSI(APPLET_NOEXEC(lsscsi, lsscsi, BB_DIR_USR_BIN, BB_SUID_DROP, lsscsi)) |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 19 | |
| 20 | //kbuild:lib-$(CONFIG_LSSCSI) += lsscsi.o |
| 21 | |
| 22 | //usage:#define lsscsi_trivial_usage NOUSAGE_STR |
| 23 | //usage:#define lsscsi_full_usage "" |
| 24 | |
| 25 | #include "libbb.h" |
| 26 | |
Markus Gothe | 045327a | 2017-10-21 21:34:22 +0200 | [diff] [blame] | 27 | static const char scsi_dir[] ALIGN1 = "/sys/bus/scsi/devices"; |
| 28 | |
Denys Vlasenko | 2dd82f4 | 2020-07-20 02:03:02 +0200 | [diff] [blame] | 29 | static char *get_line(const char *filename, char *buf, char *bufend) |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 30 | { |
Denys Vlasenko | 2dd82f4 | 2020-07-20 02:03:02 +0200 | [diff] [blame] | 31 | ssize_t sz = bufend - buf - 2; /* -2 for two NULs */ |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 32 | |
Denys Vlasenko | 2dd82f4 | 2020-07-20 02:03:02 +0200 | [diff] [blame] | 33 | if (sz <= 0) |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 34 | return buf; |
| 35 | |
Denys Vlasenko | 2dd82f4 | 2020-07-20 02:03:02 +0200 | [diff] [blame] | 36 | sz = open_read_close(filename, buf, sz); |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 37 | if (sz < 0) |
| 38 | sz = 0; |
| 39 | buf[sz] = '\0'; |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 40 | |
Denys Vlasenko | 2dd82f4 | 2020-07-20 02:03:02 +0200 | [diff] [blame] | 41 | buf = trim(buf) + 1; |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 42 | buf[0] = '\0'; |
| 43 | |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 44 | return buf; |
| 45 | } |
| 46 | |
| 47 | int lsscsi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 48 | int lsscsi_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 49 | { |
| 50 | struct dirent *de; |
| 51 | DIR *dir; |
| 52 | |
Markus Gothe | 045327a | 2017-10-21 21:34:22 +0200 | [diff] [blame] | 53 | xchdir(scsi_dir); |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 54 | |
| 55 | dir = xopendir("."); |
| 56 | while ((de = readdir(dir)) != NULL) { |
| 57 | char buf[256]; |
| 58 | char *ptr; |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 59 | const char *vendor; |
| 60 | const char *type_str; |
| 61 | const char *type_name; |
| 62 | const char *model; |
| 63 | const char *rev; |
| 64 | unsigned type; |
| 65 | |
| 66 | if (!isdigit(de->d_name[0])) |
| 67 | continue; |
| 68 | if (!strchr(de->d_name, ':')) |
| 69 | continue; |
| 70 | if (chdir(de->d_name) != 0) |
| 71 | continue; |
| 72 | |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 73 | vendor = buf; |
Denys Vlasenko | 2dd82f4 | 2020-07-20 02:03:02 +0200 | [diff] [blame] | 74 | ptr = get_line("vendor", buf, buf + sizeof(buf)); |
| 75 | |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 76 | type_str = ptr; |
Denys Vlasenko | 2dd82f4 | 2020-07-20 02:03:02 +0200 | [diff] [blame] | 77 | ptr = get_line("type", ptr, buf + sizeof(buf)); |
| 78 | |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 79 | model = ptr; |
Denys Vlasenko | 2dd82f4 | 2020-07-20 02:03:02 +0200 | [diff] [blame] | 80 | ptr = get_line("model", ptr, buf + sizeof(buf)); |
| 81 | |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 82 | rev = ptr; |
Denys Vlasenko | 2dd82f4 | 2020-07-20 02:03:02 +0200 | [diff] [blame] | 83 | /*ptr =*/ get_line("rev", ptr, buf + sizeof(buf)); |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 84 | |
| 85 | printf("[%s]\t", de->d_name); |
| 86 | |
| 87 | #define scsi_device_types \ |
| 88 | "disk\0" "tape\0" "printer\0" "process\0" \ |
| 89 | "worm\0" "\0" "scanner\0" "optical\0" \ |
| 90 | "mediumx\0" "comms\0" "\0" "\0" \ |
| 91 | "storage\0" "enclosu\0" "sim dsk\0" "opti rd\0" \ |
| 92 | "bridge\0" "osd\0" "adi\0" "\0" \ |
| 93 | "\0" "\0" "\0" "\0" \ |
| 94 | "\0" "\0" "\0" "\0" \ |
| 95 | "\0" "\0" "wlun\0" "no dev" |
| 96 | type = bb_strtou(type_str, NULL, 10); |
| 97 | if (errno |
| 98 | || type >= 0x20 |
| 99 | || (type_name = nth_string(scsi_device_types, type))[0] == '\0' |
| 100 | ) { |
| 101 | printf("(%s)\t", type_str); |
| 102 | } else { |
| 103 | printf("%s\t", type_name); |
| 104 | } |
| 105 | |
| 106 | printf("%s\t""%s\t""%s\n", |
| 107 | vendor, |
| 108 | model, |
| 109 | rev |
| 110 | ); |
| 111 | /* TODO: also output device column, e.g. "/dev/sdX" */ |
| 112 | |
Markus Gothe | 045327a | 2017-10-21 21:34:22 +0200 | [diff] [blame] | 113 | /* chdir("..") may not work as expected, |
| 114 | * since we might have followed a symlink. |
| 115 | */ |
| 116 | xchdir(scsi_dir); |
Markus Gothe | 9c7ee14 | 2017-04-18 19:25:49 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (ENABLE_FEATURE_CLEAN_UP) |
| 120 | closedir(dir); |
| 121 | |
| 122 | return EXIT_SUCCESS; |
| 123 | } |