ProcFS.cpp 35 KB

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