From patchwork Tue Aug 8 17:01:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 21990 Received: (qmail 40984 invoked by alias); 8 Aug 2017 17:01:56 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 37780 invoked by uid 89); 8 Aug 2017 17:01:46 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=CONTENTS X-HELO: mx1.redhat.com DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 725DD65721 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=fweimer@redhat.com Date: Tue, 08 Aug 2017 19:01:42 +0200 To: libc-alpha@sourceware.org Subject: [PATCH] support_chroot_create: Add support for /etc/hosts, /etc/host.conf User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Message-Id: <20170808170142.AA6874029923B@oldenburg.str.redhat.com> From: fweimer@redhat.com (Florian Weimer) 2017-08-08 Florian Weimer * support/namespace.h (struct support_chroot_configuration): Add hosts, host_conf. (struct support_chroot): Add path_hosts, path_host_conf. * support/support_chroot.c (write_file): New function. (support_chroot_create): Call it to process /etc/resolv.conf, /etc/hosts, /etc/host.conf. (support_chroot_free): Update. diff --git a/support/namespace.h b/support/namespace.h index 859c2fd..9eddb1a 100644 --- a/support/namespace.h +++ b/support/namespace.h @@ -66,7 +66,9 @@ struct support_chroot_configuration { /* File contents. The files are not created if the field is NULL. */ - const char *resolv_conf; + const char *resolv_conf; /* /etc/resolv.conf. */ + const char *hosts; /* /etc/hosts. */ + const char *host_conf; /* /etc/host.conf. */ }; /* The result of the creation of a chroot. */ @@ -78,8 +80,11 @@ struct support_chroot /* Path to the chroot directory. */ char *path_chroot; - /* Path to the /etc/resolv.conf file. */ - char *path_resolv_conf; + /* Paths to files in the chroot. These are absolute and outside of + the chroot. */ + char *path_resolv_conf; /* /etc/resolv.conf. */ + char *path_hosts; /* /etc/hosts. */ + char *path_host_conf; /* /etc/host.conf. */ }; /* Create a chroot environment. The returned data should be freed diff --git a/support/support_chroot.c b/support/support_chroot.c index c0807b3..f3ef551 100644 --- a/support/support_chroot.c +++ b/support/support_chroot.c @@ -24,6 +24,23 @@ #include #include +/* If CONTENTS is not NULL, write it to the file at DIRECTORY/RELPATH, + and store the name in *ABSPATH. If CONTENTS is NULL, store NULL in + *ABSPATH. */ +static void +write_file (const char *directory, const char *relpath, const char *contents, + char **abspath) +{ + if (contents != NULL) + { + *abspath = xasprintf ("%s/%s", directory, relpath); + add_temp_file (*abspath); + support_write_file_string (*abspath, contents); + } + else + *abspath = NULL; +} + struct support_chroot * support_chroot_create (struct support_chroot_configuration conf) { @@ -39,15 +56,10 @@ support_chroot_create (struct support_chroot_configuration conf) xmkdir (path_etc, 0777); add_temp_file (path_etc); - if (conf.resolv_conf != NULL) - { - /* Create an empty resolv.conf file. */ - chroot->path_resolv_conf = xasprintf ("%s/resolv.conf", path_etc); - add_temp_file (chroot->path_resolv_conf); - support_write_file_string (chroot->path_resolv_conf, conf.resolv_conf); - } - else - chroot->path_resolv_conf = NULL; + write_file (path_etc, "resolv.conf", conf.resolv_conf, + &chroot->path_resolv_conf); + write_file (path_etc, "hosts", conf.hosts, &chroot->path_hosts); + write_file (path_etc, "host.conf", conf.host_conf, &chroot->path_host_conf); free (path_etc); @@ -67,5 +79,7 @@ support_chroot_free (struct support_chroot *chroot) { free (chroot->path_chroot); free (chroot->path_resolv_conf); + free (chroot->path_hosts); + free (chroot->path_host_conf); free (chroot); }