Skip to content

Commit 0bfed77

Browse files
committed
std: net: Add function to return the system hostname
1 parent 3323bbe commit 0bfed77

22 files changed

+1289
-434
lines changed

library/std/src/net/hostname.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use libc::gethostname;
2+
use std::ffi::CStr;
3+
use std::ffi::OsString;
4+
use std::os::raw::c_char;
5+
6+
/// Returns the system hostname.
7+
///
8+
/// The returned result will, on success, return the same result [`libc::gethostname`] would return
9+
/// (as it is implemented using the very same function), and on error, what `errno` contains, also
10+
/// set by [`libc::gethostname`].
11+
#[unstable(feature = "gethostname", issue = "135142")]
12+
pub fn hostname() -> std::io::Result<OsString> {
13+
// 255 bytes is the maximum allowable length for a hostname (as per the DNS spec),
14+
// so we shouldn't ever have problems with this. I (@orowith2os) considered using a constant
15+
// and letting the platform set the length, but it was determined after some discussion that
16+
// this could break things if the platform changes their length. Possible alternative is to
17+
// read the sysconf setting for the max hostname length, but that might be a bit too much work.
18+
let mut temp_buffer: [c_char; 255] = [0; 255];
19+
// 0 = no problem, and there isn't any other relevant error code to check for. Only stuff for
20+
// sethostname, and ENAMETOOLONG, which is only relevant for glibc 2.1 or newer. With the
21+
// previous information given in mind, we shouldn't ever encounter any error other than the
22+
// fact that the system *somehow* failed to get the hostname.
23+
// SAFETY: should never be unsafe, as we're passing in a valid (0-initialized) buffer, and the
24+
// length of said buffer.
25+
let gethostname_result = unsafe { gethostname(&mut temp_buffer as _, temp_buffer.len()) };
26+
27+
match gethostname_result {
28+
0 => {
29+
// SAFETY: we already know the pointer here is valid, we made it from safe Rust earlier.
30+
let cstring = unsafe { CStr::from_ptr(&mut temp_buffer as _) };
31+
return Ok(OsString::from(cstring.to_string_lossy().as_ref()));
32+
}
33+
_ => Err(std::io::Error::last_os_error()),
34+
}
35+
}

library/std/src/net/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Networking primitives for TCP/UDP communication.
22
//!
33
//! This module provides networking functionality for the Transmission Control and User
4-
//! Datagram Protocols, as well as types for IP and socket addresses.
4+
//! Datagram Protocols, as well as types for IP and socket addresses, and functions related
5+
//! to network properties.
56
//!
67
//! # Organization
78
//!
@@ -35,13 +36,16 @@ pub use self::tcp::{Incoming, TcpListener, TcpStream};
3536
#[stable(feature = "rust1", since = "1.0.0")]
3637
pub use self::udp::UdpSocket;
3738
use crate::io::{self, ErrorKind};
39+
#[unstable(feature = "gethostname", issue = "135142")]
40+
pub use self::hostname::hostname;
3841

3942
mod ip_addr;
4043
mod socket_addr;
4144
mod tcp;
4245
#[cfg(test)]
4346
pub(crate) mod test;
4447
mod udp;
48+
mod hostname;
4549

4650
/// Possible values which can be passed to the [`TcpStream::shutdown`] method.
4751
#[derive(Copy, Clone, PartialEq, Eq, Debug)]

src/doc/rustc-dev-guide/README.md

+18-43
Original file line numberDiff line numberDiff line change
@@ -70,46 +70,21 @@ $ ENABLE_LINKCHECK=1 mdbook serve
7070
We use `mdbook-toc` to auto-generate TOCs for long sections. You can invoke the preprocessor by
7171
including the `<!-- toc -->` marker at the place where you want the TOC.
7272

73-
## How to fix toolstate failures
74-
75-
> [!NOTE]
76-
> Currently, we do not track the rustc-dev-guide toolstate due to
77-
> [spurious failures](https://github.com/rust-lang/rust/pull/71731),
78-
> but we leave these instructions for when we do it again in the future.
79-
80-
1. You will get a ping from the toolstate commit. e.g. https://github.com/rust-lang-nursery/rust-toolstate/commit/8ffa0e4c30ac9ba8546b7046e5c4ccc2b96ebdd4
81-
82-
2. The commit contains a link to the PR that caused the breakage. e.g. https://github.com/rust-lang/rust/pull/64321
83-
84-
3. If you go to that PR's thread, there is a post from bors with a link to the CI status: https://github.com/rust-lang/rust/pull/64321#issuecomment-529763807
85-
86-
4. Follow the check-actions link to get to the Actions page for that build
87-
88-
5. There will be approximately 1 billion different jobs for the build. They are for different configurations and platforms. The rustc-dev-guide build only runs on the Linux x86_64-gnu-tools job. So click on that job in the list, which is about 60% down in the list.
89-
90-
6. Click the Run build step in the job to get the console log for the step.
91-
92-
7. Click on the log and Ctrl-f to get a search box in the log
93-
94-
8. Search for rustc-dev-guide. This gets you to the place where the links are checked. It is usually ~11K lines into the log.
95-
96-
9. Look at the links in the log near that point in the log
97-
98-
10. Fix those links in the rustc-dev-guide (by making a PR in the rustc-dev-guide repo)
99-
100-
11. Make a PR on the rust-lang/rust repo to update the rustc-dev-guide git submodule in src/docs/rustc-dev-guide.
101-
To make a PR, the following steps are useful.
102-
103-
```bash
104-
# Assuming you already cloned the rust-lang/rust repo and you're in the correct directory
105-
git submodule update --remote src/doc/rustc-dev-guide
106-
git add -u
107-
git commit -m "Update rustc-dev-guide"
108-
# Note that you can use -i, which is short for --incremental, in the following command
109-
./x test --incremental src/doc/rustc-dev-guide # This is optional and should succeed anyway
110-
# Open a PR in rust-lang/rust
111-
```
112-
113-
12. Wait for PR to merge
114-
115-
Voilà!
73+
## Synchronizing josh subtree with rustc
74+
75+
This repository is linked to `rust-lang/rust` as a [josh](https://josh-project.github.io/josh/intro.html) subtree. You can use the following commands to synchronize the subtree in both directions.
76+
77+
### Pull changes from `rust-lang/rust` into this repository
78+
1) Checkout a new branch that will be used to create a PR into `rust-lang/rustc-dev-guide`
79+
2) Run the pull command
80+
```
81+
$ cargo run --manifest-path josh-sync/Cargo.toml rustc-pull
82+
```
83+
3) Push the branch to your fork and create a PR into `rustc-dev-guide`
84+
85+
### Push changes from this repository into `rust-lang/rust`
86+
1) Run the push command to create a branch named `<branch-name>` in a `rustc` fork under the `<gh-username>` account
87+
```
88+
$ cargo run --manifest-path josh-sync/Cargo.toml rustc-push <branch-name> <gh-username>
89+
```
90+
2) Create a PR from `<branch-name>` into `rust-lang/rust`

0 commit comments

Comments
 (0)