stdlib: qsort: Move some macros to inline function
[glibc.git] / elf / tst-env-setuid.c
blob032ab44be224046c1e70d28d6d42436ca4848458
1 /* Copyright (C) 2012-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 /* Verify that tunables correctly filter out unsafe environment variables like
19 MALLOC_CHECK_ and MALLOC_MMAP_THRESHOLD_ but also retain
20 MALLOC_MMAP_THRESHOLD_ in an unprivileged child. */
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
32 #include <support/check.h>
33 #include <support/support.h>
34 #include <support/test-driver.h>
35 #include <support/capture_subprocess.h>
37 static char SETGID_CHILD[] = "setgid-child";
39 #ifndef test_child
40 static int
41 test_child (void)
43 if (getenv ("MALLOC_CHECK_") != NULL)
45 printf ("MALLOC_CHECK_ is still set\n");
46 return 1;
49 if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
51 printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
52 return 1;
55 if (getenv ("LD_HWCAP_MASK") != NULL)
57 printf ("LD_HWCAP_MASK still set\n");
58 return 1;
61 return 0;
63 #endif
65 #ifndef test_parent
66 static int
67 test_parent (void)
69 if (getenv ("MALLOC_CHECK_") == NULL)
71 printf ("MALLOC_CHECK_ lost\n");
72 return 1;
75 if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
77 printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
78 return 1;
81 if (getenv ("LD_HWCAP_MASK") == NULL)
83 printf ("LD_HWCAP_MASK lost\n");
84 return 1;
87 return 0;
89 #endif
91 static int
92 do_test (int argc, char **argv)
94 /* Setgid child process. */
95 if (argc == 2 && strcmp (argv[1], SETGID_CHILD) == 0)
97 if (getgid () == getegid ())
98 /* This can happen if the file system is mounted nosuid. */
99 FAIL_UNSUPPORTED ("SGID failed: GID and EGID match (%jd)\n",
100 (intmax_t) getgid ());
102 int ret = test_child ();
104 if (ret != 0)
105 exit (1);
107 exit (EXIT_SUCCESS);
109 else
111 if (test_parent () != 0)
112 exit (1);
114 int status = support_capture_subprogram_self_sgid (SETGID_CHILD);
116 if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
117 return EXIT_UNSUPPORTED;
119 if (!WIFEXITED (status))
120 FAIL_EXIT1 ("Unexpected exit status %d from child process\n", status);
122 return 0;
126 #define TEST_FUNCTION_ARGV do_test
127 #include <support/test-driver.c>