ProcFS.cpp 58 KB

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