From nobody Wed Nov 5 14:46:49 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1535727622029481.2794342709635; Fri, 31 Aug 2018 08:00:22 -0700 (PDT) Received: from localhost ([::1]:54179 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fvkuK-0000rT-Sx for importer@patchew.org; Fri, 31 Aug 2018 11:00:20 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60524) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fvkny-0002aH-Ca for qemu-devel@nongnu.org; Fri, 31 Aug 2018 10:53:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fvknw-0005Rf-Tg for qemu-devel@nongnu.org; Fri, 31 Aug 2018 10:53:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46012) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fvknt-0005Ox-Fx; Fri, 31 Aug 2018 10:53:41 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D2B9A85540; Fri, 31 Aug 2018 14:53:40 +0000 (UTC) Received: from localhost (ovpn-117-133.phx2.redhat.com [10.3.117.133]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82044106A784; Fri, 31 Aug 2018 14:53:36 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 31 Aug 2018 16:53:12 +0200 Message-Id: <20180831145314.14736-2-marcandre.lureau@redhat.com> In-Reply-To: <20180831145314.14736-1-marcandre.lureau@redhat.com> References: <20180831145314.14736-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 31 Aug 2018 14:53:40 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/3] util: add qemu_write_pidfile() 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: Fam Zheng , qemu-block@nongnu.org, Stefan Weil , Michael Roth , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" There are variants of qemu_create_pidfile() in qemu-pr-helper and qemu-ga. Let's have a common implementation in libqemuutil. The code is initially based from pr-helper write_pidfile(), with various improvements and suggestions from Daniel Berrang=C3=A9: QEMU will leave the pidfile existing on disk when it exits which initially made me think it avoids the deletion race. The app managing QEMU, however, may well delete the pidfile after it has seen QEMU exit, and even if the app locks the pidfile before deleting it, there is still a race. eg consider the following sequence QEMU 1 libvirtd QEMU 2 1. lock(pidfile) 2. exit() 3. open(pidfile) 4. lock(pidfile) 5. open(pidfile) 6. unlink(pidfile) 7. close(pidfile) 8. lock(pidfile) IOW, at step 8 the new QEMU has successfully acquired the lock, but the pidfile no longer exists on disk because it was deleted after the original QEMU exited. While we could just say no external app should ever delete the pidfile, I don't think that is satisfactory as people don't read docs, and admins don't like stale pidfiles being left around on disk. To make this robust, I think we might want to copy libvirt's approach to pidfile acquisition which runs in a loop and checks that the file on disk /after/ acquiring the lock matches the file that was locked. Then we could in fact safely let QEMU delete its own pidfiles on clean exit.. Signed-off-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Daniel P. Berrang=C3=A9 --- include/qemu/osdep.h | 3 +- os-posix.c | 24 --------------- os-win32.c | 25 ---------------- qga/main.c | 54 +++++++--------------------------- scsi/qemu-pr-helper.c | 40 ++++--------------------- util/oslib-posix.c | 68 +++++++++++++++++++++++++++++++++++++++++++ util/oslib-win32.c | 27 +++++++++++++++++ vl.c | 4 +-- 8 files changed, 114 insertions(+), 131 deletions(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index a91068df0e..47fa570bd4 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -448,7 +448,8 @@ bool qemu_has_ofd_lock(void); #define FMT_pid "%d" #endif =20 -int qemu_create_pidfile(const char *filename); +bool qemu_write_pidfile(const char *pidfile, Error **errp); + int qemu_get_thread_id(void); =20 #ifndef CONFIG_IOVEC diff --git a/os-posix.c b/os-posix.c index 9ce6f74513..0e9403b4ff 100644 --- a/os-posix.c +++ b/os-posix.c @@ -352,30 +352,6 @@ void os_set_line_buffering(void) setvbuf(stdout, NULL, _IOLBF, 0); } =20 -int qemu_create_pidfile(const char *filename) -{ - char buffer[128]; - int len; - int fd; - - fd =3D qemu_open(filename, O_RDWR | O_CREAT, 0600); - if (fd =3D=3D -1) { - return -1; - } - if (lockf(fd, F_TLOCK, 0) =3D=3D -1) { - close(fd); - return -1; - } - len =3D snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid()); - if (write(fd, buffer, len) !=3D len) { - close(fd); - return -1; - } - - /* keep pidfile open & locked forever */ - return 0; -} - bool is_daemonized(void) { return daemonize; diff --git a/os-win32.c b/os-win32.c index 0674f94b57..0e0d7f50f3 100644 --- a/os-win32.c +++ b/os-win32.c @@ -97,28 +97,3 @@ int os_parse_cmd_args(int index, const char *optarg) { return -1; } - -int qemu_create_pidfile(const char *filename) -{ - char buffer[128]; - int len; - HANDLE file; - OVERLAPPED overlap; - BOOL ret; - memset(&overlap, 0, sizeof(overlap)); - - file =3D CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL, - OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - - if (file =3D=3D INVALID_HANDLE_VALUE) { - return -1; - } - len =3D snprintf(buffer, sizeof(buffer), "%d\n", getpid()); - ret =3D WriteFile(file, (LPCVOID)buffer, (DWORD)len, - NULL, &overlap); - CloseHandle(file); - if (ret =3D=3D 0) { - return -1; - } - return 0; -} diff --git a/qga/main.c b/qga/main.c index 6d70242d05..c399320d3c 100644 --- a/qga/main.c +++ b/qga/main.c @@ -340,46 +340,6 @@ static FILE *ga_open_logfile(const char *logfile) return f; } =20 -#ifndef _WIN32 -static bool ga_open_pidfile(const char *pidfile) -{ - int pidfd; - char pidstr[32]; - - pidfd =3D qemu_open(pidfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR); - if (pidfd =3D=3D -1 || lockf(pidfd, F_TLOCK, 0)) { - g_critical("Cannot lock pid file, %s", strerror(errno)); - if (pidfd !=3D -1) { - close(pidfd); - } - return false; - } - - if (ftruncate(pidfd, 0)) { - g_critical("Failed to truncate pid file"); - goto fail; - } - snprintf(pidstr, sizeof(pidstr), "%d\n", getpid()); - if (write(pidfd, pidstr, strlen(pidstr)) !=3D strlen(pidstr)) { - g_critical("Failed to write pid file"); - goto fail; - } - - /* keep pidfile open & locked forever */ - return true; - -fail: - unlink(pidfile); - close(pidfd); - return false; -} -#else /* _WIN32 */ -static bool ga_open_pidfile(const char *pidfile) -{ - return true; -} -#endif - static gint ga_strcmp(gconstpointer str1, gconstpointer str2) { return strcmp(str1, str2); @@ -479,8 +439,11 @@ void ga_unset_frozen(GAState *s) ga_enable_logging(s); g_warning("logging re-enabled due to filesystem unfreeze"); if (s->deferred_options.pid_filepath) { - if (!ga_open_pidfile(s->deferred_options.pid_filepath)) { - g_warning("failed to create/open pid file"); + Error *err =3D NULL; + + if (!qemu_write_pidfile(s->deferred_options.pid_filepath, &err)) { + g_warning("%s", error_get_pretty(err)); + error_free(err); } s->deferred_options.pid_filepath =3D NULL; } @@ -515,8 +478,11 @@ static void become_daemon(const char *pidfile) } =20 if (pidfile) { - if (!ga_open_pidfile(pidfile)) { - g_critical("failed to create pidfile"); + Error *err =3D NULL; + + if (!qemu_write_pidfile(pidfile, &err)) { + g_critical("%s", error_get_pretty(err)); + error_free(err); exit(EXIT_FAILURE); } } diff --git a/scsi/qemu-pr-helper.c b/scsi/qemu-pr-helper.c index ed037aabee..ce40008bfc 100644 --- a/scsi/qemu-pr-helper.c +++ b/scsi/qemu-pr-helper.c @@ -117,39 +117,6 @@ QEMU_COPYRIGHT "\n" , name); } =20 -static void write_pidfile(void) -{ - int pidfd; - char pidstr[32]; - - pidfd =3D qemu_open(pidfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR); - if (pidfd =3D=3D -1) { - error_report("Cannot open pid file, %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - if (lockf(pidfd, F_TLOCK, 0)) { - error_report("Cannot lock pid file, %s", strerror(errno)); - goto fail; - } - if (ftruncate(pidfd, 0)) { - error_report("Failed to truncate pid file"); - goto fail; - } - - snprintf(pidstr, sizeof(pidstr), "%d\n", getpid()); - if (write(pidfd, pidstr, strlen(pidstr)) !=3D strlen(pidstr)) { - error_report("Failed to write pid file"); - goto fail; - } - return; - -fail: - unlink(pidfile); - close(pidfd); - exit(EXIT_FAILURE); -} - /* SG_IO support */ =20 typedef struct PRHelperSGIOData { @@ -1080,8 +1047,11 @@ int main(int argc, char **argv) } } =20 - if (daemonize || pidfile_specified) - write_pidfile(); + if ((daemonize || pidfile_specified) && + !qemu_write_pidfile(pidfile, &local_err)) { + error_report_err(local_err); + exit(EXIT_FAILURE); + } =20 #ifdef CONFIG_LIBCAP if (drop_privileges() < 0) { diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 13b6f8d776..0e3ab9d959 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -88,6 +88,74 @@ int qemu_daemon(int nochdir, int noclose) return daemon(nochdir, noclose); } =20 +bool qemu_write_pidfile(const char *path, Error **errp) +{ + int fd; + char pidstr[32]; + + while (1) { + struct stat a, b; + + fd =3D qemu_open(path, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); + if (fd =3D=3D -1) { + error_setg_errno(errp, errno, "Cannot open pid file"); + return false; + } + + if (fstat(fd, &b) < 0) { + error_setg_errno(errp, errno, "Cannot stat file"); + goto fail_close; + } + + if (lockf(fd, F_TLOCK, 0) < 0) { + error_setg_errno(errp, errno, "Cannot lock pid file"); + goto fail_close; + } + + /* + * Now make sure the path we locked is the same one that now + * exists on the filesystem. + */ + if (stat(path, &a) < 0) { + /* + * PID file disappeared, someone else must be racing with + * us, so try again. + */ + close(fd); + continue; + } + + if (a.st_ino =3D=3D b.st_ino) { + break; + } + + /* + * PID file was recreated, someone else must be racing with + * us, so try again. + */ + close(fd); + } + + if (ftruncate(fd, 0) < 0) { + error_setg_errno(errp, errno, "Failed to truncate pid file"); + goto fail_unlink; + } + + snprintf(pidstr, sizeof(pidstr), FMT_pid "\n", getpid()); + if (write(fd, pidstr, strlen(pidstr)) !=3D strlen(pidstr)) { + error_setg(errp, "Failed to write pid file"); + goto fail_unlink; + } + + return true; + +fail_unlink: + unlink(path); +fail_close: + close(fd); + return false; +} + void *qemu_oom_check(void *ptr) { if (ptr =3D=3D NULL) { diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 25dd1595ad..a83fc24a33 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -776,3 +776,30 @@ ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size= _t len, int flags, } return ret; } + +bool qemu_write_pidfile(const char *filename, Error **errp) +{ + char buffer[128]; + int len; + HANDLE file; + OVERLAPPED overlap; + BOOL ret; + memset(&overlap, 0, sizeof(overlap)); + + file =3D CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL, + OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + + if (file =3D=3D INVALID_HANDLE_VALUE) { + error_setg(errp, "Failed to create PID file"); + return false; + } + len =3D snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid()); + ret =3D WriteFile(file, (LPCVOID)buffer, (DWORD)len, + NULL, &overlap); + CloseHandle(file); + if (ret =3D=3D 0) { + error_setg(errp, "Failed to write PID file"); + return false; + } + return true; +} diff --git a/vl.c b/vl.c index 5ba06adf78..0eaf948d32 100644 --- a/vl.c +++ b/vl.c @@ -3996,8 +3996,8 @@ int main(int argc, char **argv, char **envp) os_daemonize(); rcu_disable_atfork(); =20 - if (pid_file && qemu_create_pidfile(pid_file) !=3D 0) { - error_report("could not acquire pid file: %s", strerror(errno)); + if (pid_file && !qemu_write_pidfile(pid_file, &err)) { + error_reportf_err(err, "cannot create PID file: "); exit(1); } =20 --=20 2.19.0.rc1 From nobody Wed Nov 5 14:46:49 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1535727747315345.4749512341956; Fri, 31 Aug 2018 08:02:27 -0700 (PDT) Received: from localhost ([::1]:54198 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fvkwK-00028b-C0 for importer@patchew.org; Fri, 31 Aug 2018 11:02:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60656) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fvko7-0002ic-MO for qemu-devel@nongnu.org; Fri, 31 Aug 2018 10:53:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fvko7-0005aa-13 for qemu-devel@nongnu.org; Fri, 31 Aug 2018 10:53:55 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48178) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fvko1-0005Ug-Qk; Fri, 31 Aug 2018 10:53:49 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2885D81256; Fri, 31 Aug 2018 14:53:49 +0000 (UTC) Received: from localhost (ovpn-117-133.phx2.redhat.com [10.3.117.133]) by smtp.corp.redhat.com (Postfix) with ESMTP id E604C65F78; Fri, 31 Aug 2018 14:53:41 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 31 Aug 2018 16:53:13 +0200 Message-Id: <20180831145314.14736-3-marcandre.lureau@redhat.com> In-Reply-To: <20180831145314.14736-1-marcandre.lureau@redhat.com> References: <20180831145314.14736-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 31 Aug 2018 14:53:49 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/3] util: use fcntl() for qemu_write_pidfile() locking 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: Fam Zheng , qemu-block@nongnu.org, Stefan Weil , Michael Roth , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Daniel Berrang=C3=A9 suggested to use fcntl() locks rather than lockf(). 'man lockf': On Linux, lockf() is just an interface on top of fcntl(2) locking. Many other systems implement lockf() in this way, but note that POSIX.1 leaves the relationship between lockf() and fcntl(2) locks unspecified. A portable application should probably avoid mixing calls to these interfaces. IOW, if its just a shim around fcntl() on many systems, it is clearer if we just use fcntl() directly, as we then know how fcntl() locks will behave if they're on a network filesystem like NFS. Suggested-by: Daniel P. Berrang=C3=A9 Signed-off-by: Marc-Andr=C3=A9 Lureau --- util/oslib-posix.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 0e3ab9d959..fbd0dc8c57 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -95,6 +95,11 @@ bool qemu_write_pidfile(const char *path, Error **errp) =20 while (1) { struct stat a, b; + struct flock lock =3D { + .l_type =3D F_WRLCK, + .l_whence =3D SEEK_SET, + .l_len =3D 0, + }; =20 fd =3D qemu_open(path, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); if (fd =3D=3D -1) { @@ -107,7 +112,7 @@ bool qemu_write_pidfile(const char *path, Error **errp) goto fail_close; } =20 - if (lockf(fd, F_TLOCK, 0) < 0) { + if (fcntl(fd, F_SETLK, &lock)) { error_setg_errno(errp, errno, "Cannot lock pid file"); goto fail_close; } --=20 2.19.0.rc1 From nobody Wed Nov 5 14:46:49 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1535727748898130.5637034638953; Fri, 31 Aug 2018 08:02:28 -0700 (PDT) Received: from localhost ([::1]:54199 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fvkwN-00029s-T2 for importer@patchew.org; Fri, 31 Aug 2018 11:02:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60647) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fvko7-0002iQ-Eo for qemu-devel@nongnu.org; Fri, 31 Aug 2018 10:53:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fvko6-0005ZM-NG for qemu-devel@nongnu.org; Fri, 31 Aug 2018 10:53:55 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35994) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fvko3-0005WM-AR; Fri, 31 Aug 2018 10:53:51 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 985EC86676; Fri, 31 Aug 2018 14:53:50 +0000 (UTC) Received: from localhost (ovpn-117-133.phx2.redhat.com [10.3.117.133]) by smtp.corp.redhat.com (Postfix) with ESMTP id 387F9106A784; Fri, 31 Aug 2018 14:53:50 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 31 Aug 2018 16:53:14 +0200 Message-Id: <20180831145314.14736-4-marcandre.lureau@redhat.com> In-Reply-To: <20180831145314.14736-1-marcandre.lureau@redhat.com> References: <20180831145314.14736-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 31 Aug 2018 14:53:50 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 3/3] RFC: delete PID file on exit 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: Fam Zheng , qemu-block@nongnu.org, Stefan Weil , Michael Roth , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Register an exit handler to remove the PID file. By the time atexit() is called, qemu_write_pidfile() guarantees QEMU owns the PID file, thus we could safely remove it when exiting. Signed-off-by: Marc-Andr=C3=A9 Lureau --- vl.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/vl.c b/vl.c index 0eaf948d32..743679ddb7 100644 --- a/vl.c +++ b/vl.c @@ -2603,6 +2603,15 @@ static void qemu_run_exit_notifiers(void) notifier_list_notify(&exit_notifiers, NULL); } =20 +static const char *pid_file; + +static void qemu_unlink_pidfile(void) +{ + if (pid_file) { + unlink(pid_file); + } +} + bool machine_init_done; =20 void qemu_add_machine_init_done_notifier(Notifier *notify) @@ -2927,7 +2936,6 @@ int main(int argc, char **argv, char **envp) const char *vga_model =3D NULL; const char *qtest_chrdev =3D NULL; const char *qtest_log =3D NULL; - const char *pid_file =3D NULL; const char *incoming =3D NULL; bool userconfig =3D true; bool nographic =3D false; @@ -4000,6 +4008,7 @@ int main(int argc, char **argv, char **envp) error_reportf_err(err, "cannot create PID file: "); exit(1); } + atexit(qemu_unlink_pidfile); =20 if (qemu_init_main_loop(&main_loop_err)) { error_report_err(main_loop_err); --=20 2.19.0.rc1