procfs.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include <map>
  2. #include <errno.h>
  3. #include <stdint.h>
  4. #include <sys/mount.h>
  5. #include <unistd.h>
  6. #include <kernel/module.hpp>
  7. #include <kernel/vfs.hpp>
  8. #include <kernel/vfs/vfs.hpp>
  9. struct mount_flags_opt {
  10. unsigned long flag;
  11. const char* name;
  12. };
  13. static struct mount_flags_opt mount_opts[] = {
  14. {MS_NOSUID, ",nosuid"},
  15. {MS_NODEV, ",nodev"},
  16. {MS_NOEXEC, ",noexec"},
  17. {MS_NOATIME, ",noatime"},
  18. {MS_RELATIME, ",relatime"},
  19. {MS_LAZYTIME, ",lazytime"},
  20. };
  21. static std::string get_mount_opts(unsigned long mnt_flags)
  22. {
  23. std::string retval;
  24. if (mnt_flags & MS_RDONLY)
  25. retval += "ro";
  26. else
  27. retval += "rw";
  28. for (const auto& opt : mount_opts) {
  29. if (mnt_flags & opt.flag)
  30. retval += opt.name;
  31. }
  32. return retval;
  33. }
  34. static ssize_t mounts_read(char* page, size_t n)
  35. {
  36. auto orig_n = n;
  37. for (const auto& [ _, mdata ] : fs::mounts) {
  38. if (n == 0)
  39. break;
  40. // TODO: get vfs options
  41. auto mount_flags = get_mount_opts(mdata.flags);
  42. int nwrote = snprintf(page, n, "%s %s %s %s 0 0\n",
  43. mdata.source.c_str(), mdata.mount_point.c_str(),
  44. mdata.fstype.c_str(), mount_flags.c_str());
  45. n -= nwrote;
  46. page += nwrote;
  47. }
  48. return orig_n - n;
  49. }
  50. namespace fs::proc {
  51. struct proc_file {
  52. std::string name;
  53. ssize_t (*read)(char* page_buffer, size_t n);
  54. ssize_t (*write)(const char* data, size_t n);
  55. };
  56. class procfs : public virtual fs::vfs {
  57. private:
  58. std::string source;
  59. std::map<ino_t, proc_file> files;
  60. ino_t free_ino = 1;
  61. public:
  62. static procfs* create(const char* source, unsigned long, const void*)
  63. {
  64. // TODO: flags
  65. return new procfs(source);
  66. }
  67. int create_file(std::string name,
  68. ssize_t (*read_func)(char*, size_t),
  69. ssize_t (*write_func)(const char*, size_t))
  70. {
  71. auto ino = free_ino++;
  72. auto [ _, inserted ] =
  73. files.insert({ino, proc_file {name, read_func, write_func}});
  74. cache_inode(0, ino, S_IFREG | 0666, 0, 0);
  75. return inserted ? 0 : -EEXIST;
  76. }
  77. procfs(const char* _source)
  78. : source{_source}
  79. {
  80. auto* ind = cache_inode(0, 0, S_IFDIR | 0777, 0, 0);
  81. create_file("mounts", mounts_read, nullptr);
  82. register_root_node(ind);
  83. }
  84. size_t read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n) override
  85. {
  86. if (file->ino == 0)
  87. return -EISDIR;
  88. auto iter = files.find(file->ino);
  89. if (!iter)
  90. return -EIO;
  91. auto& [ ino, pf ] = *iter;
  92. if (!pf.read)
  93. return -EINVAL;
  94. // TODO: fix it
  95. if (offset)
  96. return 0;
  97. // TODO: allocate page buffer
  98. char* page_buffer = new char[4096];
  99. ssize_t nread = pf.read(page_buffer, 4096);
  100. if (nread < 0) {
  101. delete[] page_buffer;
  102. return nread;
  103. }
  104. n = std::min(n, buf_size);
  105. n = std::min(n, (size_t)nread);
  106. strncpy(buf, page_buffer, n);
  107. delete[] page_buffer;
  108. return n;
  109. }
  110. int readdir(inode *dir, size_t offset, const filldir_func &callback) override
  111. {
  112. if (dir->ino != 0)
  113. return -ENOTDIR;
  114. // TODO: fix it
  115. if (offset)
  116. return 0;
  117. int nread = 0;
  118. for (const auto& [ ino, pf ] : files) {
  119. auto* ind = get_inode(ino);
  120. int ret = callback(pf.name.c_str(), 0, ind, ind->mode);
  121. if (ret != 0)
  122. return -EIO;
  123. ++nread;
  124. }
  125. return nread;
  126. }
  127. virtual int inode_statx(dentry* dent, statx* st, unsigned int mask) override
  128. {
  129. auto* ind = dent->ind;
  130. st->stx_mask = 0;
  131. if (mask & STATX_NLINK) {
  132. st->stx_nlink = ind->nlink;
  133. st->stx_mask |= STATX_NLINK;
  134. }
  135. // TODO: set modification time
  136. if (mask & STATX_MTIME) {
  137. st->stx_mtime = {};
  138. st->stx_mask |= STATX_MTIME;
  139. }
  140. if (mask & STATX_SIZE) {
  141. st->stx_size = ind->size;
  142. st->stx_mask |= STATX_SIZE;
  143. }
  144. st->stx_mode = 0;
  145. if (mask & STATX_MODE) {
  146. st->stx_mode |= ind->mode & ~S_IFMT;
  147. st->stx_mask |= STATX_MODE;
  148. }
  149. if (mask & STATX_TYPE) {
  150. st->stx_mode |= ind->mode & S_IFMT;
  151. st->stx_mask |= STATX_TYPE;
  152. }
  153. if (mask & STATX_INO) {
  154. st->stx_ino = ind->ino;
  155. st->stx_mask |= STATX_INO;
  156. }
  157. if (mask & STATX_BLOCKS) {
  158. st->stx_blocks = 0;
  159. st->stx_blksize = 4096;
  160. st->stx_mask |= STATX_BLOCKS;
  161. }
  162. if (mask & STATX_UID) {
  163. st->stx_uid = ind->uid;
  164. st->stx_mask |= STATX_UID;
  165. }
  166. if (mask & STATX_GID) {
  167. st->stx_gid = ind->gid;
  168. st->stx_mask |= STATX_GID;
  169. }
  170. return 0;
  171. }
  172. };
  173. class procfs_module : public virtual kernel::module::module {
  174. public:
  175. procfs_module() : module("procfs") { }
  176. virtual int init() override
  177. {
  178. int ret = fs::register_fs("procfs", procfs::create);
  179. if (ret != 0)
  180. return kernel::module::MODULE_FAILED;
  181. return kernel::module::MODULE_SUCCESS;
  182. }
  183. };
  184. static kernel::module::module* procfs_init()
  185. {
  186. return new procfs_module;
  187. }
  188. INTERNAL_MODULE(procfs, procfs_init);
  189. } // namespace fs::proc