ProcFS.cpp 60 KB

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