]> Git Repo - qemu.git/blobdiff - net/tap.c
tap: invoke downscript when we exit abnormally
[qemu.git] / net / tap.c
index 7a7320c1a223e47cba3c0be94f0aba32d9dd0365..e42c555029db931ab39691d4600a1d6a0d5e76bc 100644 (file)
--- a/net/tap.c
+++ b/net/tap.c
@@ -38,6 +38,7 @@
 #include "sysemu.h"
 #include "qemu-char.h"
 #include "qemu-common.h"
+#include "qemu-error.h"
 
 #include "net/tap-linux.h"
 
@@ -52,6 +53,7 @@ typedef struct TAPState {
     char down_script[1024];
     char down_script_arg[128];
     uint8_t buf[TAP_BUFSIZE];
+    Notifier exit_notifier;
     unsigned int read_poll : 1;
     unsigned int write_poll : 1;
     unsigned int has_vnet_hdr : 1;
@@ -260,6 +262,19 @@ static void tap_cleanup(VLANClientState *nc)
     tap_read_poll(s, 0);
     tap_write_poll(s, 0);
     close(s->fd);
+    exit_notifier_remove(&s->exit_notifier);
+}
+
+/* Instead of exiting gracefully, we're exiting because someone called
+ * exit(), make sure to invoke down script at least
+ */
+static void tap_cleanup_at_exit(Notifier *notifier)
+{
+    TAPState *s = container_of(notifier, TAPState, exit_notifier);
+
+    if (s->down_script[0]) {
+        launch_script(s->down_script, s->down_script_arg, s->fd);
+    }
 }
 
 static void tap_poll(VLANClientState *nc, bool enable)
@@ -298,6 +313,8 @@ static TAPState *net_tap_fd_init(VLANState *vlan,
     s->has_vnet_hdr = vnet_hdr != 0;
     s->using_vnet_hdr = 0;
     s->has_ufo = tap_probe_has_ufo(s->fd);
+    s->exit_notifier.notify = tap_cleanup_at_exit;
+    exit_notifier_add(&s->exit_notifier);
     tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
     tap_read_poll(s, 1);
     return s;
@@ -393,7 +410,7 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
             qemu_opt_get(opts, "script") ||
             qemu_opt_get(opts, "downscript") ||
             qemu_opt_get(opts, "vnet_hdr")) {
-            qemu_error("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=\n");
+            error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
             return -1;
         }
 
This page took 0.026635 seconds and 4 git commands to generate.