Blame misc/tst-gethostid.c

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