Skip to content

Commit ef20ddb

Browse files
committed
Add binding for res_init.
Some notes about the particularities of the changes: res_init has been deprecated in favor of res_ninit, and many (but not all) targets have therefore renamed the link name to __res_init. For example, this happened in glibc in version 2.2: https://bugzilla.redhat.com/show_bug.cgi?id=43822#c6 In these systems, res_init is #defined to __res_init in resolv.h, which lets existing C programs continue to be compiled. Unfortunately, this define doesn't automatically apply to our Rust code. We therefore need to manually map the link name of res_init as appropriate for each target: macOS and iOS use res_9_init: https://github.com/practicalswift/osx/blob/3908694d6328baa293f0d7fc337348c01d13ed8f/src/libresolv/resolv.h#L316 Solaris uses res_init: https://java.net/projects/solaris/sources/on-src/content/usr/src/head/resolv.h OpenBSD uses __res_init: https://github.com/openbsd/src/blob/f3b3b7c7ca9a921db3a5650eed40f2b2e4d731d8/include/resolv.h#L268 FreeBSD uses __res_init: https://github.com/freebsd/freebsd/blob/6911f4a88c9832e5985b788f5e84010424f9e020/include/resolv.h#L290 NetBSD uses __res_init: http://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/include/resolv.h glibc uses __res_init: https://github.com/lattera/glibc/blob/a2f34833b1042d5d8eeb263b4cf4caaea138c4ad/resolv/resolv.h#L259 eglibc uses __res_init: https://github.com/Xilinx/eglibc/blob/7f0bcce417c47aefad06ddfec7cd4ced3a4e10ff/resolv/resolv.h#L259 musl uses res_init: https://github.com/runtimejs/musl-libc/blob/0a11d7cb13e243782da36e2e5747b8b151933cca/include/resolv.h#L128 Android uses res_init: https://github.com/android/platform_bionic/blob/306ea559528255e19a5bcd68cc2a9b1afc2cfb27/libc/include/resolv.h#L57 One caveat here is that NetBSD doesn't seem to use the symbol name __res_init yet, despite the redefine being present in resolv.h. At least Travis fails for the netbsd target if __res_init is used. This may change in the future. iOS and macOS both unfortunately require linking with libresolv, despite the symbols technically being available without libresolv: resolv: http://blog.achernya.com/2013/03/os-x-has-silly-libsystem.html Android and OpenBSD fall in the same category.
1 parent 288ed55 commit ef20ddb

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

libc-test/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ fn main() {
7777
cfg.header("netinet/in.h");
7878
cfg.header("netinet/ip.h");
7979
cfg.header("netinet/tcp.h");
80+
cfg.header("resolv.h");
8081
cfg.header("pthread.h");
8182
cfg.header("dlfcn.h");
8283
cfg.header("signal.h");

src/unix/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,22 @@ pub const PRIO_MAX: ::c_int = 20;
206206
cfg_if! {
207207
if #[cfg(dox)] {
208208
// on dox builds don't pull in anything
209-
} else if #[cfg(all(not(stdbuild), feature = "use_std"))] {
209+
} else if #[cfg(all(not(stdbuild),
210+
feature = "use_std",
211+
not(any(target_os = "macos",
212+
target_os = "ios",
213+
target_os = "android",
214+
target_os = "openbsd",
215+
target_os = "bitrig")
216+
)))] {
210217
// cargo build, don't pull in anything extra as the libstd dep
211218
// already pulls in all libs.
219+
} else if #[cfg(all(not(stdbuild), feature = "use_std"))] {
220+
// except on macOS and iOS, where we must link with lib resolv
221+
// for res_init, despite libsystem_info including it:
222+
// http://blog.achernya.com/2013/03/os-x-has-silly-libsystem.html
223+
#[link(name = "resolv")]
224+
extern {}
212225
} else if #[cfg(any(all(target_env = "musl", not(target_arch = "mips"))))] {
213226
#[link(name = "c", kind = "static", cfg(target_feature = "crt-static"))]
214227
#[link(name = "c", cfg(not(target_feature = "crt-static")))]
@@ -229,6 +242,7 @@ cfg_if! {
229242
target_os = "bitrig"))] {
230243
#[link(name = "c")]
231244
#[link(name = "m")]
245+
#[link(name = "resolv")]
232246
extern {}
233247
} else if #[cfg(target_os = "haiku")] {
234248
#[link(name = "root")]
@@ -694,6 +708,17 @@ extern {
694708
res: *mut *mut addrinfo) -> ::c_int;
695709
pub fn freeaddrinfo(res: *mut addrinfo);
696710
pub fn gai_strerror(errcode: ::c_int) -> *const ::c_char;
711+
#[cfg_attr(all(unix,
712+
not(target_os = "macos"),
713+
not(target_os = "ios"),
714+
not(target_os = "netbsd"),
715+
not(target_os = "solaris"),
716+
not(target_env = "musl")
717+
),
718+
link_name = "__res_init")]
719+
#[cfg_attr(any(target_os = "macos", target_os = "ios"),
720+
link_name = "res_9_init")]
721+
pub fn res_init() -> ::c_int;
697722

698723
#[cfg_attr(target_os = "netbsd", link_name = "__gmtime_r50")]
699724
pub fn gmtime_r(time_p: *const time_t, result: *mut tm) -> *mut tm;

0 commit comments

Comments
 (0)