From patchwork Thu Apr 9 21:15:28 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: 185331 Delivered-To: patch@linaro.org Received: by 2002:a92:de47:0:0:0:0:0 with SMTP id e7csp2325184ilr; Thu, 9 Apr 2020 14:22:30 -0700 (PDT) X-Google-Smtp-Source: APiQypIfEP2wF5h/6aBTrh4+kfuYIonZnewirSRSvDEjHiCp7M41EotMjZJ9YL7Z/qdtUS4wdjbA X-Received: by 2002:a05:620a:10b4:: with SMTP id h20mr993697qkk.85.1586467349438; Thu, 09 Apr 2020 14:22:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586467349; cv=none; d=google.com; s=arc-20160816; b=Y2EqcbZuyhQizF/7WEpdKWvRU64N+8o06Zikkbs+A9lMHRxPGBLhpbD4vXyZZGeuSA +oRez8tab1NoH/Z6O34xbzBrwmn+UYlPjwJrhtzyx/Zsg3j8svQglvsT7ES8i1qyuYOE h4V+Wlz6A7PM8RLyhDb0tp4qGjA7Vxn4Rvix3TC+JbtkkYvzXJBBuoCMCJevUr542jh3 qt1xkFgSEg+84n1eMjRUerVmT5KLvSKDqJVUsuZ0eQBxTLlnE9yy8AzP5bDq7Qf9kkOL Hm2C0npQNkvyf/JidRZGNIVSBVxXb1l1ceFL7qTcsAnRwFzc28kmUblpZv9fkZo/KNWM aDUw== 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=UOHKfqIKF1jCjy8KTE3gn1Ffb6t6apDQDsVpcDQdBmM=; b=fExwhAEDMv8YKGKyMwcur48tffyYbGfmD8lcqgw8tG6mniP3MCxVnU5Xwm6AK9on0f 4uBrS7rajS80xGrDaOrvzBQRreKyJtWKTtL5I90lHFwYgso+v8YAV5oqWv7L6Hw9ko+v 8wqDVtgwQ003Mp0jSTvj9sgZuQaxbJBDi6g4/f4SSK7cf/8hRkfmCApZPOAXcmykN2jv 5X9IsAtG2CM+zEV4ZiXV2ZQ8EVm/TPsupiV5gUQLn8jRwPD2kGG+17mdqaJ6m2BRZJM+ dpepfkbRFzT76O2Q8xC/1e0j6pEZEOjp/B33JuibhtloJM4C7ki7OOvVHQP5P2H3x0RP Ffag== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=zvkleqK5; 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 f38si6048638qtb.307.2020.04.09.14.22.29 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 09 Apr 2020 14:22:29 -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=zvkleqK5; 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]:55738 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMed2-0003nd-VE for patch@linaro.org; Thu, 09 Apr 2020 17:22:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42396) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMeWY-0002CU-6t for qemu-devel@nongnu.org; Thu, 09 Apr 2020 17:15:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jMeWX-0005qF-2y for qemu-devel@nongnu.org; Thu, 09 Apr 2020 17:15:46 -0400 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:46569) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jMeWW-0005pW-T2 for qemu-devel@nongnu.org; Thu, 09 Apr 2020 17:15:45 -0400 Received: by mail-wr1-x444.google.com with SMTP id f13so7119751wrm.13 for ; Thu, 09 Apr 2020 14:15:44 -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=UOHKfqIKF1jCjy8KTE3gn1Ffb6t6apDQDsVpcDQdBmM=; b=zvkleqK54b0q8+MePDP5WrH3YP8Drad0R5SwOyaUvXPWXJhLxfw6gqYrwoC5QPyBKY bP0RqyDQg1pjNqAOgTVKjJPFk/mnkD+8HJe296Kj3Ak4/3iMBh2eiY643dDWMKNQ3zzG CLoOvVAyz/9LRGM4RNX0nv3LNL3dzs/RONV7nlcjbMcpkhCMYYJKQP0oDeYErzfIlsFi WtFblGD5dH/qhcdpD3YKHZAMndOiQa6xDCBxNeQv6xaiL5bUJKxw7ZNXk7qzfo7SOUdn cvH1kK5WS7N/Siy18w3nszXNoJEfl3i5P+r/DFRqe07+nV0SK+/ixNbHXbrL9XTAr3Nh q/yg== 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=UOHKfqIKF1jCjy8KTE3gn1Ffb6t6apDQDsVpcDQdBmM=; b=L6RSb5i2Dn0cGMojWMbEMsJ50XloHPO+AIav1yJZH+Qa6CcESB6UcvoxYrzgNv7vZU J/G/fC5JMWevujOPKnxU0WehaZT+XZ25CGzQQ7Pa/wZKfdrliXthfN6rTSjXdKP0/fLl kAPSNT9Nxubu95ywbuy2NtbV2eoEDG+tj7WD8jya7offrqFO4CbMpiwGeZ07fTErqY7q DFy3bmV0nx27GdgUm0A8xplpMdaZrjvQPPBPkOsDDhtnB3IUMf1FhhitXaqWou9JNdf1 g2mkCZsdubv47YHc+5vPWdFB22liL28upQid8pKE/QhpVhT/GIo6uIlRDtvxqhvB8VUy le7A== X-Gm-Message-State: AGi0PuZ23H6uTwB+kzwq/cvC0EU/P8Cr7zaNG5Wld1rGDo7cdg/jb6qf CUr6MpWuoTr38DvoJ1yxn8hrCw== X-Received: by 2002:adf:d851:: with SMTP id k17mr1214694wrl.157.1586466943892; Thu, 09 Apr 2020 14:15:43 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id x18sm5045661wmi.29.2020.04.09.14.15.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Apr 2020 14:15:37 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id EE4091FF99; Thu, 9 Apr 2020 22:15:30 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v1 10/11] linux-user: fix /proc/self/stat handling Date: Thu, 9 Apr 2020 22:15:28 +0100 Message-Id: <20200409211529.5269-11-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200409211529.5269-1-alex.bennee@linaro.org> References: <20200409211529.5269-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::444 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?Philippe=5FMathieu-Daud=C3=A9?= , Riku Voipio , =?utf-8?q?Alex_Benn=C3=A9e?= , Laurent Vivier , Brice Goglin 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. 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 Cc: Philippe_Mathieu-Daudé Signed-off-by: Alex Bennée --- linux-user/syscall.c | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) -- 2.20.1 Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Richard Henderson 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;