lsscsi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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"
  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(lsscsi, BB_DIR_USR_BIN, BB_SUID_DROP))
  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 char *get_line(const char *filename, char *buf, unsigned *bufsize_p)
  24. {
  25. unsigned bufsize = *bufsize_p;
  26. ssize_t sz;
  27. if ((int)(bufsize - 2) <= 0)
  28. return buf;
  29. sz = open_read_close(filename, buf, bufsize - 2);
  30. if (sz < 0)
  31. sz = 0;
  32. buf[sz] = '\0';
  33. trim(buf);
  34. sz = strlen(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("/sys/bus/scsi/devices");
  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. xchdir("..");
  99. }
  100. if (ENABLE_FEATURE_CLEAN_UP)
  101. closedir(dir);
  102. return EXIT_SUCCESS;
  103. }