diff options
author | Sander Vrijders <sander.vrijders@intec.ugent.be> | 2016-07-07 15:44:16 +0200 |
---|---|---|
committer | Sander Vrijders <sander.vrijders@intec.ugent.be> | 2016-07-07 15:44:16 +0200 |
commit | cc377e56c6fd25403fc7ccf5f83c82e3d85a767b (patch) | |
tree | 33604c48880d3a01d7053608584236dd15fc43e8 /src/lib | |
parent | 80441117cf61137c6a8c97e0779e70d76ae8541d (diff) | |
parent | b3870ef695a256be4a4ed28a991c39aab37bd6f3 (diff) | |
download | ouroboros-cc377e56c6fd25403fc7ccf5f83c82e3d85a767b.tar.gz ouroboros-cc377e56c6fd25403fc7ccf5f83c82e3d85a767b.zip |
Merged in dstaesse/ouroboros/be-lockfile (pull request #165)
lib: Add lockfile
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/lib/ipcp.c | 2 | ||||
-rw-r--r-- | src/lib/lockfile.c | 170 | ||||
-rw-r--r-- | src/lib/shm_ap_rbuff.c | 16 | ||||
-rw-r--r-- | src/lib/shm_du_map.c | 10 | ||||
-rw-r--r-- | src/lib/sockets.c | 2 |
6 files changed, 184 insertions, 17 deletions
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) { |