blob: 8f7eda761160a86a15c8f7562adf7940c7390b1a [file] [log] [blame]
Markus Gothe9c7ee142017-04-18 19:25:49 +02001/* 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 Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "lsscsi (2.5 kb)"
Markus Gothe9c7ee142017-04-18 19:25:49 +020011//config: default y
Markus Gothe9c7ee142017-04-18 19:25:49 +020012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: lsscsi is a utility for displaying information about SCSI buses in the
14//config: system and devices connected to them.
Markus Gothe9c7ee142017-04-18 19:25:49 +020015//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: This version uses sysfs (/sys/bus/scsi/devices) only.
Markus Gothe9c7ee142017-04-18 19:25:49 +020017
Denys Vlasenko3239ab82017-08-05 23:28:19 +020018//applet:IF_LSSCSI(APPLET_NOEXEC(lsscsi, lsscsi, BB_DIR_USR_BIN, BB_SUID_DROP, lsscsi))
Markus Gothe9c7ee142017-04-18 19:25:49 +020019
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 Gothe045327a2017-10-21 21:34:22 +020027static const char scsi_dir[] ALIGN1 = "/sys/bus/scsi/devices";
28
Denys Vlasenko2dd82f42020-07-20 02:03:02 +020029static char *get_line(const char *filename, char *buf, char *bufend)
Markus Gothe9c7ee142017-04-18 19:25:49 +020030{
Denys Vlasenko2dd82f42020-07-20 02:03:02 +020031 ssize_t sz = bufend - buf - 2; /* -2 for two NULs */
Markus Gothe9c7ee142017-04-18 19:25:49 +020032
Denys Vlasenko2dd82f42020-07-20 02:03:02 +020033 if (sz <= 0)
Markus Gothe9c7ee142017-04-18 19:25:49 +020034 return buf;
35
Denys Vlasenko2dd82f42020-07-20 02:03:02 +020036 sz = open_read_close(filename, buf, sz);
Markus Gothe9c7ee142017-04-18 19:25:49 +020037 if (sz < 0)
38 sz = 0;
39 buf[sz] = '\0';
Markus Gothe9c7ee142017-04-18 19:25:49 +020040
Denys Vlasenko2dd82f42020-07-20 02:03:02 +020041 buf = trim(buf) + 1;
Markus Gothe9c7ee142017-04-18 19:25:49 +020042 buf[0] = '\0';
43
Markus Gothe9c7ee142017-04-18 19:25:49 +020044 return buf;
45}
46
47int lsscsi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
48int lsscsi_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
49{
50 struct dirent *de;
51 DIR *dir;
52
Markus Gothe045327a2017-10-21 21:34:22 +020053 xchdir(scsi_dir);
Markus Gothe9c7ee142017-04-18 19:25:49 +020054
55 dir = xopendir(".");
56 while ((de = readdir(dir)) != NULL) {
57 char buf[256];
58 char *ptr;
Markus Gothe9c7ee142017-04-18 19:25:49 +020059 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 Gothe9c7ee142017-04-18 19:25:49 +020073 vendor = buf;
Denys Vlasenko2dd82f42020-07-20 02:03:02 +020074 ptr = get_line("vendor", buf, buf + sizeof(buf));
75
Markus Gothe9c7ee142017-04-18 19:25:49 +020076 type_str = ptr;
Denys Vlasenko2dd82f42020-07-20 02:03:02 +020077 ptr = get_line("type", ptr, buf + sizeof(buf));
78
Markus Gothe9c7ee142017-04-18 19:25:49 +020079 model = ptr;
Denys Vlasenko2dd82f42020-07-20 02:03:02 +020080 ptr = get_line("model", ptr, buf + sizeof(buf));
81
Markus Gothe9c7ee142017-04-18 19:25:49 +020082 rev = ptr;
Denys Vlasenko2dd82f42020-07-20 02:03:02 +020083 /*ptr =*/ get_line("rev", ptr, buf + sizeof(buf));
Markus Gothe9c7ee142017-04-18 19:25:49 +020084
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 Gothe045327a2017-10-21 21:34:22 +0200113 /* chdir("..") may not work as expected,
114 * since we might have followed a symlink.
115 */
116 xchdir(scsi_dir);
Markus Gothe9c7ee142017-04-18 19:25:49 +0200117 }
118
119 if (ENABLE_FEATURE_CLEAN_UP)
120 closedir(dir);
121
122 return EXIT_SUCCESS;
123}