ProcFS.cpp 59 KB

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