ProcFS.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. #include "ProcFS.h"
  2. #include "Process.h"
  3. #include <Kernel/VirtualFileSystem.h>
  4. #include "system.h"
  5. #include "MemoryManager.h"
  6. #include "StdLib.h"
  7. #include "i386.h"
  8. #include "KSyms.h"
  9. #include "Console.h"
  10. #include <AK/StringBuilder.h>
  11. #include <LibC/errno_numbers.h>
  12. enum ProcParentDirectory {
  13. PDI_AbstractRoot = 0,
  14. PDI_Root,
  15. PDI_Root_sys,
  16. PDI_PID,
  17. PDI_PID_fd,
  18. };
  19. enum ProcFileType {
  20. FI_Invalid = 0,
  21. FI_Root = 1, // directory
  22. __FI_Root_Start,
  23. FI_Root_mm,
  24. FI_Root_mounts,
  25. FI_Root_kmalloc,
  26. FI_Root_summary,
  27. FI_Root_cpuinfo,
  28. FI_Root_inodes,
  29. FI_Root_dmesg,
  30. FI_Root_self, // symlink
  31. FI_Root_sys, // directory
  32. __FI_Root_End,
  33. FI_PID,
  34. __FI_PID_Start,
  35. FI_PID_vm,
  36. FI_PID_vmo,
  37. FI_PID_stack,
  38. FI_PID_regs,
  39. FI_PID_fds,
  40. FI_PID_exe, // symlink
  41. FI_PID_cwd, // symlink
  42. FI_PID_fd, // directory
  43. __FI_PID_End,
  44. FI_MaxStaticFileIndex,
  45. };
  46. static inline pid_t to_pid(const InodeIdentifier& identifier)
  47. {
  48. #ifdef PROCFS_DEBUG
  49. dbgprintf("to_pid, index=%08x -> %u\n", identifier.index(), identifier.index() >> 16);
  50. #endif
  51. return identifier.index() >> 16u;
  52. }
  53. static inline ProcParentDirectory to_proc_parent_directory(const InodeIdentifier& identifier)
  54. {
  55. return (ProcParentDirectory)((identifier.index() >> 12) & 0xf);
  56. }
  57. static inline int to_fd(const InodeIdentifier& identifier)
  58. {
  59. ASSERT(to_proc_parent_directory(identifier) == PDI_PID_fd);
  60. return (identifier.index() & 0xff) - FI_MaxStaticFileIndex;
  61. }
  62. static inline unsigned to_sys_index(const InodeIdentifier& identifier)
  63. {
  64. ASSERT(to_proc_parent_directory(identifier) == PDI_Root_sys);
  65. return identifier.index() & 0xff;
  66. }
  67. static inline InodeIdentifier to_identifier(unsigned fsid, ProcParentDirectory parent, pid_t pid, ProcFileType proc_file_type)
  68. {
  69. return { fsid, ((unsigned)parent << 12u) | ((unsigned)pid << 16u) | (unsigned)proc_file_type };
  70. }
  71. static inline InodeIdentifier to_identifier_with_fd(unsigned fsid, pid_t pid, int fd)
  72. {
  73. return { fsid, (PDI_PID_fd << 12u) | ((unsigned)pid << 16u) | (FI_MaxStaticFileIndex + fd) };
  74. }
  75. static inline InodeIdentifier sys_var_to_identifier(unsigned fsid, unsigned index)
  76. {
  77. ASSERT(index < 256);
  78. return { fsid, (PDI_Root_sys << 12u) | index };
  79. }
  80. static inline InodeIdentifier to_parent_id(const InodeIdentifier& identifier)
  81. {
  82. switch (to_proc_parent_directory(identifier)) {
  83. case PDI_AbstractRoot:
  84. case PDI_Root:
  85. return { identifier.fsid(), FI_Root };
  86. case PDI_Root_sys:
  87. return { identifier.fsid(), FI_Root_sys };
  88. case PDI_PID:
  89. return to_identifier(identifier.fsid(), PDI_Root, to_pid(identifier), FI_PID);
  90. case PDI_PID_fd:
  91. return to_identifier(identifier.fsid(), PDI_PID, to_pid(identifier), FI_PID_fd);
  92. }
  93. }
  94. #if 0
  95. static inline byte to_unused_metadata(const InodeIdentifier& identifier)
  96. {
  97. return (identifier.index() >> 8) & 0xf;
  98. }
  99. #endif
  100. static inline ProcFileType to_proc_file_type(const InodeIdentifier& identifier)
  101. {
  102. return (ProcFileType)(identifier.index() & 0xff);
  103. }
  104. static inline bool is_process_related_file(const InodeIdentifier& identifier)
  105. {
  106. if (to_proc_file_type(identifier) == FI_PID)
  107. return true;
  108. auto proc_parent_directory = to_proc_parent_directory(identifier);
  109. switch (proc_parent_directory) {
  110. case PDI_PID:
  111. case PDI_PID_fd:
  112. return true;
  113. default:
  114. return false;
  115. }
  116. }
  117. static inline bool is_directory(const InodeIdentifier& identifier)
  118. {
  119. auto proc_file_type = to_proc_file_type(identifier);
  120. switch (proc_file_type) {
  121. case FI_Root:
  122. case FI_Root_sys:
  123. case FI_PID:
  124. case FI_PID_fd:
  125. return true;
  126. default:
  127. return false;
  128. }
  129. }
  130. static inline bool is_persistent_inode(const InodeIdentifier& identifier)
  131. {
  132. return to_proc_parent_directory(identifier) == PDI_Root_sys;
  133. }
  134. static ProcFS* s_the;
  135. ProcFS& ProcFS::the()
  136. {
  137. ASSERT(s_the);
  138. return *s_the;
  139. }
  140. RetainPtr<ProcFS> ProcFS::create()
  141. {
  142. return adopt(*new ProcFS);
  143. }
  144. ProcFS::~ProcFS()
  145. {
  146. }
  147. ByteBuffer procfs$pid_fds(InodeIdentifier identifier)
  148. {
  149. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  150. if (!handle)
  151. return { };
  152. auto& process = handle->process();
  153. if (process.number_of_open_file_descriptors() == 0)
  154. return { };
  155. StringBuilder builder;
  156. for (size_t i = 0; i < process.max_open_file_descriptors(); ++i) {
  157. auto* descriptor = process.file_descriptor(i);
  158. if (!descriptor)
  159. continue;
  160. builder.appendf("% 3u %s\n", i, descriptor->absolute_path().characters());
  161. }
  162. return builder.to_byte_buffer();
  163. }
  164. ByteBuffer procfs$pid_fd_entry(InodeIdentifier identifier)
  165. {
  166. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  167. if (!handle)
  168. return { };
  169. auto& process = handle->process();
  170. int fd = to_fd(identifier);
  171. auto* descriptor = process.file_descriptor(fd);
  172. if (!descriptor)
  173. return { };
  174. return descriptor->absolute_path().to_byte_buffer();
  175. }
  176. ByteBuffer procfs$pid_vm(InodeIdentifier identifier)
  177. {
  178. #ifdef PROCFS_DEBUG
  179. dbgprintf("pid_vm: pid=%d\n", to_pid(identifier));
  180. #endif
  181. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  182. if (!handle)
  183. return { };
  184. auto& process = handle->process();
  185. StringBuilder builder;
  186. builder.appendf("BEGIN END SIZE COMMIT NAME\n");
  187. for (auto& region : process.regions()) {
  188. builder.appendf("%x -- %x %x %x %s\n",
  189. region->laddr().get(),
  190. region->laddr().offset(region->size() - 1).get(),
  191. region->size(),
  192. region->committed(),
  193. region->name().characters());
  194. }
  195. return builder.to_byte_buffer();
  196. }
  197. ByteBuffer procfs$pid_vmo(InodeIdentifier identifier)
  198. {
  199. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  200. if (!handle)
  201. return { };
  202. auto& process = handle->process();
  203. StringBuilder builder;
  204. builder.appendf("BEGIN END SIZE NAME\n");
  205. for (auto& region : process.regions()) {
  206. builder.appendf("%x -- %x %x %s\n",
  207. region->laddr().get(),
  208. region->laddr().offset(region->size() - 1).get(),
  209. region->size(),
  210. region->name().characters());
  211. builder.appendf("VMO: %s \"%s\" @ %x(%u)\n",
  212. region->vmo().is_anonymous() ? "anonymous" : "file-backed",
  213. region->vmo().name().characters(),
  214. &region->vmo(),
  215. region->vmo().retain_count());
  216. for (size_t i = 0; i < region->vmo().page_count(); ++i) {
  217. auto& physical_page = region->vmo().physical_pages()[i];
  218. builder.appendf("P%x%s(%u) ",
  219. physical_page ? physical_page->paddr().get() : 0,
  220. region->cow_map().get(i) ? "!" : "",
  221. physical_page ? physical_page->retain_count() : 0
  222. );
  223. }
  224. builder.appendf("\n");
  225. }
  226. return builder.to_byte_buffer();
  227. }
  228. ByteBuffer procfs$pid_stack(InodeIdentifier identifier)
  229. {
  230. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  231. if (!handle)
  232. return { };
  233. auto& process = handle->process();
  234. ProcessPagingScope paging_scope(process);
  235. struct RecognizedSymbol {
  236. dword address;
  237. const KSym* ksym;
  238. };
  239. Vector<RecognizedSymbol> recognized_symbols;
  240. if (auto* eip_ksym = ksymbolicate(process.tss().eip))
  241. recognized_symbols.append({ process.tss().eip, eip_ksym });
  242. for (dword* stack_ptr = (dword*)process.frame_ptr(); process.validate_read_from_kernel(LinearAddress((dword)stack_ptr)); stack_ptr = (dword*)*stack_ptr) {
  243. dword retaddr = stack_ptr[1];
  244. if (auto* ksym = ksymbolicate(retaddr))
  245. recognized_symbols.append({ retaddr, ksym });
  246. }
  247. StringBuilder builder;
  248. for (auto& symbol : recognized_symbols) {
  249. unsigned offset = symbol.address - symbol.ksym->address;
  250. builder.appendf("%p %s +%u\n", symbol.address, symbol.ksym->name, offset);
  251. }
  252. return builder.to_byte_buffer();
  253. }
  254. ByteBuffer procfs$pid_regs(InodeIdentifier identifier)
  255. {
  256. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  257. if (!handle)
  258. return { };
  259. auto& process = handle->process();
  260. auto& tss = process.tss();
  261. StringBuilder builder;
  262. builder.appendf("eax: %x\n", tss.eax);
  263. builder.appendf("ebx: %x\n", tss.ebx);
  264. builder.appendf("ecx: %x\n", tss.ecx);
  265. builder.appendf("edx: %x\n", tss.edx);
  266. builder.appendf("esi: %x\n", tss.esi);
  267. builder.appendf("edi: %x\n", tss.edi);
  268. builder.appendf("ebp: %x\n", tss.ebp);
  269. builder.appendf("cr3: %x\n", tss.cr3);
  270. builder.appendf("flg: %x\n", tss.eflags);
  271. builder.appendf("sp: %w:%x\n", tss.ss, tss.esp);
  272. builder.appendf("pc: %w:%x\n", tss.cs, tss.eip);
  273. return builder.to_byte_buffer();
  274. }
  275. ByteBuffer procfs$pid_exe(InodeIdentifier identifier)
  276. {
  277. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  278. if (!handle)
  279. return { };
  280. auto& process = handle->process();
  281. auto inode = process.executable_inode();
  282. ASSERT(inode);
  283. return VFS::the().absolute_path(*inode).to_byte_buffer();
  284. }
  285. ByteBuffer procfs$pid_cwd(InodeIdentifier identifier)
  286. {
  287. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  288. if (!handle)
  289. return { };
  290. auto& process = handle->process();
  291. auto inode = process.cwd_inode();
  292. ASSERT(inode);
  293. return VFS::the().absolute_path(*inode).to_byte_buffer();
  294. }
  295. ByteBuffer procfs$self(InodeIdentifier)
  296. {
  297. char buffer[16];
  298. ksprintf(buffer, "%u", current->pid());
  299. return ByteBuffer::copy((const byte*)buffer, strlen(buffer));
  300. }
  301. ByteBuffer procfs$mm(InodeIdentifier)
  302. {
  303. // FIXME: Implement
  304. InterruptDisabler disabler;
  305. StringBuilder builder;
  306. for (auto* vmo : MM.m_vmos) {
  307. builder.appendf("VMO: %p %s(%u): p:%4u %s\n",
  308. vmo,
  309. vmo->is_anonymous() ? "anon" : "file",
  310. vmo->retain_count(),
  311. vmo->page_count(),
  312. vmo->name().characters());
  313. }
  314. builder.appendf("VMO count: %u\n", MM.m_vmos.size());
  315. builder.appendf("Free physical pages: %u\n", MM.m_free_physical_pages.size());
  316. builder.appendf("Free supervisor physical pages: %u\n", MM.m_free_supervisor_physical_pages.size());
  317. return builder.to_byte_buffer();
  318. }
  319. ByteBuffer procfs$dmesg(InodeIdentifier)
  320. {
  321. InterruptDisabler disabler;
  322. StringBuilder builder;
  323. for (char ch : Console::the().logbuffer())
  324. builder.append(ch);
  325. return builder.to_byte_buffer();
  326. }
  327. ByteBuffer procfs$mounts(InodeIdentifier)
  328. {
  329. // FIXME: This is obviously racy against the VFS mounts changing.
  330. StringBuilder builder;
  331. VFS::the().for_each_mount([&builder] (auto& mount) {
  332. auto& fs = mount.guest_fs();
  333. builder.appendf("%s @ ", fs.class_name());
  334. if (!mount.host().is_valid())
  335. builder.appendf("/");
  336. else {
  337. builder.appendf("%u:%u", mount.host().fsid(), mount.host().index());
  338. auto path = VFS::the().absolute_path(mount.host());
  339. builder.append(' ');
  340. builder.append(path);
  341. }
  342. builder.append('\n');
  343. });
  344. return builder.to_byte_buffer();
  345. }
  346. ByteBuffer procfs$cpuinfo(InodeIdentifier)
  347. {
  348. StringBuilder builder;
  349. {
  350. CPUID cpuid(0);
  351. builder.appendf("cpuid: ");
  352. auto emit_dword = [&] (dword value) {
  353. builder.appendf("%c%c%c%c",
  354. value & 0xff,
  355. (value >> 8) & 0xff,
  356. (value >> 16) & 0xff,
  357. (value >> 24) & 0xff);
  358. };
  359. emit_dword(cpuid.ebx());
  360. emit_dword(cpuid.edx());
  361. emit_dword(cpuid.ecx());
  362. builder.appendf("\n");
  363. }
  364. {
  365. CPUID cpuid(1);
  366. dword stepping = cpuid.eax() & 0xf;
  367. dword model = (cpuid.eax() >> 4) & 0xf;
  368. dword family = (cpuid.eax() >> 8) & 0xf;
  369. dword type = (cpuid.eax() >> 12) & 0x3;
  370. dword extended_model = (cpuid.eax() >> 16) & 0xf;
  371. dword extended_family = (cpuid.eax() >> 20) & 0xff;
  372. dword display_model;
  373. dword display_family;
  374. if (family == 15) {
  375. display_family = family + extended_family;
  376. display_model = model + (extended_model << 4);
  377. } else if (family == 6) {
  378. display_family = family;
  379. display_model = model + (extended_model << 4);
  380. } else {
  381. display_family = family;
  382. display_model = model;
  383. }
  384. builder.appendf("family: %u\n", display_family);
  385. builder.appendf("model: %u\n", display_model);
  386. builder.appendf("stepping: %u\n", stepping);
  387. builder.appendf("type: %u\n", type);
  388. }
  389. {
  390. // FIXME: Check first that this is supported by calling CPUID with eax=0x80000000
  391. // and verifying that the returned eax>=0x80000004.
  392. char buffer[48];
  393. dword* bufptr = reinterpret_cast<dword*>(buffer);
  394. auto copy_brand_string_part_to_buffer = [&] (dword i) {
  395. CPUID cpuid(0x80000002 + i);
  396. *bufptr++ = cpuid.eax();
  397. *bufptr++ = cpuid.ebx();
  398. *bufptr++ = cpuid.ecx();
  399. *bufptr++ = cpuid.edx();
  400. };
  401. copy_brand_string_part_to_buffer(0);
  402. copy_brand_string_part_to_buffer(1);
  403. copy_brand_string_part_to_buffer(2);
  404. builder.appendf("brandstr: \"%s\"\n", buffer);
  405. }
  406. return builder.to_byte_buffer();
  407. }
  408. ByteBuffer procfs$kmalloc(InodeIdentifier)
  409. {
  410. StringBuilder builder;
  411. builder.appendf(
  412. "eternal: %u\n"
  413. "allocated: %u\n"
  414. "free: %u\n",
  415. kmalloc_sum_eternal,
  416. sum_alloc,
  417. sum_free
  418. );
  419. return builder.to_byte_buffer();
  420. }
  421. ByteBuffer procfs$summary(InodeIdentifier)
  422. {
  423. InterruptDisabler disabler;
  424. auto processes = Process::all_processes();
  425. StringBuilder builder;
  426. builder.appendf("PID TPG PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n");
  427. for (auto* process : processes) {
  428. builder.appendf("% 3u % 3u % 3u % 3u % 4u % 8s % 3u % 9u % 3u % 4s %s\n",
  429. process->pid(),
  430. process->tty() ? process->tty()->pgid() : 0,
  431. process->pgid(),
  432. process->sid(),
  433. process->uid(),
  434. to_string(process->state()),
  435. process->ppid(),
  436. process->times_scheduled(),
  437. process->number_of_open_file_descriptors(),
  438. process->tty() ? strrchr(process->tty()->tty_name().characters(), '/') + 1 : "n/a",
  439. process->name().characters());
  440. }
  441. return builder.to_byte_buffer();
  442. }
  443. ByteBuffer procfs$inodes(InodeIdentifier)
  444. {
  445. extern HashTable<Inode*>& all_inodes();
  446. auto& vfs = VFS::the();
  447. StringBuilder builder;
  448. for (auto it : all_inodes()) {
  449. RetainPtr<Inode> inode = *it;
  450. String path = vfs.absolute_path(*inode);
  451. builder.appendf("Inode{K%x} %02u:%08u (%u) %s\n", inode.ptr(), inode->fsid(), inode->index(), inode->retain_count(), path.characters());
  452. }
  453. return builder.to_byte_buffer();
  454. }
  455. struct SysVariableData final : public ProcFSInodeCustomData {
  456. virtual ~SysVariableData() override { }
  457. enum Type {
  458. Invalid,
  459. Boolean,
  460. };
  461. Type type { Invalid };
  462. Function<void()> notify_callback;
  463. void* address;
  464. };
  465. static ByteBuffer read_sys_bool(InodeIdentifier inode_id)
  466. {
  467. auto inode_ptr = ProcFS::the().get_inode(inode_id);
  468. if (!inode_ptr)
  469. return { };
  470. auto& inode = static_cast<ProcFSInode&>(*inode_ptr);
  471. ASSERT(inode.custom_data());
  472. auto buffer = ByteBuffer::create_uninitialized(2);
  473. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  474. ASSERT(custom_data.type == SysVariableData::Boolean);
  475. ASSERT(custom_data.address);
  476. buffer[0] = *reinterpret_cast<bool*>(custom_data.address) ? '1' : '0';
  477. buffer[1] = '\n';
  478. return buffer;
  479. }
  480. static ssize_t write_sys_bool(InodeIdentifier inode_id, const ByteBuffer& data)
  481. {
  482. auto inode_ptr = ProcFS::the().get_inode(inode_id);
  483. if (!inode_ptr)
  484. return { };
  485. auto& inode = static_cast<ProcFSInode&>(*inode_ptr);
  486. ASSERT(inode.custom_data());
  487. if (data.size() >= 1 && (data[0] == '0' || data[0] == '1')) {
  488. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  489. ASSERT(custom_data.address);
  490. bool new_value = data[0] == '1';
  491. *reinterpret_cast<bool*>(custom_data.address) = new_value;
  492. if (custom_data.notify_callback)
  493. custom_data.notify_callback();
  494. }
  495. return data.size();
  496. }
  497. void ProcFS::add_sys_bool(String&& name, bool* var, Function<void()>&& notify_callback)
  498. {
  499. InterruptDisabler disabler;
  500. unsigned index = m_sys_entries.size();
  501. auto inode = adopt(*new ProcFSInode(*this, sys_var_to_identifier(fsid(), index).index()));
  502. auto data = make<SysVariableData>();
  503. data->type = SysVariableData::Boolean;
  504. data->notify_callback = move(notify_callback);
  505. data->address = var;
  506. inode->set_custom_data(move(data));
  507. m_sys_entries.append({ strdup(name.characters()), name.length(), read_sys_bool, write_sys_bool, move(inode) });
  508. }
  509. bool ProcFS::initialize()
  510. {
  511. return true;
  512. }
  513. const char* ProcFS::class_name() const
  514. {
  515. return "ProcFS";
  516. }
  517. RetainPtr<Inode> ProcFS::create_inode(InodeIdentifier parentInode, const String& name, mode_t mode, unsigned size, int& error)
  518. {
  519. (void) parentInode;
  520. (void) name;
  521. (void) mode;
  522. (void) size;
  523. (void) error;
  524. kprintf("FIXME: Implement ProcFS::create_inode()?\n");
  525. return { };
  526. }
  527. RetainPtr<Inode> ProcFS::create_directory(InodeIdentifier, const String&, mode_t, int& error)
  528. {
  529. error = -EROFS;
  530. return nullptr;
  531. }
  532. RetainPtr<Inode> ProcFSInode::parent() const
  533. {
  534. return fs().get_inode(to_parent_id(identifier()));
  535. }
  536. InodeIdentifier ProcFS::root_inode() const
  537. {
  538. return { fsid(), FI_Root };
  539. }
  540. RetainPtr<Inode> ProcFS::get_inode(InodeIdentifier inode_id) const
  541. {
  542. #ifdef PROCFS_DEBUG
  543. dbgprintf("ProcFS::get_inode(%u)\n", inode_id.index());
  544. #endif
  545. if (inode_id == root_inode())
  546. return m_root_inode;
  547. if (to_proc_parent_directory(inode_id) == ProcParentDirectory::PDI_Root_sys) {
  548. auto sys_index = to_sys_index(inode_id);
  549. if (sys_index < m_sys_entries.size())
  550. return m_sys_entries[sys_index].inode;
  551. }
  552. LOCKER(m_inodes_lock);
  553. auto it = m_inodes.find(inode_id.index());
  554. if (it == m_inodes.end()) {
  555. auto inode = adopt(*new ProcFSInode(const_cast<ProcFS&>(*this), inode_id.index()));
  556. m_inodes.set(inode_id.index(), inode.ptr());
  557. return inode;
  558. }
  559. return (*it).value;
  560. }
  561. ProcFSInode::ProcFSInode(ProcFS& fs, unsigned index)
  562. : Inode(fs, index)
  563. {
  564. }
  565. ProcFSInode::~ProcFSInode()
  566. {
  567. LOCKER(fs().m_inodes_lock);
  568. fs().m_inodes.remove(index());
  569. }
  570. InodeMetadata ProcFSInode::metadata() const
  571. {
  572. #ifdef PROCFS_DEBUG
  573. dbgprintf("ProcFSInode::metadata(%u)\n", index());
  574. #endif
  575. InodeMetadata metadata;
  576. metadata.inode = identifier();
  577. metadata.ctime = mepoch;
  578. metadata.atime = mepoch;
  579. metadata.mtime = mepoch;
  580. auto proc_parent_directory = to_proc_parent_directory(identifier());
  581. auto pid = to_pid(identifier());
  582. auto proc_file_type = to_proc_file_type(identifier());
  583. #ifdef PROCFS_DEBUG
  584. dbgprintf(" -> pid: %d, fi: %u, pdi: %u\n", pid, proc_file_type, proc_parent_directory);
  585. #endif
  586. if (is_process_related_file(identifier())) {
  587. auto handle = ProcessInspectionHandle::from_pid(pid);
  588. metadata.uid = handle->process().sys$getuid();
  589. metadata.gid = handle->process().sys$getgid();
  590. }
  591. if (proc_parent_directory == PDI_PID_fd) {
  592. metadata.mode = 00120777;
  593. return metadata;
  594. }
  595. switch (proc_file_type) {
  596. case FI_Root_self:
  597. case FI_PID_cwd:
  598. case FI_PID_exe:
  599. metadata.mode = 0120777;
  600. break;
  601. case FI_Root:
  602. case FI_Root_sys:
  603. case FI_PID:
  604. case FI_PID_fd:
  605. metadata.mode = 040777;
  606. break;
  607. default:
  608. metadata.mode = 0100644;
  609. break;
  610. }
  611. #ifdef PROCFS_DEBUG
  612. dbgprintf("Returning mode %o\n", metadata.mode);
  613. #endif
  614. return metadata;
  615. }
  616. ssize_t ProcFSInode::read_bytes(off_t offset, size_t count, byte* buffer, FileDescriptor* descriptor) const
  617. {
  618. #ifdef PROCFS_DEBUG
  619. dbgprintf("ProcFS: read_bytes %u\n", index());
  620. #endif
  621. ASSERT(offset >= 0);
  622. ASSERT(buffer);
  623. auto* directory_entry = fs().get_directory_entry(identifier());
  624. Function<ByteBuffer(InodeIdentifier)> callback_tmp;
  625. Function<ByteBuffer(InodeIdentifier)>* read_callback { nullptr };
  626. if (directory_entry) {
  627. read_callback = &directory_entry->read_callback;
  628. } else {
  629. if (to_proc_parent_directory(identifier()) == PDI_PID_fd) {
  630. callback_tmp = procfs$pid_fd_entry;
  631. read_callback = &callback_tmp;
  632. }
  633. }
  634. ASSERT(read_callback);
  635. ByteBuffer generated_data;
  636. if (!descriptor) {
  637. generated_data = (*read_callback)(identifier());
  638. } else {
  639. if (!descriptor->generator_cache())
  640. descriptor->generator_cache() = (*read_callback)(identifier());
  641. generated_data = descriptor->generator_cache();
  642. }
  643. auto& data = generated_data;
  644. ssize_t nread = min(static_cast<off_t>(data.size() - offset), static_cast<off_t>(count));
  645. memcpy(buffer, data.pointer() + offset, nread);
  646. if (nread == 0 && descriptor && descriptor->generator_cache())
  647. descriptor->generator_cache().clear();
  648. return nread;
  649. }
  650. InodeIdentifier ProcFS::ProcFSDirectoryEntry::identifier(unsigned fsid) const
  651. {
  652. return to_identifier(fsid, PDI_Root, 0, (ProcFileType)proc_file_type);
  653. }
  654. bool ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)> callback) const
  655. {
  656. #ifdef PROCFS_DEBUG
  657. dbgprintf("ProcFS: traverse_as_directory %u\n", index());
  658. #endif
  659. if (!::is_directory(identifier()))
  660. return false;
  661. auto pid = to_pid(identifier());
  662. auto proc_file_type = to_proc_file_type(identifier());
  663. auto parent_id = to_parent_id(identifier());
  664. callback({ ".", 1, identifier(), 2 });
  665. callback({ "..", 2, parent_id, 2 });
  666. switch (proc_file_type) {
  667. case FI_Root:
  668. for (auto& entry : fs().m_entries) {
  669. // FIXME: strlen() here is sad.
  670. if (!entry.name)
  671. continue;
  672. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End)
  673. callback({ entry.name, strlen(entry.name), to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type), 0 });
  674. }
  675. for (auto pid_child : Process::all_pids()) {
  676. char name[16];
  677. size_t name_length = ksprintf(name, "%u", pid_child);
  678. callback({ name, name_length, to_identifier(fsid(), PDI_Root, pid_child, FI_PID), 0 });
  679. }
  680. break;
  681. case FI_Root_sys:
  682. for (size_t i = 0; i < fs().m_sys_entries.size(); ++i) {
  683. auto& entry = fs().m_sys_entries[i];
  684. callback({ entry.name, strlen(entry.name), sys_var_to_identifier(fsid(), i), 0 });
  685. }
  686. break;
  687. case FI_PID: {
  688. auto handle = ProcessInspectionHandle::from_pid(pid);
  689. if (!handle)
  690. return false;
  691. auto& process = handle->process();
  692. for (auto& entry : fs().m_entries) {
  693. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  694. if (entry.proc_file_type == FI_PID_exe || entry.proc_file_type == FI_PID_cwd) {
  695. if (entry.proc_file_type == FI_PID_exe && !process.executable_inode())
  696. continue;
  697. if (entry.proc_file_type == FI_PID_cwd && !process.cwd_inode())
  698. continue;
  699. }
  700. // FIXME: strlen() here is sad.
  701. callback({ entry.name, strlen(entry.name), to_identifier(fsid(), PDI_PID, pid, (ProcFileType)entry.proc_file_type), 0 });
  702. }
  703. }
  704. }
  705. break;
  706. case FI_PID_fd: {
  707. auto handle = ProcessInspectionHandle::from_pid(pid);
  708. if (!handle)
  709. return false;
  710. auto& process = handle->process();
  711. for (size_t i = 0; i < process.max_open_file_descriptors(); ++i) {
  712. auto* descriptor = process.file_descriptor(i);
  713. if (!descriptor)
  714. continue;
  715. char name[16];
  716. size_t name_length = ksprintf(name, "%u", i);
  717. callback({ name, name_length, to_identifier_with_fd(fsid(), pid, i), 0 });
  718. }
  719. }
  720. break;
  721. default:
  722. return true;
  723. }
  724. return true;
  725. }
  726. InodeIdentifier ProcFSInode::lookup(const String& name)
  727. {
  728. ASSERT(is_directory());
  729. if (name == ".")
  730. return identifier();
  731. if (name == "..")
  732. return to_parent_id(identifier());
  733. auto proc_file_type = to_proc_file_type(identifier());
  734. if (proc_file_type == FI_Root) {
  735. for (auto& entry : fs().m_entries) {
  736. if (entry.name == nullptr)
  737. continue;
  738. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End) {
  739. if (!strcmp(entry.name, name.characters())) {
  740. return to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type);
  741. }
  742. }
  743. }
  744. bool ok;
  745. unsigned name_as_number = name.to_uint(ok);
  746. if (ok) {
  747. bool process_exists = false;
  748. {
  749. InterruptDisabler disabler;
  750. process_exists = Process::from_pid(name_as_number);
  751. }
  752. if (process_exists)
  753. return to_identifier(fsid(), PDI_Root, name_as_number, FI_PID);
  754. }
  755. return { };
  756. }
  757. if (proc_file_type == FI_Root_sys) {
  758. for (size_t i = 0; i < fs().m_sys_entries.size(); ++i) {
  759. auto& entry = fs().m_sys_entries[i];
  760. if (!strcmp(entry.name, name.characters()))
  761. return sys_var_to_identifier(fsid(), i);
  762. }
  763. return { };
  764. }
  765. if (proc_file_type == FI_PID) {
  766. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier()));
  767. if (!handle)
  768. return { };
  769. auto& process = handle->process();
  770. for (auto& entry : fs().m_entries) {
  771. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  772. if (entry.proc_file_type == FI_PID_exe || entry.proc_file_type == FI_PID_cwd) {
  773. if (entry.proc_file_type == FI_PID_exe && !process.executable_inode())
  774. continue;
  775. if (entry.proc_file_type == FI_PID_cwd && !process.cwd_inode())
  776. continue;
  777. }
  778. if (entry.name == nullptr)
  779. continue;
  780. if (!strcmp(entry.name, name.characters())) {
  781. return to_identifier(fsid(), PDI_PID, to_pid(identifier()), (ProcFileType)entry.proc_file_type);
  782. }
  783. }
  784. }
  785. return { };
  786. }
  787. if (proc_file_type == FI_PID_fd) {
  788. bool ok;
  789. unsigned name_as_number = name.to_uint(ok);
  790. if (ok) {
  791. bool fd_exists = false;
  792. {
  793. InterruptDisabler disabler;
  794. if (auto* process = Process::from_pid(to_pid(identifier())))
  795. fd_exists = process->file_descriptor(name_as_number);
  796. }
  797. if (fd_exists)
  798. return to_identifier_with_fd(fsid(), to_pid(identifier()), name_as_number);
  799. }
  800. }
  801. return { };
  802. }
  803. String ProcFSInode::reverse_lookup(InodeIdentifier child_id)
  804. {
  805. ASSERT(is_directory());
  806. auto proc_file_type = to_proc_file_type(identifier());
  807. if (proc_file_type == FI_Root) {
  808. for (auto& entry : fs().m_entries) {
  809. if (child_id == to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type)) {
  810. return entry.name;
  811. }
  812. }
  813. auto child_proc_file_type = to_proc_file_type(child_id);
  814. if (child_proc_file_type == FI_PID)
  815. return String::format("%u", to_pid(child_id));
  816. return { };
  817. }
  818. // FIXME: Implement
  819. ASSERT_NOT_REACHED();
  820. return { };
  821. }
  822. void ProcFSInode::flush_metadata()
  823. {
  824. }
  825. ssize_t ProcFSInode::write_bytes(off_t offset, size_t size, const byte* buffer, FileDescriptor*)
  826. {
  827. auto* directory_entry = fs().get_directory_entry(identifier());
  828. if (!directory_entry || !directory_entry->write_callback)
  829. return -EPERM;
  830. ASSERT(is_persistent_inode(identifier()));
  831. // FIXME: Being able to write into ProcFS at a non-zero offset seems like something we should maybe support..
  832. ASSERT(offset == 0);
  833. bool success = directory_entry->write_callback(identifier(), ByteBuffer::wrap((byte*)buffer, size));
  834. ASSERT(success);
  835. return 0;
  836. }
  837. bool ProcFSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error)
  838. {
  839. (void) child_id;
  840. (void) name;
  841. (void) file_type;
  842. (void) error;
  843. ASSERT_NOT_REACHED();
  844. return false;
  845. }
  846. bool ProcFSInode::remove_child(const String& name, int& error)
  847. {
  848. (void) name;
  849. (void) error;
  850. ASSERT_NOT_REACHED();
  851. return false;
  852. }
  853. ProcFSInodeCustomData::~ProcFSInodeCustomData()
  854. {
  855. }
  856. size_t ProcFSInode::directory_entry_count() const
  857. {
  858. ASSERT(is_directory());
  859. size_t count = 0;
  860. traverse_as_directory([&count] (const FS::DirectoryEntry&) {
  861. ++count;
  862. return true;
  863. });
  864. return count;
  865. }
  866. bool ProcFSInode::chmod(mode_t, int& error)
  867. {
  868. error = -EPERM;
  869. return false;
  870. }
  871. ProcFS::ProcFS()
  872. {
  873. s_the = this;
  874. m_root_inode = adopt(*new ProcFSInode(*this, 1));
  875. m_entries.resize(FI_MaxStaticFileIndex);
  876. m_entries[FI_Root_mm] = { "mm", FI_Root_mm, procfs$mm };
  877. m_entries[FI_Root_mounts] = { "mounts", FI_Root_mounts, procfs$mounts };
  878. m_entries[FI_Root_kmalloc] = { "kmalloc", FI_Root_kmalloc, procfs$kmalloc };
  879. m_entries[FI_Root_summary] = { "summary", FI_Root_summary, procfs$summary };
  880. m_entries[FI_Root_cpuinfo] = { "cpuinfo", FI_Root_summary, procfs$cpuinfo};
  881. m_entries[FI_Root_inodes] = { "inodes", FI_Root_inodes, procfs$inodes };
  882. m_entries[FI_Root_dmesg] = { "dmesg", FI_Root_dmesg, procfs$dmesg };
  883. m_entries[FI_Root_self] = { "self", FI_Root_self, procfs$self };
  884. m_entries[FI_Root_sys] = { "sys", FI_Root_sys };
  885. m_entries[FI_PID_vm] = { "vm", FI_PID_vm, procfs$pid_vm };
  886. m_entries[FI_PID_vmo] = { "vmo", FI_PID_vmo, procfs$pid_vmo };
  887. m_entries[FI_PID_stack] = { "stack", FI_PID_stack, procfs$pid_stack };
  888. m_entries[FI_PID_regs] = { "regs", FI_PID_regs, procfs$pid_regs };
  889. m_entries[FI_PID_fds] = { "fds", FI_PID_fds, procfs$pid_fds };
  890. m_entries[FI_PID_exe] = { "exe", FI_PID_exe, procfs$pid_exe };
  891. m_entries[FI_PID_cwd] = { "cwd", FI_PID_cwd, procfs$pid_cwd };
  892. m_entries[FI_PID_fd] = { "fd", FI_PID_fd };
  893. }
  894. ProcFS::ProcFSDirectoryEntry* ProcFS::get_directory_entry(InodeIdentifier identifier) const
  895. {
  896. if (to_proc_parent_directory(identifier) == PDI_Root_sys) {
  897. auto sys_index = to_sys_index(identifier);
  898. if (sys_index < m_sys_entries.size())
  899. return const_cast<ProcFSDirectoryEntry*>(&m_sys_entries[sys_index]);
  900. return nullptr;
  901. }
  902. auto proc_file_type = to_proc_file_type(identifier);
  903. if (proc_file_type != FI_Invalid && proc_file_type < FI_MaxStaticFileIndex)
  904. return const_cast<ProcFSDirectoryEntry*>(&m_entries[proc_file_type]);
  905. return nullptr;
  906. }