From nobody Sat Apr 27 07:09:54 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1573823579; cv=none; d=zoho.com; s=zohoarc; b=evslEgjFmQD4yLTuh/18vZQblA7UCv1vbQ/oYZICZFbqWxNsqQ/4VandPLpILD42eqsbyOOyaX/TSmQP37SlJ23m3alD8nt4v2QHtudVoDKFV8GEq9SMawZCMMD58IyIkkfC+zFZ2uGs1I6OWjdwwebps4NWkhTXhybf2Ftlf/Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573823579; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=diXnF1IPkkxinXlGIqTBKK6HjgFjUcsfRfseHjAG8V0=; b=Qp1jb1zyoHym8KbCG0cw3xR6CO25y7V9nYTxlL/mHPCfEUQ+HfUf/Lq/UFlWt6bqd0xtsRUZ8efzDr0q33IYdC37Xm1dT2qkd5mPSgXWPi1dBVF9EaldaN06hKVtbEzlpGtfzHfkYbf4/dyXPjOmj9oXO1rq/BVlAfzxKH+dYUg= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573823579709182.4029450840967; Fri, 15 Nov 2019 05:12:59 -0800 (PST) Received: from localhost ([::1]:39072 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbPD-0005fs-Bs for importer@patchew.org; Fri, 15 Nov 2019 08:12:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49403) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbNw-0004Ns-9m for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVbNv-0004O9-CL for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:36 -0500 Received: from mail-pf1-x444.google.com ([2607:f8b0:4864:20::444]:36676) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iVbNv-0004Nu-7N for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:35 -0500 Received: by mail-pf1-x444.google.com with SMTP id b19so6673833pfd.3 for ; Fri, 15 Nov 2019 05:11:35 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id w138sm12007249pfc.68.2019.11.15.05.11.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2019 05:11:33 -0800 (PST) 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=diXnF1IPkkxinXlGIqTBKK6HjgFjUcsfRfseHjAG8V0=; b=cd6e2128QvkQXDsV9YD8foQDT+InMB8+/EC8yNh5eodUOIOk46ov2fxlrC+Rga724A 0eMaAEnLcc18PQIqTYUj8DLuT3btXEuJHiFbHurp/AV4jQfzlOKNRtg8i0YNbGewu8Ne SGk4KzfftSfi19UBpr1BV17IZsj7/U1d89Ua400q+aY4kVO5DAvConKmWLgQN+BZEnxS jpsYNtbfC0lymsaj2ku5k9nozilZLoiQMsivwAPpvFHvo684hOr8UW4bte5mN/VbR8Hr bbElEXLyo+edAk0n7m71zOM7dt/ilwtKT+vMH1gGtSBi+qUDDmnqb38ZvxCpVxYMQQw8 2VeQ== 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=diXnF1IPkkxinXlGIqTBKK6HjgFjUcsfRfseHjAG8V0=; b=MWXHgV8y1c9yF1NnFf5zm3cUBpzX934xKpyCI/zxgNJi5imlZkZRkrRh0IyInSxoCy BJyuNNCgj5LwT2giGeZLicLYgQr+vExa+6rJvH34X+r4nXOV02fjeYWLmDPyIG4YqQtf HIkrR9VXlonAPnEWguJXBIiCJGlYp1ddTe+GG0G3esJAYx/aPQy/sItrTeBJJuRh0z/5 F7i44vwJrflwS4oWdOoF4b5CR9lfzjR1xLwVj8zM1nkusaTFhoOMPoMcOfO7GtxO6lS8 q8WGlodHCqtOMQcrDkEmbSpPoGa9HHHeNxgHz0+Gu94SiIIuqpsDr2w4WfI+Bl3qfBEl wQdA== X-Gm-Message-State: APjAAAVQKPfn6UVJBg7O8oda7bsWmzcLA5xh3U2Gg5pxYWx4u0Zz7Lju cTrDEIAQYZE4PON36mOqdvxtYO8NIS0= X-Google-Smtp-Source: APXvYqzYh86OPnfKTXwOx5xsSrZjjdf7vDEALFp2r2B5zizglkngToqonsr+KnJhwEWDWc2OGc+jKQ== X-Received: by 2002:a63:201b:: with SMTP id g27mr14642133pgg.56.1573823493901; Fri, 15 Nov 2019 05:11:33 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v2 1/6] Fix double free issue in qemu_set_log_filename(). Date: Fri, 15 Nov 2019 08:10:35 -0500 Message-Id: <20191115131040.2834-2-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191115131040.2834-1-robert.foley@linaro.org> References: <20191115131040.2834-1-robert.foley@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0: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: peter.puhov@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) After freeing the logfilename, we set logfilename to NULL, in case of an error which returns without setting logfilename. Signed-off-by: Robert Foley Reviewed-by: Alex Benn=C3=A9e --- v2 - moved this change to the beginning of the patch series. --- v1 - This is new in the patch v1. --- util/log.c | 1 + 1 file changed, 1 insertion(+) diff --git a/util/log.c b/util/log.c index 1ca13059ee..4316fe74ee 100644 --- a/util/log.c +++ b/util/log.c @@ -113,6 +113,7 @@ void qemu_set_log_filename(const char *filename, Error = **errp) { char *pidstr; g_free(logfilename); + logfilename =3D NULL; =20 pidstr =3D strstr(filename, "%"); if (pidstr) { --=20 2.17.1 From nobody Sat Apr 27 07:09:54 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1573823581; cv=none; d=zoho.com; s=zohoarc; b=PRTxZoF52EBGcYFIxmyAroxb/exIP/PQgBZlij7cZEBtJ08LsgQvKFt6FAbs/+1DvPoxrxHd7wVQ5Q2Jr2z+ddnRnC2/z4T1cVQNt0h7Cs6HUdLCnaHXuTchMPYdJp9EbRzOkify77a8igNohK8fBxKglkEsOA+t95jGs0X9XMg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573823581; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=YlXtkX/1xb+644dG6zbF+PNq9V+WmAv19VZ33vFg8pI=; b=RaR9ODFofjZlEWPqjTARVG45dHOfnlVR0Db0CEztSf63g8Q0oCAk67/ubmmbXC+A5rBqZ4uV9d8vwQOnX4Gc+2StoXPeMG/ZGR731zZ7r2S/OZMT810AvZdc9HKUhqQHcDJhKhlC20hvL4ccGVXXXbbXxS7fcbIRrkLCqCCumqg= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573823581915692.9253739831826; Fri, 15 Nov 2019 05:13:01 -0800 (PST) Received: from localhost ([::1]:39076 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbPH-0005oM-Js for importer@patchew.org; Fri, 15 Nov 2019 08:12:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49440) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbO2-0004TG-2I for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVbNx-0004Oy-6g for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:41 -0500 Received: from mail-pl1-x641.google.com ([2607:f8b0:4864:20::641]:42118) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iVbNx-0004Oa-1K for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:37 -0500 Received: by mail-pl1-x641.google.com with SMTP id j12so4641045plt.9 for ; Fri, 15 Nov 2019 05:11:36 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id w138sm12007249pfc.68.2019.11.15.05.11.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2019 05:11:35 -0800 (PST) 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; bh=YlXtkX/1xb+644dG6zbF+PNq9V+WmAv19VZ33vFg8pI=; b=RsSj9gUhivxTJkHSr+Ptto0VCdQhnuD4ABejmu1BpfVu+bl+ZAoLY2uLJNC1IE3eZp 09pIgCx6/ZxuyuouSIxAgK6mOb+VeyLoLLUrMUuL0WZ5qhsqiqr1mN/xJbPXHIkM3EfY QwyxCCH2PTftcwAgD0sREuaj+QImYd0AdJZ1eAAdgP7duj7LSqaYMc5nk+sjqMKuOqVX +m3xjcGdaa/7fbq7dwnpkqHwRSx+J9l9ddp6VV8ItC8ALI6s3tMJNdavuqozlFOb0lST S/R4Ni5ipqO6AFN/yAdNsMn6mL+wqZnpAGVyU8dlJujBcIFg44PFg0xz3zgTGktrJyQH /NLQ== 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; bh=YlXtkX/1xb+644dG6zbF+PNq9V+WmAv19VZ33vFg8pI=; b=hFmGb0rz55MjoyOCXDRBk9QCHdgGx89OIYAym2v87khEroQCdrZ9T9f6r5SA3inFRr YP9GDDQy4I7wbQPF8TUr7YD1X9PoJST4QJoihNSBJeHvmNA2i1E1RfDd2XUopACYLMym rDi4ccUoBcDoy4szYpoSXkNflOw5PGLx/u5hFluWNK/UKj6rwKlu+YJMG/nTcX3IwJyY o4mfweaT/CJkYU2HAixzrcp/gTERwZtCIKP6AahJntGeazsTxeBlqaulubFw/70Qci9j yZFZH0lQnCqnHTgGDGrLPJU3j8lYI3nnBoC8uptBl5eYyN86gcBFUxuzOoA4fvw22v/d tDbg== X-Gm-Message-State: APjAAAXd5PFLv6G4s9GvXe2iOWa0r5XKvrskEFC55ze+f63KQPFlUrC+ IYvjncGABSCzDFdw138afGHUTEY2h/8= X-Google-Smtp-Source: APXvYqxN+wL51K/KYg3RIbO8SlEw+Wy//+lN1mNmEJavmvEqT3WY3kV6t4lxSdxpWsNX9YX0wDkQfg== X-Received: by 2002:a17:902:9a04:: with SMTP id v4mr15261685plp.192.1573823495736; Fri, 15 Nov 2019 05:11:35 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v2 2/6] Cleaned up flow of code in qemu_set_log(), to simplify and clarify. Date: Fri, 15 Nov 2019 08:10:36 -0500 Message-Id: <20191115131040.2834-3-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191115131040.2834-1-robert.foley@linaro.org> References: <20191115131040.2834-1-robert.foley@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::641 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: peter.puhov@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Also added some explanation of the reasoning behind the branches. Signed-off-by: Robert Foley Reviewed-by: Alex Benn=C3=A9e --- v2 - This is new in patch v2. --- util/log.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/util/log.c b/util/log.c index 4316fe74ee..417d16ec66 100644 --- a/util/log.c +++ b/util/log.c @@ -54,12 +54,25 @@ static bool log_uses_own_buffers; /* enable or disable low levels log */ void qemu_set_log(int log_flags) { + bool need_to_open_file =3D false; qemu_loglevel =3D log_flags; #ifdef CONFIG_TRACE_LOG qemu_loglevel |=3D LOG_TRACE; #endif - if (!qemu_logfile && - (is_daemonized() ? logfilename !=3D NULL : qemu_loglevel)) { + /* + * In all cases we only log if qemu_loglevel is set. + * Also: + * If not daemonized we will always log either to stderr + * or to a file (if there is a logfilename). + * If we are daemonized, + * we will only log if there is a logfilename. + */ + if (qemu_loglevel && (!is_daemonized() || logfilename)) { + need_to_open_file =3D true; + } + if (qemu_logfile && !need_to_open_file) { + qemu_log_close(); + } else if (!qemu_logfile && need_to_open_file) { if (logfilename) { qemu_logfile =3D fopen(logfilename, log_append ? "a" : "w"); if (!qemu_logfile) { @@ -93,10 +106,6 @@ void qemu_set_log(int log_flags) log_append =3D 1; } } - if (qemu_logfile && - (is_daemonized() ? logfilename =3D=3D NULL : !qemu_loglevel)) { - qemu_log_close(); - } } =20 void qemu_log_needs_buffers(void) --=20 2.17.1 From nobody Sat Apr 27 07:09:54 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1573823701; cv=none; d=zoho.com; s=zohoarc; b=c9AWmEZwKxjdJrSY2iUXcFDXzLh/xbEo/J9nwWga7iwYip/LzDO1OOKq1VsH8bi2tXchQvnrdZxp73bFxMrIpAopbqSelSjcq23eqSjng2lpVhnK7WjAYCdCA7RbWDvXRYWr33yu8OI3wc+lrPztbTTJ7d+R0VmtLrwdDVTAUOc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573823701; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=jRenRuOm4Z6evRvZHH+d3jhAxtFeu++eNMWL2guRWVY=; b=mHhNzTwjXLSfaStMrFx8URTjAXGGvAwavIAc7ht7XiLTabD968gDzJds9YYy+Dzg6NCbljHxmpotTHvpNyPxd1KrwYcKJW2I21ZuaFgA61Rx5VTqCXl8ORLDHA0iwh0HYb2x9nr7mYgNcMDzEmwENgtDDicGWKHZkjDm8MaAaJs= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573823701103620.0460013312916; Fri, 15 Nov 2019 05:15:01 -0800 (PST) Received: from localhost ([::1]:39100 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbRD-0007uC-TT for importer@patchew.org; Fri, 15 Nov 2019 08:14:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49432) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbO0-0004Q8-4C for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:41 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVbNy-0004PT-VE for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:40 -0500 Received: from mail-pg1-x543.google.com ([2607:f8b0:4864:20::543]:39836) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iVbNy-0004PG-OD for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:38 -0500 Received: by mail-pg1-x543.google.com with SMTP id 29so6007121pgm.6 for ; Fri, 15 Nov 2019 05:11:38 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id w138sm12007249pfc.68.2019.11.15.05.11.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2019 05:11:36 -0800 (PST) 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=jRenRuOm4Z6evRvZHH+d3jhAxtFeu++eNMWL2guRWVY=; b=h2GbYr87HvO2wfQxpXY34QBWwuM8HUUdVpXw7rgKxdKL+VHHTjcogthG3kC6Go6i9e G9TPtMxTaJrt74cd+XuTwEJo34l5uSkwJj8HWdQIbOjbgEO1kcsmJ9FgCcVv/0NqwqMO Lw6sjH5C86mZTbPacRlkG1TLgPkwmwUAUg9bQAnGvhrhqvmXQkP26ZmoTGaGcNZar4gF WgieA3KkqKbOZCr3V/m+9q/mbqPeRZywAma+k9wCQu7hxBKnH1IMHGNnwRgQSLbiGOda 6eMcuS7ZBFkGewxTIs5yC8H30p/I0VPE1z1rORuTvUFoNc+TgNP9WPkPmlIEGRlSHYYX KUxg== 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=jRenRuOm4Z6evRvZHH+d3jhAxtFeu++eNMWL2guRWVY=; b=V5IObTb2ChuUHnkssaBeoxWX8sKs/HZsZomB9h2yK7QKuEhbtCovzx0LrXP/+n945p Q1wY8FubtJwSdZTXmR9NNr2EwD9Dn3Is3u6mxMqDMgMyGqERkmhfSoe7h/bvd3VHycA4 wmJHGUBpryZ+YUEcM8/gfM1fqckBfyYJMIecgyRvUSA/UflfafOjhXuiwaKGp6KThFEJ 3O4ePyuIvtqsoGL1uu5lcCATnX1qk951oqe28lTd3W7KDVR4r75uJA4E2VJsmYlNJ2o/ Ncdsz5Ok5Wq8LN6Ll1gllbpusyUX4iOFMDkVf8XZwKdEIgnSm62Xzg5dzBKC4l7qtbcA F2iQ== X-Gm-Message-State: APjAAAVm751K2r7jqQyzliUfXFglC0Qk6pbEFxkD8HiOjjJ22UY8IM6F Qabd5SgLAM400NG8s8Vpj9S9odImQeI= X-Google-Smtp-Source: APXvYqxCzmWUzeMEkBLcnmt9bneQ9yVR61djNrrZEt3dEP/pC3osMpkmrGVPHIpWkZ0FVfyuUBFHsg== X-Received: by 2002:a63:fe4d:: with SMTP id x13mr15657215pgj.82.1573823497512; Fri, 15 Nov 2019 05:11:37 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v2 3/6] Add a mutex to guarantee single writer to qemu_logfile handle. Date: Fri, 15 Nov 2019 08:10:37 -0500 Message-Id: <20191115131040.2834-4-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191115131040.2834-1-robert.foley@linaro.org> References: <20191115131040.2834-1-robert.foley@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::543 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: peter.puhov@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Also added qemu_logfile_init() for initializing the logfile mutex. Signed-off-by: Robert Foley Reviewed-by: Alex Benn=C3=A9e --- v2 - In qemu_set_log() moved location of mutex lock/unlock=20 due to cleanup changes. --- v1 - changed qemu_logfile_init() to use __constructor__. --- util/log.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/util/log.c b/util/log.c index 417d16ec66..91ebb5c924 100644 --- a/util/log.c +++ b/util/log.c @@ -24,8 +24,10 @@ #include "qapi/error.h" #include "qemu/cutils.h" #include "trace/control.h" +#include "qemu/thread.h" =20 static char *logfilename; +static QemuMutex qemu_logfile_mutex; FILE *qemu_logfile; int qemu_loglevel; static int log_append =3D 0; @@ -49,6 +51,11 @@ int qemu_log(const char *fmt, ...) return ret; } =20 +static void __attribute__((__constructor__)) qemu_logfile_init(void) +{ + qemu_mutex_init(&qemu_logfile_mutex); +} + static bool log_uses_own_buffers; =20 /* enable or disable low levels log */ @@ -70,7 +77,10 @@ void qemu_set_log(int log_flags) if (qemu_loglevel && (!is_daemonized() || logfilename)) { need_to_open_file =3D true; } + g_assert(qemu_logfile_mutex.initialized); + qemu_mutex_lock(&qemu_logfile_mutex); if (qemu_logfile && !need_to_open_file) { + qemu_mutex_unlock(&qemu_logfile_mutex); qemu_log_close(); } else if (!qemu_logfile && need_to_open_file) { if (logfilename) { @@ -105,6 +115,7 @@ void qemu_set_log(int log_flags) #endif log_append =3D 1; } + qemu_mutex_unlock(&qemu_logfile_mutex); } } =20 @@ -240,12 +251,15 @@ void qemu_log_flush(void) /* Close the log file */ void qemu_log_close(void) { + g_assert(qemu_logfile_mutex.initialized); + qemu_mutex_lock(&qemu_logfile_mutex); if (qemu_logfile) { if (qemu_logfile !=3D stderr) { fclose(qemu_logfile); } qemu_logfile =3D NULL; } + qemu_mutex_unlock(&qemu_logfile_mutex); } =20 const QEMULogItem qemu_log_items[] =3D { --=20 2.17.1 From nobody Sat Apr 27 07:09:54 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1573823909; cv=none; d=zoho.com; s=zohoarc; b=AuLDTt8sWJwOyZZEpEuJleQ7qA2tyA/B4ZMnPyQ2mHPfDw/Ek+07J4pyow6YO0kX2P0hvn9CtdkxWKUtpNf8wTdp1yIBGRu5d0USGMcDMRUI+y9Z++KkHSgcT7kS8qR7/+BncLpRCdBDeMuXQN9wDlGAGq5tvo7Xo3A4QwznRSM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573823909; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=CTtCqGcFjzZbuh6vMFCG0KnvT7OIWYmijX3kRnpAThE=; b=MkZTU+TCfYxByUZfVJxtUiYcLLBLRYAvVJDPSnmVptb0SDYyCzPKdhoBR8cQU4ZovyvDN9PXOrB0xb21OSDXCVOrGtxPNlsfPxMgDyIye/f6zD995OzK9dgJffBcxwUFF+tbe+/DSqPF+zKQhjDzQmBxcCVQ50SWLfZLWHczDLU= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573823909401162.35346195238878; Fri, 15 Nov 2019 05:18:29 -0800 (PST) Received: from localhost ([::1]:39142 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbUV-0001dT-9X for importer@patchew.org; Fri, 15 Nov 2019 08:18:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49452) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbO3-0004V8-96 for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVbO1-0004Q9-6U for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:43 -0500 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]:39709) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iVbO0-0004Pv-UY for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:41 -0500 Received: by mail-pf1-x442.google.com with SMTP id x28so6670189pfo.6 for ; Fri, 15 Nov 2019 05:11:40 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id w138sm12007249pfc.68.2019.11.15.05.11.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2019 05:11:38 -0800 (PST) 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=CTtCqGcFjzZbuh6vMFCG0KnvT7OIWYmijX3kRnpAThE=; b=Z0QWpRLEWajjU/2U7Kx3aZA78FMAs4BhPNtwxEbaj7Si6y+SJ+e1kEaqiFA8o0RpD4 BNBn2juvQ+cJgt6q4Xb1IxNK6NUHUgJkEHwnIpoh+soOl2Haq3AVPjvoSRRPW/UI893P AmfhFBMR3MvxZvQ5iwKB5V4SF1afZ1fd4IVyT0SRo3r3z/BFZi2S1qRl2j47Vi8GBod6 Ep83YnnexUio8ARbq0CPkrXBPIwCNO655KAVmjXMdZ4kEF0pVACeN7GliBJEa8mgzFTP uS5Ii6yzRxhEF+/YqfxIMQRXJJZRCbBYRpoalEHWMTalIHZ3t2dJ9J6KxXt7FS4YNmnt XBew== 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=CTtCqGcFjzZbuh6vMFCG0KnvT7OIWYmijX3kRnpAThE=; b=N/ALjSLRPXU+lfskxV7NpWPZ6cTBZOcx7uQG4zRZwWthgcCZ3cwNdnFjqsGNU0bT06 +kSQkYXVvKwj4fSEx56ku+wUdgR6tRpWEiI8NROkKr57VAuygRW+HWe19a/TeEZbAt/y D7pfSvILPfg4r9jB4y2BYI5REG1dbdMvBzTPprK3BwQKOInKMi/LkmBTf7ic3owfyNyJ bogMMc0HLcFIjItfgED0RCQh2vhKW924pU2HNNyCT2JPLFlhR2LMyiCkouvFFmmV2ypg os6NZl0t6pZWWzVR/HtJp+tZg1vYJO2x1LEFMwqoKjLtaf7MH3fn3JjQHBkyOusaIcMN ZCvw== X-Gm-Message-State: APjAAAWKwhePeXY6Nmpzn5OGDrvk07vLlyqBpbMkfhLfuQNYsQQahwla T/laH2P62bPFeGe0zAxrK0dSrBmxVJ4= X-Google-Smtp-Source: APXvYqwz0empvu1ACll9aB86sdUAysKxl2C/SwojwkV0UU/y7K0MZs4MZGXxLaGDUwPQqloFovcOOQ== X-Received: by 2002:a63:9d41:: with SMTP id i62mr16754292pgd.310.1573823499488; Fri, 15 Nov 2019 05:11:39 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v2 4/6] qemu_log_lock/unlock now preserves the qemu_logfile handle. Date: Fri, 15 Nov 2019 08:10:38 -0500 Message-Id: <20191115131040.2834-5-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191115131040.2834-1-robert.foley@linaro.org> References: <20191115131040.2834-1-robert.foley@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::442 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: peter.puhov@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) qemu_log_lock() now returns a handle and qemu_log_unlock() receives a handle to unlock. This allows for changing the handle during logging and ensures the lock() and unlock() are for the same file. Signed-off-by: Robert Foley Reviewed-by: Alex Benn=C3=A9e --- v1 - Moved this up in the patch sequence to be before adding RCU for qemu_logfile. --- include/qemu/log.h | 9 ++++++--- accel/tcg/cpu-exec.c | 4 ++-- accel/tcg/translate-all.c | 4 ++-- accel/tcg/translator.c | 4 ++-- exec.c | 4 ++-- hw/net/can/can_sja1000.c | 4 ++-- net/can/can_socketcan.c | 5 ++--- target/cris/translate.c | 4 ++-- target/i386/translate.c | 5 +++-- target/lm32/translate.c | 4 ++-- target/microblaze/translate.c | 4 ++-- target/nios2/translate.c | 4 ++-- target/tilegx/translate.c | 7 ++++--- target/unicore32/translate.c | 4 ++-- tcg/tcg.c | 16 ++++++++-------- 15 files changed, 43 insertions(+), 39 deletions(-) diff --git a/include/qemu/log.h b/include/qemu/log.h index a91105b2ad..a7c5b01571 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -53,14 +53,17 @@ static inline bool qemu_log_separate(void) * qemu_loglevel is never set when qemu_logfile is unset. */ =20 -static inline void qemu_log_lock(void) +static inline FILE *qemu_log_lock(void) { qemu_flockfile(qemu_logfile); + return logfile->fd; } =20 -static inline void qemu_log_unlock(void) +static inline void qemu_log_unlock(FILE *fd) { - qemu_funlockfile(qemu_logfile); + if (fd) { + qemu_funlockfile(fd); + } } =20 /* Logging functions: */ diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c index c01f59c743..62068d10c3 100644 --- a/accel/tcg/cpu-exec.c +++ b/accel/tcg/cpu-exec.c @@ -156,7 +156,7 @@ static inline tcg_target_ulong cpu_tb_exec(CPUState *cp= u, TranslationBlock *itb) #if defined(DEBUG_DISAS) if (qemu_loglevel_mask(CPU_LOG_TB_CPU) && qemu_log_in_addr_range(itb->pc)) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); int flags =3D 0; if (qemu_loglevel_mask(CPU_LOG_TB_FPU)) { flags |=3D CPU_DUMP_FPU; @@ -165,7 +165,7 @@ static inline tcg_target_ulong cpu_tb_exec(CPUState *cp= u, TranslationBlock *itb) flags |=3D CPU_DUMP_CCOP; #endif log_cpu_state(cpu, flags); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif /* DEBUG_DISAS */ =20 diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 9f48da9472..bb325a2bc4 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -1804,7 +1804,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu, #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM) && qemu_log_in_addr_range(tb->pc)) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("OUT: [size=3D%d]\n", gen_code_size); if (tcg_ctx->data_gen_ptr) { size_t code_size =3D tcg_ctx->data_gen_ptr - tb->tc.ptr; @@ -1829,7 +1829,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu, } qemu_log("\n"); qemu_log_flush(); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif =20 diff --git a/accel/tcg/translator.c b/accel/tcg/translator.c index f977682be7..603d17ff83 100644 --- a/accel/tcg/translator.c +++ b/accel/tcg/translator.c @@ -138,11 +138,11 @@ void translator_loop(const TranslatorOps *ops, DisasC= ontextBase *db, #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(db->pc_first)) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("----------------\n"); ops->disas_log(db, cpu); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif } diff --git a/exec.c b/exec.c index ffdb518535..c994a00f10 100644 --- a/exec.c +++ b/exec.c @@ -1223,13 +1223,13 @@ void cpu_abort(CPUState *cpu, const char *fmt, ...) fprintf(stderr, "\n"); cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP); if (qemu_log_separate()) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("qemu: fatal: "); qemu_log_vprintf(fmt, ap2); qemu_log("\n"); log_cpu_state(cpu, CPU_DUMP_FPU | CPU_DUMP_CCOP); qemu_log_flush(); - qemu_log_unlock(); + qemu_log_unlock(logfile); qemu_log_close(); } va_end(ap2); diff --git a/hw/net/can/can_sja1000.c b/hw/net/can/can_sja1000.c index 1f81341554..39c78faf9b 100644 --- a/hw/net/can/can_sja1000.c +++ b/hw/net/can/can_sja1000.c @@ -247,8 +247,8 @@ int can_sja_accept_filter(CanSJA1000State *s, static void can_display_msg(const char *prefix, const qemu_can_frame *msg) { int i; + FILE *logfile =3D qemu_log_lock(); =20 - qemu_log_lock(); qemu_log("%s%03X [%01d] %s %s", prefix, msg->can_id & QEMU_CAN_EFF_MASK, @@ -261,7 +261,7 @@ static void can_display_msg(const char *prefix, const q= emu_can_frame *msg) } qemu_log("\n"); qemu_log_flush(); - qemu_log_unlock(); + qemu_log_unlock(logfile); } =20 static void buff2frame_pel(const uint8_t *buff, qemu_can_frame *frame) diff --git a/net/can/can_socketcan.c b/net/can/can_socketcan.c index 8a6ffad40c..29bfacd4f8 100644 --- a/net/can/can_socketcan.c +++ b/net/can/can_socketcan.c @@ -76,8 +76,7 @@ QEMU_BUILD_BUG_ON(offsetof(qemu_can_frame, data) static void can_host_socketcan_display_msg(struct qemu_can_frame *msg) { int i; - - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("[cansocketcan]: %03X [%01d] %s %s", msg->can_id & QEMU_CAN_EFF_MASK, msg->can_dlc, @@ -89,7 +88,7 @@ static void can_host_socketcan_display_msg(struct qemu_ca= n_frame *msg) } qemu_log("\n"); qemu_log_flush(); - qemu_log_unlock(); + qemu_log_unlock(logfile); } =20 static void can_host_socketcan_read(void *opaque) diff --git a/target/cris/translate.c b/target/cris/translate.c index e752bd0609..cb57516a44 100644 --- a/target/cris/translate.c +++ b/target/cris/translate.c @@ -3273,11 +3273,11 @@ void gen_intermediate_code(CPUState *cs, Translatio= nBlock *tb, int max_insns) #if !DISAS_CRIS if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(pc_start)) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("--------------\n"); qemu_log("IN: %s\n", lookup_symbol(pc_start)); log_target_disas(cs, pc_start, dc->pc - pc_start); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif #endif diff --git a/target/i386/translate.c b/target/i386/translate.c index 77e932d827..7c99ef1385 100644 --- a/target/i386/translate.c +++ b/target/i386/translate.c @@ -2502,14 +2502,15 @@ static void gen_unknown_opcode(CPUX86State *env, Di= sasContext *s) gen_illegal_opcode(s); =20 if (qemu_loglevel_mask(LOG_UNIMP)) { + FILE *logfile =3D qemu_log_lock(); target_ulong pc =3D s->pc_start, end =3D s->pc; - qemu_log_lock(); + qemu_log("ILLOPC: " TARGET_FMT_lx ":", pc); for (; pc < end; ++pc) { qemu_log(" %02x", cpu_ldub_code(env, pc)); } qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } } =20 diff --git a/target/lm32/translate.c b/target/lm32/translate.c index 778cae1e81..73db9654d6 100644 --- a/target/lm32/translate.c +++ b/target/lm32/translate.c @@ -1137,10 +1137,10 @@ void gen_intermediate_code(CPUState *cs, Translatio= nBlock *tb, int max_insns) #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(pc_start)) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("\n"); log_target_disas(cs, pc_start, dc->pc - pc_start); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif } diff --git a/target/microblaze/translate.c b/target/microblaze/translate.c index 761f535357..cc1ad15656 100644 --- a/target/microblaze/translate.c +++ b/target/microblaze/translate.c @@ -1766,10 +1766,10 @@ void gen_intermediate_code(CPUState *cs, Translatio= nBlock *tb, int max_insns) #if !SIM_COMPAT if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(pc_start)) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("--------------\n"); log_target_disas(cs, pc_start, dc->pc - pc_start); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif #endif diff --git a/target/nios2/translate.c b/target/nios2/translate.c index e17656e66f..82107bf270 100644 --- a/target/nios2/translate.c +++ b/target/nios2/translate.c @@ -892,11 +892,11 @@ void gen_intermediate_code(CPUState *cs, TranslationB= lock *tb, int max_insns) #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(tb->pc)) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("IN: %s\n", lookup_symbol(tb->pc)); log_target_disas(cs, tb->pc, dc->pc - tb->pc); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif } diff --git a/target/tilegx/translate.c b/target/tilegx/translate.c index 68dd4aa2d8..fd406f4f71 100644 --- a/target/tilegx/translate.c +++ b/target/tilegx/translate.c @@ -2377,6 +2377,7 @@ void gen_intermediate_code(CPUState *cs, TranslationB= lock *tb, int max_insns) uint64_t pc_start =3D tb->pc; uint64_t page_start =3D pc_start & TARGET_PAGE_MASK; int num_insns =3D 0; + FILE *logfile =3D NULL; =20 dc->pc =3D pc_start; dc->mmuidx =3D 0; @@ -2388,7 +2389,7 @@ void gen_intermediate_code(CPUState *cs, TranslationB= lock *tb, int max_insns) dc->zero =3D NULL; =20 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) { - qemu_log_lock(); + logfile =3D qemu_log_lock(); qemu_log("IN: %s\n", lookup_symbol(pc_start)); } gen_tb_start(tb); @@ -2418,9 +2419,9 @@ void gen_intermediate_code(CPUState *cs, TranslationB= lock *tb, int max_insns) tb->size =3D dc->pc - pc_start; tb->icount =3D num_insns; =20 - if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) { + if (logfile) { qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } } =20 diff --git a/target/unicore32/translate.c b/target/unicore32/translate.c index 0e01f35856..0f6891b8aa 100644 --- a/target/unicore32/translate.c +++ b/target/unicore32/translate.c @@ -1994,12 +1994,12 @@ done_generating: #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(pc_start)) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("----------------\n"); qemu_log("IN: %s\n", lookup_symbol(pc_start)); log_target_disas(cs, pc_start, dc->pc - pc_start); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif tb->size =3D dc->pc - pc_start; diff --git a/tcg/tcg.c b/tcg/tcg.c index 5475d49ed1..0511266d85 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -1085,7 +1085,7 @@ void tcg_prologue_init(TCGContext *s) =20 #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("PROLOGUE: [size=3D%zu]\n", prologue_size); if (s->data_gen_ptr) { size_t code_size =3D s->data_gen_ptr - buf0; @@ -1110,7 +1110,7 @@ void tcg_prologue_init(TCGContext *s) } qemu_log("\n"); qemu_log_flush(); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif =20 @@ -4041,11 +4041,11 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *t= b) #ifdef DEBUG_DISAS if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP) && qemu_log_in_addr_range(tb->pc))) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("OP:\n"); tcg_dump_ops(s, false); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif =20 @@ -4086,11 +4086,11 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *t= b) #ifdef DEBUG_DISAS if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND) && qemu_log_in_addr_range(tb->pc))) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("OP before indirect lowering:\n"); tcg_dump_ops(s, false); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif /* Replace indirect temps with direct temps. */ @@ -4107,11 +4107,11 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *t= b) #ifdef DEBUG_DISAS if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT) && qemu_log_in_addr_range(tb->pc))) { - qemu_log_lock(); + FILE *logfile =3D qemu_log_lock(); qemu_log("OP after optimization and liveness analysis:\n"); tcg_dump_ops(s, true); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif =20 --=20 2.17.1 From nobody Sat Apr 27 07:09:54 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1573823700; cv=none; d=zoho.com; s=zohoarc; b=h+RBt8T67oZ+acVW2j/4E8O/GRPhussCAF0tJMyRzh6DSSfPFLwhmtjge+Wln0yeURoozomdmc1Xv9mflu1VRrtSeM3P+iwYFBXuqm3gITydra9VgWRgHBYRpVDDiJcv+AQ5va1qIz8eC+AjbgfFHY9zVwDxa4c6m8jl/Vpsdqk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573823700; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=PTGGv6BbjW5b5UzOcudWx+Llbn7h0ebnhlDgy3PCo6A=; b=OLND9mMOprnDoIMmgZNMcti9q3whK716fas0bkUOnFacnX0PRv31RTYHjDezRT+0mYAwR6XVWTGkSFlzzYk3sUWXu9g6YZUdcXmgUFb3kvbNpuU8G795H9S12p8lizVl3QJcFC8c4B8tSi6sv2b/0j8lzcI56S0Q4PSVLKwNBkc= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573823700799218.46357019645131; Fri, 15 Nov 2019 05:15:00 -0800 (PST) Received: from localhost ([::1]:39104 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbRD-0007ul-IF for importer@patchew.org; Fri, 15 Nov 2019 08:14:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49464) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbO4-0004Y2-RJ for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVbO2-0004Qg-Uj for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:44 -0500 Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]:34719) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iVbO2-0004QQ-Mq for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:42 -0500 Received: by mail-pl1-x642.google.com with SMTP id h13so4662115plr.1 for ; Fri, 15 Nov 2019 05:11:42 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id w138sm12007249pfc.68.2019.11.15.05.11.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2019 05:11:40 -0800 (PST) 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; bh=PTGGv6BbjW5b5UzOcudWx+Llbn7h0ebnhlDgy3PCo6A=; b=zcwcxRr5s0X8XKc7odSh9d6X2Igz6nn4qO5MehiFBPBFphEawTY1mbU0scFHRm6IxI Uk6v+djcJInqvJLPFTXqiWn+0hqOBQmnVUZCK9vXBFUMykNFqh5IJP9ABgCEwJr4UH0t fmglZFV4t8GY36Y3mozJjWLJuITP8Sq7xbCrYONMOrCzXZvcr8mNmrpUHK8NzH8DWIY7 S7fR4csxVZbFPe9rOXGgl2rHZc3EThahTzhhQXndIv1cZcWxBhb+jXwH0P9laXa/FgFJ QK7rQKlMoxqXf3gPrNDkTNfE3PnjbhaczxqiNWcISZI7LsuhDg0ZjWVpbiEtfXykhuO8 asLQ== 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; bh=PTGGv6BbjW5b5UzOcudWx+Llbn7h0ebnhlDgy3PCo6A=; b=D3/pf5sflyhzzPjz3Lm/ITJPAALTPHE3MsFg+pQVW8BMjV0eHVUIex16z9HrPb/Yz7 mAfi+LU14Xarl0b3YSMbaj55EKqDN6/IVR/RJ+u/jeT5M7V+3htEP+oG0eDoT8BLF3JX O8nNCVNCY1NY+30xcp40jQW0MWfiTKZxSJu9ErFnYmZMzwcEqXHFYkW6rU53NBACLchK S/5rfAFZJQfYmvjx4YUaWTJWDLKc2EIPaAF0EruYUAKqStpHed4ET6KaX41I+QeNVkms 7dgElvTZnqF3mxjRqFtXVDCQg3MIfCA3LL14VH0sjPSn/f/xaJvYy/Z8jv/N2gMMtcKx N7iA== X-Gm-Message-State: APjAAAX4/QiZbP5+CWaASq8Y8KP7QuGoD31Yv+xCGwC23mKZztRndqrX /rfon+lEjZwVsVwXsBnNTCOvLqG1zOY= X-Google-Smtp-Source: APXvYqyJgv5xOdEier+OC3ZqRlUpT34KvWKfkphYJL5AfXd+22GngfHnAV0ikfbuQ3fvNjgQlfAH2A== X-Received: by 2002:a17:90a:a483:: with SMTP id z3mr18699681pjp.55.1573823501265; Fri, 15 Nov 2019 05:11:41 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v2 5/6] Add use of RCU for qemu_logfile. Date: Fri, 15 Nov 2019 08:10:39 -0500 Message-Id: <20191115131040.2834-6-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191115131040.2834-1-robert.foley@linaro.org> References: <20191115131040.2834-1-robert.foley@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::642 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: peter.puhov@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This now allows changing the logfile while logging is active, and also solves the issue of a seg fault while changing the logfile. Any read access to the qemu_logfile handle will use the rcu_read_lock()/unlock() around the use of the handle. To fetch the handle we will use atomic_rcu_read(). We also in many cases do a check for validity of the logfile handle before using it to deal with the case where the file is closed and set to NULL. The cases where we write to the qemu_logfile will use atomic_rcu_set(). Writers will also use call_rcu() with a newly added qemu_logfile_free function for freeing/closing when readers have finished. Signed-off-by: Robert Foley Reviewed-by: Alex Benn=C3=A9e --- v2 - No specific changes, just merging in cleanup changes in qemu_set_log(= ). --- v1 - Changes for review comments. - Minor changes to definition of QemuLogFile. - Changed qemu_log_separate() to fix unbalanced and=20 remove qemu_log_enabled() check. - changed qemu_log_lock() to include else. - make qemu_logfile_free static. - use g_assert(logfile) in qemu_logfile_free. - Relocated unlock out of if/else in qemu_log_close(), and in qemu_set_log(). --- include/qemu/log.h | 42 +++++++++++++++++++++++---- util/log.c | 72 ++++++++++++++++++++++++++++++++-------------- include/exec/log.h | 33 ++++++++++++++++++--- tcg/tcg.c | 12 ++++++-- 4 files changed, 126 insertions(+), 33 deletions(-) diff --git a/include/qemu/log.h b/include/qemu/log.h index a7c5b01571..528e1f9dd7 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -3,9 +3,16 @@ =20 /* A small part of this API is split into its own header */ #include "qemu/log-for-trace.h" +#include "qemu/rcu.h" + +typedef struct QemuLogFile { + struct rcu_head rcu; + FILE *fd; +} QemuLogFile; =20 /* Private global variable, don't use */ -extern FILE *qemu_logfile; +extern QemuLogFile *qemu_logfile; + =20 /*=20 * The new API: @@ -25,7 +32,16 @@ static inline bool qemu_log_enabled(void) */ static inline bool qemu_log_separate(void) { - return qemu_logfile !=3D NULL && qemu_logfile !=3D stderr; + QemuLogFile *logfile; + bool res =3D false; + + rcu_read_lock(); + logfile =3D atomic_rcu_read(&qemu_logfile); + if (logfile && logfile->fd !=3D stderr) { + res =3D true; + } + rcu_read_unlock(); + return res; } =20 #define CPU_LOG_TB_OUT_ASM (1 << 0) @@ -55,14 +71,23 @@ static inline bool qemu_log_separate(void) =20 static inline FILE *qemu_log_lock(void) { - qemu_flockfile(qemu_logfile); - return logfile->fd; + QemuLogFile *logfile; + rcu_read_lock(); + logfile =3D atomic_rcu_read(&qemu_logfile); + if (logfile) { + qemu_flockfile(logfile->fd); + return logfile->fd; + } else { + rcu_read_unlock(); + return NULL; + } } =20 static inline void qemu_log_unlock(FILE *fd) { if (fd) { qemu_funlockfile(fd); + rcu_read_unlock(); } } =20 @@ -73,9 +98,14 @@ static inline void qemu_log_unlock(FILE *fd) static inline void GCC_FMT_ATTR(1, 0) qemu_log_vprintf(const char *fmt, va_list va) { - if (qemu_logfile) { - vfprintf(qemu_logfile, fmt, va); + QemuLogFile *logfile; + + rcu_read_lock(); + logfile =3D atomic_rcu_read(&qemu_logfile); + if (logfile) { + vfprintf(logfile->fd, fmt, va); } + rcu_read_unlock(); } =20 /* log only if a bit is set on the current loglevel mask: diff --git a/util/log.c b/util/log.c index 91ebb5c924..9f9b6b74b7 100644 --- a/util/log.c +++ b/util/log.c @@ -28,7 +28,7 @@ =20 static char *logfilename; static QemuMutex qemu_logfile_mutex; -FILE *qemu_logfile; +QemuLogFile *qemu_logfile; int qemu_loglevel; static int log_append =3D 0; static GArray *debug_regions; @@ -37,10 +37,14 @@ static GArray *debug_regions; int qemu_log(const char *fmt, ...) { int ret =3D 0; - if (qemu_logfile) { + QemuLogFile *logfile; + + rcu_read_lock(); + logfile =3D atomic_rcu_read(&qemu_logfile); + if (logfile) { va_list ap; va_start(ap, fmt); - ret =3D vfprintf(qemu_logfile, fmt, ap); + ret =3D vfprintf(logfile->fd, fmt, ap); va_end(ap); =20 /* Don't pass back error results. */ @@ -48,6 +52,7 @@ int qemu_log(const char *fmt, ...) ret =3D 0; } } + rcu_read_unlock(); return ret; } =20 @@ -56,12 +61,24 @@ static void __attribute__((__constructor__)) qemu_logfi= le_init(void) qemu_mutex_init(&qemu_logfile_mutex); } =20 +static void qemu_logfile_free(QemuLogFile *logfile) +{ + g_assert(logfile); + + if (logfile->fd !=3D stderr) { + fclose(logfile->fd); + } + g_free(logfile); +} + static bool log_uses_own_buffers; =20 /* enable or disable low levels log */ void qemu_set_log(int log_flags) { bool need_to_open_file =3D false; + QemuLogFile *logfile; + qemu_loglevel =3D log_flags; #ifdef CONFIG_TRACE_LOG qemu_loglevel |=3D LOG_TRACE; @@ -80,43 +97,47 @@ void qemu_set_log(int log_flags) g_assert(qemu_logfile_mutex.initialized); qemu_mutex_lock(&qemu_logfile_mutex); if (qemu_logfile && !need_to_open_file) { - qemu_mutex_unlock(&qemu_logfile_mutex); - qemu_log_close(); + logfile =3D qemu_logfile; + atomic_rcu_set(&qemu_logfile, NULL); + call_rcu(logfile, qemu_logfile_free, rcu); } else if (!qemu_logfile && need_to_open_file) { + logfile =3D g_new0(QemuLogFile, 1); if (logfilename) { - qemu_logfile =3D fopen(logfilename, log_append ? "a" : "w"); - if (!qemu_logfile) { + logfile->fd =3D fopen(logfilename, log_append ? "a" : "w"); + if (!logfile->fd) { + g_free(logfile); perror(logfilename); _exit(1); } /* In case we are a daemon redirect stderr to logfile */ if (is_daemonized()) { - dup2(fileno(qemu_logfile), STDERR_FILENO); - fclose(qemu_logfile); + dup2(fileno(logfile->fd), STDERR_FILENO); + fclose(logfile->fd); /* This will skip closing logfile in qemu_log_close() */ - qemu_logfile =3D stderr; + logfile->fd =3D stderr; } } else { /* Default to stderr if no log file specified */ assert(!is_daemonized()); - qemu_logfile =3D stderr; + logfile->fd =3D stderr; } /* must avoid mmap() usage of glibc by setting a buffer "by hand" = */ if (log_uses_own_buffers) { static char logfile_buf[4096]; =20 - setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)= ); + setvbuf(logfile->fd, logfile_buf, _IOLBF, sizeof(logfile_buf)); } else { #if defined(_WIN32) /* Win32 doesn't support line-buffering, so use unbuffered out= put. */ - setvbuf(qemu_logfile, NULL, _IONBF, 0); + setvbuf(logfile->fd, NULL, _IONBF, 0); #else - setvbuf(qemu_logfile, NULL, _IOLBF, 0); + setvbuf(logfile->fd, NULL, _IOLBF, 0); #endif log_append =3D 1; } - qemu_mutex_unlock(&qemu_logfile_mutex); + atomic_rcu_set(&qemu_logfile, logfile); } + qemu_mutex_unlock(&qemu_logfile_mutex); } =20 void qemu_log_needs_buffers(void) @@ -245,19 +266,28 @@ out: /* fflush() the log file */ void qemu_log_flush(void) { - fflush(qemu_logfile); + QemuLogFile *logfile; + + rcu_read_lock(); + logfile =3D atomic_rcu_read(&qemu_logfile); + if (logfile) { + fflush(logfile->fd); + } + rcu_read_unlock(); } =20 /* Close the log file */ void qemu_log_close(void) { + QemuLogFile *logfile; + g_assert(qemu_logfile_mutex.initialized); qemu_mutex_lock(&qemu_logfile_mutex); - if (qemu_logfile) { - if (qemu_logfile !=3D stderr) { - fclose(qemu_logfile); - } - qemu_logfile =3D NULL; + logfile =3D qemu_logfile; + + if (logfile) { + atomic_rcu_set(&qemu_logfile, NULL); + call_rcu(logfile, qemu_logfile_free, rcu); } qemu_mutex_unlock(&qemu_logfile_mutex); } diff --git a/include/exec/log.h b/include/exec/log.h index e2cfd436e6..9bd1e4aa20 100644 --- a/include/exec/log.h +++ b/include/exec/log.h @@ -15,8 +15,15 @@ */ static inline void log_cpu_state(CPUState *cpu, int flags) { + QemuLogFile *logfile; + if (qemu_log_enabled()) { - cpu_dump_state(cpu, qemu_logfile, flags); + rcu_read_lock(); + logfile =3D atomic_rcu_read(&qemu_logfile); + if (logfile) { + cpu_dump_state(cpu, logfile->fd, flags); + } + rcu_read_unlock(); } } =20 @@ -40,19 +47,37 @@ static inline void log_cpu_state_mask(int mask, CPUStat= e *cpu, int flags) static inline void log_target_disas(CPUState *cpu, target_ulong start, target_ulong len) { - target_disas(qemu_logfile, cpu, start, len); + QemuLogFile *logfile; + rcu_read_lock(); + logfile =3D atomic_rcu_read(&qemu_logfile); + if (logfile) { + target_disas(logfile->fd, cpu, start, len); + } + rcu_read_unlock(); } =20 static inline void log_disas(void *code, unsigned long size) { - disas(qemu_logfile, code, size); + QemuLogFile *logfile; + rcu_read_lock(); + logfile =3D atomic_rcu_read(&qemu_logfile); + if (logfile) { + disas(logfile->fd, code, size); + } + rcu_read_unlock(); } =20 #if defined(CONFIG_USER_ONLY) /* page_dump() output to the log file: */ static inline void log_page_dump(void) { - page_dump(qemu_logfile); + QemuLogFile *logfile; + rcu_read_lock(); + logfile =3D atomic_rcu_read(&qemu_logfile); + if (logfile) { + page_dump(logfile->fd); + } + rcu_read_unlock(); } #endif #endif diff --git a/tcg/tcg.c b/tcg/tcg.c index 0511266d85..4f616ba38b 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -2114,9 +2114,17 @@ static void tcg_dump_ops(TCGContext *s, bool have_pr= efs) } =20 if (have_prefs || op->life) { - for (; col < 40; ++col) { - putc(' ', qemu_logfile); + + QemuLogFile *logfile; + + rcu_read_lock(); + logfile =3D atomic_rcu_read(&qemu_logfile); + if (logfile) { + for (; col < 40; ++col) { + putc(' ', logfile->fd); + } } + rcu_read_unlock(); } =20 if (op->life) { --=20 2.17.1 From nobody Sat Apr 27 07:09:54 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1573824029; cv=none; d=zoho.com; s=zohoarc; b=GMZAjSBuoXNKnRjltUcjgw4qGCsfmgBiwuxOQRcRs+Yyb7fBtVzz8bB8vIbyS99sw1J74kkvq8eIwu/733TOF6YpAuWMhO6ZEsQ36GLM8pzrLaqjT+grnKHUURnZzKWDb4Hu+isSThA8cLB+nRGu7rlAWTpz80AK49AYfyd7vxY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573824029; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=l2vROXNnUFY1UYlqir6gW1pq2UjcxHQai6CiYsBYIgQ=; b=ePzmSvAp0dfH8uSivzQoB6uRs+mu8jW/SCRtv4SJL+DwXqgll7aKqHxfLtytRP/N3kZbHnrwjtcMztol9eo6N+QqFkRbBR2bVHpwja7Vz82uXbHLHwC4xREhpBLFdizhekoWuZZQp6TRCnm41zok5Tnk5eEMqTcnWdSHuKgv3T0= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573824029763808.8261083038401; Fri, 15 Nov 2019 05:20:29 -0800 (PST) Received: from localhost ([::1]:39172 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbWW-0004Bn-Ic for importer@patchew.org; Fri, 15 Nov 2019 08:20:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49473) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVbO5-0004ZS-JH for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVbO4-0004RG-Ba for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:45 -0500 Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]:44255) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iVbO4-0004R0-5Y for qemu-devel@nongnu.org; Fri, 15 Nov 2019 08:11:44 -0500 Received: by mail-pl1-x642.google.com with SMTP id az9so4638036plb.11 for ; Fri, 15 Nov 2019 05:11:44 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id w138sm12007249pfc.68.2019.11.15.05.11.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2019 05:11:42 -0800 (PST) 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=l2vROXNnUFY1UYlqir6gW1pq2UjcxHQai6CiYsBYIgQ=; b=XhktTi7O5+7Mh00Z0888n3UAedn6Aa5bfPNd1m3szElA408i8Ykoyc5EOBxTZptH9U ogwOD4jWuWOG7EX5UYmI8eGmdGw+74fbo0X96xmoVj9zPhsLh3soZfTtZ8527EikYKAH hc9yEK+cZGYEvBAISOX6M7zfztJcYl8nDdXxMwcooZ9a2SdaR9oGlI+tzOalHUpZ1OTM jc0qQIL7o2e3CIITr0RghPuOuG1behMDb4pCVwIggls8zf+Q7ur+/gMcNgWSXa0Qy2I2 z5JPTEZKEuBlXatiT1NX22J67hHG4bEmpQnsQR3yCSh94tIeOcQOPpfuVx9YlGR5CZVI tFpg== 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=l2vROXNnUFY1UYlqir6gW1pq2UjcxHQai6CiYsBYIgQ=; b=CUap0RRcFLQrBh6EHUiP5abY63xXdStirVetUJaSI1bwuDXl1rT+SzG2J23TwHiwTP F9P/ur/4UnSQzuuLxES2S/ih+lLYB9EnfAAdGWBDKs9ImN8gNWeWr3eb1/q2mvpp3B3n 6/gMJyRYfkQaQmQP80ewAGN+HXP4u9IOY3m3kW2AtMVJKf2vSiRKOSKBO0GYPJ3MkRs+ i6jKB773HT5NvsASaUw8SCOYTnG74hnFje9+Mv08TDRdburGEFQXNakSufyg9dl9UB1k /bd2EOvGoMjUN0zCySf3mpr2tzp7bkDl2KCJd9Fv8JZvKazu6bhKTDPFjr32VUeO2I5l 3Z8Q== X-Gm-Message-State: APjAAAV6AqczT1RBmzDpBQiXGQfYoPPNfb5Q1d8+z2Ch41cpjncLuxGc e6+rIT/+lL4CWC/Gj4aJYEkOoV5WSOU= X-Google-Smtp-Source: APXvYqzivQOTJ4Af1BvDdiuQCytg3NpTusky1x+ljFEzl8cB2N24NDKM4M1gj+FAf/7ZP/ely08tuA== X-Received: by 2002:a17:90a:348c:: with SMTP id p12mr19372857pjb.105.1573823502875; Fri, 15 Nov 2019 05:11:42 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v2 6/6] Added tests for close and change of logfile. Date: Fri, 15 Nov 2019 08:10:40 -0500 Message-Id: <20191115131040.2834-7-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191115131040.2834-1-robert.foley@linaro.org> References: <20191115131040.2834-1-robert.foley@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::642 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: peter.puhov@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) One test ensures that the logfile handle is still valid even if the logfile is changed during logging. The other test validates that the logfile handle remains valid under the logfile lock even if the logfile is closed. Signed-off-by: Robert Foley Reviewed-by: Alex Benn=C3=A9e --- v1 - Changes for first round of code review comments. - Added in use of g_autofree, removed the g_free()s. - Added in use of logfile2 and changed sequence of asserts. --- tests/test-logging.c | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tests/test-logging.c b/tests/test-logging.c index a12585f70a..1e646f045d 100644 --- a/tests/test-logging.c +++ b/tests/test-logging.c @@ -108,6 +108,82 @@ static void test_parse_path(gconstpointer data) error_free_or_abort(&err); } =20 +static void test_logfile_write(gconstpointer data) +{ + QemuLogFile *logfile; + QemuLogFile *logfile2; + gchar const *dir =3D data; + Error *err =3D NULL; + g_autofree gchar *file_path; + g_autofree gchar *file_path1; + FILE *orig_fd; + + /* + * Before starting test, set log flags, to ensure the file gets + * opened below with the call to qemu_set_log_filename(). + * In cases where a logging backend other than log is used, + * this is needed. + */ + qemu_set_log(CPU_LOG_TB_OUT_ASM); + file_path =3D g_build_filename(dir, "qemu_test_log_write0.log", NULL); + file_path1 =3D g_build_filename(dir, "qemu_test_log_write1.log", NULL); + + /* + * Test that even if an open file handle is changed, + * our handle remains valid due to RCU. + */ + qemu_set_log_filename(file_path, &err); + g_assert(!err); + rcu_read_lock(); + logfile =3D atomic_rcu_read(&qemu_logfile); + orig_fd =3D logfile->fd; + g_assert(logfile && logfile->fd); + fprintf(logfile->fd, "%s 1st write to file\n", __func__); + fflush(logfile->fd); + + /* Change the logfile and ensure that the handle is still valid. */ + qemu_set_log_filename(file_path1, &err); + g_assert(!err); + logfile2 =3D atomic_rcu_read(&qemu_logfile); + g_assert(logfile->fd =3D=3D orig_fd); + g_assert(logfile2->fd !=3D logfile->fd); + fprintf(logfile->fd, "%s 2nd write to file\n", __func__); + fflush(logfile->fd); + rcu_read_unlock(); +} + +static void test_logfile_lock(gconstpointer data) +{ + FILE *logfile; + gchar const *dir =3D data; + Error *err =3D NULL; + g_autofree gchar *file_path; + + file_path =3D g_build_filename(dir, "qemu_test_logfile_lock0.log", NUL= L); + + /* + * Test the use of the logfile lock, such + * that even if an open file handle is closed, + * our handle remains valid for use due to RCU. + */ + qemu_set_log_filename(file_path, &err); + logfile =3D qemu_log_lock(); + g_assert(logfile); + fprintf(logfile, "%s 1st write to file\n", __func__); + fflush(logfile); + + /* + * Initiate a close file and make sure our handle remains + * valid since we still have the logfile lock. + */ + qemu_log_close(); + fprintf(logfile, "%s 2nd write to file\n", __func__); + fflush(logfile); + qemu_log_unlock(logfile); + + g_assert(!err); +} + /* Remove a directory and all its entries (non-recursive). */ static void rmdir_full(gchar const *root) { @@ -134,6 +210,10 @@ int main(int argc, char **argv) =20 g_test_add_func("/logging/parse_range", test_parse_range); g_test_add_data_func("/logging/parse_path", tmp_path, test_parse_path); + g_test_add_data_func("/logging/logfile_write_path", + tmp_path, test_logfile_write); + g_test_add_data_func("/logging/logfile_lock_path", + tmp_path, test_logfile_lock); =20 rc =3D g_test_run(); =20 --=20 2.17.1