Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KyUj4-0006js-G6 for qemu-devel@nongnu.org; Fri, 07 Nov 2008 12:02:27 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KyUj0-0006go-Pv for qemu-devel@nongnu.org; Fri, 07 Nov 2008 12:02:25 -0500 Received: from [199.232.76.173] (port=59015 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KyUiz-0006fh-By for qemu-devel@nongnu.org; Fri, 07 Nov 2008 12:02:21 -0500 Received: from ey-out-1920.google.com ([74.125.78.149]:39045) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KyUiz-0007Uj-05 for qemu-devel@nongnu.org; Fri, 07 Nov 2008 12:02:21 -0500 Received: by ey-out-1920.google.com with SMTP id 4so573093eyk.4 for ; Fri, 07 Nov 2008 09:02:19 -0800 (PST) Date: Fri, 7 Nov 2008 17:02:15 +0000 From: Jim Bailey Message-ID: <20081107170215.7ca43a08@dgym.ktulu> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] bug writing pidfile under unix (and fix) Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Hello, In qemu_create_pidfile (osdep.c:229) the current pid and a newline is written to the pidfile. However, the pidfile isn't truncated, so if it is longer than the length of the pid and the newline character you get trailing junk that can really mess up scripts. I noticed this when going from a 5 digit pid to a 3 digit pid, so it can happen in regular operation, especially if the OS randomizes pids. Truncating the file fixes the bug. dgym *** osdep.c.orig Fri Nov 7 16:56:12 2008 --- osdep.c Fri Nov 7 16:58:49 2008 *************** *** 236,241 **** --- 236,243 ---- len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); if (write(fd, buffer, len) != len) return -1; + + ftruncate(fd, len); #else HANDLE file; DWORD flags;