From nobody Wed Nov 27 09:55:16 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C16E81D1E63 for ; Fri, 11 Oct 2024 22:04:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728684250; cv=none; b=mp3fwbGRio0fzsHVuxlLBF/ZD4H5Tm+FqBNFrjmKBeO4XyP5BD1w5KQ4OMocDL4oYbPknUQCpfsuHy6iUgNWUa9QlYo4JiyqTpwNDjYkvA9pHmFpYAH6oUY+zBMwx4tVqJg5H1r5KF2Vpt1M6MhvEx6l9Ds5Iw/XZMfZ4uK5/fk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728684250; c=relaxed/simple; bh=32CmiUO1Nu2w18z0VnqAHZyV/SwfhR5oLEkO3QAxKfY=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=WholSniUe72frJP3/E/uZGdQwe2wZWKbF+iAxw5NtwPaETcjgDanqHEZBN0sf1hlQC8wfiwIzXet4UFhuxbo4p/fTZSyl6316NSjgmRZEbRbQ+7siImwNXhecqdTgkrIADP9lsGn1Mjv0dytYOd2SfWt9l1hwH/3imc5w/QcBiw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=XUYiItyV; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="XUYiItyV" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e02b5792baaso3507111276.2 for ; Fri, 11 Oct 2024 15:04:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728684247; x=1729289047; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=pZqf2//xoOnb1H5/bkKLpMOcQ8vCY8XnITbcBJn/Kk0=; b=XUYiItyVexYmSCGBksEkVp7O9ddScH8lPGOrdlo/WY/drSzJY7RZ1+i1dD+F30A5vv NuTl4IAuwiJNrWnLwK6ZehbGOI5Z937VVkLOwXHrZ5YjKBF2bW6K4WxFSL14Vsj77a4/ ykO00OgomaDsC5r4fyMRpxHKf1jYbSoYawvI/zF6KfkfcDmT3K/qQ0fTiaINIyok3tKa P4tCQQ3glpir6OsLBPvZQGU5cfm8gCoWnnyMeSfFDUMiJkdDSgHIHaV+zFuHUwL3XBQC salC0cUGKhGf1HANZRTZdiukwcYNW5yqdsGJu+lSdFL6SSkjsUqRzzTn1x/COr7+p3oK y2xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728684247; x=1729289047; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pZqf2//xoOnb1H5/bkKLpMOcQ8vCY8XnITbcBJn/Kk0=; b=TebMuGtjteEZ6dBlA5Y9rCH9582bBxhee93tuJdna/oigwVaN0GA4Pd67G9CEfPXf2 p264cOM8r33fhbQifOqkfdR0ixpIasGF6r1N7p7p3ketRMSZEi/6j97zObYExoCqnhxY GP0YVVhz1/2vNbz5SOHdWCbt3Sp14sAaADnXx8/KQWW3XQv5Wrf3RYt70zDv+bBOP21q x/lE6LXDvzlLFOdzz8Wun8fRZRxfQ0xVIKponFqxWxzpckAESPw0R5kHtyc8snwIHHWa YuJ/udchMeL7x780xQwn2Qkvn/o1Cm/M/FKGmmnf6ZVd1lSg5aBu55COI1RLF7/bTz84 t90Q== X-Forwarded-Encrypted: i=1; AJvYcCXH00StyiDa0EG4JuLAQ1WMYw+jgNsCIWyadW+RA0SJX0xapCP6vjVmqTW5m1+cRnbA9bg/DS9pynyV8o0=@vger.kernel.org X-Gm-Message-State: AOJu0YxcCY9bRJTNIpPHLDTE4MVjZBeFOremB4whjNSO74Dv9yTTrBRh xLNkHsQTI2/xs1d7oZZc1IrF4yt/h3A/dFCZszsGXp+cKUApNa4t7bGOvZpA/Zql5lL5HrWy4X1 ycQkORA== X-Google-Smtp-Source: AGHT+IFnQwK3S6BS+70+ID2qAC5s213z8mYXWb6kgkgpvLhH25KIn26m/qM2zLiTiXCF/WTydr0Zy+VcCrx+ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:8144:56f:2e1e:4db]) (user=irogers job=sendgmr) by 2002:a25:ae8c:0:b0:e25:d596:4847 with SMTP id 3f1490d57ef6-e2919ded25cmr12084276.6.1728684246675; Fri, 11 Oct 2024 15:04:06 -0700 (PDT) Date: Fri, 11 Oct 2024 15:03:47 -0700 In-Reply-To: <20241011220354.756798-1-irogers@google.com> Message-Id: <20241011220354.756798-2-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241011220354.756798-1-irogers@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Subject: [PATCH v2 1/8] tools subcmd: Add non-waitpid check_if_command_finished() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Howard Chu , Athira Jajeev , Michael Petlan , Veronika Molnarova , Dapeng Mi , Thomas Richter , Ilya Leoshkevich , Colin Ian King , Weilin Wang , Andi Kleen , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Using waitpid can cause stdout/stderr of the child process to be lost. Use Linux's /prod//status file to determine if the process has reached the zombie state. Use the 'status' file rather than 'stat' to avoid issues around skipping the process name. Tested-by: James Clark Signed-off-by: Ian Rogers --- tools/lib/subcmd/run-command.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tools/lib/subcmd/run-command.c b/tools/lib/subcmd/run-command.c index 4e3a557a2f37..0a764c25c384 100644 --- a/tools/lib/subcmd/run-command.c +++ b/tools/lib/subcmd/run-command.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -217,8 +218,40 @@ static int wait_or_whine(struct child_process *cmd, bo= ol block) =20 int check_if_command_finished(struct child_process *cmd) { +#ifdef __linux__ + char filename[FILENAME_MAX + 12]; + char status_line[256]; + FILE *status_file; + + /* + * Check by reading /proc//status as calling waitpid causes + * stdout/stderr to be closed and data lost. + */ + sprintf(filename, "/proc/%d/status", cmd->pid); + status_file =3D fopen(filename, "r"); + if (status_file =3D=3D NULL) { + /* Open failed assume finish_command was called. */ + return true; + } + while (fgets(status_line, sizeof(status_line), status_file) !=3D NULL) { + char *p; + + if (strncmp(status_line, "State:", 6)) + continue; + + fclose(status_file); + p =3D status_line + 6; + while (isspace(*p)) + p++; + return *p =3D=3D 'Z' ? 1 : 0; + } + /* Read failed assume finish_command was called. */ + fclose(status_file); + return 1; +#else wait_or_whine(cmd, /*block=3D*/false); return cmd->finished; +#endif } =20 int finish_command(struct child_process *cmd) --=20 2.47.0.rc1.288.g06298d1525-goog