From patchwork Wed Apr 15 10:42:07 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: 185476 Delivered-To: patch@linaro.org Received: by 2002:a50:3f90:0:0:0:0:0 with SMTP id e16csp296485ecu; Wed, 15 Apr 2020 03:46:11 -0700 (PDT) X-Google-Smtp-Source: APiQypIiCTJnRFmOfOTVvDOGpWQEceAf6ntDNjABU+lUkXR6iqLEQSSmPPZeZs6UqEk1aK0uNTMU X-Received: by 2002:a37:5907:: with SMTP id n7mr25915994qkb.227.1586947570968; Wed, 15 Apr 2020 03:46:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586947570; cv=none; d=google.com; s=arc-20160816; b=FOUdPdUu4IcYL+LBD8uHgTPguF3Q882irL3wtmwj9HFX6qTDrYZNP027Chj1htuFi8 VKUf8Rh9jooH5tOEUSC02W5sQhz48Lk5RVlljXJT+m1H4RR427mJ1jkHmrdU9yHcrNWN fpcwPp98DMvl2mREwzXMtyCCXK2Dsk3PKqrgvcvFbGmlYYB9ZkUmAOfrsy4X0SoFcdFu TVa/n8d+Q6Lu7l5tngWKBUZsP+pTJZS+bHPQo444O0uLfcIZhPfe/g4wHapAHxI+PrFW jZG3FaewAeQw/72ZDgl3VT2GjzziiL9QapAOAFKH3DJu9cyfIiUZOD6Zxb4u6ZC9jqSm 2iLA== 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=xbro/RkD78CkyyXw+NBefFNc/SpIU1l0Ej5jYONi0ms=; b=WMbDUXrHBZU7egumUjKAFZpP1zWP4wx/fdgcLbTPeo/G4zSs/MzUIzymRXlS4eLf8F lToN903rW47M2qcU5lfsASAX48WEgX/dMeHmNpoeG6+aHYyiUNsG8YTlSdPeZLzK+frw JzaWb0rmtEWbM4v6qVYSJPXunkZM649oeOBAdI0DUeQsEXvNfcz4qY9ImBnZc4KtUFFd Qae1R11ysX2EwfEwOGXdtW/wFP4CZIToLU/YkFD8xjJw5CMPAKqK4tBkN3CoY8g2ujRU hiC4HfKwJGf3/xARR80GbLTG0PhH2q013yAc3781gPaQyfvaCLTyskdos/2vb1yqx28i oWYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Z8oriZLa; 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 i13si8174154qtj.137.2020.04.15.03.46.10 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 15 Apr 2020 03:46:10 -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=Z8oriZLa; 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]:47734 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jOfYY-0000pQ-Bi for patch@linaro.org; Wed, 15 Apr 2020 06:46:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45490) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jOfUr-0002Db-DU for qemu-devel@nongnu.org; Wed, 15 Apr 2020 06:42:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jOfUq-00038D-9f for qemu-devel@nongnu.org; Wed, 15 Apr 2020 06:42:21 -0400 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]:39953) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jOfUq-00037t-2u for qemu-devel@nongnu.org; Wed, 15 Apr 2020 06:42:20 -0400 Received: by mail-wm1-x343.google.com with SMTP id a81so18368554wmf.5 for ; Wed, 15 Apr 2020 03:42:20 -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=xbro/RkD78CkyyXw+NBefFNc/SpIU1l0Ej5jYONi0ms=; b=Z8oriZLafPy1iDQxG3Fe9GeQv78TtU963w4AhWk6iHSTB9iK7i334CwDJSsL1HAIZ8 R5SsYqQ/Yzgs18TP1rGfzSybzphIRIAdttoj5M0IfYn5oHPa57+nGy8HIb0pRx1AmPkM 9+QuJOOgtJ8u8oERT4F/ss6WcSm/Y2RLafTr8S0XqePZc0WWk5wjK/7a9jXb3ylRPtC4 hUfWtWoxizcsdQDfZRBI1pjN/tU7uRgdY72hly0j76/Cu2glTi8EQRPnwP59PLIVOnLp K9SJB0aA7gUTcMQd7X/Nc6zzQJMWx26kuOSb730B6w+R1MR9gBG/vNVa7nkSFPYVsTgB QJmA== 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=xbro/RkD78CkyyXw+NBefFNc/SpIU1l0Ej5jYONi0ms=; b=A9ICF1nPbhAvSyXZa3zlrgXd27qFu+oOTyCQ4c4hDjQ09Np9z1fkASiNOL34MhRYJU sxfgfmpzfyO/VB6WtuvVC/D/gnzLA5ETBa3o5z6tSouaa1+Qk9I7z2Z6FluC7qBqM+WH JtkN15Gi4zsRVPbVLrP0nhl4Se+e5evrNA6cFp0I/7EfpyiykvjSPLQsCOYToRIs8Glo vdNgK4GPw8NffPqKwf/sOTGCn72HJL0/xh6QGOHMt7t5pJ+BkL/mVpl+3TEMSy2mbIMj JWlvJWwsbI/Koyjet1mOnwUXFmjH2RuF5VffWUth6KZTnIsVoN48jDKToh+S7VZ27UXe Zh9A== X-Gm-Message-State: AGi0PuaTKddSmC3qxyr7T6tOeQdyd+xCamGoH7iuxiyuACzB3BKHlPWx I4JAHQTug2UCtRhK3rHTbpoAfw== X-Received: by 2002:a7b:c755:: with SMTP id w21mr4465467wmk.120.1586947339104; Wed, 15 Apr 2020 03:42:19 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id r27sm3810254wra.83.2020.04.15.03.42.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Apr 2020 03:42:17 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 5689F1FF90; Wed, 15 Apr 2020 11:42:12 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org Subject: [PULL 4/8] linux-user: fix /proc/self/stat handling Date: Wed, 15 Apr 2020 11:42:07 +0100 Message-Id: <20200415104211.9388-5-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200415104211.9388-1-alex.bennee@linaro.org> References: <20200415104211.9388-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::343 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 , qemu-devel@nongnu.org, Laurent Vivier , Brice Goglin , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= 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 Message-Id: <20200414200631.12799-10-alex.bennee@linaro.org> -- 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;