@@ -769,6 +769,60 @@ pub fn gethostname() -> Result<OsString> {
769
769
} )
770
770
}
771
771
772
+ cfg_if ! {
773
+ if #[ cfg( any( target_os = "dragonfly" , target_os = "freebsd" ,
774
+ target_os = "ios" , target_os = "macos" ) ) ] {
775
+ type namelen_t = c_int;
776
+ } else if #[ cfg( not( target_os = "android" ) ) ] {
777
+ type namelen_t = size_t;
778
+ }
779
+ }
780
+
781
+ /// Set the NIS domain name (see
782
+ /// [setdomainname(2)](http://man7.org/linux/man-pages/man2/setdomainname.2.html)).
783
+ #[ cfg( any( target_os = "dragonfly" , target_os = "freebsd" , target_os = "ios" ,
784
+ target_os = "linux" , target_os = "macos" , target_os = "netbsd" ,
785
+ target_os = "openbsd" ) ) ]
786
+ pub fn setdomainname < S : AsRef < OsStr > > ( name : S ) -> Result < ( ) > {
787
+ let ptr = name. as_ref ( ) . as_bytes ( ) . as_ptr ( ) as * const c_char ;
788
+ let len = name. as_ref ( ) . as_bytes ( ) . len ( ) as namelen_t ;
789
+ let res = unsafe { libc:: setdomainname ( ptr, len) } ;
790
+ Errno :: result ( res) . map ( drop)
791
+ }
792
+
793
+ /// Get the NIS domain name, returning a `OsString` on success (see
794
+ /// [getdomainname(2)](http://man7.org/linux/man-pages/man2/getdomainname.2.html)).
795
+ ///
796
+ /// This function call attempts to get the NIS domain name for the
797
+ /// running system.
798
+ ///
799
+ /// # Examples
800
+ ///
801
+ /// ```
802
+ /// use nix::unistd;
803
+ ///
804
+ /// let domainname_os = unistd::getdomainname().expect("Failed getting domain name");
805
+ /// let domainname = domainname_os.into_string().expect("Domain name wasn't valid UTF-8");
806
+ /// println!("Domain name: {}", domainname);
807
+ /// ```
808
+ #[ cfg( any( target_os = "dragonfly" , target_os = "freebsd" , target_os = "ios" ,
809
+ target_os = "linux" , target_os = "macos" , target_os = "netbsd" ,
810
+ target_os = "openbsd" ) ) ]
811
+ pub fn getdomainname ( ) -> Result < OsString > {
812
+ // Minimum hostname maximum length as defined by POSIX,
813
+ // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html
814
+ const _POSIX_HOST_NAME_MAX: c_long = 255 ;
815
+ let buf_len = sysconf ( SysconfVar :: HOST_NAME_MAX )
816
+ . unwrap_or ( None ) . unwrap_or ( _POSIX_HOST_NAME_MAX + 1 ) ;
817
+ let mut buf = vec ! [ 0 ; buf_len as usize ] ;
818
+
819
+ let res = unsafe { libc:: getdomainname ( buf. as_mut_ptr ( ) as * mut c_char , buf_len as namelen_t ) } ;
820
+ Errno :: result ( res) . map ( |_| {
821
+ buf[ ( buf_len - 1 ) as usize ] = 0 ;
822
+ OsString :: from_vec ( buf)
823
+ } )
824
+ }
825
+
772
826
/// Close a raw file descriptor
773
827
///
774
828
/// Be aware that many Rust types implicitly close-on-drop, including
0 commit comments