1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-26 10:04:02 +03:00

util: Ensure debugger is not started until it is allowed to attach

Use a pipe to ensure that the debugger is not started until after the
prctl() call allowing it to attach to the parent, avoiding a potential
race condition.

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>

Autobuild-User(master): Andreas Schneider <asn@cryptomilk.org>
Autobuild-Date(master): Tue Apr 20 12:33:40 UTC 2021 on sn-devel-184
This commit is contained in:
Joseph Sutton 2021-04-12 10:23:20 +12:00 committed by Andreas Schneider
parent 05a1ca2f4c
commit 416c9bbc4f

View File

@ -1166,27 +1166,56 @@ void anonymous_shared_free(void *ptr)
*/
void samba_start_debugger(void)
{
pid_t pid = fork();
if (pid == -1) {
return;
} else if (pid) {
int ready_pipe[2];
char c;
int ret;
pid_t pid;
ret = pipe(ready_pipe);
SMB_ASSERT(ret == 0);
pid = fork();
SMB_ASSERT(pid >= 0);
if (pid) {
c = 0;
ret = close(ready_pipe[0]);
SMB_ASSERT(ret == 0);
#if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
/*
* Make sure the child process can attach a debugger.
*
* We don't check the error code as the debugger
* will tell us if it can't attach.
*/
prctl(PR_SET_PTRACER, pid, 0, 0, 0);
(void)prctl(PR_SET_PTRACER, pid, 0, 0, 0);
#endif
ret = write(ready_pipe[1], &c, 1);
SMB_ASSERT(ret == 1);
ret = close(ready_pipe[1]);
SMB_ASSERT(ret == 0);
/* Wait for gdb to attach. */
sleep(2);
} else {
char *cmd = NULL;
if (asprintf(&cmd, "gdb --pid %u", getppid()) == -1) {
_exit(EXIT_FAILURE);
}
ret = close(ready_pipe[1]);
SMB_ASSERT(ret == 0);
ret = read(ready_pipe[0], &c, 1);
SMB_ASSERT(ret == 1);
ret = close(ready_pipe[0]);
SMB_ASSERT(ret == 0);
ret = asprintf(&cmd, "gdb --pid %u", getppid());
SMB_ASSERT(ret != -1);
execlp("xterm", "xterm", "-e", cmd, (char *) NULL);
free(cmd);
_exit(errno);
smb_panic("execlp() failed");
}
}
#endif