summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/ouroboros/config.h.in5
-rw-r--r--include/ouroboros/lockfile.h37
-rw-r--r--include/ouroboros/shm_du_map.h5
-rw-r--r--src/ipcpd/local/main.c1
-rw-r--r--src/ipcpd/normal/shm_pci.c3
-rw-r--r--src/ipcpd/shim-eth-llc/main.c2
-rw-r--r--src/irmd/main.c34
-rw-r--r--src/irmd/registry.c2
-rw-r--r--src/lib/CMakeLists.txt1
-rw-r--r--src/lib/ipcp.c2
-rw-r--r--src/lib/lockfile.c170
-rw-r--r--src/lib/shm_ap_rbuff.c16
-rw-r--r--src/lib/shm_du_map.c10
-rw-r--r--src/lib/sockets.c2
-rw-r--r--src/tools/irm/irm_bind.c2
15 files changed, 253 insertions, 39 deletions
diff --git a/include/ouroboros/config.h.in b/include/ouroboros/config.h.in
index c11c2d05..7bb4cee0 100644
--- a/include/ouroboros/config.h.in
+++ b/include/ouroboros/config.h.in
@@ -34,12 +34,13 @@
#define AP_MAX_FLOWS 256
#define SHM_DU_BUFF_BLOCK_SIZE sysconf(_SC_PAGESIZE)
#define SHM_DU_MAP_MULTI_BLOCK
-#define SHM_DU_MAP_FILENAME "ouroboros_du_map"
+#define SHM_DU_MAP_FILENAME "ouroboros.shm"
+#define LOCKFILE_NAME "ouroboros.lockfile"
#define SHM_BLOCKS_IN_MAP (1 << 14)
#define SHM_DU_TIMEOUT_MICROS 15000
#define DU_BUFF_HEADSPACE 128
#define DU_BUFF_TAILSPACE 0
-#define SHM_AP_RBUFF_PREFIX "ouroboros_rb_"
+#define SHM_AP_RBUFF_PREFIX "ouroboros.rbuff."
#define SHM_RBUFF_SIZE (1 << 14)
#define IRMD_MAX_FLOWS 4096
#define IRMD_THREADPOOL_SIZE 3
diff --git a/include/ouroboros/lockfile.h b/include/ouroboros/lockfile.h
new file mode 100644
index 00000000..c2cf4572
--- /dev/null
+++ b/include/ouroboros/lockfile.h
@@ -0,0 +1,37 @@
+/*
+ * Ouroboros - Copyright (C) 2016
+ *
+ * Lockfile for ouroboros system
+ *
+ * Dimitri Staessens <dimitri.staessens@intec.ugent.be>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef OUROBOROS_LOCKFILE_H
+#define OUROBOROS_LOCKFILE_H
+
+#include <sys/types.h>
+
+struct lockfile;
+
+struct lockfile * lockfile_create();
+struct lockfile * lockfile_open();
+void lockfile_close(struct lockfile * lf);
+void lockfile_destroy(struct lockfile * lf);
+
+pid_t lockfile_owner(struct lockfile * lf);
+
+#endif
diff --git a/include/ouroboros/shm_du_map.h b/include/ouroboros/shm_du_map.h
index 59cd94a1..23c0c3aa 100644
--- a/include/ouroboros/shm_du_map.h
+++ b/include/ouroboros/shm_du_map.h
@@ -24,11 +24,9 @@
#ifndef OUROBOROS_SHM_DU_MAP_H
#define OUROBOROS_SHM_DU_MAP_H
-#include <errno.h>
-#include <unistd.h>
#include <stdint.h>
-#include <sys/types.h>
#include <pthread.h>
+#include <sys/types.h>
struct shm_du_buff;
struct shm_du_map;
@@ -38,7 +36,6 @@ 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);
/* returns the index of the buffer in the DU map */
diff --git a/src/ipcpd/local/main.c b/src/ipcpd/local/main.c
index ae75ae5e..d72269b8 100644
--- a/src/ipcpd/local/main.c
+++ b/src/ipcpd/local/main.c
@@ -23,6 +23,7 @@
#include <ouroboros/config.h>
#include "ipcp.h"
#include "flow.h"
+#include <ouroboros/errno.h>
#include <ouroboros/shm_du_map.h>
#include <ouroboros/shm_ap_rbuff.h>
#include <ouroboros/list.h>
diff --git a/src/ipcpd/normal/shm_pci.c b/src/ipcpd/normal/shm_pci.c
index e76d8009..144bd15e 100644
--- a/src/ipcpd/normal/shm_pci.c
+++ b/src/ipcpd/normal/shm_pci.c
@@ -21,9 +21,10 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#include <ourboros/errno.h>
+
#include "shm_pci.h"
#include <stdlib.h>
-#include <errno.h>
#define SHM_PCI_HEAD_SIZE(a, b) a.addr_size * 2 + \
a.cep_id_size * 2 + \
diff --git a/src/ipcpd/shim-eth-llc/main.c b/src/ipcpd/shim-eth-llc/main.c
index 4c260301..f82438c6 100644
--- a/src/ipcpd/shim-eth-llc/main.c
+++ b/src/ipcpd/shim-eth-llc/main.c
@@ -26,6 +26,7 @@
#include "ipcp.h"
#include "flow.h"
+#include <ouroboros/errno.h>
#include <ouroboros/shm_du_map.h>
#include <ouroboros/shm_ap_rbuff.h>
#include <ouroboros/list.h>
@@ -47,7 +48,6 @@
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
-#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
diff --git a/src/irmd/main.c b/src/irmd/main.c
index 1f72fa1a..19bc5b9b 100644
--- a/src/irmd/main.c
+++ b/src/irmd/main.c
@@ -24,6 +24,7 @@
#define OUROBOROS_PREFIX "irmd"
#include <ouroboros/config.h>
+#include <ouroboros/errno.h>
#include <ouroboros/logs.h>
#include <ouroboros/sockets.h>
#include <ouroboros/ipcp.h>
@@ -31,6 +32,7 @@
#include <ouroboros/list.h>
#include <ouroboros/utils.h>
#include <ouroboros/irm_config.h>
+#include <ouroboros/lockfile.h>
#include <ouroboros/shm_ap_rbuff.h>
#include <ouroboros/shm_du_map.h>
#include <ouroboros/bitmap.h>
@@ -45,7 +47,6 @@
#include <sys/un.h>
#include <signal.h>
#include <stdlib.h>
-#include <errno.h>
#include <string.h>
#include <limits.h>
#include <pthread.h>
@@ -106,6 +107,7 @@ struct irm {
pthread_rwlock_t flows_lock;
enum irm_state state;
+ struct lockfile * lf;
struct shm_du_map * dum;
pthread_t * threadpool;
int sockfd;
@@ -1331,6 +1333,9 @@ static void irm_destroy()
if (instance->dum != NULL)
shm_du_map_destroy(instance->dum);
+ if (instance->lf != NULL)
+ lockfile_destroy(instance->lf);
+
close(instance->sockfd);
pthread_rwlock_unlock(&instance->state_lock);
@@ -1698,28 +1703,28 @@ static struct irm * irm_create()
instance->state = IRMD_NULL;
- if (access("/dev/shm/" SHM_DU_MAP_FILENAME, F_OK) != -1) {
- struct shm_du_map * dum = shm_du_map_open();
-
- if (dum == NULL) {
- LOG_ERR("Could not examine existing shm file.");
+ if (access("/dev/shm/" LOCKFILE_NAME, F_OK) != -1) {
+ struct lockfile * lf = lockfile_open();
+ if (lf == NULL) {
+ LOG_ERR("Failed to open existing lockfile.");
free(instance);
return NULL;
}
- if (kill(shm_du_map_owner(dum), 0) < 0) {
+ if (kill(lockfile_owner(lf), 0) < 0) {
LOG_INFO("IRMd didn't properly shut down last time.");
- shm_du_map_destroy(dum);
- LOG_INFO("Stale shm file removed.");
+ shm_du_map_destroy(shm_du_map_open());
+ LOG_INFO("Stale resources cleaned");
+ lockfile_destroy(lf);
} else {
LOG_INFO("IRMd already running (%d), exiting.",
- shm_du_map_owner(dum));
- shm_du_map_close(dum);
+ lockfile_owner(lf));
+ lockfile_close(lf);
free(instance);
return NULL;
}
- shm_du_map_close(dum);
+ lockfile_close(lf);
}
if (pthread_rwlock_init(&instance->state_lock, NULL)) {
@@ -1777,6 +1782,11 @@ static struct irm * irm_create()
return NULL;
}
+ if ((instance->lf = lockfile_create()) == NULL) {
+ irm_destroy();
+ return NULL;
+ }
+
if ((instance->dum = shm_du_map_create()) == NULL) {
irm_destroy();
return NULL;
diff --git a/src/irmd/registry.c b/src/irmd/registry.c
index ae532d5a..c5f81d77 100644
--- a/src/irmd/registry.c
+++ b/src/irmd/registry.c
@@ -26,13 +26,13 @@
#define OUROBOROS_PREFIX "registry"
+#include <ouroboros/errno.h>
#include <ouroboros/logs.h>
#include <ouroboros/irm_config.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
-#include <errno.h>
struct reg_instance * reg_instance_create(pid_t api)
{
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index 9c45269c..a440f7be 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -31,6 +31,7 @@ set(SOURCE_FILES
ipcp.c
irm.c
list.c
+ lockfile.c
logs.c
nsm.c
shm_ap_rbuff.c
diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c
index 5d46940d..b0e593e4 100644
--- a/src/lib/ipcp.c
+++ b/src/lib/ipcp.c
@@ -23,6 +23,7 @@
#define OUROBOROS_PREFIX "lib-ipcp"
#include <ouroboros/config.h>
+#include <ouroboros/errno.h>
#include <ouroboros/ipcp.h>
#include <ouroboros/common.h>
#include <ouroboros/logs.h>
@@ -32,7 +33,6 @@
#include <stdlib.h>
#include <string.h>
#include <signal.h>
-#include <errno.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/wait.h>
diff --git a/src/lib/lockfile.c b/src/lib/lockfile.c
new file mode 100644
index 00000000..a93d5f17
--- /dev/null
+++ b/src/lib/lockfile.c
@@ -0,0 +1,170 @@
+/*
+ * Ouroboros - Copyright (C) 2016
+ *
+ * Lockfile for ouroboros system
+ *
+ * Dimitri Staessens <dimitri.staessens@intec.ugent.be>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <ouroboros/config.h>
+#include <ouroboros/lockfile.h>
+
+#define OUROBOROS_PREFIX "lockfile"
+
+#include <ouroboros/logs.h>
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <signal.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#define LF_SIZE (sizeof(pid_t))
+
+struct lockfile {
+ pid_t * api;
+ int fd;
+};
+
+struct lockfile * lockfile_create() {
+ struct lockfile * lf = malloc(sizeof(*lf));
+ if (lf == NULL)
+ return NULL;
+
+ lf->fd = shm_open(LOCKFILE_NAME, O_CREAT | O_EXCL | O_RDWR, 0666);
+ if (lf->fd == -1) {
+ LOG_DBGF("Could not create lock file.");
+ free(lf);
+ return NULL;
+ }
+
+ if (fchmod(lf->fd, 0666)) {
+ LOG_DBGF("Failed to chmod lockfile.");
+ free(lf);
+ return NULL;
+ }
+
+ if (lseek(lf->fd, LF_SIZE - 1, SEEK_SET) < 0) {
+ LOG_DBGF("Failed to extend lockfile.");
+ free(lf);
+ return NULL;
+ }
+
+ if (write(lf->fd, "", 1) != 1) {
+ LOG_DBGF("Failed to finalise lockfile.");
+ free(lf);
+ return NULL;
+ }
+
+ lf->api = mmap(NULL,
+ LF_SIZE, PROT_READ | PROT_WRITE,
+ MAP_SHARED,
+ lf->fd,
+ 0);
+
+ if (lf->api == MAP_FAILED) {
+ LOG_DBGF("Failed to map lockfile.");
+
+ if (shm_unlink(LOCKFILE_NAME) == -1)
+ LOG_DBGF("Failed to remove invalid lockfile.");
+
+ free(lf);
+ return NULL;
+ }
+
+ *lf->api = getpid();
+
+ return lf;
+}
+
+struct lockfile * lockfile_open() {
+ struct lockfile * lf = malloc(sizeof(*lf));
+ if (lf == NULL)
+ return NULL;
+
+ lf->fd = shm_open(LOCKFILE_NAME, O_RDWR, 0666);
+ if (lf->fd == -1) {
+ LOG_DBGF("Could not open lock file.");
+ free(lf);
+ return NULL;
+ }
+
+ lf->api = mmap(NULL,
+ LF_SIZE, PROT_READ | PROT_WRITE,
+ MAP_SHARED,
+ lf->fd,
+ 0);
+
+ if (lf->api == MAP_FAILED) {
+ LOG_DBGF("Failed to map lockfile.");
+
+ if (shm_unlink(LOCKFILE_NAME) == -1)
+ LOG_DBGF("Failed to remove invalid lockfile.");
+
+ free(lf);
+ return NULL;
+ }
+
+ return lf;
+}
+
+void lockfile_close(struct lockfile * lf)
+{
+ if (lf == NULL) {
+ LOG_DBGF("Bogus input. Bugging out.");
+ return;
+ }
+
+ if (close(lf->fd) < 0)
+ LOG_DBGF("Couldn't close lockfile.");
+
+ if (munmap(lf->api, LF_SIZE) == -1)
+ LOG_DBGF("Couldn't unmap lockfile.");
+
+ free(lf);
+}
+
+void lockfile_destroy(struct lockfile * lf)
+{
+ if (lf == NULL) {
+ LOG_DBGF("Bogus input. Bugging out.");
+ return;
+ }
+
+ if (getpid() != *lf->api && kill(*lf->api, 0) == 0) {
+ LOG_DBGF("Only IRMd can destroy %s.", LOCKFILE_NAME);
+ return;
+ }
+
+ if (close(lf->fd) < 0)
+ LOG_DBGF("Couldn't close lockfile.");
+
+ if (munmap(lf->api, LF_SIZE) == -1)
+ LOG_DBGF("Couldn't unmap lockfile.");
+
+ if (shm_unlink(LOCKFILE_NAME) == -1)
+ LOG_DBGF("Failed to remove lockfile.");
+
+ free(lf);
+}
+
+pid_t lockfile_owner(struct lockfile * lf)
+{
+ return *lf->api;
+}
diff --git a/src/lib/shm_ap_rbuff.c b/src/lib/shm_ap_rbuff.c
index 4eb91136..618c4c19 100644
--- a/src/lib/shm_ap_rbuff.c
+++ b/src/lib/shm_ap_rbuff.c
@@ -21,12 +21,13 @@
*/
#include <ouroboros/config.h>
+#include <ouroboros/errno.h>
#define OUROBOROS_PREFIX "shm_ap_rbuff"
#include <ouroboros/logs.h>
#include <ouroboros/shm_ap_rbuff.h>
-#include <ouroboros/shm_du_map.h>
+#include <ouroboros/lockfile.h>
#include <pthread.h>
#include <sys/mman.h>
@@ -35,7 +36,6 @@
#include <string.h>
#include <stdint.h>
#include <unistd.h>
-#include <errno.h>
#include <signal.h>
#include <sys/stat.h>
@@ -215,7 +215,7 @@ void shm_ap_rbuff_close(struct shm_ap_rbuff * rb)
void shm_ap_rbuff_destroy(struct shm_ap_rbuff * rb)
{
char fn[25];
- struct shm_du_map * dum = NULL;
+ struct lockfile * lf = NULL;
if (rb == NULL) {
LOG_DBGF("Bogus input. Bugging out.");
@@ -223,15 +223,17 @@ void shm_ap_rbuff_destroy(struct shm_ap_rbuff * rb)
}
if (rb->api != getpid()) {
- dum = shm_du_map_open();
- if (shm_du_map_owner(dum) == getpid()) {
+ lf = lockfile_open();
+ if (lf == NULL)
+ return;
+ if (lockfile_owner(lf) == getpid()) {
LOG_DBGF("Ringbuffer %d destroyed by IRMd %d.",
rb->api, getpid());
- shm_du_map_close(dum);
+ lockfile_close(lf);
} else {
LOG_ERR("AP-I %d tried to destroy rbuff owned by %d.",
getpid(), rb->api);
- shm_du_map_close(dum);
+ lockfile_close(lf);
return;
}
}
diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c
index 53959676..6a4b7361 100644
--- a/src/lib/shm_du_map.c
+++ b/src/lib/shm_du_map.c
@@ -22,6 +22,7 @@
*/
#include <ouroboros/config.h>
+#include <ouroboros/errno.h>
#include <ouroboros/shm_du_map.h>
#include <ouroboros/shm_ap_rbuff.h>
#include <ouroboros/time_utils.h>
@@ -29,6 +30,7 @@
#include <pthread.h>
#include <sys/mman.h>
#include <fcntl.h>
+#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
@@ -270,14 +272,6 @@ struct shm_du_map * shm_du_map_open()
return dum;
}
-pid_t shm_du_map_owner(struct shm_du_map * dum)
-{
- if (dum == NULL)
- return -1;
-
- return *dum->api;
-}
-
void * shm_du_map_sanitize(void * o)
{
struct shm_du_map * dum = (struct shm_du_map *) o;
diff --git a/src/lib/sockets.c b/src/lib/sockets.c
index d60ea91a..9e13b687 100644
--- a/src/lib/sockets.c
+++ b/src/lib/sockets.c
@@ -23,6 +23,7 @@
#define OUROBOROS_PREFIX "libouroboros-sockets"
#include <ouroboros/config.h>
+#include <ouroboros/errno.h>
#include <ouroboros/logs.h>
#include <ouroboros/common.h>
#include <ouroboros/sockets.h>
@@ -32,7 +33,6 @@
#include <sys/un.h>
#include <string.h>
#include <stdlib.h>
-#include <errno.h>
int client_socket_open(char * file_name)
{
diff --git a/src/tools/irm/irm_bind.c b/src/tools/irm/irm_bind.c
index 45da6e4c..061aeef5 100644
--- a/src/tools/irm/irm_bind.c
+++ b/src/tools/irm/irm_bind.c
@@ -22,9 +22,9 @@
#include <stdio.h>
#include <string.h>
-#include <errno.h>
#include <ouroboros/irm.h>
+#include <ouroboros/errno.h>
#include "irm_ops.h"
#include "irm_utils.h"