From nobody Sat Oct 25 21:42:48 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1521567297695331.54952807041093; Tue, 20 Mar 2018 10:34:57 -0700 (PDT) Received: from localhost ([::1]:50908 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eyL9r-0006gb-96 for importer@patchew.org; Tue, 20 Mar 2018 13:34:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50466) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eyL8u-0006HY-OY for qemu-devel@nongnu.org; Tue, 20 Mar 2018 13:33:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eyL8r-0006TM-BX for qemu-devel@nongnu.org; Tue, 20 Mar 2018 13:33:48 -0400 Received: from nyx.n621.de ([176.9.182.227]:35625) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eyL8r-0006Ss-5E for qemu-devel@nongnu.org; Tue, 20 Mar 2018 13:33:45 -0400 From: Florian Larysch To: qemu-devel@nongnu.org Date: Tue, 20 Mar 2018 18:33:24 +0100 Message-Id: <20180320173324.30844-1-fl@n621.de> X-Mailer: git-send-email 2.16.2 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 176.9.182.227 Subject: [Qemu-devel] [PATCH v3] os: truncate pidfile on creation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: sw@weilnetz.de, Florian Larysch Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" qemu_create_pidfile does not truncate the pidfile when it creates it, but rather overwrites its contents with the new pid. This works fine as long as the length of the pid doesn't decrease, but this might happen in case of wraparounds, causing pidfiles to contain trailing garbage which breaks operations such as 'kill $(cat pidfile)'. Instead, always truncate the file before writing it. Note that the order is important here: We cannot simply use O_TRUNC in the open() call because another qemu process might truncate the pidfile of a process that is still running before reaching the lockf() barrier. The Windows version suffers from a similar problem, but as it does not provide effective mutual exclusion anyway (because the file handle is closed immediately after writing to it), adopting this behavior still seems to be an improvement, as it at least prevents garbled pidfiles. Signed-off-by: Florian Larysch --- os-posix.c | 6 ++++++ os-win32.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/os-posix.c b/os-posix.c index b9c2343b1e..f2318aef55 100644 --- a/os-posix.c +++ b/os-posix.c @@ -309,6 +309,12 @@ int qemu_create_pidfile(const char *filename) close(fd); return -1; } + + if (ftruncate(fd, 0)) { + close(fd); + return -1; + } + len =3D snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid()); if (write(fd, buffer, len) !=3D len) { close(fd); diff --git a/os-win32.c b/os-win32.c index 586a7c7d49..85dbad7af8 100644 --- a/os-win32.c +++ b/os-win32.c @@ -108,7 +108,7 @@ int qemu_create_pidfile(const char *filename) memset(&overlap, 0, sizeof(overlap)); =20 file =3D CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL, - OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); =20 if (file =3D=3D INVALID_HANDLE_VALUE) { return -1; --=20 2.16.2