Skip to main content

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