Skip to content

Commit d2e1e6d

Browse files
authored
Rollup merge of #88025 - devnexen:netbsd_scm_creds, r=Amanieu
ScmCredentials netbsd implementation.
2 parents f262ca1 + 23e6314 commit d2e1e6d

File tree

4 files changed

+103
-13
lines changed

4 files changed

+103
-13
lines changed

library/std/src/os/unix/net/ancillary.rs

+88-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::slice::from_raw_parts;
1010
use crate::sys::net::Socket;
1111

1212
// FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
13-
#[cfg(all(doc, not(target_os = "linux"), not(target_os = "android")))]
13+
#[cfg(all(doc, not(target_os = "linux"), not(target_os = "android"), not(target_os = "netbsd")))]
1414
#[allow(non_camel_case_types)]
1515
mod libc {
1616
pub use libc::c_int;
@@ -177,13 +177,24 @@ impl<'a, T> Iterator for AncillaryDataIter<'a, T> {
177177
}
178178
}
179179

180+
#[cfg(all(doc, not(target_os = "android"), not(target_os = "linux"), not(target_os = "netbsd")))]
181+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
182+
#[derive(Clone)]
183+
pub struct SocketCred(());
184+
180185
/// Unix credential.
181-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
186+
#[cfg(any(target_os = "android", target_os = "linux",))]
182187
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
183188
#[derive(Clone)]
184189
pub struct SocketCred(libc::ucred);
185190

186-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
191+
#[cfg(target_os = "netbsd")]
192+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
193+
#[derive(Clone)]
194+
pub struct SocketCred(libc::sockcred);
195+
196+
#[doc(cfg(any(target_os = "android", target_os = "linux")))]
197+
#[cfg(any(target_os = "android", target_os = "linux"))]
187198
impl SocketCred {
188199
/// Create a Unix credential struct.
189200
///
@@ -234,6 +245,61 @@ impl SocketCred {
234245
}
235246
}
236247

248+
#[cfg(target_os = "netbsd")]
249+
impl SocketCred {
250+
/// Create a Unix credential struct.
251+
///
252+
/// PID, UID and GID is set to 0.
253+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
254+
pub fn new() -> SocketCred {
255+
SocketCred(libc::sockcred {
256+
sc_pid: 0,
257+
sc_uid: 0,
258+
sc_euid: 0,
259+
sc_gid: 0,
260+
sc_egid: 0,
261+
sc_ngroups: 0,
262+
sc_groups: [0u32; 1],
263+
})
264+
}
265+
266+
/// Set the PID.
267+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
268+
pub fn set_pid(&mut self, pid: libc::pid_t) {
269+
self.0.sc_pid = pid;
270+
}
271+
272+
/// Get the current PID.
273+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
274+
pub fn get_pid(&self) -> libc::pid_t {
275+
self.0.sc_pid
276+
}
277+
278+
/// Set the UID.
279+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
280+
pub fn set_uid(&mut self, uid: libc::uid_t) {
281+
self.0.sc_uid = uid;
282+
}
283+
284+
/// Get the current UID.
285+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
286+
pub fn get_uid(&self) -> libc::uid_t {
287+
self.0.sc_uid
288+
}
289+
290+
/// Set the GID.
291+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
292+
pub fn set_gid(&mut self, gid: libc::gid_t) {
293+
self.0.sc_gid = gid;
294+
}
295+
296+
/// Get the current GID.
297+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
298+
pub fn get_gid(&self) -> libc::gid_t {
299+
self.0.sc_gid
300+
}
301+
}
302+
237303
/// This control message contains file descriptors.
238304
///
239305
/// The level is equal to `SOL_SOCKET` and the type is equal to `SCM_RIGHTS`.
@@ -249,14 +315,22 @@ impl<'a> Iterator for ScmRights<'a> {
249315
}
250316
}
251317

318+
#[cfg(all(doc, not(target_os = "android"), not(target_os = "linux"), not(target_os = "netbsd")))]
319+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
320+
pub struct ScmCredentials<'a>(AncillaryDataIter<'a, ()>);
321+
252322
/// This control message contains unix credentials.
253323
///
254324
/// The level is equal to `SOL_SOCKET` and the type is equal to `SCM_CREDENTIALS` or `SCM_CREDS`.
255-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
325+
#[cfg(any(target_os = "android", target_os = "linux",))]
256326
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
257327
pub struct ScmCredentials<'a>(AncillaryDataIter<'a, libc::ucred>);
258328

