1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 //! uds
16 #![allow(missing_docs)]
17 #![allow(clippy::missing_safety_doc)]
18 #[cfg(not(feature = "host"))]
19 use crate::utils::hdc_log::*;
20 use libc::bind;
21 use libc::c_void;
22 use libc::{accept4, close, connect};
23 use libc::{c_char, listen, poll, recv, socket, MSG_NOSIGNAL};
24 use libc::{c_int, sa_family_t, sockaddr, sockaddr_un, socklen_t, AF_UNIX};
25 use libc::{eventfd, fcntl, pipe, read, send, socketpair, write, MSG_EOR};
26 use libc::{EFD_CLOEXEC, EFD_NONBLOCK, SOCK_CLOEXEC, SOCK_STREAM};
27 use libc::{POLLERR, POLLHUP, POLLNVAL, POLLRDHUP};
28 use std::io::{self, ErrorKind, Result};
29 use std::mem;
30
31 const LISTEN_BACKLOG: c_int = 10;
32 const MAX_CLIENT_FD_COUNT: usize = 256;
33
34 #[derive(PartialEq, Debug, Clone)]
35 pub struct PollNode {
36 pub fd: i32,
37 pub events: i16,
38 pub revents: i16,
39 pub ppid: u32,
40 pub pkg_name: String,
41 pub debug_or_release: bool,
42 }
43
44 impl PollNode {
new(fd: i32, ppid: u32, pkg_name: String, debug_or_release: bool) -> Self45 pub fn new(fd: i32, ppid: u32, pkg_name: String, debug_or_release: bool) -> Self {
46 Self {
47 fd,
48 events: POLLNVAL | POLLRDHUP | POLLHUP | POLLERR,
49 revents: 0,
50 ppid,
51 pkg_name,
52 debug_or_release,
53 }
54 }
55 }
56
name_index() -> socklen_t57 fn name_index() -> socklen_t {
58 unsafe {
59 let total_size = mem::size_of::<sockaddr_un>();
60 let name_size = mem::size_of_val(&mem::zeroed::<sockaddr_un>().sun_path);
61 (total_size - name_size) as socklen_t
62 }
63 }
64
65 // const fn as_u8(slice: &[c_char]) -> &[u8] {
66 // unsafe { &*(slice as *const [c_char] as *const [u8]) }
67 // }
68
as_char(slice: &[u8]) -> &[c_char]69 const fn as_char(slice: &[u8]) -> &[c_char] {
70 unsafe { &*(slice as *const [u8] as *const [c_char]) }
71 }
72
73 pub struct UdsAddr {
74 addr: sockaddr_un,
75 len: socklen_t,
76 }
77
78 impl Default for UdsAddr {
default() -> Self79 fn default() -> Self {
80 let mut addr: sockaddr_un = unsafe { mem::zeroed() };
81 addr.sun_family = AF_UNIX as sa_family_t;
82 Self {
83 len: name_index(),
84 addr,
85 }
86 }
87 }
88
89 impl UdsAddr {
parse_abstract(name: &[u8]) -> io::Result<Self>90 pub fn parse_abstract(name: &[u8]) -> io::Result<Self> {
91 let mut addr = UdsAddr::default();
92 addr.addr.sun_path[1..1 + name.len()].copy_from_slice(as_char(name));
93 addr.len = name_index() + 1 + name.len() as socklen_t;
94 Ok(addr)
95 }
96
as_raw_general(&self) -> (&sockaddr, socklen_t)97 pub fn as_raw_general(&self) -> (&sockaddr, socklen_t) {
98 (
99 unsafe { &*(&self.addr as *const sockaddr_un as *const sockaddr) },
100 self.len,
101 )
102 }
103
as_raw_mut_general(&mut self) -> (&mut sockaddr, &mut socklen_t)104 pub unsafe fn as_raw_mut_general(&mut self) -> (&mut sockaddr, &mut socklen_t) {
105 (
106 &mut *(&mut self.addr as *mut sockaddr_un as *mut sockaddr),
107 &mut self.len,
108 )
109 }
110 }
111
112 pub struct UdsServer {}
113
114 impl UdsServer {
wrap_socket(socket_type: c_int) -> i32115 pub fn wrap_socket(socket_type: c_int) -> i32 {
116 let flags = socket_type | SOCK_CLOEXEC;
117 unsafe { socket(AF_UNIX, flags, 0) }
118 }
119
wrap_bind(socket_fd: i32, addr: &UdsAddr) -> Result<()>120 pub fn wrap_bind(socket_fd: i32, addr: &UdsAddr) -> Result<()> {
121 unsafe {
122 let (addr_raw, len_raw) = addr.as_raw_general();
123 loop {
124 let ret = bind(socket_fd, addr_raw, len_raw);
125 if ret != -1 {
126 break Ok(());
127 }
128 crate::error!("bind failed ret : {}", ret);
129 let err = io::Error::last_os_error();
130 if err.kind() != ErrorKind::Interrupted {
131 break Err(err);
132 }
133 }
134 }
135 }
136
wrap_listen(socket_fd: i32) -> c_int137 pub fn wrap_listen(socket_fd: i32) -> c_int {
138 unsafe { listen(socket_fd, LISTEN_BACKLOG) }
139 }
140
wrap_accept(socket_fd: i32) -> i32141 pub fn wrap_accept(socket_fd: i32) -> i32 {
142 let mut addr = UdsAddr::default();
143 let capacity = mem::size_of_val(&addr.addr) as socklen_t;
144 addr.len = capacity;
145 unsafe {
146 let (addr_ptr, len_ptr) = addr.as_raw_mut_general();
147 accept4(socket_fd, addr_ptr, len_ptr, SOCK_CLOEXEC)
148 }
149 }
150
wrap_recv(socket_fd: i32, buffer: &mut [u8]) -> isize151 pub fn wrap_recv(socket_fd: i32, buffer: &mut [u8]) -> isize {
152 let ptr = buffer.as_ptr() as *mut c_void;
153 unsafe { recv(socket_fd, ptr, buffer.len(), MSG_NOSIGNAL) }
154 }
155
wrap_read(socket_fd: i32, buffer: &mut [u8]) -> isize156 pub fn wrap_read(socket_fd: i32, buffer: &mut [u8]) -> isize {
157 let ptr = buffer.as_ptr() as *mut c_void;
158 unsafe { read(socket_fd, ptr, buffer.len()) }
159 }
160
wrap_write(socket_fd: i32, buffer: &[u8]) -> isize161 pub fn wrap_write(socket_fd: i32, buffer: &[u8]) -> isize {
162 let ptr = buffer.as_ptr() as *const c_void;
163 unsafe { write(socket_fd, ptr, buffer.len()) }
164 }
165
wrap_event_fd() -> i32166 pub fn wrap_event_fd() -> i32 {
167 unsafe { eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK) }
168 }
169
wrap_write_fd(fd: i32)170 pub fn wrap_write_fd(fd: i32) {
171 unsafe {
172 let buf = [1u64; 1];
173 let ret = write(fd, buf.as_ptr() as *const c_void, mem::size_of_val(&buf));
174 crate::info!("uds wrap_write fd, fd:{}, ret:{}", fd, ret);
175 }
176 }
177
wrap_read_fd(fd: i32)178 pub fn wrap_read_fd(fd: i32) {
179 unsafe {
180 let buf = [1u64; 1];
181 read(fd, buf.as_ptr() as *mut c_void, mem::size_of_val(&buf));
182 }
183 }
184
wrap_poll(fds: &mut [PollNode], size: u32, timeout: i32) -> i32185 pub fn wrap_poll(fds: &mut [PollNode], size: u32, timeout: i32) -> i32 {
186 let init_value = unsafe { mem::zeroed() };
187 let pollfds: &mut [libc::pollfd; MAX_CLIENT_FD_COUNT] =
188 &mut [init_value; MAX_CLIENT_FD_COUNT];
189 for (index, node) in fds.iter_mut().enumerate() {
190 if !(0..MAX_CLIENT_FD_COUNT).contains(&index) {
191 continue;
192 }
193 pollfds[index].fd = node.fd;
194 pollfds[index].events = node.events;
195 pollfds[index].revents = node.revents;
196 }
197 unsafe {
198 let ret = poll(pollfds.as_mut_ptr(), size as libc::nfds_t, timeout);
199 crate::info!("uds wrap_poll, poll ret:{}", ret);
200 if ret == -1 {
201 ret
202 } else {
203 for i in 0..size as usize {
204 if i >= fds.len() {
205 break;
206 }
207 fds[i].revents = pollfds[i].revents;
208 fds[i].events = pollfds[i].events;
209 }
210 0
211 }
212 }
213 }
214
215 #[allow(unused)]
wrap_send(socket_fd: i32, buffer: &[u8]) -> isize216 pub fn wrap_send(socket_fd: i32, buffer: &[u8]) -> isize {
217 let ptr = buffer.as_ptr() as *const c_void;
218 let flags = MSG_NOSIGNAL | MSG_EOR;
219 unsafe { send(socket_fd, ptr, buffer.len(), flags) }
220 }
221
222 #[allow(unused)]
wrap_close(socket_fd: i32)223 pub fn wrap_close(socket_fd: i32) {
224 unsafe { close(socket_fd) };
225 }
226
227 #[allow(unused)]
wrap_socketpair(socket_type: c_int) -> Result<(i32, i32)>228 pub fn wrap_socketpair(socket_type: c_int) -> Result<(i32, i32)> {
229 let flags = socket_type | SOCK_CLOEXEC;
230 let mut fd_buf = [-1; 2];
231 unsafe {
232 socketpair(AF_UNIX, flags, 0, fd_buf[..].as_mut_ptr());
233 fcntl(fd_buf[1], 100, 20);
234 }
235 Ok((fd_buf[0], fd_buf[1]))
236 }
237
238 #[allow(unused)]
wrap_pipe() -> Result<(i32, i32)>239 pub fn wrap_pipe() -> Result<(i32, i32)> {
240 let mut fd_buf = [-1; 2];
241 unsafe {
242 crate::debug!("pipe() begin...");
243 let ret = pipe(fd_buf[..].as_mut_ptr());
244 crate::debug!("pipe() ret:{}", ret);
245 if ret >= 0 {
246 fcntl(fd_buf[1], 100, 20);
247 }
248 }
249 Ok((fd_buf[0], fd_buf[1]))
250 }
251 }
252
253 pub struct UdsClient {}
254
255 impl UdsClient {
wrap_socket(af: i32) -> i32256 pub fn wrap_socket(af: i32) -> i32 {
257 let flags = SOCK_STREAM | SOCK_CLOEXEC;
258 unsafe { socket(af, flags, 0) }
259 }
260
wrap_bind(socket_fd: i32, addr: &UdsAddr) -> Result<()>261 pub fn wrap_bind(socket_fd: i32, addr: &UdsAddr) -> Result<()> {
262 unsafe {
263 let (addr_raw, len_raw) = addr.as_raw_general();
264 loop {
265 let ret = bind(socket_fd, addr_raw, len_raw);
266 crate::debug!("bind ret : {}", ret);
267 if ret != -1 {
268 break Ok(());
269 }
270 let err = io::Error::last_os_error();
271 if err.kind() != ErrorKind::Interrupted {
272 break Err(err);
273 }
274 }
275 }
276 }
277
wrap_listen(socket_fd: i32) -> c_int278 pub fn wrap_listen(socket_fd: i32) -> c_int {
279 unsafe { listen(socket_fd, LISTEN_BACKLOG) }
280 }
281
wrap_connect(socket_fd: i32, addr: &UdsAddr) -> Result<()>282 pub fn wrap_connect(socket_fd: i32, addr: &UdsAddr) -> Result<()> {
283 unsafe {
284 let (addr_raw, len_raw) = addr.as_raw_general();
285 crate::debug!("wrap_connect:len_raw: {:#?}", len_raw);
286 crate::debug!("wrap_connect:addr_raw: {:#?}", addr_raw.sa_data);
287 loop {
288 let ret = connect(socket_fd, addr_raw, len_raw);
289 crate::debug!("connect ret ++++++---->: {}", ret);
290 if ret != -1 {
291 break Ok(());
292 }
293 let err = io::Error::last_os_error();
294 if err.kind() != ErrorKind::Interrupted {
295 break Err(err);
296 }
297 }
298 }
299 }
300
wrap_recv(socket_fd: i32, buffer: &mut [u8]) -> isize301 pub fn wrap_recv(socket_fd: i32, buffer: &mut [u8]) -> isize {
302 let ptr = buffer.as_ptr() as *mut c_void;
303 unsafe { recv(socket_fd, ptr, buffer.len(), MSG_NOSIGNAL) }
304 }
305
wrap_read(socket_fd: i32, buffer: &mut [u8]) -> isize306 pub fn wrap_read(socket_fd: i32, buffer: &mut [u8]) -> isize {
307 let ptr = buffer.as_ptr() as *mut c_void;
308 unsafe { read(socket_fd, ptr, buffer.len()) }
309 }
310
wrap_write(socket_fd: i32, buffer: &[u8]) -> isize311 pub fn wrap_write(socket_fd: i32, buffer: &[u8]) -> isize {
312 let ptr = buffer.as_ptr() as *const c_void;
313 unsafe { write(socket_fd, ptr, buffer.len()) }
314 }
315
wrap_send(socket_fd: i32, buffer: &[u8]) -> isize316 pub fn wrap_send(socket_fd: i32, buffer: &[u8]) -> isize {
317 let ptr = buffer.as_ptr() as *const c_void;
318 let flags = MSG_NOSIGNAL | MSG_EOR;
319 unsafe { send(socket_fd, ptr, buffer.len(), flags) }
320 }
321 }
322