Skip to content

Conversation

joboet
Copy link
Member

@joboet joboet commented Sep 23, 2025

Resolves rust-lang/libs-team#330
Tracking issue: #135142

This is based on #135141, but I've reimplemented the UNIX version, which now:

  • uses sysconf(_SC_HOST_NAME_MAX) as an initial buffer length
  • returns OutOfMemory if the Vec allocation fails
  • retries the operation if it detects that the name returned by gethostname was truncated

Additionally, as part of the rebase, I had to move some WinSock abstractions (initialisation and error access) to sys::pal so that they can be accessed from sys::net::hostname.

CC @orowith2os (and thank you for your work!)

@rustbot rustbot added O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 23, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 23, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer

This comment has been minimized.

@joboet joboet added the O-unix Operating system: Unix-like label Sep 23, 2025
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me with nit fixed

View changes since this review

}
}
// As `buf.capacity()` is always positive (as Rust allocations cannot
// be larger than `isize::MAX` bytes), the only way `EINVAL` can be
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buf.capacity() is a usize, so saying it's "always positive" feels confusing to me. I'm not sure what relevance isize has here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gethostname documents that EINVAL is returned if size is negative (even though it is an unsigned integer). I've changed the comment to mention only isize::MAX.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 28, 2025
@joboet
Copy link
Member Author

joboet commented Sep 29, 2025

I've rephrased the comment and also changed the Windows version to use cvt instead of cvt_gai (it makes no functional difference, but cvt_gai is kind of getaddrinfo-specific).

@joboet joboet added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 29, 2025
@Mark-Simulacrum
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 29, 2025

📌 Commit 97333f8 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 29, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Sep 29, 2025
std: implement `hostname`

Resolves rust-lang/libs-team#330
Tracking issue: rust-lang#135142

This is based on rust-lang#135141, but I've reimplemented the UNIX version, which now:
* uses `sysconf(_SC_HOST_NAME_MAX)` as an initial buffer length
* returns `OutOfMemory` if the `Vec` allocation fails
* retries the operation if it detects that the name returned by `gethostname` was truncated

Additionally, as part of the rebase, I had to move some WinSock abstractions (initialisation and error access) to `sys::pal` so that they can be accessed from `sys::net::hostname`.

CC `@orowith2os` (and thank you for your work!)
bors added a commit that referenced this pull request Sep 29, 2025
Rollup of 6 pull requests

Successful merges:

 - #143613 (Fix backtraces with `-C panic=abort` on linux; emit unwind tables by default)
 - #146937 (std: implement `hostname`)
 - #147040 (mbe: macro_check: Fix function comments referencing non-existent parameters)
 - #147131 (Use MirPatch in simplify_branches.)
 - #147133 (Remove one loop in `extract_cfg_from_attrs`)
 - #147150 (Emit allocator attributes for allocator shim)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Sep 29, 2025
Rollup of 5 pull requests

Successful merges:

 - #146937 (std: implement `hostname`)
 - #147040 (mbe: macro_check: Fix function comments referencing non-existent parameters)
 - #147131 (Use MirPatch in simplify_branches.)
 - #147133 (Remove one loop in `extract_cfg_from_attrs`)
 - #147150 (Emit allocator attributes for allocator shim)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8223831 into rust-lang:master Sep 30, 2025
10 checks passed
@rustbot rustbot added this to the 1.92.0 milestone Sep 30, 2025
rust-timer added a commit that referenced this pull request Sep 30, 2025
Rollup merge of #146937 - joboet:gethostname, r=Mark-Simulacrum

std: implement `hostname`

Resolves rust-lang/libs-team#330
Tracking issue: #135142

This is based on #135141, but I've reimplemented the UNIX version, which now:
* uses `sysconf(_SC_HOST_NAME_MAX)` as an initial buffer length
* returns `OutOfMemory` if the `Vec` allocation fails
* retries the operation if it detects that the name returned by `gethostname` was truncated

Additionally, as part of the rebase, I had to move some WinSock abstractions (initialisation and error access) to `sys::pal` so that they can be accessed from `sys::net::hostname`.

CC ``@orowith2os`` (and thank you for your work!)
Zalathar added a commit to Zalathar/rust that referenced this pull request Oct 1, 2025
…boet

Fix broken STD build for ESP-IDF

PRs rust-lang#147162 and rust-lang#146937 did [broke](https://github.com/esp-rs/esp-idf-sys/actions/runs/18151791720/job/51663969786) the STD build for `target_os = "espidf"` because that target [does not have neither a `gethostname`, not a `libc::_SC_HOST_NAME_MAX` by default](espressif/esp-idf#14849).

While there is a [3rd party component for this syscall](https://components.espressif.com/components/espressif/sock_utils/versions/0.2.2/readme) in the ESP-IDF component registry, I don't think we should use it, because it does not come with ESP-IDF by default.

Therefore, the one-liner fix just re-routes ESP-IDF into the `unsupported` branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-unix Operating system: Unix-like O-windows Operating system: Windows S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add net::hostname to retrieve the devices host name
6 participants