From 4c52aa9faa4d6e3ba2243a4f45563cb4a1439bb8 Mon Sep 17 00:00:00 2001 From: SUN Date: Sun, 20 Jun 2021 16:34:04 +0800 Subject: [PATCH] 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. --- src/config.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/config.c b/src/config.c index 29de26c5b..40003f270 100644 --- a/src/config.c +++ b/src/config.c @@ -492,21 +492,8 @@ void loadServerConfigFromString(char *config) { goto loaderr; } } else if (!strcasecmp(argv[0],"logfile") && argc == 2) { - FILE *logfp; - zfree(server.logfile); 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) { loadServerConfig(argv[1], 0, NULL); } else if ((!strcasecmp(argv[0],"slaveof") || @@ -615,6 +602,20 @@ void loadServerConfigFromString(char *config) { 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. */ if (server.cluster_enabled && server.masterhost) { linenum = slaveof_linenum;