From patchwork Tue Apr 14 20:06:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 185443 Delivered-To: patch@linaro.org Received: by 2002:a92:3d9a:0:0:0:0:0 with SMTP id k26csp1833236ilf; Tue, 14 Apr 2020 13:09:06 -0700 (PDT) X-Google-Smtp-Source: APiQypLlgsGw24cK32HUuXkcL6nHfMOmuIHuCNElxesM+KFLH1UNgYdoPv0OysSzCTeqO55A2Vsi X-Received: by 2002:ac8:19dd:: with SMTP id s29mr12171000qtk.164.1586894946644; Tue, 14 Apr 2020 13:09:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586894946; cv=none; d=google.com; s=arc-20160816; b=eTXjX1PXvLB+eXrjcxdc7V440/fGZxYnDQH5KHYICwjktBW8hFFqRQaVZLUgpFHxX4 ge/QItia+XnUTpTLeIbnGK6rRPobIgmVl68fZQG1RmQGIP3PVcgD6zU8ZZ8PGm43X4LN xtcLAeE4g8p0qzxf2vmHt6iplxigkrQQJiZrJpPfwRTuAEXI4Phzx4gSRwEzJ4NmP/hO fsQVP1oejoK/QU1RV7ApEXA6qWF5KgJ2MgnmkLDr5Saqyhn3OEwD3yv1/zlbIA8uXtAb PIh+HZQHRoFofdjQSVWmKwQFLJ3tGsQRun7DEQlL9ha5SezLHubr9tyin5jj4cGiRxkM idKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=9Tb6fI3tVBkHUd/y7Zo+dSFehRr8QpWy7fD+f4ic+ig=; b=F0vJAt/Q0X95vZeCZ4rNu+uXFiLnE+ivIOA9FRn/Z0lox+3iINO0IMbYHtejOzIAKk qPZzsm+3RSRKqQTkGF3HN+w6FJOaoxkAEdXTaGbHXggc8kIWGpYbFl7135SipzKQU7nk j5w4z/4d90bIpJZ+QKFZHee1JbUZniP5NpTKQJZqGgTdynmEbcLzy518eCJgaDM1PEGu lTq74ogP5cHB5QVXuQ0kMy9HDhuwEuP0+7RoSToQd0h+dsJwW0dlY61xwVZSi1azLaRT dATRyT4+9InRBVx6GBYc3sm7m99lf8CCqIbyu56YTl411ltMgsVxCD776cfwR8dENFjp JBUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=wttuBit2; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id g5si7997797qvs.207.2020.04.14.13.09.06 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 14 Apr 2020 13:09:06 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=wttuBit2; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:38388 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jORrm-0001C3-3a for patch@linaro.org; Tue, 14 Apr 2020 16:09:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44564) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jORpa-0006BS-1h for qemu-devel@nongnu.org; Tue, 14 Apr 2020 16:06:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jORpY-0005z4-Ts for qemu-devel@nongnu.org; Tue, 14 Apr 2020 16:06:49 -0400 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]:36153) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jORpY-0005yf-N6 for qemu-devel@nongnu.org; Tue, 14 Apr 2020 16:06:48 -0400 Received: by mail-wr1-x441.google.com with SMTP id u13so15521559wrp.3 for ; Tue, 14 Apr 2020 13:06:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9Tb6fI3tVBkHUd/y7Zo+dSFehRr8QpWy7fD+f4ic+ig=; b=wttuBit2rudWSw4TCjpzebO7LSNobJFxOG3lmzE6092o9UW39mKyyA6MWVc90M9t41 gu/bsMymQ+RVAyJXqyb6BqIs1JzECzshdqlSo/EmFdM4Ktbt2H+Rjsp4DEWFCmFN92oB ko3wIVlQ/dMRRK7/eu0RN77b6UJVAkG3lWOw5vdZoQw6umLE41FaHlzcAstlCnNmzHw0 w7jjfu1L4gHmZMXgaAzRxuh+z+RcCRxV0sagbb9WhsbwxkQRrDi/8/xhE5dn71aD3gKj 2jWlZCoqYdoMfCXUiHWYPOglrUxB7EnQziqIhglIUuEJYz+zDhlc/IIAa/eP91eG6ill N8VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9Tb6fI3tVBkHUd/y7Zo+dSFehRr8QpWy7fD+f4ic+ig=; b=Ul21illD1mxwBWhwW3606Pm14XDCGxjhdGB59kB5F1TjwbOcBdVXh4aItD2Ke0cXbD kNQzt7T2uDlINqp1uGStNlZNcdAob14oS5kdwjP0DBomeQ9ykpaq+PjBbcQh6sVFKrFH HqqcT8yKw4Ht0ZOUnmp+N+REhniInN6UH/4+QjlPFA97N9MqOheOQDdBizAYXLjwLjpI 18jQzUFyHx64HtvXFt3ZR5sp4BXxZ+nMbUcaVwKzOuq+IOhquvf5K6J4yXm2tGeJI7sx WihOJvmcOnBhQYN3iWbfxURyCJ7iSQBcuOchBQKCjLIqa7dqmgR21PFRUw4oFv48kMmP ogjg== X-Gm-Message-State: AGi0Pub28TF2PuOgtzmX5HaaQQNzIikMiIIbVMBkCjpbm7qG788kKcu4 9oaLUHUEIjj51nO56CavYF5l/w== X-Received: by 2002:adf:d087:: with SMTP id y7mr4275303wrh.321.1586894807624; Tue, 14 Apr 2020 13:06:47 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id p10sm19781721wrm.6.2020.04.14.13.06.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 13:06:44 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 080751FF98; Tue, 14 Apr 2020 21:06:33 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v2 09/17] linux-user: fix /proc/self/stat handling Date: Tue, 14 Apr 2020 21:06:23 +0100 Message-Id: <20200414200631.12799-10-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200414200631.12799-1-alex.bennee@linaro.org> References: <20200414200631.12799-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::441 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Alex_Benn=C3=A9e?= , Riku Voipio , Richard Henderson , Laurent Vivier , Brice Goglin , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?b?w6k=?= Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" In the original bug report long files names in Guix caused /proc/self/stat be truncated without the trailing ") " as specified in proc manpage which says: (2) comm %s The filename of the executable, in parentheses. This is visible whether or not the executable is swapped out. In the kernel this is currently done by do_task_stat calling proc_task_name() which uses a structure limited by TASK_COMM_LEN (16). Additionally it should only be reporting the executable name rather than the full path. Fix both these failings while cleaning up the code to use GString to build up the reported values. As the whole function is cleaned up also adjust the white space to the current coding style. Message-ID: Reported-by: Brice Goglin Signed-off-by: Alex Bennée Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Richard Henderson --- linux-user/syscall.c | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) -- 2.20.1 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 6495ddc4cda..674f70e70a5 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -7295,34 +7295,29 @@ static int open_self_stat(void *cpu_env, int fd) { CPUState *cpu = env_cpu((CPUArchState *)cpu_env); TaskState *ts = cpu->opaque; - abi_ulong start_stack = ts->info->start_stack; + g_autoptr(GString) buf = g_string_new(NULL); int i; for (i = 0; i < 44; i++) { - char buf[128]; - int len; - uint64_t val = 0; - - if (i == 0) { - /* pid */ - val = getpid(); - snprintf(buf, sizeof(buf), "%"PRId64 " ", val); - } else if (i == 1) { - /* app name */ - snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]); - } else if (i == 27) { - /* stack bottom */ - val = start_stack; - snprintf(buf, sizeof(buf), "%"PRId64 " ", val); - } else { - /* for the rest, there is MasterCard */ - snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' '); - } + if (i == 0) { + /* pid */ + g_string_printf(buf, FMT_pid " ", getpid()); + } else if (i == 1) { + /* app name */ + gchar *bin = g_strrstr(ts->bprm->argv[0], "/"); + bin = bin ? bin + 1 : ts->bprm->argv[0]; + g_string_printf(buf, "(%.15s) ", bin); + } else if (i == 27) { + /* stack bottom */ + g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack); + } else { + /* for the rest, there is MasterCard */ + g_string_printf(buf, "0%c", i == 43 ? '\n' : ' '); + } - len = strlen(buf); - if (write(fd, buf, len) != len) { - return -1; - } + if (write(fd, buf->str, buf->len) != buf->len) { + return -1; + } } return 0;