1 /* Basic test for gethostid.
2 Copyright (C) 2018-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <gnu/lib-names.h>
24 #include <support/namespace.h>
25 #include <support/support.h>
26 #include <support/temp_file.h>
27 #include <support/xdlfcn.h>
28 #include <support/xstdio.h>
29 #include <support/xunistd.h>
32 /* Initial test is run outside a chroot, to increase the likelihood of
35 outside_chroot (void *closure
)
37 long id
= gethostid ();
38 printf ("info: host ID outside chroot: 0x%lx\n", id
);
41 /* The same, but this time perform a chroot operation. */
43 in_chroot (void *closure
)
45 const char *chroot_path
= closure
;
46 xchroot (chroot_path
);
47 long id
= gethostid ();
48 printf ("info: host ID in chroot: 0x%lx\n", id
);
54 support_isolate_in_subprocess (outside_chroot
, NULL
);
56 /* Now run the test inside a chroot. */
57 support_become_root ();
58 if (!support_can_chroot ())
59 /* Cannot perform further tests. */
62 /* Only use nss_files. */
63 __nss_configure_lookup ("hosts", "files");
65 /* Load the DSO outside of the chroot. */
66 xdlopen (LIBNSS_FILES_SO
, RTLD_LAZY
);
68 char *chroot_dir
= support_create_temp_directory ("tst-gethostid-");
69 support_isolate_in_subprocess (in_chroot
, chroot_dir
);
71 /* Tests with /etc/hosts in the chroot. */
73 char *path
= xasprintf ("%s/etc", chroot_dir
);
77 path
= xasprintf ("%s/etc/hosts", chroot_dir
);
80 FILE *fp
= xfopen (path
, "w");
82 printf ("info: chroot test with an empty /etc/hosts file\n");
83 support_isolate_in_subprocess (in_chroot
, chroot_dir
);
86 int ret
= gethostname (hostname
, sizeof (hostname
));
88 printf ("warning: invalid result from gethostname: %d\n", ret
);
89 else if (strlen (hostname
) == 0)
90 puts ("warning: gethostname returned empty string");
93 printf ("info: chroot test with IPv6 address in /etc/hosts for: %s\n",
95 fp
= xfopen (path
, "w");
96 /* Use an IPv6 address to induce another lookup failure. */
97 fprintf (fp
, "2001:db8::1 %s\n", hostname
);
99 support_isolate_in_subprocess (in_chroot
, chroot_dir
);
108 #include <support/test-driver.c>