mkdev.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "init.h"
  27. static char **patterns;
  28. static int n_patterns;
  29. static char buf[PATH_MAX];
  30. static char buf2[PATH_MAX];
  31. static unsigned int mode = 0600;
  32. static bool find_pattern(const char *name)
  33. {
  34. int i;
  35. for (i = 0; i < n_patterns; i++)
  36. if (!fnmatch(patterns[i], name, 0))
  37. return true;
  38. return false;
  39. }
  40. static void make_dev(const char *path, bool block, int major, int minor)
  41. {
  42. unsigned int oldumask = umask(0);
  43. unsigned int _mode = mode | (block ? S_IFBLK : S_IFCHR);
  44. DEBUG(4, "Creating %s device %s(%d,%d)\n",
  45. block ? "block" : "character",
  46. path, major, minor);
  47. mknod(path, _mode, makedev(major, minor));
  48. umask(oldumask);
  49. }
  50. static void find_devs(bool block)
  51. {
  52. char *path = block ? "/sys/dev/block" : "/sys/dev/char";
  53. struct dirent *dp;
  54. DIR *dir;
  55. dir = opendir(path);
  56. if (!dir)
  57. return;
  58. path = buf2 + sprintf(buf2, "%s/", path);
  59. while ((dp = readdir(dir)) != NULL) {
  60. char *c;
  61. int major = 0, minor = 0;
  62. int len;
  63. if (dp->d_type != DT_LNK)
  64. continue;
  65. if (sscanf(dp->d_name, "%d:%d", &major, &minor) != 2)
  66. continue;
  67. strcpy(path, dp->d_name);
  68. len = readlink(buf2, buf, sizeof(buf) - 1);
  69. if (len <= 0)
  70. continue;
  71. buf[len] = 0;
  72. if (!find_pattern(buf))
  73. continue;
  74. c = strrchr(buf, '/');
  75. if (!c)
  76. continue;
  77. c++;
  78. make_dev(c, block, major, minor);
  79. }
  80. closedir(dir);
  81. }
  82. static char *add_pattern(const char *name)
  83. {
  84. char *str = malloc(strlen(name) + 2);
  85. str[0] = '*';
  86. strcpy(str + 1, name);
  87. return str;
  88. }
  89. int mkdev(const char *name, int _mode)
  90. {
  91. char *pattern;
  92. if (chdir("/dev"))
  93. return 1;
  94. pattern = add_pattern(name);
  95. patterns = &pattern;
  96. mode = _mode;
  97. n_patterns = 1;
  98. find_devs(true);
  99. find_devs(false);
  100. free(pattern);
  101. return chdir("/");
  102. }