diff options
| -rw-r--r-- | include/ouroboros/shm_du_map.h | 1 | ||||
| -rw-r--r-- | src/ipcpd/local/main.c | 2 | ||||
| -rw-r--r-- | src/ipcpd/shim-eth-llc/main.c | 2 | ||||
| -rw-r--r-- | src/ipcpd/shim-udp/main.c | 2 | ||||
| -rw-r--r-- | src/irmd/main.c | 1 | ||||
| -rw-r--r-- | src/lib/dev.c | 4 | ||||
| -rw-r--r-- | src/lib/shm_ap_rbuff.c | 3 | ||||
| -rw-r--r-- | src/lib/shm_du_map.c | 29 | 
8 files changed, 36 insertions, 8 deletions
| diff --git a/include/ouroboros/shm_du_map.h b/include/ouroboros/shm_du_map.h index 9d6d7aaf..59cd94a1 100644 --- a/include/ouroboros/shm_du_map.h +++ b/include/ouroboros/shm_du_map.h @@ -36,6 +36,7 @@ struct shm_du_map;  struct shm_du_map * shm_du_map_create();  struct shm_du_map * shm_du_map_open();  void                shm_du_map_close(struct shm_du_map * dum); +void                shm_du_map_close_on_exit(struct shm_du_map * dum);  void                shm_du_map_destroy(struct shm_du_map * dum);  pid_t               shm_du_map_owner(struct shm_du_map * dum);  void *              shm_du_map_sanitize(void * o); diff --git a/src/ipcpd/local/main.c b/src/ipcpd/local/main.c index c705f74c..ffe69dfb 100644 --- a/src/ipcpd/local/main.c +++ b/src/ipcpd/local/main.c @@ -141,7 +141,7 @@ void shim_ap_fini()          if (_ap_instance->fds != NULL)                  bmp_destroy(_ap_instance->fds);          if (_ap_instance->dum != NULL) -                shm_du_map_close(_ap_instance->dum); +                shm_du_map_close_on_exit(_ap_instance->dum);          if (_ap_instance->rb != NULL)                  shm_ap_rbuff_destroy(_ap_instance->rb); diff --git a/src/ipcpd/shim-eth-llc/main.c b/src/ipcpd/shim-eth-llc/main.c index ba912829..5e355bd7 100644 --- a/src/ipcpd/shim-eth-llc/main.c +++ b/src/ipcpd/shim-eth-llc/main.c @@ -176,7 +176,7 @@ void eth_llc_ipcp_data_destroy()                  LOG_WARN("Cleaning up while not in shutdown.");          if (shim_data(_ipcp)->dum != NULL) -                shm_du_map_close(shim_data(_ipcp)->dum); +                shm_du_map_close_on_exit(shim_data(_ipcp)->dum);          if (shim_data(_ipcp)->rb != NULL)                  shm_ap_rbuff_destroy(shim_data(_ipcp)->rb);          if (shim_data(_ipcp)->indices != NULL) diff --git a/src/ipcpd/shim-udp/main.c b/src/ipcpd/shim-udp/main.c index b0fe1c11..92204970 100644 --- a/src/ipcpd/shim-udp/main.c +++ b/src/ipcpd/shim-udp/main.c @@ -162,7 +162,7 @@ void shim_ap_fini()          if (_ap_instance->fds != NULL)                  bmp_destroy(_ap_instance->fds);          if (_ap_instance->dum != NULL) -                shm_du_map_close(_ap_instance->dum); +                shm_du_map_close_on_exit(_ap_instance->dum);          if (_ap_instance->rb != NULL)                  shm_ap_rbuff_destroy(_ap_instance->rb); diff --git a/src/irmd/main.c b/src/irmd/main.c index ff129b71..26251858 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -1712,6 +1712,7 @@ static struct irm * irm_create()                  } else {                          LOG_INFO("IRMd already running (%d), exiting.",                                   shm_du_map_owner(dum)); +                        shm_du_map_close(dum);                          free(instance);                          exit(EXIT_SUCCESS);                  } diff --git a/src/lib/dev.c b/src/lib/dev.c index d85afc45..f13c8423 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -113,10 +113,10 @@ void ap_fini(void)          if (_ap_instance->fds != NULL)                  bmp_destroy(_ap_instance->fds); -        if (_ap_instance->dum != NULL) -                shm_du_map_close(_ap_instance->dum);          if (_ap_instance->rb != NULL)                  shm_ap_rbuff_destroy(_ap_instance->rb); +        if (_ap_instance->dum != NULL) +                shm_du_map_close_on_exit(_ap_instance->dum);          pthread_rwlock_rdlock(&_ap_instance->flows_lock); diff --git a/src/lib/shm_ap_rbuff.c b/src/lib/shm_ap_rbuff.c index e90afe19..4eb91136 100644 --- a/src/lib/shm_ap_rbuff.c +++ b/src/lib/shm_ap_rbuff.c @@ -36,6 +36,7 @@  #include <stdint.h>  #include <unistd.h>  #include <errno.h> +#include <signal.h>  #include <sys/stat.h>  #define SHM_RBUFF_FILE_SIZE (SHM_RBUFF_SIZE * sizeof(struct rb_entry)          \ @@ -161,7 +162,7 @@ struct shm_ap_rbuff * shm_ap_rbuff_open(pid_t api)          shm_fd = shm_open(fn, O_RDWR, 0666);          if (shm_fd == -1) { -                LOG_DBGF("Failed opening shared memory %s.", fn); +                LOG_DBGF("%d failed opening shared memory %s.", getpid(), fn);                  return NULL;          } diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c index 6289857f..68452158 100644 --- a/src/lib/shm_du_map.c +++ b/src/lib/shm_du_map.c @@ -123,8 +123,10 @@ static void clean_sdus(struct shm_du_map * dum, pid_t api)          if (kill(api, 0) == 0) {                  struct shm_ap_rbuff * rb;                  rb = shm_ap_rbuff_open(api); -                shm_ap_rbuff_reset(rb); -                shm_ap_rbuff_close(rb); +                if (rb != NULL) { +                        shm_ap_rbuff_reset(rb); +                        shm_ap_rbuff_close(rb); +                }          }          *dum->choked = 0; @@ -347,6 +349,24 @@ void * shm_du_map_sanitize(void * o)          return (void *) 0;  } +void shm_du_map_close_on_exit(struct shm_du_map * dum) +{ +        if (dum == NULL) { +                LOG_DBGF("Bogus input. Bugging out."); +                return; +        } + +        clean_sdus(dum, getpid()); + +        if (close(dum->fd) < 0) +                LOG_DBGF("Couldn't close shared memory."); + +        if (munmap(dum->shm_base, SHM_FILE_SIZE) == -1) +                LOG_DBGF("Couldn't unmap shared memory."); + +        free(dum); +} +  void shm_du_map_close(struct shm_du_map * dum)  {          if (dum == NULL) { @@ -370,6 +390,11 @@ void shm_du_map_destroy(struct shm_du_map * dum)                  return;          } +        if (getpid() != *dum->api) { +                LOG_DBGF("Only IRMd can destroy %s.", SHM_DU_MAP_FILENAME); +                return; +        } +          if (close(dum->fd) < 0)                  LOG_DBGF("Couldn't close shared memory."); | 
