Rewrite the configuration file at "path". * If the configuration file already exists, we try at best to retain comments * and overall structure. * * Configuration parameters that are at their default value, unless already * explicitly included in the old configuration file, are not rewritten. * The force_all flag overrides this behavior and forces everything to be * written. This is current
| 1934 | * |
| 1935 | * On error -1 is returned and errno is set accordingly, otherwise 0. */ |
| 1936 | int rewriteConfig(char *path, int force_all) { |
| 1937 | struct rewriteConfigState *state; |
| 1938 | sds newcontent; |
| 1939 | int retval; |
| 1940 | |
| 1941 | /* Step 1: read the old config into our rewrite state. */ |
| 1942 | if ((state = rewriteConfigReadOldFile(path)) == NULL) return -1; |
| 1943 | if (force_all) state->force_all = 1; |
| 1944 | |
| 1945 | /* Step 2: rewrite every single option, replacing or appending it inside |
| 1946 | * the rewrite state. */ |
| 1947 | |
| 1948 | /* Iterate the configs that are standard */ |
| 1949 | for (standardConfig *config = configs; config->name != NULL; config++) { |
| 1950 | config->interface.rewrite(config->data, config->name, state); |
| 1951 | } |
| 1952 | |
| 1953 | rewriteConfigBindOption(state); |
| 1954 | rewriteConfigOctalOption(state,"unixsocketperm",g_pserver->unixsocketperm,CONFIG_DEFAULT_UNIX_SOCKET_PERM); |
| 1955 | rewriteConfigStringOption(state,"logfile",g_pserver->logfile,CONFIG_DEFAULT_LOGFILE); |
| 1956 | rewriteConfigSaveOption(state); |
| 1957 | rewriteConfigUserOption(state); |
| 1958 | rewriteConfigDirOption(state); |
| 1959 | rewriteConfigSlaveofOption(state,"replicaof"); |
| 1960 | rewriteConfigStringOption(state,"cluster-config-file",g_pserver->cluster_configfile,CONFIG_DEFAULT_CLUSTER_CONFIG_FILE); |
| 1961 | rewriteConfigNotifykeyspaceeventsOption(state); |
| 1962 | rewriteConfigClientoutputbufferlimitOption(state); |
| 1963 | rewriteConfigYesNoOption(state,"active-replica",g_pserver->fActiveReplica,CONFIG_DEFAULT_ACTIVE_REPLICA); |
| 1964 | rewriteConfigStringOption(state, "version-override",KEYDB_SET_VERSION,KEYDB_REAL_VERSION); |
| 1965 | rewriteConfigOOMScoreAdjValuesOption(state); |
| 1966 | |
| 1967 | if (!g_pserver->tls_allowlist.empty()) { |
| 1968 | sds conf = sdsnew("tls-allowlist "); |
| 1969 | for (auto &elem : g_pserver->tls_allowlist) { |
| 1970 | conf = sdscatsds(conf, (sds)elem.get()); |
| 1971 | conf = sdscat(conf, " "); |
| 1972 | } |
| 1973 | // trim the trailing space |
| 1974 | sdsrange(conf, 0, -1); |
| 1975 | rewriteConfigRewriteLine(state,"tls-allowlist",conf,1 /*force*/); |
| 1976 | // note: conf is owned by rewriteConfigRewriteLine - no need to free |
| 1977 | } else { |
| 1978 | rewriteConfigMarkAsProcessed(state, "tls-allowlist"); // ensure the line is removed if it existed |
| 1979 | } |
| 1980 | |
| 1981 | /* Rewrite Sentinel config if in Sentinel mode. */ |
| 1982 | if (g_pserver->sentinel_mode) rewriteConfigSentinelOption(state); |
| 1983 | |
| 1984 | /* Step 3: remove all the orphaned lines in the old file, that is, lines |
| 1985 | * that were used by a config option and are no longer used, like in case |
| 1986 | * of multiple "save" options or duplicated options. */ |
| 1987 | rewriteConfigRemoveOrphaned(state); |
| 1988 | |
| 1989 | /* Step 4: generate a new configuration file from the modified state |
| 1990 | * and write it into the original file. */ |
| 1991 | newcontent = rewriteConfigGetContentFromState(state); |
| 1992 | retval = rewriteConfigOverwriteFile(cserver.configfile,newcontent); |
| 1993 |
no test coverage detected