From c1c22333819f744287fb78fb317ff70fa82beab8 Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Kalever Date: Tue, 21 Feb 2017 00:06:49 +0530 Subject: lockfile: don't allow multiple instances of daemon to run until now there is no check to defend on multiple runs of daemon within the same node. This patch takes a non blocking lock on 'gluster-blockd.lock' file, if it succeeds only then daemon is allowed to run, if there is already a lock on lock-file (taken by some other instance of gluster-blockd) we will exit. This patch also renames GB_UNIX_ADDRESS from gluster-block.socket to gluster-blockd.socket, as that makes better sense. Change-Id: I43b285f9da15d078fe3df4d231d0ef8d44272d8f Signed-off-by: Prasanna Kumar Kalever --- daemon/gluster-blockd.c | 42 ++++++++++++++++++++++++++++++++++++------ utils/common.h | 3 ++- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/daemon/gluster-blockd.c b/daemon/gluster-blockd.c index 399df1f..d4cae91 100644 --- a/daemon/gluster-blockd.c +++ b/daemon/gluster-blockd.c @@ -8,6 +8,8 @@ cases as published by the Free Software Foundation. */ + +# include # include # include # include @@ -168,26 +170,54 @@ glusterBlockServerThreadProc(void *vargp) int main (int argc, char **argv) { + int fd; pthread_t cli_thread; pthread_t server_thread; + struct flock lock = {0, }; + int errnosv = 0; + if (!glusterBlockLogdirCreate()) { return -1; } - pmap_unset(GLUSTER_BLOCK_CLI, GLUSTER_BLOCK_CLI_VERS); + /* is gluster-blockd running ? */ + fd = creat(GB_LOCK_FILE, S_IRUSR | S_IWUSR); + if (fd == -1) { + LOG("mgmt", GB_LOG_ERROR, "creat(%s) failed[%s]", + GB_LOCK_FILE, strerror(errno)); + goto out; + } + + lock.l_type = F_WRLCK; + if (fcntl(fd, F_SETLK, &lock) == -1) { + errnosv = errno; + LOG("mgmt", GB_LOG_ERROR, "%s", + "gluster-blockd is already running..."); + close(fd); + exit(errnosv); + } + + pmap_unset(GLUSTER_BLOCK_CLI, GLUSTER_BLOCK_CLI_VERS); pmap_unset(GLUSTER_BLOCK, GLUSTER_BLOCK_VERS); - pthread_create(&cli_thread, NULL, glusterBlockCliThreadProc , NULL); - pthread_create(&server_thread, NULL, glusterBlockServerThreadProc , NULL); + pthread_create(&cli_thread, NULL, glusterBlockCliThreadProc, NULL); + pthread_create(&server_thread, NULL, glusterBlockServerThreadProc, NULL); pthread_join(cli_thread, NULL); pthread_join(server_thread, NULL); + LOG("mgmt", GB_LOG_ERROR, "svc_run returned (%s)", strerror (errno)); - LOG("mgmt", GB_LOG_ERROR, "svc_run returned (%s)", - strerror (errno)); + lock.l_type = F_UNLCK; + if (fcntl(fd, F_SETLK, &lock) == -1) { + LOG("mgmt", GB_LOG_ERROR, "fcntl(UNLCK) on pidfile %s failed[%s]", + GB_LOCK_FILE, strerror(errno)); + } - exit (errno); + close(fd); + + out: + exit (1); /* NOTREACHED */ } diff --git a/utils/common.h b/utils/common.h index 6bb3273..c544029 100644 --- a/utils/common.h +++ b/utils/common.h @@ -17,7 +17,8 @@ # define GB_LOGDIR DATADIR "/log/gluster-block" # define GB_INFODIR DATADIR "/run" -# define GB_UNIX_ADDRESS GB_INFODIR "/gluster-block.socket" +# define GB_LOCK_FILE GB_INFODIR "/gluster-blockd.lock" +# define GB_UNIX_ADDRESS GB_INFODIR "/gluster-blockd.socket" # define GB_TCP_PORT 24006 # define DAEMON_LOG_FILE GB_LOGDIR "/gluster-blockd.log" -- cgit