Skip to content

Commit 3f10471

Browse files
jk-ozlabsmkj
authored andcommitted
mctp-linux: use OwnedFd rather than RawFd for MctpSocket
We're currently using an RawFd, and manually implementing Drop. Instead, use OwnedFd to express ownership of the socket. Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
1 parent 0a0fe88 commit 3f10471

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

mctp-linux/src/lib.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
use core::mem;
4343
use std::fmt;
4444
use std::io::Error;
45-
use std::os::unix::io::RawFd;
45+
use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd, RawFd};
4646
use std::time::Duration;
4747

4848
use mctp::{
@@ -139,13 +139,7 @@ fn last_os_error() -> mctp::Error {
139139
}
140140

141141
/// MCTP socket object.
142-
pub struct MctpSocket(RawFd);
143-
144-
impl Drop for MctpSocket {
145-
fn drop(&mut self) {
146-
unsafe { libc::close(self.0) };
147-
}
148-
}
142+
pub struct MctpSocket(OwnedFd);
149143

150144
impl MctpSocket {
151145
/// Create a new MCTP socket. This can then be used for send/receive
@@ -161,7 +155,9 @@ impl MctpSocket {
161155
if rc < 0 {
162156
return Err(last_os_error());
163157
}
164-
Ok(MctpSocket(rc))
158+
// safety: the fd is valid, and we have exclusive ownership
159+
let fd = unsafe { OwnedFd::from_raw_fd(rc) };
160+
Ok(MctpSocket(fd))
165161
}
166162

167163
/// Blocking receive from a socket, into `buf`, returning a length
@@ -174,9 +170,10 @@ impl MctpSocket {
174170
let (addr_ptr, mut addr_len) = addr.as_raw_mut();
175171
let buf_ptr = buf.as_mut_ptr() as *mut libc::c_void;
176172
let buf_len = buf.len() as libc::size_t;
173+
let fd = self.as_raw_fd();
177174

178175
let rc = unsafe {
179-
libc::recvfrom(self.0, buf_ptr, buf_len, 0, addr_ptr, &mut addr_len)
176+
libc::recvfrom(fd, buf_ptr, buf_len, 0, addr_ptr, &mut addr_len)
180177
};
181178

182179
if rc < 0 {
@@ -194,9 +191,10 @@ impl MctpSocket {
194191
let (addr_ptr, addr_len) = addr.as_raw();
195192
let buf_ptr = buf.as_ptr() as *const libc::c_void;
196193
let buf_len = buf.len() as libc::size_t;
194+
let fd = self.as_raw_fd();
197195

198196
let rc = unsafe {
199-
libc::sendto(self.0, buf_ptr, buf_len, 0, addr_ptr, addr_len)
197+
libc::sendto(fd, buf_ptr, buf_len, 0, addr_ptr, addr_len)
200198
};
201199

202200
if rc < 0 {
@@ -209,8 +207,9 @@ impl MctpSocket {
209207
/// Bind the socket to a local address.
210208
pub fn bind(&self, addr: &MctpSockAddr) -> Result<()> {
211209
let (addr_ptr, addr_len) = addr.as_raw();
210+
let fd = self.as_raw_fd();
212211

213-
let rc = unsafe { libc::bind(self.0, addr_ptr, addr_len) };
212+
let rc = unsafe { libc::bind(fd, addr_ptr, addr_len) };
214213

215214
if rc < 0 {
216215
Err(last_os_error())
@@ -231,9 +230,10 @@ impl MctpSocket {
231230
tv_sec: dur.as_secs() as libc::time_t,
232231
tv_usec: dur.subsec_micros() as libc::suseconds_t,
233232
};
233+
let fd = self.as_raw_fd();
234234
let rc = unsafe {
235235
libc::setsockopt(
236-
self.0,
236+
fd,
237237
libc::SOL_SOCKET,
238238
libc::SO_RCVTIMEO,
239239
(&tv as *const libc::timeval) as *const libc::c_void,
@@ -260,9 +260,10 @@ impl MctpSocket {
260260
let mut tv = std::mem::MaybeUninit::<libc::timeval>::uninit();
261261
let mut tv_len =
262262
std::mem::size_of::<libc::timeval>() as libc::socklen_t;
263+
let fd = self.as_raw_fd();
263264
let rc = unsafe {
264265
libc::getsockopt(
265-
self.0,
266+
fd,
266267
libc::SOL_SOCKET,
267268
libc::SO_RCVTIMEO,
268269
tv.as_mut_ptr() as *mut libc::c_void,
@@ -293,7 +294,7 @@ impl MctpSocket {
293294

294295
impl std::os::fd::AsRawFd for MctpSocket {
295296
fn as_raw_fd(&self) -> RawFd {
296-
self.0
297+
self.0.as_raw_fd()
297298
}
298299
}
299300

0 commit comments

Comments
 (0)