starnix_core/vfs/socket/
socket_file.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::security;
6use crate::task::{CurrentTask, EventHandler, FullCredentials, WaitCanceler, Waiter};
7use crate::vfs::buffers::{AncillaryData, InputBuffer, MessageReadInfo, OutputBuffer};
8use crate::vfs::file_server::serve_file;
9use crate::vfs::socket::{
10    Socket, SocketAddress, SocketDomain, SocketHandle, SocketMessageFlags, SocketProtocol,
11    SocketType,
12};
13use crate::vfs::{
14    Anon, DowncastedFile, FileHandle, FileObject, FileObjectState, FileOps, FsNodeInfo,
15    fileops_impl_nonseekable, fileops_impl_noop_sync,
16};
17use starnix_sync::{FileOpsCore, LockEqualOrBefore, Locked, Unlocked};
18use starnix_syscalls::{SyscallArg, SyscallResult};
19use starnix_uapi::error;
20use starnix_uapi::errors::{Errno, errno};
21use starnix_uapi::file_mode::mode;
22use starnix_uapi::open_flags::OpenFlags;
23use starnix_uapi::vfs::FdEvents;
24use zx::HandleBased;
25
26use super::socket_fs;
27
28pub struct SocketFile {
29    pub(super) socket: SocketHandle,
30}
31
32impl SocketFile {
33    /// Creates a `FileHandle` referring to a socket.
34    ///
35    /// # Parameters
36    /// - `current_task`: The current task.
37    /// - `socket`: The socket to refer to.
38    /// - `open_flags`: The `OpenFlags` which are used to create the `FileObject`.
39    /// - `kernel_private`: `true` if the socket will be used internally by the kernel, and should
40    ///   therefore not be security labeled nor access-checked.
41    pub fn from_socket<L>(
42        locked: &mut Locked<L>,
43        current_task: &CurrentTask,
44        socket: SocketHandle,
45        open_flags: OpenFlags,
46        kernel_private: bool,
47    ) -> Result<FileHandle, Errno>
48    where
49        L: LockEqualOrBefore<FileOpsCore>,
50    {
51        let fs = socket_fs(locked, current_task.kernel());
52        let mode = mode!(IFSOCK, 0o777);
53        let node = fs.create_node_and_allocate_node_id(
54            Anon::new_for_socket(kernel_private),
55            FsNodeInfo::new(mode, current_task.current_fscred()),
56        );
57        socket.set_fs_node(&node);
58        security::socket_post_create(&socket);
59        Ok(FileObject::new_anonymous(
60            locked,
61            current_task,
62            SocketFile::new(socket),
63            node,
64            open_flags,
65        ))
66    }
67
68    /// Shortcut for Socket::new plus SocketFile::from_socket.
69    pub fn new_socket<L>(
70        locked: &mut Locked<L>,
71        current_task: &CurrentTask,
72        domain: SocketDomain,
73        socket_type: SocketType,
74        open_flags: OpenFlags,
75        protocol: SocketProtocol,
76        kernel_private: bool,
77    ) -> Result<FileHandle, Errno>
78    where
79        L: LockEqualOrBefore<FileOpsCore>,
80    {
81        {
82            let socket =
83                Socket::new(locked, current_task, domain, socket_type, protocol, kernel_private)?;
84            SocketFile::from_socket(locked, current_task, socket, open_flags, kernel_private)
85        }
86    }
87
88    pub fn get_from_file(file: &FileHandle) -> Result<DowncastedFile<'_, Self>, Errno> {
89        file.downcast_file::<SocketFile>().ok_or_else(|| errno!(ENOTSOCK))
90    }
91
92    pub fn socket(&self) -> &SocketHandle {
93        &self.socket
94    }
95}
96
97impl FileOps for SocketFile {
98    fileops_impl_nonseekable!();
99    fileops_impl_noop_sync!();
100
101    fn read(
102        &self,
103        locked: &mut Locked<FileOpsCore>,
104        file: &FileObject,
105        current_task: &CurrentTask,
106        offset: usize,
107        data: &mut dyn OutputBuffer,
108    ) -> Result<usize, Errno> {
109        debug_assert!(offset == 0);
110        // The behavior of recv differs from read: recv will block if given a zero-size buffer when
111        // there's no data available, but read will immediately return 0.
112        if data.available() == 0 {
113            return Ok(0);
114        }
115        let info =
116            self.recvmsg(locked, current_task, file, data, SocketMessageFlags::empty(), None)?;
117        Ok(info.bytes_read)
118    }
119
120    fn write(
121        &self,
122        locked: &mut Locked<FileOpsCore>,
123        file: &FileObject,
124        current_task: &CurrentTask,
125        offset: usize,
126        data: &mut dyn InputBuffer,
127    ) -> Result<usize, Errno> {
128        debug_assert!(offset == 0);
129        self.sendmsg(locked, current_task, file, data, None, vec![], SocketMessageFlags::empty())
130    }
131
132    fn wait_async(
133        &self,
134        locked: &mut Locked<FileOpsCore>,
135        _file: &FileObject,
136        current_task: &CurrentTask,
137        waiter: &Waiter,
138        events: FdEvents,
139        handler: EventHandler,
140    ) -> Option<WaitCanceler> {
141        Some(self.socket.wait_async(locked, current_task, waiter, events, handler))
142    }
143
144    fn query_events(
145        &self,
146        locked: &mut Locked<FileOpsCore>,
147        _file: &FileObject,
148        current_task: &CurrentTask,
149    ) -> Result<FdEvents, Errno> {
150        self.socket.query_events(locked, current_task)
151    }
152
153    fn ioctl(
154        &self,
155        locked: &mut Locked<Unlocked>,
156        file: &FileObject,
157        current_task: &CurrentTask,
158        request: u32,
159        arg: SyscallArg,
160    ) -> Result<SyscallResult, Errno> {
161        self.socket.ioctl(locked, file, current_task, request, arg)
162    }
163
164    fn close(
165        self: Box<Self>,
166        locked: &mut Locked<FileOpsCore>,
167        _file: &FileObjectState,
168        current_task: &CurrentTask,
169    ) {
170        self.socket.close(locked, current_task);
171    }
172
173    /// Return a handle that allows access to this file descritor through the zxio protocols.
174    ///
175    /// If None is returned, the file will act as if it was a fd to `/dev/null`.
176    fn to_handle(
177        &self,
178        file: &FileObject,
179        current_task: &CurrentTask,
180    ) -> Result<Option<zx::NullableHandle>, Errno> {
181        if let Some(handle) = self.socket.to_handle(file, current_task)? {
182            Ok(Some(handle))
183        } else {
184            serve_file(current_task, file, FullCredentials::for_kernel())
185                .map(|c| Some(c.0.into_handle()))
186        }
187    }
188}
189
190impl SocketFile {
191    pub fn new(socket: SocketHandle) -> Box<Self> {
192        Box::new(SocketFile { socket })
193    }
194
195    /// Writes the provided data into the socket in this file.
196    ///
197    /// The provided control message is
198    ///
199    /// # Parameters
200    /// - `task`: The task that the user buffers belong to.
201    /// - `file`: The file that will be used for the `blocking_op`.
202    /// - `data`: The user buffers to read data from.
203    /// - `control_bytes`: Control message bytes to write to the socket.
204    pub fn sendmsg<L>(
205        &self,
206        locked: &mut Locked<L>,
207        current_task: &CurrentTask,
208        file: &FileObject,
209        data: &mut dyn InputBuffer,
210        mut dest_address: Option<SocketAddress>,
211        mut ancillary_data: Vec<AncillaryData>,
212        flags: SocketMessageFlags,
213    ) -> Result<usize, Errno>
214    where
215        L: LockEqualOrBefore<FileOpsCore>,
216    {
217        let bytes_read_before = data.bytes_read();
218
219        // TODO: Implement more `flags`.
220        let mut op = |locked: &mut Locked<L>| {
221            let offset_before = data.bytes_read();
222            let sent_bytes = self.socket.write(
223                locked,
224                current_task,
225                data,
226                &mut dest_address,
227                &mut ancillary_data,
228            )?;
229            debug_assert!(data.bytes_read() - offset_before == sent_bytes);
230            if data.available() > 0 {
231                return error!(EAGAIN);
232            }
233            Ok(())
234        };
235
236        let result = if flags.contains(SocketMessageFlags::DONTWAIT) {
237            op(locked)
238        } else {
239            let deadline = self.socket.send_timeout().map(zx::MonotonicInstant::after);
240            file.blocking_op(
241                locked,
242                current_task,
243                FdEvents::POLLOUT | FdEvents::POLLHUP,
244                deadline,
245                op,
246            )
247        };
248
249        let bytes_written = data.bytes_read() - bytes_read_before;
250        if bytes_written == 0 {
251            // We can only return an error if no data was actually sent. If partial data was
252            // sent, swallow the error and return how much was sent.
253            result?;
254        }
255        Ok(bytes_written)
256    }
257
258    /// Reads data from the socket in this file into `data`.
259    ///
260    /// # Parameters
261    /// - `file`: The file that will be used to wait if necessary.
262    /// - `task`: The task that the user buffers belong to.
263    /// - `data`: The user buffers to write to.
264    ///
265    /// Returns the number of bytes read, as well as any control message that was encountered.
266    pub fn recvmsg<L>(
267        &self,
268        locked: &mut Locked<L>,
269        current_task: &CurrentTask,
270        file: &FileObject,
271        data: &mut dyn OutputBuffer,
272        flags: SocketMessageFlags,
273        deadline: Option<zx::MonotonicInstant>,
274    ) -> Result<MessageReadInfo, Errno>
275    where
276        L: LockEqualOrBefore<FileOpsCore>,
277    {
278        // TODO: Implement more `flags`.
279        let mut read_info = MessageReadInfo::default();
280
281        let mut op = |locked: &mut Locked<L>| {
282            let mut info = self.socket.read(locked, current_task, data, flags)?;
283            read_info.append(&mut info);
284            read_info.address = info.address;
285
286            let should_wait_all = self.socket.socket_type == SocketType::Stream
287                && flags.contains(SocketMessageFlags::WAITALL)
288                && !self.socket.query_events(locked, current_task)?.contains(FdEvents::POLLHUP);
289            if should_wait_all && data.available() > 0 {
290                return error!(EAGAIN);
291            }
292            Ok(())
293        };
294
295        let dont_wait =
296            flags.intersects(SocketMessageFlags::DONTWAIT | SocketMessageFlags::ERRQUEUE);
297        let result = if dont_wait {
298            op(locked)
299        } else {
300            let deadline =
301                deadline.or_else(|| self.socket.receive_timeout().map(zx::MonotonicInstant::after));
302            file.blocking_op(
303                locked,
304                current_task,
305                FdEvents::POLLIN | FdEvents::POLLHUP,
306                deadline,
307                op,
308            )
309        };
310
311        if read_info.bytes_read == 0 {
312            // We can only return an error if no data was actually read. If partial data was
313            // read, swallow the error and return how much was read.
314            result?;
315        }
316        Ok(read_info)
317    }
318}