@@ -100,7 +100,7 @@ pub fn grantpt(fd: &PtyMaster) -> Result<()> {
100100/// unlockpt(&master_fd)?;
101101///
102102/// // Get the name of the slave
103- /// let slave_name = ptsname(&master_fd)?;
103+ /// let slave_name = unsafe { ptsname(&master_fd) } ?;
104104///
105105/// // Try to open the slave
106106/// # #[allow(unused_variables)]
@@ -125,20 +125,26 @@ pub fn posix_openpt(flags: fcntl::OFlag) -> Result<PtyMaster> {
125125/// [ptsname(3)](http://man7.org/linux/man-pages/man3/ptsname.3.html))
126126///
127127/// `ptsname()` returns the name of the slave pseudoterminal device corresponding to the master
128- /// referred to by `fd`. Note that this function is *not* threadsafe. For that see `ptsname_r()`.
128+ /// referred to by `fd`.
129129///
130130/// This value is useful for opening the slave pty once the master has already been opened with
131131/// `posix_openpt()`.
132+ ///
133+ /// # Safety
134+ ///
135+ /// `ptsname()` mutates global variables and is *not* threadsafe.
136+ /// Mutating global variables is always considered `unsafe` by Rust and this
137+ /// function is marked as `unsafe` to reflect that.
138+ ///
139+ /// For a threadsafe and non-`unsafe` alternative on Linux, see `ptsname_r()`.
132140#[ inline]
133- pub fn ptsname ( fd : & PtyMaster ) -> Result < String > {
134- let name_ptr = unsafe { libc:: ptsname ( fd. as_raw_fd ( ) ) } ;
141+ pub unsafe fn ptsname ( fd : & PtyMaster ) -> Result < String > {
142+ let name_ptr = libc:: ptsname ( fd. as_raw_fd ( ) ) ;
135143 if name_ptr. is_null ( ) {
136144 return Err ( Error :: last ( ) . into ( ) ) ;
137145 }
138146
139- let name = unsafe {
140- CStr :: from_ptr ( name_ptr)
141- } ;
147+ let name = CStr :: from_ptr ( name_ptr) ;
142148 Ok ( name. to_string_lossy ( ) . into_owned ( ) )
143149}
144150
@@ -187,7 +193,7 @@ pub fn unlockpt(fd: &PtyMaster) -> Result<()> {
187193
188194/// Create a new pseudoterminal, returning the slave and master file descriptors
189195/// in `OpenptyResult`
190- /// (see [openpty](http://man7.org/linux/man-pages/man3/openpty.3.html)).
196+ /// (see [openpty](http://man7.org/linux/man-pages/man3/openpty.3.html)).
191197///
192198/// If `winsize` is not `None`, the window size of the slave will be set to
193199/// the values in `winsize`. If `termios` is not `None`, the pseudoterminal's
0 commit comments