ProcFS.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonArraySerializer.h>
  7. #include <AK/JsonObject.h>
  8. #include <AK/JsonObjectSerializer.h>
  9. #include <AK/JsonValue.h>
  10. #include <AK/ScopeGuard.h>
  11. #include <Kernel/Arch/x86/CPU.h>
  12. #include <Kernel/Arch/x86/ProcessorInfo.h>
  13. #include <Kernel/CommandLine.h>
  14. #include <Kernel/Console.h>
  15. #include <Kernel/DMI.h>
  16. #include <Kernel/Debug.h>
  17. #include <Kernel/Devices/BlockDevice.h>
  18. #include <Kernel/Devices/HID/HIDManagement.h>
  19. #include <Kernel/FileSystem/Custody.h>
  20. #include <Kernel/FileSystem/FileBackedFileSystem.h>
  21. #include <Kernel/FileSystem/FileDescription.h>
  22. #include <Kernel/FileSystem/ProcFS.h>
  23. #include <Kernel/FileSystem/VirtualFileSystem.h>
  24. #include <Kernel/Heap/kmalloc.h>
  25. #include <Kernel/Interrupts/GenericInterruptHandler.h>
  26. #include <Kernel/Interrupts/InterruptManagement.h>
  27. #include <Kernel/KBufferBuilder.h>
  28. #include <Kernel/KSyms.h>
  29. #include <Kernel/Module.h>
  30. #include <Kernel/Net/LocalSocket.h>
  31. #include <Kernel/Net/NetworkAdapter.h>
  32. #include <Kernel/Net/Routing.h>
  33. #include <Kernel/Net/TCPSocket.h>
  34. #include <Kernel/Net/UDPSocket.h>
  35. #include <Kernel/PCI/Access.h>
  36. #include <Kernel/PerformanceEventBuffer.h>
  37. #include <Kernel/Process.h>
  38. #include <Kernel/Scheduler.h>
  39. #include <Kernel/StdLib.h>
  40. #include <Kernel/TTY/TTY.h>
  41. #include <Kernel/UBSanitizer.h>
  42. #include <Kernel/VM/AnonymousVMObject.h>
  43. #include <Kernel/VM/MemoryManager.h>
  44. #include <LibC/errno_numbers.h>
  45. namespace Kernel {
  46. enum ProcParentDirectory {
  47. PDI_AbstractRoot = 0,
  48. PDI_Root,
  49. PDI_Root_sys,
  50. PDI_Root_net,
  51. PDI_PID,
  52. PDI_PID_fd,
  53. PDI_PID_stacks,
  54. };
  55. static_assert(PDI_PID_stacks < 16, "Too many directories for identifier scheme");
  56. enum ProcFileType {
  57. FI_Invalid = 0,
  58. FI_Root = 1, // directory
  59. __FI_Root_Start,
  60. FI_Root_df,
  61. FI_Root_all,
  62. FI_Root_memstat,
  63. FI_Root_cpuinfo,
  64. FI_Root_dmesg,
  65. FI_Root_interrupts,
  66. FI_Root_dmi,
  67. FI_Root_smbios_entry_point,
  68. FI_Root_keymap,
  69. FI_Root_pci,
  70. FI_Root_devices,
  71. FI_Root_uptime,
  72. FI_Root_cmdline,
  73. FI_Root_modules,
  74. FI_Root_profile,
  75. FI_Root_self, // symlink
  76. FI_Root_sys, // directory
  77. FI_Root_net, // directory
  78. __FI_Root_End,
  79. FI_Root_sys_variable,
  80. FI_Root_net_adapters,
  81. FI_Root_net_arp,
  82. FI_Root_net_tcp,
  83. FI_Root_net_udp,
  84. FI_Root_net_local,
  85. FI_PID,
  86. __FI_PID_Start,
  87. FI_PID_perf_events,
  88. FI_PID_vm,
  89. FI_PID_stacks, // directory
  90. FI_PID_fds,
  91. FI_PID_unveil,
  92. FI_PID_exe, // symlink
  93. FI_PID_cwd, // symlink
  94. FI_PID_root, // symlink
  95. FI_PID_fd, // directory
  96. __FI_PID_End,
  97. FI_MaxStaticFileIndex,
  98. };
  99. static inline ProcessID to_pid(const InodeIdentifier& identifier)
  100. {
  101. return identifier.index().value() >> 16u;
  102. }
  103. static inline ThreadID to_tid(const InodeIdentifier& identifier)
  104. {
  105. // Sneakily, use the exact same mechanism.
  106. return to_pid(identifier).value();
  107. }
  108. static inline ProcParentDirectory to_proc_parent_directory(const InodeIdentifier& identifier)
  109. {
  110. return (ProcParentDirectory)((identifier.index().value() >> 12) & 0xf);
  111. }
  112. static inline ProcFileType to_proc_file_type(const InodeIdentifier& identifier)
  113. {
  114. return (ProcFileType)(identifier.index().value() & 0xff);
  115. }
  116. static inline int to_fd(const InodeIdentifier& identifier)
  117. {
  118. VERIFY(to_proc_parent_directory(identifier) == PDI_PID_fd);
  119. return (identifier.index().value() & 0xff) - FI_MaxStaticFileIndex;
  120. }
  121. static inline size_t to_sys_index(const InodeIdentifier& identifier)
  122. {
  123. VERIFY(to_proc_parent_directory(identifier) == PDI_Root_sys);
  124. VERIFY(to_proc_file_type(identifier) == FI_Root_sys_variable);
  125. return identifier.index().value() >> 16u;
  126. }
  127. static inline InodeIdentifier to_identifier(unsigned fsid, ProcParentDirectory parent, ProcessID pid, ProcFileType proc_file_type)
  128. {
  129. return { fsid, ((unsigned)parent << 12u) | ((unsigned)pid.value() << 16u) | (unsigned)proc_file_type };
  130. }
  131. static inline InodeIdentifier to_identifier_with_fd(unsigned fsid, ProcessID pid, int fd)
  132. {
  133. return { fsid, (PDI_PID_fd << 12u) | ((unsigned)pid.value() << 16u) | (FI_MaxStaticFileIndex + fd) };
  134. }
  135. static inline InodeIdentifier to_identifier_with_stack(unsigned fsid, ThreadID tid)
  136. {
  137. return { fsid, (PDI_PID_stacks << 12u) | ((unsigned)tid.value() << 16u) | FI_MaxStaticFileIndex };
  138. }
  139. static inline InodeIdentifier sys_var_to_identifier(unsigned fsid, unsigned index)
  140. {
  141. VERIFY(index < 256);
  142. return { fsid, (PDI_Root_sys << 12u) | (index << 16u) | FI_Root_sys_variable };
  143. }
  144. static inline InodeIdentifier to_parent_id(const InodeIdentifier& identifier)
  145. {
  146. switch (to_proc_parent_directory(identifier)) {
  147. case PDI_AbstractRoot:
  148. case PDI_Root:
  149. return { identifier.fsid(), FI_Root };
  150. case PDI_Root_sys:
  151. return { identifier.fsid(), FI_Root_sys };
  152. case PDI_Root_net:
  153. return { identifier.fsid(), FI_Root_net };
  154. case PDI_PID:
  155. return to_identifier(identifier.fsid(), PDI_Root, to_pid(identifier), FI_PID);
  156. case PDI_PID_fd:
  157. return to_identifier(identifier.fsid(), PDI_PID, to_pid(identifier), FI_PID_fd);
  158. case PDI_PID_stacks:
  159. return to_identifier(identifier.fsid(), PDI_PID, to_pid(identifier), FI_PID_stacks);
  160. }
  161. VERIFY_NOT_REACHED();
  162. }
  163. #if 0
  164. static inline u8 to_unused_metadata(const InodeIdentifier& identifier)
  165. {
  166. return (identifier.index() >> 8) & 0xf;
  167. }
  168. #endif
  169. static inline bool is_process_related_file(const InodeIdentifier& identifier)
  170. {
  171. if (to_proc_file_type(identifier) == FI_PID)
  172. return true;
  173. auto proc_parent_directory = to_proc_parent_directory(identifier);
  174. switch (proc_parent_directory) {
  175. case PDI_PID:
  176. case PDI_PID_fd:
  177. return true;
  178. default:
  179. return false;
  180. }
  181. }
  182. static inline bool is_thread_related_file(const InodeIdentifier& identifier)
  183. {
  184. auto proc_parent_directory = to_proc_parent_directory(identifier);
  185. return proc_parent_directory == PDI_PID_stacks;
  186. }
  187. static inline bool is_directory(const InodeIdentifier& identifier)
  188. {
  189. auto proc_file_type = to_proc_file_type(identifier);
  190. switch (proc_file_type) {
  191. case FI_Root:
  192. case FI_Root_sys:
  193. case FI_Root_net:
  194. case FI_PID:
  195. case FI_PID_fd:
  196. case FI_PID_stacks:
  197. return true;
  198. default:
  199. return false;
  200. }
  201. }
  202. static inline bool is_persistent_inode(const InodeIdentifier& identifier)
  203. {
  204. return to_proc_parent_directory(identifier) == PDI_Root_sys;
  205. }
  206. struct ProcFSInodeData : public FileDescriptionData {
  207. RefPtr<KBufferImpl> buffer;
  208. };
  209. NonnullRefPtr<ProcFS> ProcFS::create()
  210. {
  211. return adopt_ref(*new ProcFS);
  212. }
  213. ProcFS::~ProcFS()
  214. {
  215. }
  216. static bool procfs$pid_fds(InodeIdentifier identifier, KBufferBuilder& builder)
  217. {
  218. JsonArraySerializer array { builder };
  219. auto process = Process::from_pid(to_pid(identifier));
  220. if (!process) {
  221. array.finish();
  222. return true;
  223. }
  224. if (process->number_of_open_file_descriptors() == 0) {
  225. array.finish();
  226. return true;
  227. }
  228. for (int i = 0; i < process->max_open_file_descriptors(); ++i) {
  229. auto description = process->file_description(i);
  230. if (!description)
  231. continue;
  232. bool cloexec = process->fd_flags(i) & FD_CLOEXEC;
  233. auto description_object = array.add_object();
  234. description_object.add("fd", i);
  235. description_object.add("absolute_path", description->absolute_path());
  236. description_object.add("seekable", description->file().is_seekable());
  237. description_object.add("class", description->file().class_name());
  238. description_object.add("offset", description->offset());
  239. description_object.add("cloexec", cloexec);
  240. description_object.add("blocking", description->is_blocking());
  241. description_object.add("can_read", description->can_read());
  242. description_object.add("can_write", description->can_write());
  243. }
  244. array.finish();
  245. return true;
  246. }
  247. static bool procfs$pid_fd_entry(InodeIdentifier identifier, KBufferBuilder& builder)
  248. {
  249. auto process = Process::from_pid(to_pid(identifier));
  250. if (!process)
  251. return false;
  252. int fd = to_fd(identifier);
  253. auto description = process->file_description(fd);
  254. if (!description)
  255. return false;
  256. builder.append_bytes(description->absolute_path().bytes());
  257. return true;
  258. }
  259. static bool procfs$pid_vm(InodeIdentifier identifier, KBufferBuilder& builder)
  260. {
  261. auto process = Process::from_pid(to_pid(identifier));
  262. if (!process)
  263. return false;
  264. JsonArraySerializer array { builder };
  265. {
  266. ScopedSpinLock lock(process->space().get_lock());
  267. for (auto& region : process->space().regions()) {
  268. if (!region->is_user() && !Process::current()->is_superuser())
  269. continue;
  270. auto region_object = array.add_object();
  271. region_object.add("readable", region->is_readable());
  272. region_object.add("writable", region->is_writable());
  273. region_object.add("executable", region->is_executable());
  274. region_object.add("stack", region->is_stack());
  275. region_object.add("shared", region->is_shared());
  276. region_object.add("syscall", region->is_syscall_region());
  277. region_object.add("purgeable", region->vmobject().is_anonymous());
  278. if (region->vmobject().is_anonymous()) {
  279. region_object.add("volatile", static_cast<const AnonymousVMObject&>(region->vmobject()).is_any_volatile());
  280. }
  281. region_object.add("cacheable", region->is_cacheable());
  282. region_object.add("address", region->vaddr().get());
  283. region_object.add("size", region->size());
  284. region_object.add("amount_resident", region->amount_resident());
  285. region_object.add("amount_dirty", region->amount_dirty());
  286. region_object.add("cow_pages", region->cow_pages());
  287. region_object.add("name", region->name());
  288. region_object.add("vmobject", region->vmobject().class_name());
  289. StringBuilder pagemap_builder;
  290. for (size_t i = 0; i < region->page_count(); ++i) {
  291. auto* page = region->physical_page(i);
  292. if (!page)
  293. pagemap_builder.append('N');
  294. else if (page->is_shared_zero_page() || page->is_lazy_committed_page())
  295. pagemap_builder.append('Z');
  296. else
  297. pagemap_builder.append('P');
  298. }
  299. region_object.add("pagemap", pagemap_builder.to_string());
  300. }
  301. }
  302. array.finish();
  303. return true;
  304. }
  305. static bool procfs$pci(InodeIdentifier, KBufferBuilder& builder)
  306. {
  307. JsonArraySerializer array { builder };
  308. PCI::enumerate([&array](PCI::Address address, PCI::ID id) {
  309. auto obj = array.add_object();
  310. obj.add("seg", address.seg());
  311. obj.add("bus", address.bus());
  312. obj.add("device", address.device());
  313. obj.add("function", address.function());
  314. obj.add("vendor_id", id.vendor_id);
  315. obj.add("device_id", id.device_id);
  316. obj.add("revision_id", PCI::get_revision_id(address));
  317. obj.add("subclass", PCI::get_subclass(address));
  318. obj.add("class", PCI::get_class(address));
  319. obj.add("subsystem_id", PCI::get_subsystem_id(address));
  320. obj.add("subsystem_vendor_id", PCI::get_subsystem_vendor_id(address));
  321. });
  322. array.finish();
  323. return true;
  324. }
  325. static bool procfs$dmi(InodeIdentifier, KBufferBuilder& builder)
  326. {
  327. if (!DMIExpose::the().is_available())
  328. return false;
  329. auto structures_ptr = DMIExpose::the().structure_table();
  330. builder.append_bytes(ReadonlyBytes { structures_ptr->data(), structures_ptr->size() });
  331. return true;
  332. }
  333. static bool procfs$smbios_entry_point(InodeIdentifier, KBufferBuilder& builder)
  334. {
  335. if (!DMIExpose::the().is_available())
  336. return false;
  337. auto structures_ptr = DMIExpose::the().entry_point();
  338. builder.append_bytes(ReadonlyBytes { structures_ptr->data(), structures_ptr->size() });
  339. return true;
  340. }
  341. static bool procfs$interrupts(InodeIdentifier, KBufferBuilder& builder)
  342. {
  343. JsonArraySerializer array { builder };
  344. InterruptManagement::the().enumerate_interrupt_handlers([&array](GenericInterruptHandler& handler) {
  345. auto obj = array.add_object();
  346. obj.add("purpose", handler.purpose());
  347. obj.add("interrupt_line", handler.interrupt_number());
  348. obj.add("controller", handler.controller());
  349. obj.add("cpu_handler", 0); // FIXME: Determine the responsible CPU for each interrupt handler.
  350. obj.add("device_sharing", (unsigned)handler.sharing_devices_count());
  351. obj.add("call_count", (unsigned)handler.get_invoking_count());
  352. });
  353. array.finish();
  354. return true;
  355. }
  356. static bool procfs$keymap(InodeIdentifier, KBufferBuilder& builder)
  357. {
  358. JsonObjectSerializer<KBufferBuilder> json { builder };
  359. json.add("keymap", HIDManagement::the().keymap_name());
  360. json.finish();
  361. return true;
  362. }
  363. static bool procfs$devices(InodeIdentifier, KBufferBuilder& builder)
  364. {
  365. JsonArraySerializer array { builder };
  366. Device::for_each([&array](auto& device) {
  367. auto obj = array.add_object();
  368. obj.add("major", device.major());
  369. obj.add("minor", device.minor());
  370. obj.add("class_name", device.class_name());
  371. if (device.is_block_device())
  372. obj.add("type", "block");
  373. else if (device.is_character_device())
  374. obj.add("type", "character");
  375. else
  376. VERIFY_NOT_REACHED();
  377. });
  378. array.finish();
  379. return true;
  380. }
  381. static bool procfs$uptime(InodeIdentifier, KBufferBuilder& builder)
  382. {
  383. builder.appendff("{}\n", TimeManagement::the().uptime_ms() / 1000);
  384. return true;
  385. }
  386. static bool procfs$cmdline(InodeIdentifier, KBufferBuilder& builder)
  387. {
  388. builder.append(kernel_command_line().string());
  389. builder.append('\n');
  390. return true;
  391. }
  392. static bool procfs$modules(InodeIdentifier, KBufferBuilder& builder)
  393. {
  394. extern HashMap<String, OwnPtr<Module>>* g_modules;
  395. JsonArraySerializer array { builder };
  396. for (auto& it : *g_modules) {
  397. auto obj = array.add_object();
  398. obj.add("name", it.value->name);
  399. obj.add("module_init", it.value->module_init);
  400. obj.add("module_fini", it.value->module_fini);
  401. u32 size = 0;
  402. for (auto& section : it.value->sections) {
  403. size += section.capacity();
  404. }
  405. obj.add("size", size);
  406. }
  407. array.finish();
  408. return true;
  409. }
  410. static bool procfs$profile(InodeIdentifier, KBufferBuilder& builder)
  411. {
  412. extern PerformanceEventBuffer* g_global_perf_events;
  413. if (!g_global_perf_events)
  414. return false;
  415. return g_global_perf_events->to_json(builder);
  416. }
  417. static bool procfs$pid_perf_events(InodeIdentifier identifier, KBufferBuilder& builder)
  418. {
  419. auto process = Process::from_pid(to_pid(identifier));
  420. if (!process)
  421. return false;
  422. InterruptDisabler disabler;
  423. if (!process->perf_events())
  424. return false;
  425. return process->perf_events()->to_json(builder);
  426. }
  427. static bool procfs$net_adapters(InodeIdentifier, KBufferBuilder& builder)
  428. {
  429. JsonArraySerializer array { builder };
  430. NetworkAdapter::for_each([&array](auto& adapter) {
  431. auto obj = array.add_object();
  432. obj.add("name", adapter.name());
  433. obj.add("class_name", adapter.class_name());
  434. obj.add("mac_address", adapter.mac_address().to_string());
  435. if (!adapter.ipv4_address().is_zero()) {
  436. obj.add("ipv4_address", adapter.ipv4_address().to_string());
  437. obj.add("ipv4_netmask", adapter.ipv4_netmask().to_string());
  438. }
  439. if (!adapter.ipv4_gateway().is_zero())
  440. obj.add("ipv4_gateway", adapter.ipv4_gateway().to_string());
  441. obj.add("packets_in", adapter.packets_in());
  442. obj.add("bytes_in", adapter.bytes_in());
  443. obj.add("packets_out", adapter.packets_out());
  444. obj.add("bytes_out", adapter.bytes_out());
  445. obj.add("link_up", adapter.link_up());
  446. obj.add("mtu", adapter.mtu());
  447. });
  448. array.finish();
  449. return true;
  450. }
  451. static bool procfs$net_arp(InodeIdentifier, KBufferBuilder& builder)
  452. {
  453. JsonArraySerializer array { builder };
  454. LOCKER(arp_table().lock(), Lock::Mode::Shared);
  455. for (auto& it : arp_table().resource()) {
  456. auto obj = array.add_object();
  457. obj.add("mac_address", it.value.to_string());
  458. obj.add("ip_address", it.key.to_string());
  459. }
  460. array.finish();
  461. return true;
  462. }
  463. static bool procfs$net_tcp(InodeIdentifier, KBufferBuilder& builder)
  464. {
  465. JsonArraySerializer array { builder };
  466. TCPSocket::for_each([&array](auto& socket) {
  467. auto obj = array.add_object();
  468. obj.add("local_address", socket.local_address().to_string());
  469. obj.add("local_port", socket.local_port());
  470. obj.add("peer_address", socket.peer_address().to_string());
  471. obj.add("peer_port", socket.peer_port());
  472. obj.add("state", TCPSocket::to_string(socket.state()));
  473. obj.add("ack_number", socket.ack_number());
  474. obj.add("sequence_number", socket.sequence_number());
  475. obj.add("packets_in", socket.packets_in());
  476. obj.add("bytes_in", socket.bytes_in());
  477. obj.add("packets_out", socket.packets_out());
  478. obj.add("bytes_out", socket.bytes_out());
  479. });
  480. array.finish();
  481. return true;
  482. }
  483. static bool procfs$net_udp(InodeIdentifier, KBufferBuilder& builder)
  484. {
  485. JsonArraySerializer array { builder };
  486. UDPSocket::for_each([&array](auto& socket) {
  487. auto obj = array.add_object();
  488. obj.add("local_address", socket.local_address().to_string());
  489. obj.add("local_port", socket.local_port());
  490. obj.add("peer_address", socket.peer_address().to_string());
  491. obj.add("peer_port", socket.peer_port());
  492. });
  493. array.finish();
  494. return true;
  495. }
  496. static bool procfs$net_local(InodeIdentifier, KBufferBuilder& builder)
  497. {
  498. JsonArraySerializer array { builder };
  499. LocalSocket::for_each([&array](auto& socket) {
  500. auto obj = array.add_object();
  501. obj.add("path", String(socket.socket_path()));
  502. obj.add("origin_pid", socket.origin_pid());
  503. obj.add("origin_uid", socket.origin_uid());
  504. obj.add("origin_gid", socket.origin_gid());
  505. obj.add("acceptor_pid", socket.acceptor_pid());
  506. obj.add("acceptor_uid", socket.acceptor_uid());
  507. obj.add("acceptor_gid", socket.acceptor_gid());
  508. });
  509. array.finish();
  510. return true;
  511. }
  512. static bool procfs$pid_unveil(InodeIdentifier identifier, KBufferBuilder& builder)
  513. {
  514. auto process = Process::from_pid(to_pid(identifier));
  515. if (!process)
  516. return false;
  517. JsonArraySerializer array { builder };
  518. for (auto& unveiled_path : process->unveiled_paths()) {
  519. if (!unveiled_path.was_explicitly_unveiled())
  520. continue;
  521. auto obj = array.add_object();
  522. obj.add("path", unveiled_path.path());
  523. StringBuilder permissions_builder;
  524. if (unveiled_path.permissions() & UnveilAccess::Read)
  525. permissions_builder.append('r');
  526. if (unveiled_path.permissions() & UnveilAccess::Write)
  527. permissions_builder.append('w');
  528. if (unveiled_path.permissions() & UnveilAccess::Execute)
  529. permissions_builder.append('x');
  530. if (unveiled_path.permissions() & UnveilAccess::CreateOrRemove)
  531. permissions_builder.append('c');
  532. if (unveiled_path.permissions() & UnveilAccess::Browse)
  533. permissions_builder.append('b');
  534. obj.add("permissions", permissions_builder.to_string());
  535. }
  536. array.finish();
  537. return true;
  538. }
  539. static bool procfs$tid_stack(InodeIdentifier identifier, KBufferBuilder& builder)
  540. {
  541. auto thread = Thread::from_tid(to_tid(identifier));
  542. if (!thread)
  543. return false;
  544. JsonArraySerializer array { builder };
  545. bool show_kernel_addresses = Process::current()->is_superuser();
  546. for (auto address : Processor::capture_stack_trace(*thread, 1024)) {
  547. if (!show_kernel_addresses && !is_user_address(VirtualAddress { address }))
  548. address = 0xdeadc0de;
  549. array.add(JsonValue(address));
  550. }
  551. array.finish();
  552. return true;
  553. }
  554. static bool procfs$pid_exe(InodeIdentifier identifier, KBufferBuilder& builder)
  555. {
  556. auto process = Process::from_pid(to_pid(identifier));
  557. if (!process)
  558. return false;
  559. auto* custody = process->executable();
  560. VERIFY(custody);
  561. builder.append(custody->absolute_path().bytes());
  562. return true;
  563. }
  564. static bool procfs$pid_cwd(InodeIdentifier identifier, KBufferBuilder& builder)
  565. {
  566. auto process = Process::from_pid(to_pid(identifier));
  567. if (!process)
  568. return false;
  569. builder.append_bytes(process->current_directory().absolute_path().bytes());
  570. return true;
  571. }
  572. static bool procfs$pid_root(InodeIdentifier identifier, KBufferBuilder& builder)
  573. {
  574. auto process = Process::from_pid(to_pid(identifier));
  575. if (!process)
  576. return false;
  577. builder.append_bytes(process->root_directory_relative_to_global_root().absolute_path().to_byte_buffer());
  578. return true;
  579. }
  580. static bool procfs$self(InodeIdentifier, KBufferBuilder& builder)
  581. {
  582. builder.appendff("{}", Process::current()->pid().value());
  583. return true;
  584. }
  585. static bool procfs$dmesg(InodeIdentifier, KBufferBuilder& builder)
  586. {
  587. InterruptDisabler disabler;
  588. for (char ch : Console::the().logbuffer())
  589. builder.append(ch);
  590. return true;
  591. }
  592. static bool procfs$df(InodeIdentifier, KBufferBuilder& builder)
  593. {
  594. // FIXME: This is obviously racy against the VFS mounts changing.
  595. JsonArraySerializer array { builder };
  596. VFS::the().for_each_mount([&array](auto& mount) {
  597. auto& fs = mount.guest_fs();
  598. auto fs_object = array.add_object();
  599. fs_object.add("class_name", fs.class_name());
  600. fs_object.add("total_block_count", fs.total_block_count());
  601. fs_object.add("free_block_count", fs.free_block_count());
  602. fs_object.add("total_inode_count", fs.total_inode_count());
  603. fs_object.add("free_inode_count", fs.free_inode_count());
  604. fs_object.add("mount_point", mount.absolute_path());
  605. fs_object.add("block_size", static_cast<u64>(fs.block_size()));
  606. fs_object.add("readonly", fs.is_readonly());
  607. fs_object.add("mount_flags", mount.flags());
  608. if (fs.is_file_backed())
  609. fs_object.add("source", static_cast<const FileBackedFS&>(fs).file_description().absolute_path());
  610. else
  611. fs_object.add("source", "none");
  612. });
  613. array.finish();
  614. return true;
  615. }
  616. static bool procfs$cpuinfo(InodeIdentifier, KBufferBuilder& builder)
  617. {
  618. JsonArraySerializer array { builder };
  619. Processor::for_each(
  620. [&](Processor& proc) -> IterationDecision {
  621. auto& info = proc.info();
  622. auto obj = array.add_object();
  623. JsonArray features;
  624. for (auto& feature : info.features().split(' '))
  625. features.append(feature);
  626. obj.add("processor", proc.get_id());
  627. obj.add("cpuid", info.cpuid());
  628. obj.add("family", info.display_family());
  629. obj.add("features", features);
  630. obj.add("model", info.display_model());
  631. obj.add("stepping", info.stepping());
  632. obj.add("type", info.type());
  633. obj.add("brandstr", info.brandstr());
  634. return IterationDecision::Continue;
  635. });
  636. array.finish();
  637. return true;
  638. }
  639. static bool procfs$memstat(InodeIdentifier, KBufferBuilder& builder)
  640. {
  641. InterruptDisabler disabler;
  642. kmalloc_stats stats;
  643. get_kmalloc_stats(stats);
  644. ScopedSpinLock mm_lock(s_mm_lock);
  645. auto user_physical_pages_total = MM.user_physical_pages();
  646. auto user_physical_pages_used = MM.user_physical_pages_used();
  647. auto user_physical_pages_committed = MM.user_physical_pages_committed();
  648. auto user_physical_pages_uncommitted = MM.user_physical_pages_uncommitted();
  649. auto super_physical_total = MM.super_physical_pages();
  650. auto super_physical_used = MM.super_physical_pages_used();
  651. mm_lock.unlock();
  652. JsonObjectSerializer<KBufferBuilder> json { builder };
  653. json.add("kmalloc_allocated", stats.bytes_allocated);
  654. json.add("kmalloc_available", stats.bytes_free);
  655. json.add("kmalloc_eternal_allocated", stats.bytes_eternal);
  656. json.add("user_physical_allocated", user_physical_pages_used);
  657. json.add("user_physical_available", user_physical_pages_total - user_physical_pages_used);
  658. json.add("user_physical_committed", user_physical_pages_committed);
  659. json.add("user_physical_uncommitted", user_physical_pages_uncommitted);
  660. json.add("super_physical_allocated", super_physical_used);
  661. json.add("super_physical_available", super_physical_total - super_physical_used);
  662. json.add("kmalloc_call_count", stats.kmalloc_call_count);
  663. json.add("kfree_call_count", stats.kfree_call_count);
  664. slab_alloc_stats([&json](size_t slab_size, size_t num_allocated, size_t num_free) {
  665. auto prefix = String::formatted("slab_{}", slab_size);
  666. json.add(String::formatted("{}_num_allocated", prefix), num_allocated);
  667. json.add(String::formatted("{}_num_free", prefix), num_free);
  668. });
  669. json.finish();
  670. return true;
  671. }
  672. static bool procfs$all(InodeIdentifier, KBufferBuilder& builder)
  673. {
  674. JsonArraySerializer array { builder };
  675. // Keep this in sync with CProcessStatistics.
  676. auto build_process = [&](const Process& process) {
  677. auto process_object = array.add_object();
  678. if (process.is_user_process()) {
  679. StringBuilder pledge_builder;
  680. #define __ENUMERATE_PLEDGE_PROMISE(promise) \
  681. if (process.has_promised(Pledge::promise)) { \
  682. pledge_builder.append(#promise " "); \
  683. }
  684. ENUMERATE_PLEDGE_PROMISES
  685. #undef __ENUMERATE_PLEDGE_PROMISE
  686. process_object.add("pledge", pledge_builder.to_string());
  687. switch (process.veil_state()) {
  688. case VeilState::None:
  689. process_object.add("veil", "None");
  690. break;
  691. case VeilState::Dropped:
  692. process_object.add("veil", "Dropped");
  693. break;
  694. case VeilState::Locked:
  695. process_object.add("veil", "Locked");
  696. break;
  697. }
  698. } else {
  699. process_object.add("pledge", String());
  700. process_object.add("veil", String());
  701. }
  702. process_object.add("pid", process.pid().value());
  703. process_object.add("pgid", process.tty() ? process.tty()->pgid().value() : 0);
  704. process_object.add("pgp", process.pgid().value());
  705. process_object.add("sid", process.sid().value());
  706. process_object.add("uid", process.uid());
  707. process_object.add("gid", process.gid());
  708. process_object.add("ppid", process.ppid().value());
  709. process_object.add("nfds", process.number_of_open_file_descriptors());
  710. process_object.add("name", process.name());
  711. process_object.add("executable", process.executable() ? process.executable()->absolute_path() : "");
  712. process_object.add("tty", process.tty() ? process.tty()->tty_name() : "notty");
  713. process_object.add("amount_virtual", process.space().amount_virtual());
  714. process_object.add("amount_resident", process.space().amount_resident());
  715. process_object.add("amount_dirty_private", process.space().amount_dirty_private());
  716. process_object.add("amount_clean_inode", process.space().amount_clean_inode());
  717. process_object.add("amount_shared", process.space().amount_shared());
  718. process_object.add("amount_purgeable_volatile", process.space().amount_purgeable_volatile());
  719. process_object.add("amount_purgeable_nonvolatile", process.space().amount_purgeable_nonvolatile());
  720. process_object.add("dumpable", process.is_dumpable());
  721. process_object.add("kernel", process.is_kernel_process());
  722. auto thread_array = process_object.add_array("threads");
  723. process.for_each_thread([&](const Thread& thread) {
  724. auto thread_object = thread_array.add_object();
  725. thread_object.add("tid", thread.tid().value());
  726. thread_object.add("name", thread.name());
  727. thread_object.add("times_scheduled", thread.times_scheduled());
  728. thread_object.add("ticks_user", thread.ticks_in_user());
  729. thread_object.add("ticks_kernel", thread.ticks_in_kernel());
  730. thread_object.add("state", thread.state_string());
  731. thread_object.add("cpu", thread.cpu());
  732. thread_object.add("priority", thread.priority());
  733. thread_object.add("syscall_count", thread.syscall_count());
  734. thread_object.add("inode_faults", thread.inode_faults());
  735. thread_object.add("zero_faults", thread.zero_faults());
  736. thread_object.add("cow_faults", thread.cow_faults());
  737. thread_object.add("file_read_bytes", thread.file_read_bytes());
  738. thread_object.add("file_write_bytes", thread.file_write_bytes());
  739. thread_object.add("unix_socket_read_bytes", thread.unix_socket_read_bytes());
  740. thread_object.add("unix_socket_write_bytes", thread.unix_socket_write_bytes());
  741. thread_object.add("ipv4_socket_read_bytes", thread.ipv4_socket_read_bytes());
  742. thread_object.add("ipv4_socket_write_bytes", thread.ipv4_socket_write_bytes());
  743. return IterationDecision::Continue;
  744. });
  745. };
  746. ScopedSpinLock lock(g_scheduler_lock);
  747. auto processes = Process::all_processes();
  748. build_process(*Scheduler::colonel());
  749. for (auto& process : processes)
  750. build_process(process);
  751. array.finish();
  752. return true;
  753. }
  754. struct SysVariable {
  755. String name;
  756. enum class Type : u8 {
  757. Invalid,
  758. Boolean,
  759. String,
  760. };
  761. Type type { Type::Invalid };
  762. Function<void()> notify_callback;
  763. void* address { nullptr };
  764. static SysVariable& for_inode(InodeIdentifier);
  765. void notify()
  766. {
  767. if (notify_callback)
  768. notify_callback();
  769. }
  770. };
  771. static Vector<SysVariable, 16>* s_sys_variables;
  772. static inline Vector<SysVariable, 16>& sys_variables()
  773. {
  774. if (s_sys_variables == nullptr) {
  775. s_sys_variables = new Vector<SysVariable, 16>;
  776. s_sys_variables->append({ "", SysVariable::Type::Invalid, nullptr, nullptr });
  777. }
  778. return *s_sys_variables;
  779. }
  780. SysVariable& SysVariable::for_inode(InodeIdentifier id)
  781. {
  782. auto index = to_sys_index(id);
  783. if (index >= sys_variables().size())
  784. return sys_variables()[0];
  785. auto& variable = sys_variables()[index];
  786. VERIFY(variable.address);
  787. return variable;
  788. }
  789. static bool read_sys_bool(InodeIdentifier inode_id, KBufferBuilder& builder)
  790. {
  791. auto& variable = SysVariable::for_inode(inode_id);
  792. VERIFY(variable.type == SysVariable::Type::Boolean);
  793. u8 buffer[2];
  794. auto* lockable_bool = reinterpret_cast<Lockable<bool>*>(variable.address);
  795. {
  796. LOCKER(lockable_bool->lock(), Lock::Mode::Shared);
  797. buffer[0] = lockable_bool->resource() ? '1' : '0';
  798. }
  799. buffer[1] = '\n';
  800. builder.append_bytes(ReadonlyBytes { buffer, sizeof(buffer) });
  801. return true;
  802. }
  803. static ssize_t write_sys_bool(InodeIdentifier inode_id, const UserOrKernelBuffer& buffer, size_t size)
  804. {
  805. auto& variable = SysVariable::for_inode(inode_id);
  806. VERIFY(variable.type == SysVariable::Type::Boolean);
  807. char value = 0;
  808. bool did_read = false;
  809. ssize_t nread = buffer.read_buffered<1>(1, [&](const u8* data, size_t) {
  810. if (did_read)
  811. return 0;
  812. value = (char)data[0];
  813. did_read = true;
  814. return 1;
  815. });
  816. if (nread < 0)
  817. return nread;
  818. VERIFY(nread == 0 || (nread == 1 && did_read));
  819. if (nread == 0 || !(value == '0' || value == '1'))
  820. return (ssize_t)size;
  821. auto* lockable_bool = reinterpret_cast<Lockable<bool>*>(variable.address);
  822. {
  823. LOCKER(lockable_bool->lock());
  824. lockable_bool->resource() = value == '1';
  825. }
  826. variable.notify();
  827. return (ssize_t)size;
  828. }
  829. static bool read_sys_string(InodeIdentifier inode_id, KBufferBuilder& builder)
  830. {
  831. auto& variable = SysVariable::for_inode(inode_id);
  832. VERIFY(variable.type == SysVariable::Type::String);
  833. auto* lockable_string = reinterpret_cast<Lockable<String>*>(variable.address);
  834. LOCKER(lockable_string->lock(), Lock::Mode::Shared);
  835. builder.append_bytes(lockable_string->resource().bytes());
  836. return true;
  837. }
  838. static ssize_t write_sys_string(InodeIdentifier inode_id, const UserOrKernelBuffer& buffer, size_t size)
  839. {
  840. auto& variable = SysVariable::for_inode(inode_id);
  841. VERIFY(variable.type == SysVariable::Type::String);
  842. auto string_copy = buffer.copy_into_string(size);
  843. if (string_copy.is_null())
  844. return -EFAULT;
  845. {
  846. auto* lockable_string = reinterpret_cast<Lockable<String>*>(variable.address);
  847. LOCKER(lockable_string->lock());
  848. lockable_string->resource() = move(string_copy);
  849. }
  850. variable.notify();
  851. return (ssize_t)size;
  852. }
  853. void ProcFS::add_sys_bool(String&& name, Lockable<bool>& var, Function<void()>&& notify_callback)
  854. {
  855. InterruptDisabler disabler;
  856. SysVariable variable;
  857. variable.name = move(name);
  858. variable.type = SysVariable::Type::Boolean;
  859. variable.notify_callback = move(notify_callback);
  860. variable.address = &var;
  861. sys_variables().append(move(variable));
  862. }
  863. void ProcFS::add_sys_string(String&& name, Lockable<String>& var, Function<void()>&& notify_callback)
  864. {
  865. InterruptDisabler disabler;
  866. SysVariable variable;
  867. variable.name = move(name);
  868. variable.type = SysVariable::Type::String;
  869. variable.notify_callback = move(notify_callback);
  870. variable.address = &var;
  871. sys_variables().append(move(variable));
  872. }
  873. bool ProcFS::initialize()
  874. {
  875. static Lockable<bool>* kmalloc_stack_helper;
  876. static Lockable<bool>* ubsan_deadly_helper;
  877. if (kmalloc_stack_helper == nullptr) {
  878. kmalloc_stack_helper = new Lockable<bool>();
  879. kmalloc_stack_helper->resource() = g_dump_kmalloc_stacks;
  880. ProcFS::add_sys_bool("kmalloc_stacks", *kmalloc_stack_helper, [] {
  881. g_dump_kmalloc_stacks = kmalloc_stack_helper->resource();
  882. });
  883. ubsan_deadly_helper = new Lockable<bool>();
  884. ubsan_deadly_helper->resource() = UBSanitizer::g_ubsan_is_deadly;
  885. ProcFS::add_sys_bool("ubsan_is_deadly", *ubsan_deadly_helper, [] {
  886. UBSanitizer::g_ubsan_is_deadly = ubsan_deadly_helper->resource();
  887. });
  888. }
  889. return true;
  890. }
  891. const char* ProcFS::class_name() const
  892. {
  893. return "ProcFS";
  894. }
  895. NonnullRefPtr<Inode> ProcFS::root_inode() const
  896. {
  897. return *m_root_inode;
  898. }
  899. RefPtr<Inode> ProcFS::get_inode(InodeIdentifier inode_id) const
  900. {
  901. dbgln_if(PROCFS_DEBUG, "ProcFS::get_inode({})", inode_id.index());
  902. if (inode_id == root_inode()->identifier())
  903. return m_root_inode;
  904. LOCKER(m_inodes_lock);
  905. auto it = m_inodes.find(inode_id.index().value());
  906. if (it != m_inodes.end()) {
  907. // It's possible that the ProcFSInode ref count was dropped to 0 or
  908. // the ~ProcFSInode destructor is even running already, but blocked
  909. // from removing it from this map. So we need to *try* to ref it,
  910. // and if that fails we cannot return this instance anymore and just
  911. // create a new one.
  912. if (it->value->try_ref())
  913. return adopt_ref(*it->value);
  914. // We couldn't ref it, so just create a new one and replace the entry
  915. }
  916. auto inode = adopt_ref(*new ProcFSInode(const_cast<ProcFS&>(*this), inode_id.index()));
  917. auto result = m_inodes.set(inode_id.index().value(), inode.ptr());
  918. VERIFY(result == ((it == m_inodes.end()) ? AK::HashSetResult::InsertedNewEntry : AK::HashSetResult::ReplacedExistingEntry));
  919. return inode;
  920. }
  921. ProcFSInode::ProcFSInode(ProcFS& fs, InodeIndex index)
  922. : Inode(fs, index)
  923. {
  924. }
  925. ProcFSInode::~ProcFSInode()
  926. {
  927. LOCKER(fs().m_inodes_lock);
  928. auto it = fs().m_inodes.find(index().value());
  929. if (it != fs().m_inodes.end() && it->value == this)
  930. fs().m_inodes.remove(it);
  931. }
  932. RefPtr<Process> ProcFSInode::process() const
  933. {
  934. return Process::from_pid(to_pid(identifier()));
  935. }
  936. KResult ProcFSInode::refresh_data(FileDescription& description) const
  937. {
  938. if (Kernel::is_directory(identifier()))
  939. return KSuccess;
  940. // For process-specific inodes, hold the process's ptrace lock across refresh
  941. // and refuse to load data if the process is not dumpable.
  942. // Without this, files opened before a process went non-dumpable could still be used for dumping.
  943. auto process = this->process();
  944. if (process) {
  945. process->ptrace_lock().lock();
  946. if (!process->is_dumpable()) {
  947. process->ptrace_lock().unlock();
  948. return EPERM;
  949. }
  950. }
  951. ScopeGuard guard = [&] {
  952. if (process)
  953. process->ptrace_lock().unlock();
  954. };
  955. auto& cached_data = description.data();
  956. auto* directory_entry = fs().get_directory_entry(identifier());
  957. bool (*read_callback)(InodeIdentifier, KBufferBuilder&) = nullptr;
  958. if (directory_entry) {
  959. read_callback = directory_entry->read_callback;
  960. VERIFY(read_callback);
  961. } else {
  962. switch (to_proc_parent_directory(identifier())) {
  963. case PDI_PID_fd:
  964. read_callback = procfs$pid_fd_entry;
  965. break;
  966. case PDI_PID_stacks:
  967. read_callback = procfs$tid_stack;
  968. break;
  969. case PDI_Root_sys:
  970. switch (SysVariable::for_inode(identifier()).type) {
  971. case SysVariable::Type::Invalid:
  972. VERIFY_NOT_REACHED();
  973. case SysVariable::Type::Boolean:
  974. read_callback = read_sys_bool;
  975. break;
  976. case SysVariable::Type::String:
  977. read_callback = read_sys_string;
  978. break;
  979. }
  980. break;
  981. default:
  982. VERIFY_NOT_REACHED();
  983. }
  984. VERIFY(read_callback);
  985. }
  986. if (!cached_data)
  987. cached_data = new ProcFSInodeData;
  988. auto& buffer = static_cast<ProcFSInodeData&>(*cached_data).buffer;
  989. if (buffer) {
  990. // If we're reusing the buffer, reset the size to 0 first. This
  991. // ensures we don't accidentally leak previously written data.
  992. buffer->set_size(0);
  993. }
  994. KBufferBuilder builder(buffer, true);
  995. if (!read_callback(identifier(), builder))
  996. return ENOENT;
  997. // We don't use builder.build() here, which would steal our buffer
  998. // and turn it into an OwnPtr. Instead, just flush to the buffer so
  999. // that we can read all the data that was written.
  1000. if (!builder.flush())
  1001. return ENOMEM;
  1002. if (!buffer)
  1003. return ENOMEM;
  1004. return KSuccess;
  1005. }
  1006. KResult ProcFSInode::attach(FileDescription& description)
  1007. {
  1008. return refresh_data(description);
  1009. }
  1010. void ProcFSInode::did_seek(FileDescription& description, off_t new_offset)
  1011. {
  1012. if (new_offset != 0)
  1013. return;
  1014. auto result = refresh_data(description);
  1015. if (result.is_error()) {
  1016. // Subsequent calls to read will return EIO!
  1017. dbgln("ProcFS: Could not refresh contents: {}", result.error());
  1018. }
  1019. }
  1020. InodeMetadata ProcFSInode::metadata() const
  1021. {
  1022. dbgln_if(PROCFS_DEBUG, "ProcFSInode::metadata({})", index());
  1023. InodeMetadata metadata;
  1024. metadata.inode = identifier();
  1025. metadata.ctime = mepoch;
  1026. metadata.atime = mepoch;
  1027. metadata.mtime = mepoch;
  1028. auto proc_parent_directory = to_proc_parent_directory(identifier());
  1029. auto proc_file_type = to_proc_file_type(identifier());
  1030. dbgln_if(PROCFS_DEBUG, " -> pid={}, fi={}, pdi={}", to_pid(identifier()).value(), (int)proc_file_type, (int)proc_parent_directory);
  1031. if (is_process_related_file(identifier())) {
  1032. ProcessID pid = to_pid(identifier());
  1033. auto process = Process::from_pid(pid);
  1034. if (process && process->is_dumpable()) {
  1035. metadata.uid = process->euid();
  1036. metadata.gid = process->egid();
  1037. } else {
  1038. metadata.uid = 0;
  1039. metadata.gid = 0;
  1040. }
  1041. } else if (is_thread_related_file(identifier())) {
  1042. ThreadID tid = to_tid(identifier());
  1043. auto thread = Thread::from_tid(tid);
  1044. if (thread && thread->process().is_dumpable()) {
  1045. metadata.uid = thread->process().euid();
  1046. metadata.gid = thread->process().egid();
  1047. } else {
  1048. metadata.uid = 0;
  1049. metadata.gid = 0;
  1050. }
  1051. }
  1052. if (proc_parent_directory == PDI_PID_fd) {
  1053. metadata.mode = S_IFLNK | S_IRUSR | S_IWUSR | S_IXUSR;
  1054. return metadata;
  1055. }
  1056. switch (proc_file_type) {
  1057. case FI_Root_self:
  1058. metadata.mode = S_IFLNK | S_IRUSR | S_IRGRP | S_IROTH;
  1059. break;
  1060. case FI_PID_cwd:
  1061. case FI_PID_exe:
  1062. case FI_PID_root:
  1063. metadata.mode = S_IFLNK | S_IRUSR;
  1064. break;
  1065. case FI_Root:
  1066. case FI_Root_sys:
  1067. case FI_Root_net:
  1068. metadata.mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
  1069. break;
  1070. case FI_PID:
  1071. case FI_PID_fd:
  1072. case FI_PID_stacks:
  1073. metadata.mode = S_IFDIR | S_IRUSR | S_IXUSR;
  1074. break;
  1075. case FI_Root_smbios_entry_point:
  1076. metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  1077. metadata.size = DMIExpose::the().entry_point_length();
  1078. break;
  1079. case FI_Root_dmi:
  1080. metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  1081. metadata.size = DMIExpose::the().structure_table_length();
  1082. break;
  1083. default:
  1084. metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  1085. break;
  1086. }
  1087. if (proc_file_type > FI_Invalid && proc_file_type < FI_MaxStaticFileIndex) {
  1088. if (fs().m_entries[proc_file_type].supervisor_only) {
  1089. metadata.uid = 0;
  1090. metadata.gid = 0;
  1091. metadata.mode &= ~077;
  1092. }
  1093. }
  1094. return metadata;
  1095. }
  1096. ssize_t ProcFSInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
  1097. {
  1098. dbgln_if(PROCFS_DEBUG, "ProcFS: read_bytes offset: {} count: {}", offset, count);
  1099. VERIFY(offset >= 0);
  1100. VERIFY(buffer.user_or_kernel_ptr());
  1101. if (!description)
  1102. return -EIO;
  1103. if (!description->data()) {
  1104. dbgln_if(PROCFS_DEBUG, "ProcFS: Do not have cached data!");
  1105. return -EIO;
  1106. }
  1107. // Be sure to keep a reference to data_buffer while we use it!
  1108. RefPtr<KBufferImpl> data_buffer = static_cast<ProcFSInodeData&>(*description->data()).buffer;
  1109. if (!data_buffer || (size_t)offset >= data_buffer->size())
  1110. return 0;
  1111. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  1112. if (!buffer.write(data_buffer->data() + offset, nread))
  1113. return -EFAULT;
  1114. return nread;
  1115. }
  1116. InodeIdentifier ProcFS::ProcFSDirectoryEntry::identifier(unsigned fsid) const
  1117. {
  1118. return to_identifier(fsid, PDI_Root, 0, (ProcFileType)proc_file_type);
  1119. }
  1120. KResult ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
  1121. {
  1122. dbgln_if(PROCFS_DEBUG, "ProcFS: traverse_as_directory {}", index());
  1123. if (!Kernel::is_directory(identifier()))
  1124. return ENOTDIR;
  1125. auto proc_file_type = to_proc_file_type(identifier());
  1126. auto parent_id = to_parent_id(identifier());
  1127. callback({ ".", identifier(), 2 });
  1128. callback({ "..", parent_id, 2 });
  1129. switch (proc_file_type) {
  1130. case FI_Root:
  1131. for (auto& entry : fs().m_entries) {
  1132. // FIXME: strlen() here is sad.
  1133. if (!entry.name)
  1134. continue;
  1135. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End)
  1136. callback({ { entry.name, strlen(entry.name) }, to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type), 0 });
  1137. }
  1138. for (auto pid_child : Process::all_pids()) {
  1139. callback({ String::number(pid_child.value()), to_identifier(fsid(), PDI_Root, pid_child, FI_PID), 0 });
  1140. }
  1141. break;
  1142. case FI_Root_sys:
  1143. for (size_t i = 1; i < sys_variables().size(); ++i) {
  1144. auto& variable = sys_variables()[i];
  1145. callback({ variable.name, sys_var_to_identifier(fsid(), i), 0 });
  1146. }
  1147. break;
  1148. case FI_Root_net:
  1149. callback({ "adapters", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_adapters), 0 });
  1150. callback({ "arp", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_arp), 0 });
  1151. callback({ "tcp", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_tcp), 0 });
  1152. callback({ "udp", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_udp), 0 });
  1153. callback({ "local", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_local), 0 });
  1154. break;
  1155. case FI_PID: {
  1156. auto pid = to_pid(identifier());
  1157. auto process = Process::from_pid(pid);
  1158. if (!process)
  1159. return ENOENT;
  1160. for (auto& entry : fs().m_entries) {
  1161. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  1162. if (entry.proc_file_type == FI_PID_exe && !process->executable())
  1163. continue;
  1164. // FIXME: strlen() here is sad.
  1165. callback({ { entry.name, strlen(entry.name) }, to_identifier(fsid(), PDI_PID, pid, (ProcFileType)entry.proc_file_type), 0 });
  1166. }
  1167. }
  1168. } break;
  1169. case FI_PID_fd: {
  1170. auto pid = to_pid(identifier());
  1171. auto process = Process::from_pid(pid);
  1172. if (!process)
  1173. return ENOENT;
  1174. for (int i = 0; i < process->max_open_file_descriptors(); ++i) {
  1175. auto description = process->file_description(i);
  1176. if (!description)
  1177. continue;
  1178. callback({ String::number(i), to_identifier_with_fd(fsid(), pid, i), 0 });
  1179. }
  1180. } break;
  1181. case FI_PID_stacks: {
  1182. auto pid = to_pid(identifier());
  1183. auto process = Process::from_pid(pid);
  1184. if (!process)
  1185. return ENOENT;
  1186. process->for_each_thread([&](const Thread& thread) -> IterationDecision {
  1187. int tid = thread.tid().value();
  1188. callback({ String::number(tid), to_identifier_with_stack(fsid(), tid), 0 });
  1189. return IterationDecision::Continue;
  1190. });
  1191. } break;
  1192. default:
  1193. return KSuccess;
  1194. }
  1195. return KSuccess;
  1196. }
  1197. RefPtr<Inode> ProcFSInode::lookup(StringView name)
  1198. {
  1199. VERIFY(is_directory());
  1200. if (name == ".")
  1201. return this;
  1202. if (name == "..")
  1203. return fs().get_inode(to_parent_id(identifier()));
  1204. auto proc_file_type = to_proc_file_type(identifier());
  1205. if (proc_file_type == FI_Root) {
  1206. for (auto& entry : fs().m_entries) {
  1207. if (entry.name == nullptr)
  1208. continue;
  1209. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End) {
  1210. if (name == entry.name) {
  1211. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type));
  1212. }
  1213. }
  1214. }
  1215. auto name_as_number = name.to_uint();
  1216. if (!name_as_number.has_value())
  1217. return {};
  1218. bool process_exists = false;
  1219. {
  1220. InterruptDisabler disabler;
  1221. process_exists = Process::from_pid(name_as_number.value());
  1222. }
  1223. if (process_exists)
  1224. return fs().get_inode(to_identifier(fsid(), PDI_Root, name_as_number.value(), FI_PID));
  1225. return {};
  1226. }
  1227. if (proc_file_type == FI_Root_sys) {
  1228. for (size_t i = 1; i < sys_variables().size(); ++i) {
  1229. auto& variable = sys_variables()[i];
  1230. if (name == variable.name)
  1231. return fs().get_inode(sys_var_to_identifier(fsid(), i));
  1232. }
  1233. return {};
  1234. }
  1235. if (proc_file_type == FI_Root_net) {
  1236. if (name == "adapters")
  1237. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_adapters));
  1238. if (name == "arp")
  1239. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_arp));
  1240. if (name == "tcp")
  1241. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_tcp));
  1242. if (name == "udp")
  1243. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_udp));
  1244. if (name == "local")
  1245. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_local));
  1246. return {};
  1247. }
  1248. if (proc_file_type == FI_PID) {
  1249. auto process = Process::from_pid(to_pid(identifier()));
  1250. if (!process)
  1251. return {};
  1252. for (auto& entry : fs().m_entries) {
  1253. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  1254. if (entry.proc_file_type == FI_PID_exe && !process->executable())
  1255. continue;
  1256. if (entry.name == nullptr)
  1257. continue;
  1258. if (name == entry.name) {
  1259. return fs().get_inode(to_identifier(fsid(), PDI_PID, to_pid(identifier()), (ProcFileType)entry.proc_file_type));
  1260. }
  1261. }
  1262. }
  1263. return {};
  1264. }
  1265. if (proc_file_type == FI_PID_fd) {
  1266. auto name_as_number = name.to_uint();
  1267. if (!name_as_number.has_value())
  1268. return {};
  1269. bool fd_exists = false;
  1270. {
  1271. if (auto process = Process::from_pid(to_pid(identifier())))
  1272. fd_exists = process->file_description(name_as_number.value());
  1273. }
  1274. if (fd_exists)
  1275. return fs().get_inode(to_identifier_with_fd(fsid(), to_pid(identifier()), name_as_number.value()));
  1276. }
  1277. if (proc_file_type == FI_PID_stacks) {
  1278. auto name_as_number = name.to_int();
  1279. if (!name_as_number.has_value())
  1280. return {};
  1281. int tid = name_as_number.value();
  1282. if (tid <= 0) {
  1283. return {};
  1284. }
  1285. bool thread_exists = false;
  1286. {
  1287. auto process = Process::from_pid(to_pid(identifier()));
  1288. auto thread = Thread::from_tid(tid);
  1289. thread_exists = process && thread && process->pid() == thread->pid();
  1290. }
  1291. if (thread_exists)
  1292. return fs().get_inode(to_identifier_with_stack(fsid(), tid));
  1293. }
  1294. return {};
  1295. }
  1296. void ProcFSInode::flush_metadata()
  1297. {
  1298. }
  1299. ssize_t ProcFSInode::write_bytes(off_t offset, ssize_t size, const UserOrKernelBuffer& buffer, FileDescription*)
  1300. {
  1301. // For process-specific inodes, hold the process's ptrace lock across the write
  1302. // and refuse to write at all data if the process is not dumpable.
  1303. // Without this, files opened before a process went non-dumpable could still be used for dumping.
  1304. auto process = this->process();
  1305. if (process) {
  1306. process->ptrace_lock().lock();
  1307. if (!process->is_dumpable()) {
  1308. process->ptrace_lock().unlock();
  1309. return EPERM;
  1310. }
  1311. }
  1312. ScopeGuard guard = [&] {
  1313. if (process)
  1314. process->ptrace_lock().unlock();
  1315. };
  1316. auto result = prepare_to_write_data();
  1317. if (result.is_error())
  1318. return result;
  1319. auto* directory_entry = fs().get_directory_entry(identifier());
  1320. ssize_t (*write_callback)(InodeIdentifier, const UserOrKernelBuffer&, size_t) = nullptr;
  1321. if (directory_entry == nullptr) {
  1322. if (to_proc_parent_directory(identifier()) == PDI_Root_sys) {
  1323. switch (SysVariable::for_inode(identifier()).type) {
  1324. case SysVariable::Type::Invalid:
  1325. VERIFY_NOT_REACHED();
  1326. case SysVariable::Type::Boolean:
  1327. write_callback = write_sys_bool;
  1328. break;
  1329. case SysVariable::Type::String:
  1330. write_callback = write_sys_string;
  1331. break;
  1332. }
  1333. } else
  1334. return -EPERM;
  1335. } else {
  1336. if (!directory_entry->write_callback)
  1337. return -EPERM;
  1338. write_callback = directory_entry->write_callback;
  1339. }
  1340. VERIFY(is_persistent_inode(identifier()));
  1341. // FIXME: Being able to write into ProcFS at a non-zero offset seems like something we should maybe support..
  1342. VERIFY(offset == 0);
  1343. ssize_t nwritten = write_callback(identifier(), buffer, (size_t)size);
  1344. if (nwritten < 0)
  1345. dbgln("ProcFS: Writing {} bytes failed: {}", size, nwritten);
  1346. return nwritten;
  1347. }
  1348. KResultOr<NonnullRefPtr<Custody>> ProcFSInode::resolve_as_link(Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level) const
  1349. {
  1350. if (FI_Root_self == to_proc_file_type(identifier())) {
  1351. return VFS::the().resolve_path(String::number(Process::current()->pid().value()), base, out_parent, options, symlink_recursion_level);
  1352. }
  1353. // The only other links are in pid directories, so it's safe to ignore
  1354. // unrelated files and the thread-specific stacks/ directory.
  1355. if (!is_process_related_file(identifier()))
  1356. return Inode::resolve_as_link(base, out_parent, options, symlink_recursion_level);
  1357. // FIXME: We should return a custody for FI_PID or FI_PID_fd here
  1358. // for correctness. It's impossible to create files in ProcFS,
  1359. // so returning null shouldn't break much.
  1360. if (out_parent)
  1361. *out_parent = nullptr;
  1362. auto pid = to_pid(identifier());
  1363. auto proc_file_type = to_proc_file_type(identifier());
  1364. auto process = Process::from_pid(pid);
  1365. if (!process)
  1366. return ENOENT;
  1367. if (to_proc_parent_directory(identifier()) == PDI_PID_fd) {
  1368. if (out_parent)
  1369. *out_parent = base;
  1370. int fd = to_fd(identifier());
  1371. auto description = process->file_description(fd);
  1372. if (!description)
  1373. return ENOENT;
  1374. auto proxy_inode = ProcFSProxyInode::create(const_cast<ProcFS&>(fs()), *description);
  1375. return Custody::create(&base, "", proxy_inode, base.mount_flags());
  1376. }
  1377. Custody* res = nullptr;
  1378. switch (proc_file_type) {
  1379. case FI_PID_cwd:
  1380. res = &process->current_directory();
  1381. break;
  1382. case FI_PID_exe:
  1383. res = process->executable();
  1384. break;
  1385. case FI_PID_root:
  1386. // Note: we open root_directory() here, not
  1387. // root_directory_relative_to_global_root().
  1388. // This seems more useful.
  1389. res = &process->root_directory();
  1390. break;
  1391. default:
  1392. VERIFY_NOT_REACHED();
  1393. }
  1394. if (!res)
  1395. return ENOENT;
  1396. return *res;
  1397. }
  1398. ProcFSProxyInode::ProcFSProxyInode(ProcFS& fs, FileDescription& fd)
  1399. : Inode(fs, 0)
  1400. , m_fd(fd)
  1401. {
  1402. }
  1403. ProcFSProxyInode::~ProcFSProxyInode()
  1404. {
  1405. }
  1406. KResult ProcFSProxyInode::attach(FileDescription& fd)
  1407. {
  1408. return m_fd->inode()->attach(fd);
  1409. }
  1410. void ProcFSProxyInode::did_seek(FileDescription& fd, off_t new_offset)
  1411. {
  1412. return m_fd->inode()->did_seek(fd, new_offset);
  1413. }
  1414. InodeMetadata ProcFSProxyInode::metadata() const
  1415. {
  1416. InodeMetadata metadata = m_fd->metadata();
  1417. if (m_fd->is_readable())
  1418. metadata.mode |= 0444;
  1419. else
  1420. metadata.mode &= ~0444;
  1421. if (m_fd->is_writable())
  1422. metadata.mode |= 0222;
  1423. else
  1424. metadata.mode &= ~0222;
  1425. if (!metadata.is_directory())
  1426. metadata.mode &= ~0111;
  1427. return metadata;
  1428. }
  1429. KResultOr<NonnullRefPtr<Inode>> ProcFSProxyInode::create_child(const String& name, mode_t mode, dev_t dev, uid_t uid, gid_t gid)
  1430. {
  1431. if (!m_fd->inode())
  1432. return EINVAL;
  1433. return m_fd->inode()->create_child(name, mode, dev, uid, gid);
  1434. }
  1435. KResult ProcFSProxyInode::add_child(Inode& child, const StringView& name, mode_t mode)
  1436. {
  1437. if (!m_fd->inode())
  1438. return EINVAL;
  1439. return m_fd->inode()->add_child(child, name, mode);
  1440. }
  1441. KResult ProcFSProxyInode::remove_child(const StringView& name)
  1442. {
  1443. if (!m_fd->inode())
  1444. return EINVAL;
  1445. return m_fd->inode()->remove_child(name);
  1446. }
  1447. RefPtr<Inode> ProcFSProxyInode::lookup(StringView name)
  1448. {
  1449. if (!m_fd->inode())
  1450. return {};
  1451. return m_fd->inode()->lookup(name);
  1452. }
  1453. KResultOr<size_t> ProcFSProxyInode::directory_entry_count() const
  1454. {
  1455. if (!m_fd->inode())
  1456. return EINVAL;
  1457. return m_fd->inode()->directory_entry_count();
  1458. }
  1459. KResultOr<NonnullRefPtr<Inode>> ProcFSInode::create_child(const String&, mode_t, dev_t, uid_t, gid_t)
  1460. {
  1461. return EPERM;
  1462. }
  1463. KResult ProcFSInode::add_child(Inode&, const StringView&, mode_t)
  1464. {
  1465. return EPERM;
  1466. }
  1467. KResult ProcFSInode::remove_child([[maybe_unused]] const StringView& name)
  1468. {
  1469. return EPERM;
  1470. }
  1471. KResultOr<size_t> ProcFSInode::directory_entry_count() const
  1472. {
  1473. VERIFY(is_directory());
  1474. size_t count = 0;
  1475. KResult result = traverse_as_directory([&count](auto&) {
  1476. ++count;
  1477. return true;
  1478. });
  1479. if (result.is_error())
  1480. return result;
  1481. return count;
  1482. }
  1483. KResult ProcFSInode::chmod(mode_t)
  1484. {
  1485. return EPERM;
  1486. }
  1487. ProcFS::ProcFS()
  1488. {
  1489. m_root_inode = adopt_ref(*new ProcFSInode(*this, 1));
  1490. m_entries.resize(FI_MaxStaticFileIndex);
  1491. m_entries[FI_Root_df] = { "df", FI_Root_df, false, procfs$df };
  1492. m_entries[FI_Root_all] = { "all", FI_Root_all, false, procfs$all };
  1493. m_entries[FI_Root_memstat] = { "memstat", FI_Root_memstat, false, procfs$memstat };
  1494. m_entries[FI_Root_cpuinfo] = { "cpuinfo", FI_Root_cpuinfo, false, procfs$cpuinfo };
  1495. m_entries[FI_Root_dmesg] = { "dmesg", FI_Root_dmesg, true, procfs$dmesg };
  1496. m_entries[FI_Root_self] = { "self", FI_Root_self, false, procfs$self };
  1497. m_entries[FI_Root_pci] = { "pci", FI_Root_pci, false, procfs$pci };
  1498. m_entries[FI_Root_interrupts] = { "interrupts", FI_Root_interrupts, false, procfs$interrupts };
  1499. m_entries[FI_Root_dmi] = { "DMI", FI_Root_dmi, false, procfs$dmi };
  1500. m_entries[FI_Root_smbios_entry_point] = { "smbios_entry_point", FI_Root_smbios_entry_point, false, procfs$smbios_entry_point };
  1501. m_entries[FI_Root_keymap] = { "keymap", FI_Root_keymap, false, procfs$keymap };
  1502. m_entries[FI_Root_devices] = { "devices", FI_Root_devices, false, procfs$devices };
  1503. m_entries[FI_Root_uptime] = { "uptime", FI_Root_uptime, false, procfs$uptime };
  1504. m_entries[FI_Root_cmdline] = { "cmdline", FI_Root_cmdline, true, procfs$cmdline };
  1505. m_entries[FI_Root_modules] = { "modules", FI_Root_modules, true, procfs$modules };
  1506. m_entries[FI_Root_profile] = { "profile", FI_Root_profile, true, procfs$profile };
  1507. m_entries[FI_Root_sys] = { "sys", FI_Root_sys, true };
  1508. m_entries[FI_Root_net] = { "net", FI_Root_net, false };
  1509. m_entries[FI_Root_net_adapters] = { "adapters", FI_Root_net_adapters, false, procfs$net_adapters };
  1510. m_entries[FI_Root_net_arp] = { "arp", FI_Root_net_arp, true, procfs$net_arp };
  1511. m_entries[FI_Root_net_tcp] = { "tcp", FI_Root_net_tcp, false, procfs$net_tcp };
  1512. m_entries[FI_Root_net_udp] = { "udp", FI_Root_net_udp, false, procfs$net_udp };
  1513. m_entries[FI_Root_net_local] = { "local", FI_Root_net_local, false, procfs$net_local };
  1514. m_entries[FI_PID_vm] = { "vm", FI_PID_vm, false, procfs$pid_vm };
  1515. m_entries[FI_PID_stacks] = { "stacks", FI_PID_stacks, false };
  1516. m_entries[FI_PID_fds] = { "fds", FI_PID_fds, false, procfs$pid_fds };
  1517. m_entries[FI_PID_exe] = { "exe", FI_PID_exe, false, procfs$pid_exe };
  1518. m_entries[FI_PID_cwd] = { "cwd", FI_PID_cwd, false, procfs$pid_cwd };
  1519. m_entries[FI_PID_unveil] = { "unveil", FI_PID_unveil, false, procfs$pid_unveil };
  1520. m_entries[FI_PID_root] = { "root", FI_PID_root, false, procfs$pid_root };
  1521. m_entries[FI_PID_perf_events] = { "perf_events", FI_PID_perf_events, false, procfs$pid_perf_events };
  1522. m_entries[FI_PID_fd] = { "fd", FI_PID_fd, false };
  1523. }
  1524. ProcFS::ProcFSDirectoryEntry* ProcFS::get_directory_entry(InodeIdentifier identifier) const
  1525. {
  1526. auto proc_file_type = to_proc_file_type(identifier);
  1527. if (proc_file_type != FI_Invalid && proc_file_type != FI_Root_sys_variable && proc_file_type < FI_MaxStaticFileIndex)
  1528. return const_cast<ProcFSDirectoryEntry*>(&m_entries[proc_file_type]);
  1529. return nullptr;
  1530. }
  1531. KResult ProcFSInode::chown(uid_t, gid_t)
  1532. {
  1533. return EPERM;
  1534. }
  1535. }