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