From 800b057facc5cf15e6eb4f5784f3dc0a6312903d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Canet?= Date: Sun, 19 Jul 2015 11:21:26 +0200 Subject: [PATCH 1/3] native: net: Make _native_tap_fd static One global variable less is always good. --- cpu/native/include/tap.h | 6 +++++- cpu/native/native_cpu.c | 4 +--- cpu/native/net/tap.c | 11 +++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/cpu/native/include/tap.h b/cpu/native/include/tap.h index 0d44548dcc..105f8f7c8b 100644 --- a/cpu/native/include/tap.h +++ b/cpu/native/include/tap.h @@ -36,7 +36,11 @@ extern "C" { */ int tap_init(char *name); -extern int _native_tap_fd; +/** + * Close tap device + */ +void tap_cleanup(void); + extern unsigned char _native_tap_mac[ETHER_ADDR_LEN]; struct nativenet_header { diff --git a/cpu/native/native_cpu.c b/cpu/native/native_cpu.c index 7f372514ea..afc31c3120 100644 --- a/cpu/native/native_cpu.c +++ b/cpu/native/native_cpu.c @@ -73,9 +73,7 @@ int reboot_arch(int mode) /* TODO: close stdio fds */ #endif #ifdef MODULE_NATIVENET - if (_native_tap_fd != -1) { - real_close(_native_tap_fd); - } + tap_cleanup(); #endif if (real_execve(_native_argv[0], _native_argv, NULL) == -1) { diff --git a/cpu/native/net/tap.c b/cpu/native/net/tap.c index e5dbc78560..3c3a7ff156 100644 --- a/cpu/native/net/tap.c +++ b/cpu/native/net/tap.c @@ -351,4 +351,15 @@ int tap_init(char *name) DEBUG("RIOT native tap initialized.\n"); return _native_tap_fd; } + +void tap_cleanup(void) +{ + if (_native_tap_fd == -1) { + return; + } + + real_close(_native_tap_fd); + _native_tap_fd = -1; + +} /** @} */ From 008b105166c2994e67d36bf401fb7e2a0095dade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Canet?= Date: Sun, 19 Jul 2015 11:37:58 +0200 Subject: [PATCH 2/3] native: net: Take care of unregistering SIGIO on reboot --- cpu/native/net/tap.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cpu/native/net/tap.c b/cpu/native/net/tap.c index 3c3a7ff156..c5590b907c 100644 --- a/cpu/native/net/tap.c +++ b/cpu/native/net/tap.c @@ -354,6 +354,11 @@ int tap_init(char *name) void tap_cleanup(void) { + unregister_interrupt(SIGIO); +#ifdef __MACH__ + kill(sigio_child_pid, SIGKILL); +#endif + if (_native_tap_fd == -1) { return; } From 22da1f834b6314d0a4500a6523f5c5c0dd56c7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Canet?= Date: Sun, 19 Jul 2015 12:05:38 +0200 Subject: [PATCH 3/3] native: ng: net: Close tap device and cleanup on reboot --- cpu/native/include/dev_eth_tap.h | 7 +++++++ cpu/native/native_cpu.c | 7 +++++++ cpu/native/ng_net/dev_eth_tap.c | 28 ++++++++++++++++++++++++++++ sys/include/net/dev_eth.h | 17 +++++++++++++++++ 4 files changed, 59 insertions(+) diff --git a/cpu/native/include/dev_eth_tap.h b/cpu/native/include/dev_eth_tap.h index 08c6774598..055da3e24c 100644 --- a/cpu/native/include/dev_eth_tap.h +++ b/cpu/native/include/dev_eth_tap.h @@ -53,6 +53,13 @@ extern dev_eth_tap_t dev_eth_tap; */ void dev_eth_tap_setup(dev_eth_tap_t *dev, const char *name); +/** + * @brief Cleanup dev_eth_tap_t structure. + * + * @param dev the dev_eth_tap device handle to cleanup + */ +void dev_eth_tap_cleanup(dev_eth_tap_t *dev); + #ifdef __cplusplus } #endif diff --git a/cpu/native/native_cpu.c b/cpu/native/native_cpu.c index afc31c3120..f0bb54bc53 100644 --- a/cpu/native/native_cpu.c +++ b/cpu/native/native_cpu.c @@ -51,6 +51,10 @@ #ifdef MODULE_NATIVENET #include "tap.h" #endif +#ifdef MODULE_NG_NATIVENET +#include "dev_eth_tap.h" +extern dev_eth_tap_t dev_eth_tap; +#endif #include "native_internal.h" @@ -75,6 +79,9 @@ int reboot_arch(int mode) #ifdef MODULE_NATIVENET tap_cleanup(); #endif +#ifdef MODULE_NG_NATIVENET + dev_eth_tap_cleanup(&dev_eth_tap); +#endif if (real_execve(_native_argv[0], _native_argv, NULL) == -1) { err(EXIT_FAILURE, "reboot: execve"); diff --git a/cpu/native/ng_net/dev_eth_tap.c b/cpu/native/ng_net/dev_eth_tap.c index 4ec1bf30fc..be819279a4 100644 --- a/cpu/native/ng_net/dev_eth_tap.c +++ b/cpu/native/ng_net/dev_eth_tap.c @@ -57,6 +57,7 @@ dev_eth_tap_t dev_eth_tap; /* dev_eth interface */ static int _init(dev_eth_t *ethdev); +static void _cleanup(dev_eth_t *ethdev); static int _send(dev_eth_t *ethdev, char* buf, int n); static int _recv(dev_eth_t *ethdev, char* buf, int n); @@ -82,6 +83,7 @@ static inline void _isr(dev_eth_t *ethdev) { static eth_driver_t eth_driver_tap = { .init = _init, + .cleanup = _cleanup, .send = _send, .recv = _recv, .get_mac_addr = _get_mac_addr, @@ -177,6 +179,13 @@ void dev_eth_tap_setup(dev_eth_tap_t *dev, const char *name) { strncpy(dev->tap_name, name, IFNAMSIZ); } +void dev_eth_tap_cleanup(dev_eth_tap_t *dev) { + if (!dev) { + return; + } + dev_eth_cleanup((dev_eth_t *) dev); +} + static void _tap_isr(void) { dev_eth_isr(((dev_eth_t *) &dev_eth_tap)); } @@ -269,6 +278,25 @@ static int _init(dev_eth_t *ethdev) return 0; } +static void _cleanup(dev_eth_t *ethdev) +{ + dev_eth_tap_t *dev = (dev_eth_tap_t*)ethdev; + + /* Do we have a device */ + if (!dev) { + return; + } + + /* cleanup signal handling */ + unregister_interrupt(SIGIO); +#ifdef __MACH__ + kill(_sigio_child_pid, SIGKILL); +#endif + + /* close the tap device */ + real_close(dev->tap_fd); +} + #ifdef __MACH__ static void _sigio_child(ng_tapnet_t *dev) { diff --git a/sys/include/net/dev_eth.h b/sys/include/net/dev_eth.h index 20d493b5da..78c8c10ec0 100644 --- a/sys/include/net/dev_eth.h +++ b/sys/include/net/dev_eth.h @@ -137,6 +137,10 @@ typedef struct eth_driver { * @return <=0 on error, >0 on success */ int (*init)(dev_eth_t *dev); + /** + * @brief the driver's cleanup function (optional) + */ + void (*cleanup)(dev_eth_t *dev); /** * @brief a driver's user-space ISR handler * @@ -162,6 +166,19 @@ static inline int dev_eth_init(dev_eth_t *dev) { return dev->driver->init(dev); } +/** + * @brief Cleanup a device given by dev (convenience function) + * + * This function is to be called on reboot if the init function is not + * idempotent. + * + */ +static inline void dev_eth_cleanup(dev_eth_t *dev) { + if (dev->driver->cleanup) { + dev->driver->cleanup(dev); + } +} + /** * @brief global dev_eth interrupt handling function. *