lsscsi.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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
  10. //config: bool "lsscsi (2.5 kb)"
  11. //config: default y
  12. //config: #select PLATFORM_LINUX
  13. //config: help
  14. //config: lsscsi is a utility for displaying information about SCSI buses in the
  15. //config: system and devices connected to them.
  16. //config:
  17. //config: This version uses sysfs (/sys/bus/scsi/devices) only.
  18. //applet:IF_LSSCSI(APPLET_NOEXEC(lsscsi, lsscsi, BB_DIR_USR_BIN, BB_SUID_DROP, lsscsi))
  19. //kbuild:lib-$(CONFIG_LSSCSI) += lsscsi.o
  20. //usage:#define lsscsi_trivial_usage NOUSAGE_STR
  21. //usage:#define lsscsi_full_usage ""
  22. #include "libbb.h"
  23. static const char scsi_dir[] ALIGN1 = "/sys/bus/scsi/devices";
  24. static char *get_line(const char *filename, char *buf, unsigned *bufsize_p)
  25. {
  26. unsigned bufsize = *bufsize_p;
  27. ssize_t sz;
  28. if ((int)(bufsize - 2) <= 0)
  29. return buf;
  30. sz = open_read_close(filename, buf, bufsize - 2);
  31. if (sz < 0)
  32. sz = 0;
  33. buf[sz] = '\0';
  34. sz = (trim(buf) - buf) + 1;
  35. bufsize -= sz;
  36. buf += sz;
  37. buf[0] = '\0';
  38. *bufsize_p = bufsize;
  39. return buf;
  40. }
  41. int lsscsi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  42. int lsscsi_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  43. {
  44. struct dirent *de;
  45. DIR *dir;
  46. xchdir(scsi_dir);
  47. dir = xopendir(".");
  48. while ((de = readdir(dir)) != NULL) {
  49. char buf[256];
  50. char *ptr;
  51. unsigned bufsize;
  52. const char *vendor;
  53. const char *type_str;
  54. const char *type_name;
  55. const char *model;
  56. const char *rev;
  57. unsigned type;
  58. if (!isdigit(de->d_name[0]))
  59. continue;
  60. if (!strchr(de->d_name, ':'))
  61. continue;
  62. if (chdir(de->d_name) != 0)
  63. continue;
  64. bufsize = sizeof(buf);
  65. vendor = buf;
  66. ptr = get_line("vendor", buf, &bufsize);
  67. type_str = ptr;
  68. ptr = get_line("type", ptr, &bufsize);
  69. model = ptr;
  70. ptr = get_line("model", ptr, &bufsize);
  71. rev = ptr;
  72. ptr = get_line("rev", ptr, &bufsize);
  73. printf("[%s]\t", de->d_name);
  74. #define scsi_device_types \
  75. "disk\0" "tape\0" "printer\0" "process\0" \
  76. "worm\0" "\0" "scanner\0" "optical\0" \
  77. "mediumx\0" "comms\0" "\0" "\0" \
  78. "storage\0" "enclosu\0" "sim dsk\0" "opti rd\0" \
  79. "bridge\0" "osd\0" "adi\0" "\0" \
  80. "\0" "\0" "\0" "\0" \
  81. "\0" "\0" "\0" "\0" \
  82. "\0" "\0" "wlun\0" "no dev"
  83. type = bb_strtou(type_str, NULL, 10);
  84. if (errno
  85. || type >= 0x20
  86. || (type_name = nth_string(scsi_device_types, type))[0] == '\0'
  87. ) {
  88. printf("(%s)\t", type_str);
  89. } else {
  90. printf("%s\t", type_name);
  91. }
  92. printf("%s\t""%s\t""%s\n",
  93. vendor,
  94. model,
  95. rev
  96. );
  97. /* TODO: also output device column, e.g. "/dev/sdX" */
  98. /* chdir("..") may not work as expected,
  99. * since we might have followed a symlink.
  100. */
  101. xchdir(scsi_dir);
  102. }
  103. if (ENABLE_FEATURE_CLEAN_UP)
  104. closedir(dir);
  105. return EXIT_SUCCESS;
  106. }