shadow_rs/host/syscall/handler/
file.rs
1use linux_api::errno::Errno;
2use linux_api::posix_types::kernel_mode_t;
3use shadow_shim_helper_rs::syscall_types::ForeignPtr;
4
5use crate::cshadow;
6use crate::host::descriptor::CompatFile;
7use crate::host::syscall::File;
8use crate::host::syscall::handler::{SyscallContext, SyscallHandler};
9use crate::host::syscall::type_formatting::SyscallStringArg;
10use crate::host::syscall::types::{SyscallError, SyscallResult};
11
12impl SyscallHandler {
13 log_syscall!(
14 open,
15 std::ffi::c_int,
16 SyscallStringArg,
17 linux_api::fcntl::OFlag,
18 nix::sys::stat::Mode,
19 );
20 pub fn open(
21 ctx: &mut SyscallContext,
22 _path: ForeignPtr<()>,
23 _flags: std::ffi::c_int,
24 _mode: kernel_mode_t,
25 ) -> SyscallResult {
26 Self::legacy_syscall(cshadow::syscallhandler_open, ctx)
27 }
28
29 log_syscall!(
30 creat,
31 std::ffi::c_int,
32 SyscallStringArg,
33 nix::sys::stat::Mode
34 );
35 pub fn creat(ctx: &mut SyscallContext) -> SyscallResult {
36 Self::legacy_syscall(cshadow::syscallhandler_creat, ctx)
37 }
38
39 log_syscall!(
40 fadvise64,
41 std::ffi::c_int,
42 std::ffi::c_int,
43 linux_api::types::loff_t,
44 linux_api::types::size_t,
45 std::ffi::c_int
46 );
47 pub fn fadvise64(ctx: &mut SyscallContext) -> SyscallResult {
48 Self::legacy_syscall(cshadow::syscallhandler_fadvise64, ctx)
49 }
50
51 log_syscall!(
52 fallocate,
53 std::ffi::c_int,
54 std::ffi::c_int,
55 std::ffi::c_int,
56 linux_api::types::loff_t,
57 linux_api::types::loff_t
58 );
59 pub fn fallocate(ctx: &mut SyscallContext) -> SyscallResult {
60 Self::legacy_syscall(cshadow::syscallhandler_fallocate, ctx)
61 }
62
63 log_syscall!(
64 fchmod,
65 std::ffi::c_int,
66 std::ffi::c_int,
67 linux_api::types::umode_t,
68 );
69 pub fn fchmod(ctx: &mut SyscallContext) -> SyscallResult {
70 Self::legacy_syscall(cshadow::syscallhandler_fchmod, ctx)
71 }
72
73 log_syscall!(
74 fchown,
75 std::ffi::c_int,
76 std::ffi::c_uint,
77 linux_api::types::uid_t,
78 linux_api::types::gid_t
79 );
80 pub fn fchown(ctx: &mut SyscallContext) -> SyscallResult {
81 Self::legacy_syscall(cshadow::syscallhandler_fchown, ctx)
82 }
83
84 log_syscall!(
85 fdatasync,
86 std::ffi::c_int,
87 std::ffi::c_uint
88 );
89 pub fn fdatasync(ctx: &mut SyscallContext) -> SyscallResult {
90 Self::legacy_syscall(cshadow::syscallhandler_fdatasync, ctx)
91 }
92
93 log_syscall!(
94 fgetxattr,
95 std::ffi::c_int,
96 std::ffi::c_int,
97 SyscallStringArg,
98 *const std::ffi::c_void,
99 linux_api::types::size_t
100 );
101 pub fn fgetxattr(ctx: &mut SyscallContext) -> SyscallResult {
102 Self::legacy_syscall(cshadow::syscallhandler_fgetxattr, ctx)
103 }
104
105 log_syscall!(
106 flistxattr,
107 std::ffi::c_int,
108 std::ffi::c_int,
109 SyscallStringArg,
114 linux_api::types::size_t
115 );
116 pub fn flistxattr(ctx: &mut SyscallContext) -> SyscallResult {
117 Self::legacy_syscall(cshadow::syscallhandler_flistxattr, ctx)
118 }
119
120 log_syscall!(
121 flock,
122 std::ffi::c_int,
123 std::ffi::c_uint,
124 std::ffi::c_uint
125 );
126 pub fn flock(ctx: &mut SyscallContext) -> SyscallResult {
127 Self::legacy_syscall(cshadow::syscallhandler_flock, ctx)
128 }
129
130 log_syscall!(
131 fremovexattr,
132 std::ffi::c_int,
133 std::ffi::c_int,
134 SyscallStringArg
135 );
136 pub fn fremovexattr(ctx: &mut SyscallContext) -> SyscallResult {
137 Self::legacy_syscall(cshadow::syscallhandler_fremovexattr, ctx)
138 }
139
140 log_syscall!(
141 fsetxattr,
142 std::ffi::c_int,
143 std::ffi::c_int,
144 SyscallStringArg,
145 *const std::ffi::c_void,
146 linux_api::types::size_t,
147 std::ffi::c_int
148 );
149 pub fn fsetxattr(ctx: &mut SyscallContext) -> SyscallResult {
150 Self::legacy_syscall(cshadow::syscallhandler_fsetxattr, ctx)
151 }
152
153 log_syscall!(
154 fsync,
155 std::ffi::c_int,
156 std::ffi::c_uint
157 );
158 pub fn fsync(ctx: &mut SyscallContext) -> SyscallResult {
159 Self::legacy_syscall(cshadow::syscallhandler_fsync, ctx)
160 }
161
162 log_syscall!(
163 ftruncate,
164 std::ffi::c_int,
165 std::ffi::c_uint,
166 linux_api::types::off_t
167 );
168 pub fn ftruncate(ctx: &mut SyscallContext) -> SyscallResult {
169 Self::legacy_syscall(cshadow::syscallhandler_ftruncate, ctx)
170 }
171
172 log_syscall!(
173 getdents,
174 std::ffi::c_int,
175 std::ffi::c_uint,
176 *const std::ffi::c_void,
177 std::ffi::c_uint
178 );
179 pub fn getdents(ctx: &mut SyscallContext) -> SyscallResult {
180 Self::legacy_syscall(cshadow::syscallhandler_getdents, ctx)
181 }
182
183 log_syscall!(
184 getdents64,
185 std::ffi::c_int,
186 std::ffi::c_uint,
187 *const std::ffi::c_void,
188 std::ffi::c_uint
189 );
190 pub fn getdents64(ctx: &mut SyscallContext) -> SyscallResult {
191 Self::legacy_syscall(cshadow::syscallhandler_getdents64, ctx)
192 }
193
194 log_syscall!(
195 lseek,
196 std::ffi::c_int,
197 std::ffi::c_uint,
198 linux_api::posix_types::kernel_off_t,
199 std::ffi::c_uint,
200 );
201 pub fn lseek(
202 ctx: &mut SyscallContext,
203 fd: std::ffi::c_uint,
204 _offset: linux_api::posix_types::kernel_off_t,
205 _whence: std::ffi::c_uint,
206 ) -> Result<linux_api::posix_types::kernel_off_t, SyscallError> {
207 let desc_table = ctx.objs.thread.descriptor_table_borrow(ctx.objs.host);
208
209 let file = {
210 match Self::get_descriptor(&desc_table, fd)?.file() {
211 CompatFile::New(file) => file,
212 CompatFile::Legacy(_) => {
214 drop(desc_table);
215 return Self::legacy_syscall(cshadow::syscallhandler_lseek, ctx);
216 }
217 }
218 };
219
220 match file.inner_file() {
221 File::Pipe(_) => Err(Errno::ESPIPE.into()),
222 _ => {
223 warn_once_then_debug!("lseek() is not implemented for this type");
224 Err(Errno::ENOTSUP.into())
225 }
226 }
227 }
228
229 log_syscall!(
230 readahead,
231 std::ffi::c_int,
232 std::ffi::c_int,
233 linux_api::types::loff_t,
234 linux_api::types::size_t
235 );
236 pub fn readahead(ctx: &mut SyscallContext) -> SyscallResult {
237 Self::legacy_syscall(cshadow::syscallhandler_readahead, ctx)
238 }
239
240 log_syscall!(
241 sync_file_range,
242 std::ffi::c_int,
243 std::ffi::c_int,
244 linux_api::types::loff_t,
245 linux_api::types::loff_t,
246 std::ffi::c_uint
247 );
248 pub fn sync_file_range(ctx: &mut SyscallContext) -> SyscallResult {
249 Self::legacy_syscall(cshadow::syscallhandler_sync_file_range, ctx)
250 }
251
252 log_syscall!(
253 syncfs,
254 std::ffi::c_int,
255 std::ffi::c_int
256 );
257 pub fn syncfs(ctx: &mut SyscallContext) -> SyscallResult {
258 Self::legacy_syscall(cshadow::syscallhandler_syncfs, ctx)
259 }
260}