From nobody Wed Jan 12 18:04:48 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id D51A6195A281; Wed, 12 Jan 2022 18:04:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JYwSd1TRwz3jsn; Wed, 12 Jan 2022 18:04:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 0994611A20; Wed, 12 Jan 2022 18:04:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 20CI4mmA038701; Wed, 12 Jan 2022 18:04:48 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 20CI4mqe038700; Wed, 12 Jan 2022 18:04:48 GMT (envelope-from git) Date: Wed, 12 Jan 2022 18:04:48 GMT Message-Id: <202201121804.20CI4mqe038700@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 12f747e6ff67 - main - truss(1): detach more carefully List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 12f747e6ff675edfc1f2f95f7fc435dc01e0c29c Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1642010689; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=j79uHreFKOxjWlxi1Rkyacjy6T7o7XPUucCaAtcJ8AY=; b=s9esUVA2tT0KiObCWUlZDpuVQBbdN/20Fl6d7NoWaDl5a12WXMF5cTTXComa++S/aX9Eaq Y2B/cOcHkPrXhZ2gpv9Qj1vph+QtdyC3nzYGC6n9InRH5QBSe/0RW5izTvh4Vjl86otqIq Ql55rOuC6PVbN2PsXXej/JmcV9BzO0oO3H4cZ2Z6U5ooSH20LCB++757eNp8BGpl9l3oGC yFQhDAEaqUfi1opTYRgOxEXj/s1VVM3/whjB7xIy6HdJtyj16sbIAcR6gcU/r1vBgsV+0Z G2utv880l8qHZoPnWNTIJlS+C43Y5mnS5uLKew85YDDv1Ts5tNemIegi/t40YA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1642010689; a=rsa-sha256; cv=none; b=pR/1EdlFGUKyXkSQwTNytDljqEpo+SLar5gMQzXjz7TRc/FcfKrDsw+TKCjSy5KrU7vdCw lWz0dFu0ZN83B82sdT2vKgqv1uXWgfcMdK1GFSO8BthzkbcbiFRAT9yqmQiZL+hUF6Xn3R QGG5ReXxZKqS1wxtAL76IW9eshMu2i/g6yB3wYsjieTxhYToCJojUxtZbnCvW2gcPHZyPQ hYyqNbNCFZ2LsPrD1/TdDHm71MIvOtuQu1XK0tPo446/Viiz9sRej62c5p9kObUV3X7B9l jPKVQMuO2GJOod0KaT1byLe7G537s5wJ0JhOSUXAyEJKiXtNhCev2sYFn9LNww== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=12f747e6ff675edfc1f2f95f7fc435dc01e0c29c commit 12f747e6ff675edfc1f2f95f7fc435dc01e0c29c Author: Konstantin Belousov AuthorDate: 2022-01-12 08:21:19 +0000 Commit: Konstantin Belousov CommitDate: 2022-01-12 18:04:41 +0000 truss(1): detach more carefully When detaching, truss(1) sends SIGSTOP to the traced process to ensure that it is detaching in the steady state. But it is possible, for multithreaded process, that wait() call returns event other than our SIGSTOP notification. As result, SIGSTOP might sit in some thread' sigqueue, which makes SIGCONT a nop. Then, the process is stopped when the queued SIGSTOP is acted upon. To handle this, loop until we drain everything before SIGSTOP, and see that the process is stopped. Note that the earlier fix makes it safe to have some more debugging events longering after SIGSTOP is acted upon. They will be ignored after PT_DETACH. Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D33861 --- usr.bin/truss/setup.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/usr.bin/truss/setup.c b/usr.bin/truss/setup.c index 00c0553b4c7c..b5a1d4e32d2b 100644 --- a/usr.bin/truss/setup.c +++ b/usr.bin/truss/setup.c @@ -205,11 +205,24 @@ restore_proc(int signo __unused) static void detach_proc(pid_t pid) { + int sig, status; - /* stop the child so that we can detach */ + /* + * Stop the child so that we can detach. Filter out possible + * lingering SIGTRAP events buffered in the threads. + */ kill(pid, SIGSTOP); - if (waitpid(pid, NULL, 0) < 0) - err(1, "Unexpected stop in waitpid"); + for (;;) { + if (waitpid(pid, &status, 0) < 0) + err(1, "Unexpected error in waitpid"); + sig = WIFSTOPPED(status) ? WSTOPSIG(status) : 0; + if (sig == SIGSTOP) + break; + if (sig == SIGTRAP) + sig = 0; + if (ptrace(PT_CONTINUE, pid, (caddr_t)1, sig) < 0) + err(1, "Can not continue for detach"); + } if (ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0) err(1, "Can not detach the process");