@@ -10,7 +10,7 @@ use crate::slice::from_raw_parts;
10
10
use crate :: sys:: net:: Socket ;
11
11
12
12
// 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" ) ) ) ]
14
14
#[ allow( non_camel_case_types) ]
15
15
mod libc {
16
16
pub use libc:: c_int;
@@ -177,13 +177,24 @@ impl<'a, T> Iterator for AncillaryDataIter<'a, T> {
177
177
}
178
178
}
179
179
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
+
180
185
/// Unix credential.
181
- #[ cfg( any( doc , target_os = "android" , target_os = "linux" , ) ) ]
186
+ #[ cfg( any( target_os = "android" , target_os = "linux" , ) ) ]
182
187
#[ unstable( feature = "unix_socket_ancillary_data" , issue = "76915" ) ]
183
188
#[ derive( Clone ) ]
184
189
pub struct SocketCred ( libc:: ucred ) ;
185
190
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" ) ) ]
187
198
impl SocketCred {
188
199
/// Create a Unix credential struct.
189
200
///
@@ -234,6 +245,61 @@ impl SocketCred {
234
245
}
235
246
}
236
247
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
+
237
303
/// This control message contains file descriptors.
238
304
///
239
305
/// 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> {
249
315
}
250
316
}
251
317
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
+
252
322
/// This control message contains unix credentials.
253
323
///
254
324
/// 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" , ) ) ]
256
326
#[ unstable( feature = "unix_socket_ancillary_data" , issue = "76915" ) ]
257
327
pub struct ScmCredentials < ' a > ( AncillaryDataIter < ' a , libc:: ucred > ) ;
258
328
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" , ) ) ]
260
334
#[ unstable( feature = "unix_socket_ancillary_data" , issue = "76915" ) ]
261
335
impl < ' a > Iterator for ScmCredentials < ' a > {
262
336
type Item = SocketCred ;
@@ -278,7 +352,7 @@ pub enum AncillaryError {
278
352
#[ unstable( feature = "unix_socket_ancillary_data" , issue = "76915" ) ]
279
353
pub enum AncillaryData < ' a > {
280
354
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" , ) ) ]
282
356
ScmCredentials ( ScmCredentials < ' a > ) ,
283
357
}
284
358
@@ -300,8 +374,8 @@ impl<'a> AncillaryData<'a> {
300
374
/// # Safety
301
375
///
302
376
/// `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" , ) ) ]
305
379
unsafe fn as_credentials ( data : & ' a [ u8 ] ) -> Self {
306
380
let ancillary_data_iter = AncillaryDataIter :: new ( data) ;
307
381
let scm_credentials = ScmCredentials ( ancillary_data_iter) ;
@@ -320,6 +394,8 @@ impl<'a> AncillaryData<'a> {
320
394
libc:: SCM_RIGHTS => Ok ( AncillaryData :: as_rights ( data) ) ,
321
395
#[ cfg( any( target_os = "android" , target_os = "linux" , ) ) ]
322
396
libc:: SCM_CREDENTIALS => Ok ( AncillaryData :: as_credentials ( data) ) ,
397
+ #[ cfg( target_os = "netbsd" ) ]
398
+ libc:: SCM_CREDS => Ok ( AncillaryData :: as_credentials ( data) ) ,
323
399
cmsg_type => {
324
400
Err ( AncillaryError :: Unknown { cmsg_level : libc:: SOL_SOCKET , cmsg_type } )
325
401
}
@@ -531,7 +607,7 @@ impl<'a> SocketAncillary<'a> {
531
607
/// Technically, that means this operation adds a control message with the level `SOL_SOCKET`
532
608
/// and type `SCM_CREDENTIALS` or `SCM_CREDS`.
533
609
///
534
- #[ cfg( any( doc, target_os = "android" , target_os = "linux" , ) ) ]
610
+ #[ cfg( any( doc, target_os = "android" , target_os = "linux" , target_os = "netbsd" , ) ) ]
535
611
#[ unstable( feature = "unix_socket_ancillary_data" , issue = "76915" ) ]
536
612
pub fn add_creds ( & mut self , creds : & [ SocketCred ] ) -> bool {
537
613
self . truncated = false ;
@@ -540,7 +616,10 @@ impl<'a> SocketAncillary<'a> {
540
616
& mut self . length ,
541
617
creds,
542
618
libc:: SOL_SOCKET ,
619
+ #[ cfg( not( target_os = "netbsd" ) ) ]
543
620
libc:: SCM_CREDENTIALS ,
621
+ #[ cfg( target_os = "netbsd" ) ]
622
+ libc:: SCM_CREDS ,
544
623
)
545
624
}
546
625
0 commit comments