Change initialization to allow listening on both a port and socket
This commit is contained in:
parent
7e91f971f7
commit
a5639e7dd9
@ -20,7 +20,8 @@ daemonize no
|
|||||||
# default. You can specify a custom pid file location here.
|
# default. You can specify a custom pid file location here.
|
||||||
pidfile /var/run/redis.pid
|
pidfile /var/run/redis.pid
|
||||||
|
|
||||||
# Accept connections on the specified port, default is 6379
|
# Accept connections on the specified port, default is 6379.
|
||||||
|
# Use port -1 to disable listening on a network interface.
|
||||||
port 6379
|
port 6379
|
||||||
|
|
||||||
# If you want you can bind a single interface, if the bind option is not
|
# If you want you can bind a single interface, if the bind option is not
|
||||||
@ -28,6 +29,11 @@ port 6379
|
|||||||
#
|
#
|
||||||
# bind 127.0.0.1
|
# bind 127.0.0.1
|
||||||
|
|
||||||
|
# Specify the path for the domain socket that will be used to listen for
|
||||||
|
# incoming connections. If not specified, Redis will not use a domain socket.
|
||||||
|
#
|
||||||
|
# socket /tmp/redis.sock
|
||||||
|
|
||||||
# Close the connection after a client is idle for N seconds (0 to disable)
|
# Close the connection after a client is idle for N seconds (0 to disable)
|
||||||
timeout 300
|
timeout 300
|
||||||
|
|
||||||
|
@ -588,7 +588,8 @@ int rewriteAppendOnlyFileBackground(void) {
|
|||||||
char tmpfile[256];
|
char tmpfile[256];
|
||||||
|
|
||||||
if (server.vm_enabled) vmReopenSwapFile();
|
if (server.vm_enabled) vmReopenSwapFile();
|
||||||
close(server.fd);
|
if (server.ipfd > 0) close(server.ipfd);
|
||||||
|
if (server.sofd > 0) close(server.sofd);
|
||||||
snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
|
snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
|
||||||
if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) {
|
if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) {
|
||||||
_exit(0);
|
_exit(0);
|
||||||
|
@ -66,11 +66,15 @@ void loadServerConfig(char *filename) {
|
|||||||
}
|
}
|
||||||
} else if (!strcasecmp(argv[0],"port") && argc == 2) {
|
} else if (!strcasecmp(argv[0],"port") && argc == 2) {
|
||||||
server.port = atoi(argv[1]);
|
server.port = atoi(argv[1]);
|
||||||
if (server.port < 1 || server.port > 65535) {
|
if (server.port != -1 &&
|
||||||
|
(server.port < 1 || server.port > 65535))
|
||||||
|
{
|
||||||
err = "Invalid port"; goto loaderr;
|
err = "Invalid port"; goto loaderr;
|
||||||
}
|
}
|
||||||
} else if (!strcasecmp(argv[0],"bind") && argc == 2) {
|
} else if (!strcasecmp(argv[0],"bind") && argc == 2) {
|
||||||
server.bindaddr = zstrdup(argv[1]);
|
server.bindaddr = zstrdup(argv[1]);
|
||||||
|
} else if (!strcasecmp(argv[0],"socket") && argc == 2) {
|
||||||
|
server.sockpath = zstrdup(argv[1]);
|
||||||
} else if (!strcasecmp(argv[0],"save") && argc == 3) {
|
} else if (!strcasecmp(argv[0],"save") && argc == 3) {
|
||||||
int seconds = atoi(argv[1]);
|
int seconds = atoi(argv[1]);
|
||||||
int changes = atoi(argv[2]);
|
int changes = atoi(argv[2]);
|
||||||
|
@ -448,7 +448,8 @@ int rdbSaveBackground(char *filename) {
|
|||||||
if ((childpid = fork()) == 0) {
|
if ((childpid = fork()) == 0) {
|
||||||
/* Child */
|
/* Child */
|
||||||
if (server.vm_enabled) vmReopenSwapFile();
|
if (server.vm_enabled) vmReopenSwapFile();
|
||||||
close(server.fd);
|
if (server.ipfd > 0) close(server.ipfd);
|
||||||
|
if (server.sofd > 0) close(server.sofd);
|
||||||
if (rdbSave(filename) == REDIS_OK) {
|
if (rdbSave(filename) == REDIS_OK) {
|
||||||
_exit(0);
|
_exit(0);
|
||||||
} else {
|
} else {
|
||||||
|
39
src/redis.c
39
src/redis.c
@ -696,13 +696,16 @@ void createSharedObjects(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void initServerConfig() {
|
void initServerConfig() {
|
||||||
server.dbnum = REDIS_DEFAULT_DBNUM;
|
|
||||||
server.port = REDIS_SERVERPORT;
|
server.port = REDIS_SERVERPORT;
|
||||||
|
server.bindaddr = NULL;
|
||||||
|
server.sockpath = NULL;
|
||||||
|
server.ipfd = -1;
|
||||||
|
server.sofd = -1;
|
||||||
|
server.dbnum = REDIS_DEFAULT_DBNUM;
|
||||||
server.verbosity = REDIS_VERBOSE;
|
server.verbosity = REDIS_VERBOSE;
|
||||||
server.maxidletime = REDIS_MAXIDLETIME;
|
server.maxidletime = REDIS_MAXIDLETIME;
|
||||||
server.saveparams = NULL;
|
server.saveparams = NULL;
|
||||||
server.logfile = NULL; /* NULL = log on standard output */
|
server.logfile = NULL; /* NULL = log on standard output */
|
||||||
server.bindaddr = NULL;
|
|
||||||
server.glueoutputbuf = 1;
|
server.glueoutputbuf = 1;
|
||||||
server.daemonize = 0;
|
server.daemonize = 0;
|
||||||
server.appendonly = 0;
|
server.appendonly = 0;
|
||||||
@ -773,16 +776,23 @@ void initServer() {
|
|||||||
createSharedObjects();
|
createSharedObjects();
|
||||||
server.el = aeCreateEventLoop();
|
server.el = aeCreateEventLoop();
|
||||||
server.db = zmalloc(sizeof(redisDb)*server.dbnum);
|
server.db = zmalloc(sizeof(redisDb)*server.dbnum);
|
||||||
if (server.bindaddr == NULL || inet_aton(server.bindaddr,NULL)) {
|
if (server.port > 0) {
|
||||||
/* Either no address given, or it can be correctly parsed. */
|
server.ipfd = anetTcpServer(server.neterr,server.port,server.bindaddr);
|
||||||
server.fd = anetTcpServer(server.neterr, server.port, server.bindaddr);
|
if (server.ipfd == ANET_ERR) {
|
||||||
} else {
|
redisLog(REDIS_WARNING, "Opening port: %s", server.neterr);
|
||||||
/* Bind to a socket */
|
exit(1);
|
||||||
unlink(server.bindaddr); /* don't care if this fails */
|
|
||||||
server.fd = anetUnixServer(server.neterr,server.bindaddr);
|
|
||||||
}
|
}
|
||||||
if (server.fd == -1) {
|
}
|
||||||
redisLog(REDIS_WARNING, "Opening port/socket: %s", server.neterr);
|
if (server.sockpath != NULL) {
|
||||||
|
unlink(server.sockpath); /* don't care if this fails */
|
||||||
|
server.sofd = anetUnixServer(server.neterr,server.sockpath);
|
||||||
|
if (server.sofd == ANET_ERR) {
|
||||||
|
redisLog(REDIS_WARNING, "Opening socket: %s", server.neterr);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (server.ipfd < 0 && server.sofd < 0) {
|
||||||
|
redisLog(REDIS_WARNING, "Configured to not listen anywhere, exiting.");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
for (j = 0; j < server.dbnum; j++) {
|
for (j = 0; j < server.dbnum; j++) {
|
||||||
@ -811,7 +821,9 @@ void initServer() {
|
|||||||
server.stat_starttime = time(NULL);
|
server.stat_starttime = time(NULL);
|
||||||
server.unixtime = time(NULL);
|
server.unixtime = time(NULL);
|
||||||
aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL);
|
aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL);
|
||||||
if (aeCreateFileEvent(server.el, server.fd, AE_READABLE,
|
if (server.ipfd > 0 && aeCreateFileEvent(server.el,server.ipfd,AE_READABLE,
|
||||||
|
acceptHandler,NULL) == AE_ERR) oom("creating file event");
|
||||||
|
if (server.sofd > 0 && aeCreateFileEvent(server.el,server.sofd,AE_READABLE,
|
||||||
acceptHandler,NULL) == AE_ERR) oom("creating file event");
|
acceptHandler,NULL) == AE_ERR) oom("creating file event");
|
||||||
|
|
||||||
if (server.appendonly) {
|
if (server.appendonly) {
|
||||||
@ -1423,7 +1435,10 @@ int main(int argc, char **argv) {
|
|||||||
if (rdbLoad(server.dbfilename) == REDIS_OK)
|
if (rdbLoad(server.dbfilename) == REDIS_OK)
|
||||||
redisLog(REDIS_NOTICE,"DB loaded from disk: %ld seconds",time(NULL)-start);
|
redisLog(REDIS_NOTICE,"DB loaded from disk: %ld seconds",time(NULL)-start);
|
||||||
}
|
}
|
||||||
|
if (server.ipfd > 0)
|
||||||
redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
|
redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
|
||||||
|
if (server.sofd > 0)
|
||||||
|
redisLog(REDIS_NOTICE,"The server is now ready to accept connections at %s", server.sockpath);
|
||||||
aeSetBeforeSleepProc(server.el,beforeSleep);
|
aeSetBeforeSleepProc(server.el,beforeSleep);
|
||||||
aeMain(server.el);
|
aeMain(server.el);
|
||||||
aeDeleteEventLoop(server.el);
|
aeDeleteEventLoop(server.el);
|
||||||
|
@ -329,7 +329,10 @@ struct sharedObjectsStruct {
|
|||||||
struct redisServer {
|
struct redisServer {
|
||||||
pthread_t mainthread;
|
pthread_t mainthread;
|
||||||
int port;
|
int port;
|
||||||
int fd;
|
char *bindaddr;
|
||||||
|
char *sockpath;
|
||||||
|
int ipfd;
|
||||||
|
int sofd;
|
||||||
redisDb *db;
|
redisDb *db;
|
||||||
long long dirty; /* changes to DB from the last save */
|
long long dirty; /* changes to DB from the last save */
|
||||||
list *clients;
|
list *clients;
|
||||||
@ -365,7 +368,6 @@ struct redisServer {
|
|||||||
struct saveparam *saveparams;
|
struct saveparam *saveparams;
|
||||||
int saveparamslen;
|
int saveparamslen;
|
||||||
char *logfile;
|
char *logfile;
|
||||||
char *bindaddr;
|
|
||||||
char *dbfilename;
|
char *dbfilename;
|
||||||
char *appendfilename;
|
char *appendfilename;
|
||||||
char *requirepass;
|
char *requirepass;
|
||||||
|
Loading…
Reference in New Issue
Block a user