hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame misc/tst-gethostid.c

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