Skip to content

Instantly share code, notes, and snippets.

@badboy
Created December 1, 2014 22:16
Show Gist options
  • Save badboy/e24ffe49c77d833f1996 to your computer and use it in GitHub Desktop.
Save badboy/e24ffe49c77d833f1996 to your computer and use it in GitHub Desktop.
diff --git a/src/cluster.c b/src/cluster.c
index 80ac66e..e14ac6c 100644
--- a/src/cluster.c
+++ b/src/cluster.c
@@ -382,8 +382,21 @@ int clusterLockConfig(char *filename) {
return REDIS_ERR;
}
+#ifdef __sun
+ struct flock lock;
+ lock.l_start = 0;
+ lock.l_len = 0;
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ if (fcntl(fd, F_SETLK, &lock) == -1) {
+#else
if (flock(fd,LOCK_EX|LOCK_NB) == -1) {
+#endif
+#ifdef __sun
+ if (errno == EAGAIN) {
+#else
if (errno == EWOULDBLOCK) {
+#endif
redisLog(REDIS_WARNING,
"Sorry, the cluster configuration file %s is already used "
"by a different Redis Cluster node. Please make sure that "
diff --git a/src/cluster.c b/src/cluster.c
index 80ac66e..19043d7 100644
--- a/src/cluster.c
+++ b/src/cluster.c
@@ -74,6 +74,48 @@ void clusterSetNodeAsMaster(clusterNode *n);
void clusterDelNode(clusterNode *delnode);
sds representRedisNodeFlags(sds ci, uint16_t flags);
+#ifdef __sun
+/* Define LOCK_* for flock usage */
+#define LOCK_SH 1 /* shared lock */
+#define LOCK_EX 2 /* exclusive lock */
+#define LOCK_NB 4 /* don't block when locking */
+#define LOCK_UN 8 /* unlock */
+
+int flock(int fd, int op) {
+ int rc = 0;
+
+#if defined(F_SETLK) && defined(F_SETLKW)
+ struct flock fl = {0};
+
+ switch (op & (LOCK_EX|LOCK_SH|LOCK_UN)) {
+ case LOCK_EX:
+ fl.l_type = F_WRLCK;
+ break;
+
+ case LOCK_SH:
+ fl.l_type = F_RDLCK;
+ break;
+
+ case LOCK_UN:
+ fl.l_type = F_UNLCK;
+ break;
+
+ default:
+ errno = EINVAL;
+ return -1;
+ }
+
+ fl.l_whence = SEEK_SET;
+ rc = fcntl(fd, op & LOCK_NB ? F_SETLK : F_SETLKW, &fl);
+
+ if (rc && (errno == EAGAIN))
+ errno = EWOULDBLOCK;
+#endif
+
+ return rc;
+}
+#endif
+
/* -----------------------------------------------------------------------------
* Initialization
* -------------------------------------------------------------------------- */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment