ProcFS.cpp 59 KB

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