Unverified Commit 4c52aa9f authored by SUN's avatar SUN Committed by GitHub
Browse files

chdir to dir before fopen logfile in loadServerConfigFromString() (#6741)

Open the log file only after parsing the entire config file, so that it's
location isn't dependent on the order of configs (`dir` and `logfile`).
Also solves the problem of creating multiple log files if the `logfile`
directive appears many times in the config file.
parent 561c69c2
...@@ -492,21 +492,8 @@ void loadServerConfigFromString(char *config) { ...@@ -492,21 +492,8 @@ void loadServerConfigFromString(char *config) {
goto loaderr; goto loaderr;
} }
} else if (!strcasecmp(argv[0],"logfile") && argc == 2) { } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
FILE *logfp;
zfree(server.logfile); zfree(server.logfile);
server.logfile = zstrdup(argv[1]); server.logfile = zstrdup(argv[1]);
if (server.logfile[0] != '\0') {
/* Test if we are able to open the file. The server will not
* be able to abort just for this problem later... */
logfp = fopen(server.logfile,"a");
if (logfp == NULL) {
err = sdscatprintf(sdsempty(),
"Can't open the log file: %s", strerror(errno));
goto loaderr;
}
fclose(logfp);
}
} else if (!strcasecmp(argv[0],"include") && argc == 2) { } else if (!strcasecmp(argv[0],"include") && argc == 2) {
loadServerConfig(argv[1], 0, NULL); loadServerConfig(argv[1], 0, NULL);
} else if ((!strcasecmp(argv[0],"slaveof") || } else if ((!strcasecmp(argv[0],"slaveof") ||
...@@ -615,6 +602,20 @@ void loadServerConfigFromString(char *config) { ...@@ -615,6 +602,20 @@ void loadServerConfigFromString(char *config) {
sdsfreesplitres(argv,argc); sdsfreesplitres(argv,argc);
} }
if (server.logfile[0] != '\0') {
FILE *logfp;
/* Test if we are able to open the file. The server will not
* be able to abort just for this problem later... */
logfp = fopen(server.logfile,"a");
if (logfp == NULL) {
err = sdscatprintf(sdsempty(),
"Can't open the log file: %s", strerror(errno));
goto loaderr;
}
fclose(logfp);
}
/* Sanity checks. */ /* Sanity checks. */
if (server.cluster_enabled && server.masterhost) { if (server.cluster_enabled && server.masterhost) {
linenum = slaveof_linenum; linenum = slaveof_linenum;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment