1 /* Test sysconf with an empty chroot.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <support/check.h>
22 #include <support/namespace.h>
23 #include <support/support.h>
24 #include <support/temp_file.h>
25 #include <support/test-driver.h>
26 #include <support/xunistd.h>
27 #include <unistd.h>
28
29 /* Check for an SMP system in a forked process, so that the parent
30 process does not cache the value. */
31 static void
is_smp_callback(void * closure)32 is_smp_callback (void *closure)
33 {
34 bool *result = closure;
35
36 long cpus = sysconf (_SC_NPROCESSORS_ONLN);
37 TEST_VERIFY_EXIT (cpus > 0);
38 *result = cpus != 1;
39 }
40
41 static bool
is_smp(void)42 is_smp (void)
43 {
44 bool *result = support_shared_allocate (sizeof (*result));
45 support_isolate_in_subprocess (is_smp_callback, result);
46 bool result_copy = *result;
47 support_shared_free (result);
48 return result_copy;
49 }
50
51 static char *path_chroot;
52
53 /* Prepare an empty directory, to be used as a chroot. */
54 static void
prepare(int argc,char ** argv)55 prepare (int argc, char **argv)
56 {
57 path_chroot = xasprintf ("%s/tst-resolv-res_init-XXXXXX", test_dir);
58 if (mkdtemp (path_chroot) == NULL)
59 FAIL_EXIT1 ("mkdtemp (\"%s\"): %m", path_chroot);
60 add_temp_file (path_chroot);
61 }
62
63 /* The actual test. Run it in a subprocess, so that the test harness
64 can remove the temporary directory in --direct mode. */
65 static void
chroot_callback(void * closure)66 chroot_callback (void *closure)
67 {
68 xchroot (path_chroot);
69 long cpus = sysconf (_SC_NPROCESSORS_ONLN);
70 printf ("info: sysconf (_SC_NPROCESSORS_ONLN) in chroot: %ld\n", cpus);
71 TEST_VERIFY (cpus > 0);
72 TEST_VERIFY (cpus != 1);
73 _exit (0);
74 }
75
76 static int
do_test(void)77 do_test (void)
78 {
79 if (!is_smp ())
80 {
81 printf ("warning: test not supported on uniprocessor system\n");
82 return EXIT_UNSUPPORTED;
83 }
84
85 support_become_root ();
86 if (!support_can_chroot ())
87 return EXIT_UNSUPPORTED;
88
89 support_isolate_in_subprocess (chroot_callback, NULL);
90
91 return 0;
92 }
93
94 #define PREPARE prepare
95 #include <support/test-driver.c>
96