Skip to content

logname: add Windows support via WTSQuerySessionInformationW - #15703

Open
armadavalor wants to merge 3 commits into
uutils:mainfrom
armadavalor:logname-windows
Open

armadavalor wants to merge 3 commits into
uutils:mainfrom
armadavalor:logname-windows

Conversation

@armadavalor

@armadavalor armadavalor commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

logname was Unix-only (libc::getlogin) and listed in feat_require_unix_core, so it was absent from Windows builds.

  • Add a #[cfg(windows)] get_userlogin() using WTSQuerySessionInformationW(..., WTS_CURRENT_SESSION, WTSUserName): the user logged on to the current session, like getlogin on Unix, rather than the process token's user that whoami (GetUserNameW) reports. Session 0 (services) returns an empty name, treated as "no login name".
  • Move logname from feat_require_unix_core to feat_Tier1; libc is now a unix-only dependency, windows-sys (Win32_System_RemoteDesktop) a windows-only one.
  • test_output_format: the regex was ^\w+\n$, which rejects Windows names like ARMADA-L5-PRO; relaxed to a single non-empty line.

Fixes #11056

Verified (Windows 10, local)

  • cargo test --features logname --no-default-features test_logname: test result: ok. 4 passed; 0 failed
  • cargo clippy -p uu_logname --all-targets -- -D warnings: clean
  • cargo fmt --check: clean

Not verified

  • Running under runas / another user, and from a service (session 0).
  • Unix builds/tests (not run locally; unix code path is unchanged apart from moving use CStr inside the fn and libc to a cfg(unix) dep).
  • Other CI jobs (feature-matrix, cross builds, Android/musl, GNU test suite) not run.

Fixes uutils#11056

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oech3

This comment was marked as resolved.

Use WTSQuerySessionInformationW(WTSUserName) instead of GetUserNameW so
logname reports the user logged on to the session, not the process
token's user, matching getlogin on Unix and differing from whoami.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
let len = (bytes as usize / 2).saturating_sub(1);
// SAFETY: `buffer` is valid for `len` u16s and is freed right after copying.
let name = OsString::from_wide(unsafe { std::slice::from_raw_parts(buffer, len) });
unsafe { WTSFreeMemory(buffer.cast()) };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please add a SAFETY comment here too

@armadavalor armadavalor changed the title logname: add Windows support via GetUserNameW logname: add Windows support via WTSQuerySessionInformationW Oct 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

logname: Support Windows

3 participants