ProcFS.cpp 35 KB

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