Blame misc/tst-gethostid.c

Packit Service 202cd7
/* Basic test for gethostid.
Packit Service 202cd7
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Service 202cd7
   This file is part of the GNU C Library.
Packit Service 202cd7
Packit Service 202cd7
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 202cd7
   modify it under the terms of the GNU Lesser General Public
Packit Service 202cd7
   License as published by the Free Software Foundation; either
Packit Service 202cd7
   version 2.1 of the License, or (at your option) any later version.
Packit Service 202cd7
Packit Service 202cd7
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 202cd7
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 202cd7
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 202cd7
   Lesser General Public License for more details.
Packit Service 202cd7
Packit Service 202cd7
   You should have received a copy of the GNU Lesser General Public
Packit Service 202cd7
   License along with the GNU C Library; if not, see
Packit Service 202cd7
   <http://www.gnu.org/licenses/>.  */
Packit Service 202cd7
Packit Service 202cd7
#include <gnu/lib-names.h>
Packit Service 202cd7
#include <nss.h>
Packit Service 202cd7
#include <stdio.h>
Packit Service 202cd7
#include <stdlib.h>
Packit Service 202cd7
#include <string.h>
Packit Service 202cd7
#include <support/namespace.h>
Packit Service 202cd7
#include <support/support.h>
Packit Service 202cd7
#include <support/temp_file.h>
Packit Service 202cd7
#include <support/xdlfcn.h>
Packit Service 202cd7
#include <support/xstdio.h>
Packit Service 202cd7
#include <support/xunistd.h>
Packit Service 202cd7
#include <unistd.h>
Packit Service 202cd7
Packit Service 202cd7
/* Initial test is run outside a chroot, to increase the likelihood of
Packit Service 202cd7
   success.  */
Packit Service 202cd7
static void
Packit Service 202cd7
outside_chroot (void *closure)
Packit Service 202cd7
{
Packit Service 202cd7
  long id = gethostid ();
Packit Service 202cd7
  printf ("info: host ID outside chroot: 0x%lx\n", id);
Packit Service 202cd7
}
Packit Service 202cd7
Packit Service 202cd7
/* The same, but this time perform a chroot operation.  */
Packit Service 202cd7
static void
Packit Service 202cd7
in_chroot (void *closure)
Packit Service 202cd7
{
Packit Service 202cd7
  const char *chroot_path = closure;
Packit Service 202cd7
  xchroot (chroot_path);
Packit Service 202cd7
  long id = gethostid ();
Packit Service 202cd7
  printf ("info: host ID in chroot: 0x%lx\n", id);
Packit Service 202cd7
}
Packit Service 202cd7
Packit Service 202cd7
static int
Packit Service 202cd7
do_test (void)
Packit Service 202cd7
{
Packit Service 202cd7
  support_isolate_in_subprocess (outside_chroot, NULL);
Packit Service 202cd7
Packit Service 202cd7
  /* Now run the test inside a chroot.  */
Packit Service 202cd7
  support_become_root ();
Packit Service 202cd7
  if (!support_can_chroot ())
Packit Service 202cd7
    /* Cannot perform further tests.  */
Packit Service 202cd7
    return 0;
Packit Service 202cd7
Packit Service 202cd7
  /* Only use nss_files.  */
Packit Service 202cd7
  __nss_configure_lookup ("hosts", "files");
Packit Service 202cd7
Packit Service 202cd7
  /* Load the DSO outside of the chroot.  */
Packit Service 202cd7
  xdlopen (LIBNSS_FILES_SO, RTLD_LAZY);
Packit Service 202cd7
Packit Service 202cd7
  char *chroot_dir = support_create_temp_directory ("tst-gethostid-");
Packit Service 202cd7
  support_isolate_in_subprocess (in_chroot, chroot_dir);
Packit Service 202cd7
Packit Service 202cd7
  /* Tests with /etc/hosts in the chroot.  */
Packit Service 202cd7
  {
Packit Service 202cd7
    char *path = xasprintf ("%s/etc", chroot_dir);
Packit Service 202cd7
    add_temp_file (path);
Packit Service 202cd7
    xmkdir (path, 0777);
Packit Service 202cd7
    free (path);
Packit Service 202cd7
    path = xasprintf ("%s/etc/hosts", chroot_dir);
Packit Service 202cd7
    add_temp_file (path);
Packit Service 202cd7
Packit Service 202cd7
    FILE *fp = xfopen (path, "w");
Packit Service 202cd7
    xfclose (fp);
Packit Service 202cd7
    printf ("info: chroot test with an empty /etc/hosts file\n");
Packit Service 202cd7
    support_isolate_in_subprocess (in_chroot, chroot_dir);
Packit Service 202cd7
Packit Service 202cd7
    char hostname[1024];
Packit Service 202cd7
    int ret = gethostname (hostname, sizeof (hostname));
Packit Service 202cd7
    if (ret < 0)
Packit Service 202cd7
      printf ("warning: invalid result from gethostname: %d\n", ret);
Packit Service 202cd7
    else if (strlen (hostname) == 0)
Packit Service 202cd7
      puts ("warning: gethostname returned empty string");
Packit Service 202cd7
    else
Packit Service 202cd7
      {
Packit Service 202cd7
        printf ("info: chroot test with IPv6 address in /etc/hosts for: %s\n",
Packit Service 202cd7
                hostname);
Packit Service 202cd7
        fp = xfopen (path, "w");
Packit Service 202cd7
        /* Use an IPv6 address to induce another lookup failure.  */
Packit Service 202cd7
        fprintf (fp, "2001:db8::1 %s\n", hostname);
Packit Service 202cd7
        xfclose (fp);
Packit Service 202cd7
        support_isolate_in_subprocess (in_chroot, chroot_dir);
Packit Service 202cd7
      }
Packit Service 202cd7
    free (path);
Packit Service 202cd7
  }
Packit Service 202cd7
  free (chroot_dir);
Packit Service 202cd7
Packit Service 202cd7
  return 0;
Packit Service 202cd7
}
Packit Service 202cd7
Packit Service 202cd7
#include <support/test-driver.c>