Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ feat_Tier1 = [
"env",
"hostname",
"kill",
"logname",
"nohup",
"nproc",
"more",
Expand Down Expand Up @@ -636,7 +637,6 @@ feat_require_unix_core = [
"groups",
"id",
"install",
"logname",
"mkfifo",
"mknod",
"stat",
Expand Down
10 changes: 9 additions & 1 deletion src/uu/logname/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ doctest = false
[dependencies]
clap = { workspace = true }
fluent = { workspace = true }
libc = { workspace = true }
uucore = { workspace = true }

[target.'cfg(unix)'.dependencies]
libc = { workspace = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true, features = [
"Win32_Foundation",
"Win32_System_RemoteDesktop",
] }

[lints]
workspace = true

Expand Down
42 changes: 40 additions & 2 deletions src/uu/logname/src/logname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (ToDO) getlogin userlogin
// spell-checker:ignore (ToDO) getlogin userlogin runas

use clap::Command;
use core::ffi::CStr;
use std::io::{Write, stdout};
use uucore::translate;
use uucore::{error::UResult, show_error};

#[cfg(unix)]
fn get_userlogin() -> Option<String> {
use core::ffi::CStr;
let login_ptr = unsafe { libc::getlogin() };
if login_ptr.is_null() {
None
Expand All @@ -20,6 +21,43 @@ fn get_userlogin() -> Option<String> {
}
}

#[cfg(windows)]
fn get_userlogin() -> Option<String> {
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use windows_sys::Win32::System::RemoteDesktop::{
WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSFreeMemory, WTSQuerySessionInformationW,
WTSUserName,
};
// Ask for the user logged on to this session rather than the process token's user
// (`GetUserNameW`, as `whoami` does), so `runas` or a service account still reports
// the person who logged in, like `getlogin` does on Unix.
let mut buffer = std::ptr::null_mut();
let mut bytes = 0_u32;
// SAFETY: on success the buffer holds `bytes` bytes, a NUL-terminated UTF-16 string.
let ok = unsafe {
WTSQuerySessionInformationW(
WTS_CURRENT_SERVER_HANDLE,
WTS_CURRENT_SESSION,
WTSUserName,
&raw mut buffer,
&raw mut bytes,
)
};
if ok == 0 || buffer.is_null() {
return None;
}
// `bytes` includes the terminating NUL
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) });
// SAFETY: `buffer` was allocated by `WTSQuerySessionInformationW`, is freed exactly once
// and is not used afterwards; `name` owns its own copy.
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

// Session 0 (services) has no logged-on user and returns an empty name.
(!name.is_empty()).then(|| name.to_string_lossy().into_owned())
}

#[uucore::main(no_signals)]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let _ = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
Expand Down
2 changes: 1 addition & 1 deletion tests/by-util/test_logname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn test_output_format() {
}
result.success();
assert!(
Regex::new(r"^\w+\n$")
Regex::new(r"^[^\r\n]+\n$")
.unwrap()
.is_match(result.stdout_str()),
"unexpected logname output: {:?}",
Expand Down
Loading