]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/tst-pldd.c
elf: Fix elf/tst-pldd with --enable-hardcoded-path-in-tests (BZ#24506)
[thirdparty/glibc.git] / elf / tst-pldd.c
CommitLineData
eaea1dfb
AZ
1/* Basic tests for pldd program.
2 Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. */
18
19#include <stdio.h>
20#include <string.h>
21#include <unistd.h>
22#include <stdint.h>
eaea1dfb
AZ
23#include <stdbool.h>
24
25#include <array_length.h>
26#include <gnu/lib-names.h>
27
28#include <support/subprocess.h>
29#include <support/capture_subprocess.h>
30#include <support/check.h>
31
32static void
33target_process (void *arg)
34{
35 pause ();
36}
37
38/* The test runs in a container because pldd does not support tracing
39 a binary started by the loader iself (as with testrun.sh). */
40
3c15600e
AZ
41static bool
42in_str_list (const char *libname, const char *const strlist[])
43{
44 for (const char *const *str = strlist; *str != NULL; str++)
45 if (strcmp (libname, *str) == 0)
46 return true;
47 return false;
48}
49
eaea1dfb
AZ
50static int
51do_test (void)
52{
53 /* Create a copy of current test to check with pldd. */
54 struct support_subprocess target = support_subprocess (target_process, NULL);
55
56 /* Run 'pldd' on test subprocess. */
57 struct support_capture_subprocess pldd;
58 {
59 /* Three digits per byte plus null terminator. */
60 char pid[3 * sizeof (uint32_t) + 1];
61 snprintf (pid, array_length (pid), "%d", target.pid);
62
63 const char prog[] = "/usr/bin/pldd";
64
65 pldd = support_capture_subprogram (prog,
66 (char *const []) { (char *) prog, pid, NULL });
67
68 support_capture_subprocess_check (&pldd, "pldd", 0, sc_allow_stdout);
69 }
70
71 /* Check 'pldd' output. The test is expected to be linked against only
72 loader and libc. */
73 {
74 pid_t pid;
75 char buffer[512];
76#define STRINPUT(size) "%" # size "s"
77
78 FILE *out = fmemopen (pldd.out.buffer, pldd.out.length, "r");
79 TEST_VERIFY (out != NULL);
80
81 /* First line is in the form of <pid>: <full path of executable> */
82 TEST_COMPARE (fscanf (out, "%u: " STRINPUT (512), &pid, buffer), 2);
83
84 TEST_COMPARE (pid, target.pid);
85 TEST_COMPARE (strcmp (basename (buffer), "tst-pldd"), 0);
86
87 /* It expects only one loader and libc loaded by the program. */
88 bool interpreter_found = false, libc_found = false;
89 while (fgets (buffer, array_length (buffer), out) != NULL)
90 {
91 /* Ignore vDSO. */
92 if (buffer[0] != '/')
93 continue;
94
95 /* Remove newline so baseline (buffer) can compare against the
96 LD_SO and LIBC_SO macros unmodified. */
97 if (buffer[strlen(buffer)-1] == '\n')
98 buffer[strlen(buffer)-1] = '\0';
99
3c15600e
AZ
100 const char *libname = basename (buffer);
101
102 /* It checks for default names in case of build configure with
103 --enable-hardcoded-path-in-tests (BZ #24506). */
104 if (in_str_list (libname,
105 (const char *const []) { "ld.so", LD_SO, NULL }))
eaea1dfb
AZ
106 {
107 TEST_COMPARE (interpreter_found, false);
108 interpreter_found = true;
109 continue;
110 }
111
3c15600e
AZ
112 if (in_str_list (libname,
113 (const char *const []) { "libc.so", LIBC_SO, NULL }))
eaea1dfb
AZ
114 {
115 TEST_COMPARE (libc_found, false);
116 libc_found = true;
117 continue;
118 }
119 }
120 TEST_COMPARE (interpreter_found, true);
121 TEST_COMPARE (libc_found, true);
122
123 fclose (out);
124 }
125
126 support_capture_subprocess_free (&pldd);
127 support_process_terminate (&target);
128
129 return 0;
130}
131
132#include <support/test-driver.c>