Blame misc/tst-gethostid.c

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