mkdev.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #define _DEFAULT_SOURCE
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <sys/sysmacros.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <stdbool.h>
  23. #include <dirent.h>
  24. #include <limits.h>
  25. #include <fnmatch.h>
  26. #include "libblkid-tiny.h"
  27. #include <syslog.h>
  28. static char buf[PATH_MAX];
  29. static char buf2[PATH_MAX];
  30. static unsigned int mode = 0600;
  31. static void make_dev(const char *path, bool block, int major, int minor)
  32. {
  33. unsigned int _mode = mode | (block ? S_IFBLK : S_IFCHR);
  34. mknod(path, _mode, makedev(major, minor));
  35. }
  36. static void find_devs(bool block)
  37. {
  38. char *path = block ? "/sys/dev/block" : "/sys/dev/char";
  39. struct dirent *dp;
  40. DIR *dir;
  41. dir = opendir(path);
  42. if (!dir)
  43. return;
  44. path = buf2 + sprintf(buf2, "%s/", path);
  45. while ((dp = readdir(dir)) != NULL) {
  46. char *c;
  47. int major = 0, minor = 0;
  48. int len;
  49. if (dp->d_type != DT_LNK)
  50. continue;
  51. if (sscanf(dp->d_name, "%d:%d", &major, &minor) != 2)
  52. continue;
  53. strcpy(path, dp->d_name);
  54. len = readlink(buf2, buf, sizeof(buf));
  55. if (len <= 0)
  56. continue;
  57. buf[len] = 0;
  58. c = strrchr(buf, '/');
  59. if (!c)
  60. continue;
  61. c++;
  62. make_dev(c, block, major, minor);
  63. }
  64. closedir(dir);
  65. }
  66. int mkblkdev(void)
  67. {
  68. if (chdir("/dev"))
  69. return 1;
  70. mode = 0600;
  71. find_devs(true);
  72. return chdir("/");
  73. }