ProcFS.cpp 59 KB

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