ProcFS.cpp 59 KB

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