@@ -10,7 +10,7 @@ use crate::slice::from_raw_parts;
1010use 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) ]
1515mod 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 ) ]
184189pub 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" ) ) ]
187198impl 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" ) ]
257327pub 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" ) ]
261335impl < ' 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" ) ]
279353pub 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
0 commit comments