Skip to contentSkip to conversation

vl.c: move pidfile creation up the line

PATCH Applied Michael Tokarev 4 participants 4 messages Nov 2, 2016 – Nov 3, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vl.c: move pidfile creation up the line

With current code, pid file is open after various sockets, chardevs, fsdevs and the like. This causes interesting effects, for example when monitor is a unix-socket, and another qemu instance is already running, new qemu first "damages" the socket and next complain that it can't acquire the pid file and exits, making running qemu unreachable.

Move pid file creation earlier, right after the call to os_daemonize(), where we know our process id (pid).

---
 vl.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

v2: move the pid file creation even earlier, as per comment by Markus Armbruster

vl.c +5 -5
index 368510f..ce7e998 100644
@@ -4058,6 +4058,11 @@ int main(int argc, char **argv, char **envp)
 
     os_daemonize();
 
+    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
+        error_report("could not acquire pid file: %s", strerror(errno));
+        exit(1);
+    }
+
     if (qemu_init_main_loop(&main_loop_err)) {
         error_report_err(main_loop_err);
         exit(1);
@@ -4335,11 +4340,6 @@ int main(int argc, char **argv, char **envp)
     }
 #endif
 
-    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
-        error_report("could not acquire pid file: %s", strerror(errno));
-        exit(1);
-    }
-
     if (qemu_opts_foreach(qemu_find_opts("device"),
                           device_help_func, NULL, NULL)) {
         exit(0);
Signature · 1 line
2.1.4
Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Michael Tokarev

With current code, pid file is open after various sockets, chardevs, fsdevs and the like. This causes interesting effects, for example when monitor is a unix-socket, and another qemu instance is already running, new qemu first "damages" the socket and next complain that it can't acquire the pid file and exits, making running qemu unreachable.

Quoted 7 lines

Move pid file creation earlier, right after the call to os_daemonize(), where we know our process id (pid).

---
 vl.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
Reviewed-by on v2 1/1
#2 lore raw
In reply to Michael Tokarev
Quoted 37 lines · Michael Tokarev

With current code, pid file is open after various sockets, chardevs, fsdevs and the like. This causes interesting effects, for example when monitor is a unix-socket, and another qemu instance is already running, new qemu first "damages" the socket and next complain that it can't acquire the pid file and exits, making running qemu unreachable.

Move pid file creation earlier, right after the call to os_daemonize(), where we know our process id (pid).

---
 vl.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

v2: move the pid file creation even earlier, as per comment by Markus Armbruster

vl.c +5 -0
index 368510f..ce7e998 100644
@@ -4058,6 +4058,11 @@ int main(int argc, char **argv, char **envp)
 
     os_daemonize();
 
+    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
+        error_report("could not acquire pid file: %s", strerror(errno));
+        exit(1);
+    }
+
     if (qemu_init_main_loop(&main_loop_err)) {
         error_report_err(main_loop_err);
         exit(1);
@@ -4335,11 +4340,6 @@ int main(int argc, char **argv, char **envp)
     }

vl.c int main(int argc, char **argv, char **envp) · line 4345

Quoted 10 lines
vl.c +0 -5
 #endif
 
-    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
-        error_report("could not acquire pid file: %s", strerror(errno));
-        exit(1);
-    }
-
     if (qemu_opts_foreach(qemu_find_opts("device"),
                           device_help_func, NULL, NULL)) {
         exit(0);

Right after os_daemonize() is as early as possible. There might be stuff happening before os_daemonize() that shouldn't, but I'm not demanding you search for it to get this patch in.

The wider problem: we spread around parsing, checking and acting upon command line options pretty carelessly, even though checking after daemonize and acting before it is problematic.

Regardless,

#3 lore raw
In reply to Michael Tokarev
Quoted 38 lines · Michael Tokarev

With current code, pid file is open after various sockets, chardevs, fsdevs and the like. This causes interesting effects, for example when monitor is a unix-socket, and another qemu instance is already running, new qemu first "damages" the socket and next complain that it can't acquire the pid file and exits, making running qemu unreachable.

Move pid file creation earlier, right after the call to os_daemonize(), where we know our process id (pid).

---
 vl.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

v2: move the pid file creation even earlier, as per comment by Markus Armbruster

vl.c +5 -0
index 368510f..ce7e998 100644
@@ -4058,6 +4058,11 @@ int main(int argc, char **argv, char **envp)
 
     os_daemonize();
 
+    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
+        error_report("could not acquire pid file: %s", strerror(errno));
+        exit(1);
+    }
+
     if (qemu_init_main_loop(&main_loop_err)) {
         error_report_err(main_loop_err);
         exit(1);
@@ -4335,11 +4340,6 @@ int main(int argc, char **argv, char **envp)
     }

vl.c int main(int argc, char **argv, char **envp) · line 4345

Quoted 10 lines
vl.c +0 -5
 #endif
 
-    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
-        error_report("could not acquire pid file: %s", strerror(errno));
-        exit(1);
-    }
-
     if (qemu_opts_foreach(qemu_find_opts("device"),
                           device_help_func, NULL, NULL)) {
         exit(0);

Queued, thanks.

Paolo