Skip to content

Commit

Permalink
reject mutable operation when slave readonly mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Song committed Mar 21, 2011
1 parent 1dd4756 commit 67b319e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/config.c
Expand Up @@ -208,6 +208,10 @@ void loadServerConfig(char *filename) {
if ((server.daemonize = yesnotoi(argv[1])) == -1) {
err = "argument must be 'yes' or 'no'"; goto loaderr;
}
} else if (!strcasecmp(argv[0],"slave-readonly") && argc == 2) {
if ((server.slave_readonly= yesnotoi(argv[1])) == -1) {
err = "argument must be 'yes' or 'no'"; goto loaderr;
}
} else if (!strcasecmp(argv[0],"appendonly") && argc == 2) {
if ((server.appendonly = yesnotoi(argv[1])) == -1) {
err = "argument must be 'yes' or 'no'"; goto loaderr;
Expand Down
7 changes: 7 additions & 0 deletions src/redis.c
Expand Up @@ -795,6 +795,7 @@ void initServerConfig() {
server.syslog_ident = zstrdup("redis");
server.syslog_facility = LOG_LOCAL0;
server.daemonize = 0;
server.slave_readonly = 0;
server.appendonly = 0;
server.appendfsync = APPENDFSYNC_EVERYSEC;
server.no_appendfsync_on_rewrite = 0;
Expand Down Expand Up @@ -1051,6 +1052,12 @@ int processCommand(redisClient *c) {
return REDIS_OK;
}

/* Check readonly flag */
if (server.slave_readonly && (c->flags & REDIS_MASTER || c->flags & REDIS_SLAVE) && (cmd->flags & REDIS_CMD_IS_MUTABLE)) {
addReplyError(c,"operation is mutable, not permitted");
return REDIS_OK;
}

/* Handle the maxmemory directive.
*
* First we try to free some memory if possible (if there are volatile
Expand Down
1 change: 1 addition & 0 deletions src/redis.h
Expand Up @@ -404,6 +404,7 @@ struct redisServer {
int no_appendfsync_on_rewrite;
int shutdown_asap;
int activerehashing;
int slave_readonly;
char *requirepass;
/* Persistence */
long long dirty; /* changes to DB from the last save */
Expand Down

1 comment on commit 67b319e

@jokea
Copy link

@jokea jokea commented on 67b319e Nov 1, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (server.slave_readonly && (c->flags & REDIS_MASTER || c->flags & REDIS_SLAVE) && (cmd->flags & REDIS_CMD_IS_MUTABLE)) {
should be:
if (server.slave_readonly && (server.masterhost != NULL) && (!(c->flags & REDIS_MASTER)) && (cmd->flags & REDIS_CMD_IS_MUTABLE)) {

Please sign in to comment.