From patchwork Tue Mar 20 09:31:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Larysch X-Patchwork-Id: 888216 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=n621.de Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 405DLt3kdYz9s0w for ; Wed, 21 Mar 2018 00:29:58 +1100 (AEDT) Received: from localhost ([::1]:48537 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eyHKu-0001Az-An for incoming@patchwork.ozlabs.org; Tue, 20 Mar 2018 09:29:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52229) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eyDcY-0001v6-0b for qemu-devel@nongnu.org; Tue, 20 Mar 2018 05:31:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eyDcX-0004Am-3f for qemu-devel@nongnu.org; Tue, 20 Mar 2018 05:31:54 -0400 Received: from nyx.n621.de ([2a01:4f8:151:8ffd:2::1]:57725) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eyDcW-00048d-TO for qemu-devel@nongnu.org; Tue, 20 Mar 2018 05:31:53 -0400 From: Florian Larysch To: qemu-devel@nongnu.org Date: Tue, 20 Mar 2018 10:31:35 +0100 Message-Id: <20180320093135.26422-1-fl@n621.de> X-Mailer: git-send-email 2.16.2 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 2a01:4f8:151:8ffd:2::1 X-Mailman-Approved-At: Tue, 20 Mar 2018 09:27:42 -0400 Subject: [Qemu-devel] [PATCH] 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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. Signed-off-by: Florian Larysch Reviewed-by: Eric Blake --- os-posix.c | 2 +- os-win32.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/os-posix.c b/os-posix.c index b9c2343b1e..9a6b874180 100644 --- a/os-posix.c +++ b/os-posix.c @@ -301,7 +301,7 @@ int qemu_create_pidfile(const char *filename) int len; int fd; - fd = qemu_open(filename, O_RDWR | O_CREAT, 0600); + fd = qemu_open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600); if (fd == -1) { return -1; } 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)); file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL, - OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (file == INVALID_HANDLE_VALUE) { return -1;