Skip to content

Commit 5ba6b3a

Browse files
committed
Move glibc version lookup handling to sys::os and add a simpler glibc_version()
1 parent 99b50ef commit 5ba6b3a

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

src/libstd/sys/unix/net.rs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -383,42 +383,19 @@ impl IntoInner<c_int> for Socket {
383383
// believe it's thread-safe).
384384
#[cfg(target_env = "gnu")]
385385
fn on_resolver_failure() {
386+
use sys;
387+
386388
// If the version fails to parse, we treat it the same as "not glibc".
387-
if let Some(Ok(version_str)) = glibc_version_cstr().map(CStr::to_str) {
388-
if let Some(version) = parse_glibc_version(version_str) {
389-
if version < (2, 26) {
390-
unsafe { libc::res_init() };
391-
}
389+
if let Some(version) = sys::os::glibc_version() {
390+
if version < (2, 26) {
391+
unsafe { libc::res_init() };
392392
}
393393
}
394394
}
395395

396396
#[cfg(not(target_env = "gnu"))]
397397
fn on_resolver_failure() {}
398398

399-
#[cfg(target_env = "gnu")]
400-
fn glibc_version_cstr() -> Option<&'static CStr> {
401-
weak! {
402-
fn gnu_get_libc_version() -> *const libc::c_char
403-
}
404-
if let Some(f) = gnu_get_libc_version.get() {
405-
unsafe { Some(CStr::from_ptr(f())) }
406-
} else {
407-
None
408-
}
409-
}
410-
411-
// Returns Some((major, minor)) if the string is a valid "x.y" version,
412-
// ignoring any extra dot-separated parts. Otherwise return None.
413-
#[cfg(target_env = "gnu")]
414-
fn parse_glibc_version(version: &str) -> Option<(usize, usize)> {
415-
let mut parsed_ints = version.split(".").map(str::parse::<usize>).fuse();
416-
match (parsed_ints.next(), parsed_ints.next()) {
417-
(Some(Ok(major)), Some(Ok(minor))) => Some((major, minor)),
418-
_ => None
419-
}
420-
}
421-
422399
#[cfg(all(test, taget_env = "gnu"))]
423400
mod test {
424401
use super::*;

src/libstd/sys/unix/os.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,35 @@ pub fn getpid() -> u32 {
546546
pub fn getppid() -> u32 {
547547
unsafe { libc::getppid() as u32 }
548548
}
549+
550+
#[cfg(target_env = "gnu")]
551+
pub fn glibc_version() -> Option<(usize, usize)> {
552+
if let Some(Ok(version_str)) = glibc_version_cstr().map(CStr::to_str) {
553+
parse_glibc_version(version_str)
554+
} else {
555+
None
556+
}
557+
}
558+
559+
#[cfg(target_env = "gnu")]
560+
fn glibc_version_cstr() -> Option<&'static CStr> {
561+
weak! {
562+
fn gnu_get_libc_version() -> *const libc::c_char
563+
}
564+
if let Some(f) = gnu_get_libc_version.get() {
565+
unsafe { Some(CStr::from_ptr(f())) }
566+
} else {
567+
None
568+
}
569+
}
570+
571+
// Returns Some((major, minor)) if the string is a valid "x.y" version,
572+
// ignoring any extra dot-separated parts. Otherwise return None.
573+
#[cfg(target_env = "gnu")]
574+
fn parse_glibc_version(version: &str) -> Option<(usize, usize)> {
575+
let mut parsed_ints = version.split(".").map(str::parse::<usize>).fuse();
576+
match (parsed_ints.next(), parsed_ints.next()) {
577+
(Some(Ok(major)), Some(Ok(minor))) => Some((major, minor)),
578+
_ => None
579+
}
580+
}

0 commit comments

Comments
 (0)