malloc: Always install mtrace (bug 31892)
[glibc.git] / support / support_capture_subprocess.c
blob53847194cbc523a3ee58030b443a931b05a7af25
1 /* Capture output from a subprocess.
2 Copyright (C) 2017-2024 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 <support/subprocess.h>
20 #include <support/capture_subprocess.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <support/check.h>
26 #include <support/xunistd.h>
27 #include <support/xsocket.h>
28 #include <support/xspawn.h>
29 #include <support/support.h>
30 #include <support/test-driver.h>
32 static void
33 transfer (const char *what, struct pollfd *pfd, struct xmemstream *stream)
35 if (pfd->revents != 0)
37 char buf[1024];
38 ssize_t ret = TEMP_FAILURE_RETRY (read (pfd->fd, buf, sizeof (buf)));
39 if (ret < 0)
41 support_record_failure ();
42 printf ("error: reading from subprocess %s: %m\n", what);
43 pfd->events = 0;
44 pfd->revents = 0;
46 else if (ret == 0)
48 /* EOF reached. Stop listening. */
49 pfd->events = 0;
50 pfd->revents = 0;
52 else
53 /* Store the data just read. */
54 TEST_VERIFY (fwrite (buf, ret, 1, stream->out) == 1);
58 static void
59 support_capture_poll (struct support_capture_subprocess *result,
60 struct support_subprocess *proc)
62 struct pollfd fds[2] =
64 { .fd = proc->stdout_pipe[0], .events = POLLIN },
65 { .fd = proc->stderr_pipe[0], .events = POLLIN },
70 xpoll (fds, 2, -1);
71 transfer ("stdout", &fds[0], &result->out);
72 transfer ("stderr", &fds[1], &result->err);
74 while (fds[0].events != 0 || fds[1].events != 0);
76 xfclose_memstream (&result->out);
77 xfclose_memstream (&result->err);
79 result->status = support_process_wait (proc);
82 struct support_capture_subprocess
83 support_capture_subprocess (void (*callback) (void *), void *closure)
85 struct support_capture_subprocess result;
86 xopen_memstream (&result.out);
87 xopen_memstream (&result.err);
89 struct support_subprocess proc = support_subprocess (callback, closure);
91 support_capture_poll (&result, &proc);
92 return result;
95 struct support_capture_subprocess
96 support_capture_subprogram (const char *file, char *const argv[],
97 char *const envp[])
99 struct support_capture_subprocess result;
100 xopen_memstream (&result.out);
101 xopen_memstream (&result.err);
103 struct support_subprocess proc = support_subprogram (file, argv, envp);
105 support_capture_poll (&result, &proc);
106 return result;
109 /* Copies the executable into a restricted directory, so that we can
110 safely make it SGID with the TARGET group ID. Then runs the
111 executable. */
112 static int
113 copy_and_spawn_sgid (char *child_id, gid_t gid)
115 char *dirname = xasprintf ("%s/tst-tunables-setuid.%jd",
116 test_dir, (intmax_t) getpid ());
117 char *execname = xasprintf ("%s/bin", dirname);
118 int infd = -1;
119 int outfd = -1;
120 int ret = 1, status = 1;
122 TEST_VERIFY (mkdir (dirname, 0700) == 0);
123 if (support_record_failure_is_failed ())
124 goto err;
126 infd = open ("/proc/self/exe", O_RDONLY);
127 if (infd < 0)
128 FAIL_UNSUPPORTED ("unsupported: Cannot read binary from procfs\n");
130 outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
131 TEST_VERIFY (outfd >= 0);
132 if (support_record_failure_is_failed ())
133 goto err;
135 char buf[4096];
136 for (;;)
138 ssize_t rdcount = read (infd, buf, sizeof (buf));
139 TEST_VERIFY (rdcount >= 0);
140 if (support_record_failure_is_failed ())
141 goto err;
142 if (rdcount == 0)
143 break;
144 char *p = buf;
145 char *end = buf + rdcount;
146 while (p != end)
148 ssize_t wrcount = write (outfd, buf, end - p);
149 if (wrcount == 0)
150 errno = ENOSPC;
151 TEST_VERIFY (wrcount > 0);
152 if (support_record_failure_is_failed ())
153 goto err;
154 p += wrcount;
158 bool chowned = false;
159 TEST_VERIFY ((chowned = fchown (outfd, getuid (), gid) == 0)
160 || errno == EPERM);
161 if (support_record_failure_is_failed ())
162 goto err;
163 else if (!chowned)
165 ret = 77;
166 goto err;
169 TEST_VERIFY (fchmod (outfd, 02750) == 0);
170 if (support_record_failure_is_failed ())
171 goto err;
172 TEST_VERIFY (close (outfd) == 0);
173 if (support_record_failure_is_failed ())
174 goto err;
175 TEST_VERIFY (close (infd) == 0);
176 if (support_record_failure_is_failed ())
177 goto err;
179 /* We have the binary, now spawn the subprocess. Avoid using
180 support_subprogram because we only want the program exit status, not the
181 contents. */
182 ret = 0;
183 infd = outfd = -1;
185 char * const args[] = {execname, child_id, NULL};
187 status = support_subprogram_wait (args[0], args);
189 err:
190 if (outfd >= 0)
191 close (outfd);
192 if (infd >= 0)
193 close (infd);
194 if (execname != NULL)
196 unlink (execname);
197 free (execname);
199 if (dirname != NULL)
201 rmdir (dirname);
202 free (dirname);
205 if (ret == 77)
206 FAIL_UNSUPPORTED ("Failed to make sgid executable for test\n");
207 if (ret != 0)
208 FAIL_EXIT1 ("Failed to make sgid executable for test\n");
210 return status;
214 support_capture_subprogram_self_sgid (char *child_id)
216 gid_t target = 0;
217 const int count = 64;
218 gid_t groups[count];
220 /* Get a GID which is not our current GID, but is present in the
221 supplementary group list. */
222 int ret = getgroups (count, groups);
223 if (ret < 0)
224 FAIL_UNSUPPORTED("Could not get group list for user %jd\n",
225 (intmax_t) getuid ());
227 gid_t current = getgid ();
228 for (int i = 0; i < ret; ++i)
230 if (groups[i] != current)
232 target = groups[i];
233 break;
237 if (target == 0)
238 FAIL_UNSUPPORTED("Could not find a suitable GID for user %jd\n",
239 (intmax_t) getuid ());
241 return copy_and_spawn_sgid (child_id, target);
244 void
245 support_capture_subprocess_free (struct support_capture_subprocess *p)
247 free (p->out.buffer);
248 free (p->err.buffer);