summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPrasanna Kumar Kalever <prasanna.kalever@redhat.com>2017-02-21 00:06:49 +0530
committerPrasanna Kumar Kalever <prasanna.kalever@redhat.com>2017-02-21 15:56:52 +0530
commitc1c22333819f744287fb78fb317ff70fa82beab8 (patch)
tree25502a424c5b23cd96595fb9c3dc490f50de78cd
parentb160d10dbf9c46fec60c4fa86b03dc9d6661feb4 (diff)
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 <prasanna.kalever@redhat.com>
-rw-r--r--daemon/gluster-blockd.c42
-rw-r--r--utils/common.h3
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 <fcntl.h>
# include <dirent.h>
# include <sys/stat.h>
# include <pthread.h>
@@ -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"