259-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
329+
#[cfg(target_os = "netbsd")]
330+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
331+
pub struct ScmCredentials<'a>(AncillaryDataIter<'a, libc::sockcred>);
332+
333+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
260334
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
261335
impl<'a> Iterator for ScmCredentials<'a> {
262336
type Item = SocketCred;
@@ -278,7 +352,7 @@ pub enum AncillaryError {
278352
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
279353
pub enum AncillaryData<'a> {
280354
ScmRights(ScmRights<'a>),
281-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
355+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
282356
ScmCredentials(ScmCredentials<'a>),
283357
}
284358

@@ -300,8 +374,8 @@ impl<'a> AncillaryData<'a> {
300374
/// # Safety
301375
///
302376
/// `data` must contain a valid control message and the control message must be type of
303-
/// `SOL_SOCKET` and level of `SCM_CREDENTIALS` or `SCM_CREDENTIALS`.
304-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
377+
/// `SOL_SOCKET` and level of `SCM_CREDENTIALS` or `SCM_CREDS`.
378+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
305379
unsafe fn as_credentials(data: &'a [u8]) -> Self {
306380
let ancillary_data_iter = AncillaryDataIter::new(data);
307381
let scm_credentials = ScmCredentials(ancillary_data_iter);
@@ -320,6 +394,8 @@ impl<'a> AncillaryData<'a> {
320394
libc::SCM_RIGHTS => Ok(AncillaryData::as_rights(data)),
321395
#[cfg(any(target_os = "android", target_os = "linux",))]
322396
libc::SCM_CREDENTIALS => Ok(AncillaryData::as_credentials(data)),
397+
#[cfg(target_os = "netbsd")]
398+
libc::SCM_CREDS => Ok(AncillaryData::as_credentials(data)),
323399
cmsg_type => {
324400
Err(AncillaryError::Unknown { cmsg_level: libc::SOL_SOCKET, cmsg_type })
325401
}
@@ -531,7 +607,7 @@ impl<'a> SocketAncillary<'a> {
531607
/// Technically, that means this operation adds a control message with the level `SOL_SOCKET`
532608
/// and type `SCM_CREDENTIALS` or `SCM_CREDS`.
533609
///
534-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
610+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
535611
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
536612
pub fn add_creds(&mut self, creds: &[SocketCred]) -> bool {
537613
self.truncated = false;
@@ -540,7 +616,10 @@ impl<'a> SocketAncillary<'a> {
540616
&mut self.length,
541617
creds,
542618
libc::SOL_SOCKET,
619+
#[cfg(not(target_os = "netbsd"))]
543620
libc::SCM_CREDENTIALS,
621+
#[cfg(target_os = "netbsd")]
622+
libc::SCM_CREDS,
544623
)
545624
}
546625

library/std/src/os/unix/net/datagram.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ impl UnixDatagram {
865865
/// Ok(())
866866
/// }
867867
/// ```
868-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
868+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
869869
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
870870
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
871871
self.0.set_passcred(passcred)
@@ -877,7 +877,7 @@ impl UnixDatagram {
877877
/// Get the socket option `SO_PASSCRED`.
878878
///
879879
/// [`set_passcred`]: UnixDatagram::set_passcred
880-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
880+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
881881
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
882882
pub fn passcred(&self) -> io::Result<bool> {
883883
self.0.passcred()

library/std/src/os/unix/net/stream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ impl UnixStream {
415415
/// Ok(())
416416
/// }
417417
/// ```
418-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
418+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
419419
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
420420
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
421421
self.0.set_passcred(passcred)
@@ -427,7 +427,7 @@ impl UnixStream {
427427
/// Get the socket option `SO_PASSCRED`.
428428
///
429429
/// [`set_passcred`]: UnixStream::set_passcred
430-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
430+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
431431
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
432432
pub fn passcred(&self) -> io::Result<bool> {
433433
self.0.passcred()

library/std/src/sys/unix/net.rs

+11
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,17 @@ impl Socket {
419419
Ok(passcred != 0)
420420
}
421421

422+
#[cfg(target_os = "netbsd")]
423+
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
424+
setsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS, passcred as libc::c_int)
425+
}
426+
427+
#[cfg(target_os = "netbsd")]
428+
pub fn passcred(&self) -> io::Result<bool> {
429+
let passcred: libc::c_int = getsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS)?;
430+
Ok(passcred != 0)
431+
}
432+
422433
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
423434
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
424435
let mut nonblocking = nonblocking as libc::c_int;

0 commit comments

Comments
 (0)