Skip to contentSkip to conversation

Couple of qemu-pr-helper fixes

PATCH In review Michal Privoznik 2 participants 4 messages Apr 3, 2018 – Apr 4, 2018

lore
Share & tools
lore
#2 lore raw

PATCH 2/2 qemu-pr-helper: Write pidfile more often

In reply to Michal Privoznik

Let's write pidfile even if user did not request --daemon but they requested just --pidfile. Libvirt will use exactly this.

---
 scsi/qemu-pr-helper.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scsi/qemu-pr-helper.c +5 -1
index eeff80acf2..d0f83176e1 100644
@@ -924,6 +924,7 @@ int main(int argc, char **argv)
     Error *local_err = NULL;
     char *trace_file = NULL;
     bool daemonize = false;
+    bool pidfile_specified = false;
     unsigned socket_activation;
 
     struct sigaction sa_sigterm;
@@ -954,6 +955,7 @@ int main(int argc, char **argv)
         case 'f':
             g_free(pidfile);
             pidfile = g_strdup(optarg);
+            pidfile_specified = true;
             break;
 #ifdef CONFIG_LIBCAP
         case 'u': {
@@ -1086,9 +1088,11 @@ int main(int argc, char **argv)
             error_report("Failed to daemonize: %s", strerror(errno));
             exit(EXIT_FAILURE);
         }
-        write_pidfile();
     }
 
+    if (daemonize || pidfile_specified)
+        write_pidfile();
+
 #ifdef CONFIG_LIBCAP
     if (drop_privileges() < 0) {
         error_report("Failed to drop privileges: %s", strerror(errno));
Signature · 1 line
2.16.1
#1 lore raw

PATCH 1/2 qemu-pr-helper: Daemonize before dropping privileges

In reply to Michal Privoznik

After we've dropped privileges it might be not possible to write pidfile. For instance, if this binary is run as root (because user wants it to write pidfile to some privileged location) writing pidfile fails because privileges are dropped before we even get to that.

---
 scsi/qemu-pr-helper.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
scsi/qemu-pr-helper.c +7 -7
index 21e1b8ea60..eeff80acf2 100644
@@ -1081,13 +1081,6 @@ int main(int argc, char **argv)
                                          accept_client,
                                          NULL, NULL);
 
-#ifdef CONFIG_LIBCAP
-    if (drop_privileges() < 0) {
-        error_report("Failed to drop privileges: %s", strerror(errno));
-        exit(EXIT_FAILURE);
-    }
-#endif
-
     if (daemonize) {
         if (daemon(0, 0) < 0) {
             error_report("Failed to daemonize: %s", strerror(errno));
@@ -1096,6 +1089,13 @@ int main(int argc, char **argv)
         write_pidfile();
     }
 
+#ifdef CONFIG_LIBCAP
+    if (drop_privileges() < 0) {
+        error_report("Failed to drop privileges: %s", strerror(errno));
+        exit(EXIT_FAILURE);
+    }
+#endif
+
     state = RUNNING;
     do {
         main_loop_wait(false);
Signature · 1 line
2.16.1
#0 lore raw

cover Couple of qemu-pr-helper fixes

The first one is trivial.

The second is slightly trickier. Libvirt has this virCommand subsystem (found under src/util/vircommand.c) which it uses to spawn commands. The subsystem allows libvirt to both daemonize a process and learn its PID. The latter of course does not work if command daemonizes itself (hence libvirt will not use qemu-pr-helper --daemon). BUT, we still need qemu-pr-helper to lock the pidfile (so that when libvirt tries to lock it it is denied). NB, libvirt uses fnctl(cmd = F_SETLK) to lock pidfiles. So leaking locked FD into qemu-pr-helper is not an option because that doesn't survive fork().

Another, trivial reason might be that if I run:

  qemu-pr-helper --pidfile /tmp/pr.pid

(without --daemon), I'd still expect qemu-pr-helper to write pidfile because I told it to.

Michal Privoznik (2):
qemu-pr-helper: Daemonize before dropping privileges
qemu-pr-helper: Write pidfile more often

 scsi/qemu-pr-helper.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
Signature · 1 line
2.16.1
Reviewed-by on the v1 series
#3 lore raw
Quoted 14 lines · Michal Privoznik

The first one is trivial.

The second is slightly trickier. Libvirt has this virCommand subsystem (found under src/util/vircommand.c) which it uses to spawn commands. The subsystem allows libvirt to both daemonize a process and learn its PID. The latter of course does not work if command daemonizes itself (hence libvirt will not use qemu-pr-helper --daemon). BUT, we still need qemu-pr-helper to lock the pidfile (so that when libvirt tries to lock it it is denied). NB, libvirt uses fnctl(cmd = F_SETLK) to lock pidfiles. So leaking locked FD into qemu-pr-helper is not an option because that doesn't survive fork().

Another, trivial reason might be that if I run:

Quoted 8 lines
  qemu-pr-helper --pidfile /tmp/pr.pid

(without --daemon), I'd still expect qemu-pr-helper to write pidfile because I told it to.

Michal Privoznik (2):
qemu-pr-helper: Daemonize before dropping privileges
qemu-pr-helper: Write pidfile more often