From 74dcc7a3d57f91fd7382964ac1524ad622dd5f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20S=C3=B6ntgen?= Date: Fri, 25 May 2018 22:22:12 +0200 Subject: [PATCH] libc: use socket fs for DNS information The libc will now use the file given by the 'nameserver_file' attribute to get the DNS nameserver address instead of reading '/etc/resolv.conf'. It defaults to '/socket/nameserver' which is the common location when using the lxip VFS plugin. As a constraint the libc will read the first line and expects the nameserver address without any keywords in front of it. Fixes #2861. --- .../raw/depot_download/depot_download.config | 4 -- repos/libports/ports/libc.hash | 2 +- repos/libports/run/fetchurl.run | 3 - repos/libports/run/libc_getaddrinfo.run | 3 - .../src/lib/libc/patches/res_init_c.patch | 69 +++++++++++++++++++ repos/libports/src/lib/libc/vfs_plugin.cc | 24 +++++-- .../src/lib/libc_lwip_nic_dhcp/plugin.cc | 10 +-- .../recipes/pkg/nic_router-nat-dns/runtime | 4 +- repos/ports/run/arora.run | 3 - repos/ports/run/noux_net_netcat.run | 3 +- repos/ports/run/stubby.run | 7 +- repos/ports/run/stubby_deploy.run | 3 - repos/ports/src/noux-pkg/noux-etc/target.mk | 1 - 13 files changed, 96 insertions(+), 40 deletions(-) create mode 100644 repos/libports/src/lib/libc/patches/res_init_c.patch diff --git a/repos/gems/recipes/raw/depot_download/depot_download.config b/repos/gems/recipes/raw/depot_download/depot_download.config index 15e700f33..21e2fe7a8 100644 --- a/repos/gems/recipes/raw/depot_download/depot_download.config +++ b/repos/gems/recipes/raw/depot_download/depot_download.config @@ -43,10 +43,6 @@ - - nameserver 1.1.1.1 - - diff --git a/repos/libports/ports/libc.hash b/repos/libports/ports/libc.hash index 6daf39aa1..3016ab30a 100644 --- a/repos/libports/ports/libc.hash +++ b/repos/libports/ports/libc.hash @@ -1 +1 @@ -c9c17345f57cb2f827c271806df456d829e32ce3 +5a5de5baab6e3ce23f24012a7604abf070fa990c diff --git a/repos/libports/run/fetchurl.run b/repos/libports/run/fetchurl.run index 39435e297..c1a88c3aa 100644 --- a/repos/libports/run/fetchurl.run +++ b/repos/libports/run/fetchurl.run @@ -85,9 +85,6 @@ append config { - - nameserver 1.1.1.1 - 2000-01-01 00:00 01234567890123456789 diff --git a/repos/libports/run/libc_getaddrinfo.run b/repos/libports/run/libc_getaddrinfo.run index 119a57a10..bb6db735f 100644 --- a/repos/libports/run/libc_getaddrinfo.run +++ b/repos/libports/run/libc_getaddrinfo.run @@ -52,9 +52,6 @@ append config { - - nameserver 10.0.2.3 - diff --git a/repos/libports/src/lib/libc/patches/res_init_c.patch b/repos/libports/src/lib/libc/patches/res_init_c.patch new file mode 100644 index 000000000..03622278f --- /dev/null +++ b/repos/libports/src/lib/libc/patches/res_init_c.patch @@ -0,0 +1,69 @@ ++++ src/lib/libc/lib/libc/resolv/res_init.c +@@ -152,6 +152,10 @@ + return (__res_vinit(statp, 0)); + } + ++ ++extern char const *libc_resolv_path; ++ ++ + /*% This function has to be reachable by res_data.c but not publically. */ + int + __res_vinit(res_state statp, int preinit) { +@@ -304,8 +308,47 @@ + line[sizeof(name) - 1] == '\t')) + + nserv = 0; +- if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { ++ if ((fp = fopen(libc_resolv_path, "r")) != NULL) { + /* read the config file */ ++#if 1 ++ if (fgets(buf, sizeof(buf), fp) != NULL) { ++ /* read nameservers to query */ ++ struct addrinfo hints, *ai; ++ char sbuf[NI_MAXSERV]; ++ const size_t minsiz = sizeof(statp->_u._ext.ext->nsaddrs[0]); ++ ++ cp = buf; ++ cp[strcspn(cp, "\n")] = '\0'; ++ ++ if ((*cp != '\0') && (*cp != '\n')) { ++ ++ memset(&hints, 0, sizeof(hints)); ++ hints.ai_family = PF_UNSPEC; ++ hints.ai_socktype = SOCK_DGRAM; /*dummy*/ ++ hints.ai_flags = AI_NUMERICHOST; ++ sprintf(sbuf, "%u", NAMESERVER_PORT); ++ ++ if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && ++ ai->ai_addrlen <= minsiz) { ++ if (statp->_u._ext.ext != NULL) { ++ memcpy(&statp->_u._ext.ext->nsaddrs[nserv], ++ ai->ai_addr, ai->ai_addrlen); ++ } ++ ++ if (ai->ai_addrlen <= ++ sizeof(statp->nsaddr_list[nserv])) { ++ memcpy(&statp->nsaddr_list[nserv], ++ ai->ai_addr, ai->ai_addrlen); ++ } else { ++ statp->nsaddr_list[nserv].sin_family = 0; ++ } ++ ++ freeaddrinfo(ai); ++ nserv++; ++ } ++ } ++ } ++#else + while (fgets(buf, sizeof(buf), fp) != NULL) { + /* skip comments */ + if (*buf == ';' || *buf == '#') +@@ -502,6 +545,7 @@ + continue; + } + } ++#endif /* 1 */ + if (nserv > 0) + statp->nscount = nserv; + #ifdef RESOLVSORT diff --git a/repos/libports/src/lib/libc/vfs_plugin.cc b/repos/libports/src/lib/libc/vfs_plugin.cc index bbcee6ffd..a21f11c51 100644 --- a/repos/libports/src/lib/libc/vfs_plugin.cc +++ b/repos/libports/src/lib/libc/vfs_plugin.cc @@ -91,15 +91,11 @@ static void vfs_stat_to_libc_stat_struct(Vfs::Directory_service::Stat const &src static Genode::Xml_node *_config_node; +char const *libc_resolv_path; + namespace Libc { - void libc_config_init(Genode::Xml_node node) - { - static Genode::Xml_node config = node; - _config_node = &config; - } - Genode::Xml_node config() __attribute__((weak)); Genode::Xml_node config() { @@ -143,6 +139,22 @@ namespace Libc { return socket.string(); } + char const *config_nameserver_file() __attribute__((weak)); + char const *config_nameserver_file() + { + static Config_attr ns_file("nameserver_file", + "/socket/nameserver"); + return ns_file.string(); + } + + void libc_config_init(Genode::Xml_node node) + { + static Genode::Xml_node config = node; + _config_node = &config; + + libc_resolv_path = config_nameserver_file(); + } + void notify_read_ready(Vfs::Vfs_handle *handle) { struct Check : Libc::Suspend_functor diff --git a/repos/libports/src/lib/libc_lwip_nic_dhcp/plugin.cc b/repos/libports/src/lib/libc_lwip_nic_dhcp/plugin.cc index ca6c3e912..6af286447 100644 --- a/repos/libports/src/lib/libc_lwip_nic_dhcp/plugin.cc +++ b/repos/libports/src/lib/libc_lwip_nic_dhcp/plugin.cc @@ -1,6 +1,6 @@ /* * \brief Libc plugin providing lwIP's DNS server address in the - * '/etc/resolv.conf' file + * '/socket/nameserver' file * \author Christian Prochaska * \date 2013-05-02 */ @@ -122,13 +122,13 @@ namespace { /** * File name this plugin feels responsible for */ - static char const *_file_name() { return "/etc/resolv.conf"; } + static char const *_file_name() { return "/socket/nameserver"; } const char *_file_content() { static char result[32]; ip_addr_t nameserver_ip = dns_getserver(0); - snprintf(result, sizeof(result), "nameserver %s\n", + snprintf(result, sizeof(result), "%s\n", ipaddr_ntoa(&nameserver_ip)); return result; } @@ -150,7 +150,7 @@ namespace { bool supports_stat(const char *path) { - return (Genode::strcmp(path, "/etc") == 0) || + return (Genode::strcmp(path, "/socket") == 0) || (Genode::strcmp(path, _file_name()) == 0); } @@ -177,7 +177,7 @@ namespace { { if (buf) { Genode::memset(buf, 0, sizeof(struct stat)); - if (Genode::strcmp(path, "/etc") == 0) + if (Genode::strcmp(path, "/socket") == 0) buf->st_mode = S_IFDIR; else if (Genode::strcmp(path, _file_name()) == 0) { buf->st_mode = S_IFREG; diff --git a/repos/ports/recipes/pkg/nic_router-nat-dns/runtime b/repos/ports/recipes/pkg/nic_router-nat-dns/runtime index f752d6c70..050013470 100644 --- a/repos/ports/recipes/pkg/nic_router-nat-dns/runtime +++ b/repos/ports/recipes/pkg/nic_router-nat-dns/runtime @@ -45,14 +45,12 @@ - - nameserver 9.9.9.9 - + listen_addresses: diff --git a/repos/ports/run/arora.run b/repos/ports/run/arora.run index ad5ae6ca8..fe32d9a07 100644 --- a/repos/ports/run/arora.run +++ b/repos/ports/run/arora.run @@ -106,9 +106,6 @@ append config { 2018-01-01 00:01 - - nameserver 10.0.2.3 - diff --git a/repos/ports/run/noux_net_netcat.run b/repos/ports/run/noux_net_netcat.run index f0b51065d..7999934a7 100644 --- a/repos/ports/run/noux_net_netcat.run +++ b/repos/ports/run/noux_net_netcat.run @@ -40,7 +40,7 @@ build $build_components # # The '/bin/etc/' directory is expected to contain the -# files 'services', 'protocols', 'hosts', and 'resolv.conf'. +# files 'services', 'protocols', and 'hosts'. # Download these files from the FreeBSD source tree is possible. # exec mkdir -p bin/etc @@ -48,7 +48,6 @@ set freebsd_url "http://svn.freebsd.org/base/release/8.2.0/etc" foreach etc_file { services protocols hosts } { if {![file exists bin/etc/$etc_file]} { catch { exec wget -c -P bin/etc $freebsd_url/$etc_file } } } -exec touch bin/etc/resolv.conf exec tar rfv bin/netcat.tar -h -C bin/ etc diff --git a/repos/ports/run/stubby.run b/repos/ports/run/stubby.run index 55b108702..c904abb01 100644 --- a/repos/ports/run/stubby.run +++ b/repos/ports/run/stubby.run @@ -99,14 +99,12 @@ append config { - - nameserver 10.0.2.3 - + listen_addresses: @@ -125,9 +123,6 @@ listen_addresses: - - nameserver 10.0.53.2 - diff --git a/repos/ports/run/stubby_deploy.run b/repos/ports/run/stubby_deploy.run index 6fd64d543..e70bcc664 100644 --- a/repos/ports/run/stubby_deploy.run +++ b/repos/ports/run/stubby_deploy.run @@ -176,9 +176,6 @@ append config { - - nameserver 10.0.1.2 - diff --git a/repos/ports/src/noux-pkg/noux-etc/target.mk b/repos/ports/src/noux-pkg/noux-etc/target.mk index b484b5b01..06eab3a65 100644 --- a/repos/ports/src/noux-pkg/noux-etc/target.mk +++ b/repos/ports/src/noux-pkg/noux-etc/target.mk @@ -22,7 +22,6 @@ copy-contrib-files: done generate-files: copy-contrib-files - $(VERBOSE)echo 'nameserver 8.8.8.8' > $(TARGET_DIR)/resolv.conf $(BUILD_BIN_DIR)/$(TARGET): generate-files