Skip to content

Commit 8b0fddf

Browse files
committed
rewrite procfs
1 parent 8c5afb9 commit 8b0fddf

18 files changed

Lines changed: 854 additions & 834 deletions

File tree

kernel/interfaces/drivers/fs/procfs.cppm

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,84 @@ import std;
88

99
export namespace fs::procfs
1010
{
11-
using gen_fn = std::string (*)(sched::process_t *);
11+
enum class node_type { file, dir, symlink };
1212

13-
// /proc/<name>
14-
bool register_global(std::string name, gen_fn gen, mode_t mode, bool is_symlink = false);
13+
struct node_ops;
14+
struct node_t
15+
{
16+
std::string name;
17+
mode_t mode;
18+
node_type type;
19+
std::shared_ptr<node_ops> ops;
20+
};
1521

16-
// /proc/[pid]/<name>
17-
bool register_per_pid(std::string name, gen_fn gen, mode_t mode, bool is_symlink = false);
22+
struct node_ops
23+
{
24+
virtual ~node_ops() = default;
25+
26+
virtual bool can_trunc() { return false; }
27+
28+
virtual lib::expect<std::string> generate(sched::process_t *proc)
29+
{
30+
lib::unused(proc);
31+
return std::unexpected { lib::err::not_supported };
32+
}
33+
34+
virtual lib::expect<void> write(sched::process_t *proc, std::string_view data)
35+
{
36+
lib::unused(proc, data);
37+
return std::unexpected { lib::err::read_only_fs };
38+
}
39+
40+
virtual lib::expect<lib::path> readlink(sched::process_t *proc)
41+
{
42+
lib::unused(proc);
43+
return std::unexpected { lib::err::not_supported };
44+
}
45+
46+
virtual lib::expect<lib::list<node_t>> readdir(sched::process_t *proc)
47+
{
48+
lib::unused(proc);
49+
return std::unexpected { lib::err::not_supported };
50+
}
51+
52+
virtual lib::expect<node_t> lookup(sched::process_t *proc, std::string_view name)
53+
{
54+
lib::unused(proc, name);
55+
return std::unexpected { lib::err::not_supported };
56+
}
57+
58+
virtual bool revalidate(sched::process_t *proc)
59+
{
60+
lib::unused(proc);
61+
return true;
62+
}
63+
};
64+
65+
// I hate myself
66+
using gen_fn = std::function<lib::expect<std::string> (sched::process_t *)>;
67+
using write_fn = std::function<lib::expect<void> (sched::process_t *, std::string_view)>;
68+
using readlink_fn = std::function<lib::expect<lib::path> (sched::process_t *)>;
69+
using lookup_fn = std::function<lib::expect<node_t> (sched::process_t *, std::string_view)>;
70+
using readdir_fn = std::function<lib::expect<lib::list<node_t>> (sched::process_t *)>;
71+
using revalidate_fn = std::function<bool (sched::process_t *)>;
72+
73+
std::shared_ptr<node_ops> make_file_ops(gen_fn gfn, write_fn wfn = nullptr);
74+
std::shared_ptr<node_ops> make_symlink_ops(readlink_fn rdlfn, revalidate_fn rvfn = nullptr);
75+
76+
std::shared_ptr<node_ops> make_dir_ops(lookup_fn lfn = nullptr, readdir_fn rfn = nullptr);
77+
78+
// /proc/<path>
79+
bool register_global(
80+
lib::path path, std::shared_ptr<node_ops> ops,
81+
node_type type, mode_t mode
82+
);
83+
84+
// /proc/[pid]/<path>
85+
bool register_per_pid(
86+
lib::path path, std::shared_ptr<node_ops> ops,
87+
node_type type, mode_t mode
88+
);
1889

1990
lib::initgraph::stage *registered_stage();
2091
lib::initgraph::stage *mounted_stage();

kernel/interfaces/drivers/fs/tmpfs.cppm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export namespace fs::tmpfs
7272
-> lib::expect<lib::list<vfs::dir_entry>> override;
7373

7474
auto lookup(std::shared_ptr<vfs::dentry> dir,std::string_view name)
75-
-> lib::expect<std::optional<vfs::dir_entry>> override;
75+
-> lib::expect<vfs::dir_entry> override;
7676

7777
auto write_inode(std::shared_ptr<vfs::inode> &inode) -> lib::expect<void> override;
7878
auto dirty_inode(std::shared_ptr<vfs::inode> &inode) -> lib::expect<void> override;

kernel/interfaces/system/proc/scheduler.cppm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export namespace sched
158158

159159
std::size_t process_count();
160160

161-
void for_each_process(std::function<bool(process_t *)> func);
161+
void for_each_process(std::function<bool (process_t *)> func);
162162

163163
// called from a timer interrupt
164164
void tick(bool from_user);

kernel/interfaces/system/sysctl.cppm

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,8 @@ export namespace sysctl
1212
using int_read_fn = std::function<int ()>;
1313
using int_write_fn = std::function<lib::expect<void> (int)>;
1414

15-
struct entry
16-
{
17-
read_fn read;
18-
write_fn write;
19-
mode_t mode;
20-
};
21-
22-
bool register_entry(std::string path, read_fn read, write_fn write, mode_t mode);
23-
bool register_ro(std::string path, read_fn read, mode_t mode = 0444);
24-
bool register_int(std::string path, int_read_fn read, int_write_fn write, mode_t mode = 0644);
25-
bool register_int_stub(std::string path, int default_value, mode_t mode = 0644);
26-
27-
std::optional<entry> find(std::string_view path);
28-
29-
struct dirent
30-
{
31-
std::string name;
32-
bool is_dir;
33-
};
34-
std::vector<dirent> list(std::string_view dir);
35-
bool dir_exists(std::string_view path);
15+
bool register_entry(std::string_view path, read_fn read, write_fn write, mode_t mode);
16+
bool register_ro(std::string_view path, read_fn read, mode_t mode = 0444);
17+
bool register_int(std::string_view path, int_read_fn read, int_write_fn write, mode_t mode = 0644);
18+
bool register_int_stub(std::string_view path, int default_value, mode_t mode = 0644);
3619
} // export namespace sysctl

kernel/interfaces/system/system.cppm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export import system.pci;
1515
export import system.random;
1616
export import system.sched;
1717
export import system.syscall;
18-
export import system.sysctl;
1918
export import system.vfs;
2019
export import system.vfs.dev;
2120
export import system.vfs.pipe;

kernel/interfaces/system/vfs/socket/base.cppm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,6 @@ export namespace vfs::socket
383383
auto create_anon(std::shared_ptr<socket_t> sock, int flags) -> lib::expect<int>;
384384

385385
std::shared_ptr<vfs::ops> get_ops();
386+
387+
lib::initgraph::stage *registered_procfs_stage();
386388
} // export namespace vfs::socket

kernel/interfaces/system/vfs/vfs.cppm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ export namespace vfs
318318
-> lib::expect<lib::list<dir_entry>> = 0;
319319

320320
virtual auto lookup(std::shared_ptr<dentry> dir,std::string_view name)
321-
-> lib::expect<std::optional<dir_entry>>;
321+
-> lib::expect<dir_entry>;
322322

323323
virtual auto readlink(std::shared_ptr<dentry> dentry) -> lib::expect<lib::path>;
324324

0 commit comments

Comments
 (0)