From 7f0230e8ead0c9912a9df9246737e603c9b469e5 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Fri, 9 Aug 2002 14:36:12 +0000 Subject: [PATCH] Redo memory-management in config.c so that xmlFree() is called instead of free(), hopefully fixing win32 segfaults. svn path=/trunk/icecast/; revision=3780 --- src/config.c | 105 +++++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/src/config.c b/src/config.c index 48f18127..acba5e63 100644 --- a/src/config.c +++ b/src/config.c @@ -58,28 +58,38 @@ void config_initialize(void) void config_shutdown(void) { ice_config_dir_t *dirnode, *nextdirnode; + ice_config_t *c = &_configuration; if (_config_filename) free(_config_filename); - if (_configuration.location) free(_configuration.location); - if (_configuration.admin) free(_configuration.admin); - if (_configuration.source_password) free(_configuration.source_password); - if (_configuration.hostname) free(_configuration.hostname); - if (_configuration.base_dir) free(_configuration.base_dir); - if (_configuration.log_dir) free(_configuration.log_dir); - if (_configuration.access_log) free(_configuration.access_log); - if (_configuration.error_log) free(_configuration.error_log); - if (_configuration.bind_address) free(_configuration.bind_address); - if (_configuration.user) free(_configuration.user); - if (_configuration.group) free(_configuration.group); + + if (c->location && c->location != CONFIG_DEFAULT_LOCATION) + xmlFree(c->location); + if (c->admin && c->admin != CONFIG_DEFAULT_ADMIN) + xmlFree(c->admin); + if (c->source_password && c->source_password != CONFIG_DEFAULT_SOURCE_PASSWORD) + xmlFree(c->source_password); + if (c->hostname && c->hostname != CONFIG_DEFAULT_HOSTNAME) + xmlFree(c->hostname); + if (c->base_dir && c->base_dir != CONFIG_DEFAULT_BASE_DIR) + xmlFree(c->base_dir); + if (c->log_dir && c->log_dir != CONFIG_DEFAULT_LOG_DIR) + xmlFree(c->log_dir); + if (c->access_log && c->access_log != CONFIG_DEFAULT_ACCESS_LOG) + xmlFree(c->access_log); + if (c->error_log && c->error_log != CONFIG_DEFAULT_ERROR_LOG) + xmlFree(c->error_log); + if (c->bind_address) xmlFree(c->bind_address); + if (c->user) xmlFree(c->user); + if (c->group) xmlFree(c->group); dirnode = _configuration.dir_list; while(dirnode) { nextdirnode = dirnode->next; - free(dirnode->host); + xmlFree(dirnode->host); free(dirnode); dirnode = nextdirnode; } - memset(&_configuration, 0, sizeof(ice_config_t)); + memset(c, 0, sizeof(ice_config_t)); } int config_parse_file(const char *filename) @@ -131,27 +141,27 @@ ice_config_t *config_get_config(void) static void _set_defaults(void) { - _configuration.location = (char *)strdup(CONFIG_DEFAULT_LOCATION); - _configuration.admin = (char *)strdup(CONFIG_DEFAULT_ADMIN); + _configuration.location = CONFIG_DEFAULT_LOCATION; + _configuration.admin = CONFIG_DEFAULT_ADMIN; _configuration.client_limit = CONFIG_DEFAULT_CLIENT_LIMIT; _configuration.source_limit = CONFIG_DEFAULT_SOURCE_LIMIT; _configuration.threadpool_size = CONFIG_DEFAULT_THREADPOOL_SIZE; _configuration.client_timeout = CONFIG_DEFAULT_CLIENT_TIMEOUT; _configuration.header_timeout = CONFIG_DEFAULT_HEADER_TIMEOUT; _configuration.source_timeout = CONFIG_DEFAULT_SOURCE_TIMEOUT; - _configuration.source_password = (char *)strdup(CONFIG_DEFAULT_SOURCE_PASSWORD); + _configuration.source_password = CONFIG_DEFAULT_SOURCE_PASSWORD; _configuration.touch_freq = CONFIG_DEFAULT_TOUCH_FREQ; _configuration.dir_list = NULL; - _configuration.hostname = (char *)strdup(CONFIG_DEFAULT_HOSTNAME); + _configuration.hostname = CONFIG_DEFAULT_HOSTNAME; _configuration.port = CONFIG_DEFAULT_PORT; _configuration.bind_address = NULL; _configuration.master_server = NULL; _configuration.master_server_port = CONFIG_DEFAULT_PORT; _configuration.master_update_interval = CONFIG_MASTER_UPDATE_INTERVAL; - _configuration.base_dir = (char *)strdup(CONFIG_DEFAULT_BASE_DIR); - _configuration.log_dir = (char *)strdup(CONFIG_DEFAULT_LOG_DIR); - _configuration.access_log = (char *)strdup(CONFIG_DEFAULT_ACCESS_LOG); - _configuration.error_log = (char *)strdup(CONFIG_DEFAULT_ERROR_LOG); + _configuration.base_dir = CONFIG_DEFAULT_BASE_DIR; + _configuration.log_dir = CONFIG_DEFAULT_LOG_DIR; + _configuration.access_log = CONFIG_DEFAULT_ACCESS_LOG; + _configuration.error_log = CONFIG_DEFAULT_ERROR_LOG; _configuration.loglevel = CONFIG_DEFAULT_LOG_LEVEL; _configuration.chroot = CONFIG_DEFAULT_CHROOT; _configuration.chuid = CONFIG_DEFAULT_CHUID; @@ -168,10 +178,10 @@ static void _parse_root(xmlDocPtr doc, xmlNodePtr node) if (xmlIsBlankNode(node)) continue; if (strcmp(node->name, "location") == 0) { - if (_configuration.location) free(_configuration.location); + if (_configuration.location && _configuration.location != CONFIG_DEFAULT_LOCATION) xmlFree(_configuration.location); _configuration.location = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } else if (strcmp(node->name, "admin") == 0) { - if (_configuration.admin) free(_configuration.admin); + if (_configuration.admin && _configuration.admin != CONFIG_DEFAULT_ADMIN) xmlFree(_configuration.admin); _configuration.admin = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } else if (strcmp(node->name, "source-password") == 0) { char *mount, *pass; @@ -180,21 +190,21 @@ static void _parse_root(xmlDocPtr doc, xmlNodePtr node) /* FIXME: This is a placeholder for per-mount passwords */ } else { - if (_configuration.source_password) free(_configuration.source_password); + if (_configuration.source_password && _configuration.source_password != CONFIG_DEFAULT_SOURCE_PASSWORD) xmlFree(_configuration.source_password); _configuration.source_password = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } } else if (strcmp(node->name, "hostname") == 0) { - if (_configuration.hostname) free(_configuration.hostname); + if (_configuration.hostname && _configuration.hostname != CONFIG_DEFAULT_HOSTNAME) xmlFree(_configuration.hostname); _configuration.hostname = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } else if (strcmp(node->name, "port") == 0) { tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); _configuration.port = atoi(tmp); - if (tmp) free(tmp); + if (tmp) xmlFree(tmp); } else if (strcmp(node->name, "bind-address") == 0) { - if (_configuration.bind_address) free(_configuration.bind_address); + if (_configuration.bind_address) xmlFree(_configuration.bind_address); _configuration.bind_address = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } else if (strcmp(node->name, "master-server") == 0) { - if (_configuration.master_server) free(_configuration.master_server); + if (_configuration.master_server) xmlFree(_configuration.master_server); _configuration.master_server = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } else if (strcmp(node->name, "master-server-port") == 0) { tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); @@ -227,27 +237,27 @@ static void _parse_limits(xmlDocPtr doc, xmlNodePtr node) if (strcmp(node->name, "clients") == 0) { tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); _configuration.client_limit = atoi(tmp); - if (tmp) free(tmp); + if (tmp) xmlFree(tmp); } else if (strcmp(node->name, "sources") == 0) { tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); _configuration.source_limit = atoi(tmp); - if (tmp) free(tmp); + if (tmp) xmlFree(tmp); } else if (strcmp(node->name, "threadpool") == 0) { tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); _configuration.threadpool_size = atoi(tmp); - if (tmp) free(tmp); + if (tmp) xmlFree(tmp); } else if (strcmp(node->name, "client-timeout") == 0) { tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); _configuration.client_timeout = atoi(tmp); - if (tmp) free(tmp); + if (tmp) xmlFree(tmp); } else if (strcmp(node->name, "header-timeout") == 0) { tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); _configuration.header_timeout = atoi(tmp); - if (tmp) free(tmp); + if (tmp) xmlFree(tmp); } else if (strcmp(node->name, "source-timeout") == 0) { tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); _configuration.source_timeout = atoi(tmp); - if (tmp) free(tmp); + if (tmp) xmlFree(tmp); } } while ((node = node->next)); } @@ -265,7 +275,7 @@ static void _parse_directory(xmlDocPtr doc, xmlNodePtr node) } else if (strcmp(node->name, "touch-freq") == 0) { tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); _configuration.touch_freq = atoi(tmp); - if (tmp) free(tmp); + if (tmp) xmlFree(tmp); } } while ((node = node->next)); } @@ -277,13 +287,13 @@ static void _parse_paths(xmlDocPtr doc, xmlNodePtr node) if (xmlIsBlankNode(node)) continue; if (strcmp(node->name, "basedir") == 0) { - if (_configuration.base_dir) free(_configuration.base_dir); + if (_configuration.base_dir && _configuration.base_dir != CONFIG_DEFAULT_BASE_DIR) xmlFree(_configuration.base_dir); _configuration.base_dir = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } else if (strcmp(node->name, "logdir") == 0) { - if (_configuration.log_dir) free(_configuration.log_dir); + if (_configuration.log_dir && _configuration.log_dir != CONFIG_DEFAULT_LOG_DIR) xmlFree(_configuration.log_dir); _configuration.log_dir = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } else if (strcmp(node->name, "webroot") == 0) { - if (_configuration.webroot_dir) free(_configuration.webroot_dir); + if (_configuration.webroot_dir && _configuration.webroot_dir != CONFIG_DEFAULT_WEBROOT_DIR) xmlFree(_configuration.webroot_dir); _configuration.webroot_dir = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } } while ((node = node->next)); @@ -296,15 +306,15 @@ static void _parse_logging(xmlDocPtr doc, xmlNodePtr node) if (xmlIsBlankNode(node)) continue; if (strcmp(node->name, "accesslog") == 0) { - if (_configuration.access_log) free(_configuration.access_log); + if (_configuration.access_log && _configuration.access_log != CONFIG_DEFAULT_ACCESS_LOG) xmlFree(_configuration.access_log); _configuration.access_log = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } else if (strcmp(node->name, "errorlog") == 0) { - if (_configuration.error_log) free(_configuration.error_log); + if (_configuration.error_log && _configuration.error_log != CONFIG_DEFAULT_ERROR_LOG) xmlFree(_configuration.error_log); _configuration.error_log = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } else if (strcmp(node->name, "loglevel") == 0) { char *tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); _configuration.loglevel = atoi(tmp); - if (tmp) free(tmp); + if (tmp) xmlFree(tmp); } } while ((node = node->next)); } @@ -321,7 +331,7 @@ static void _parse_security(xmlDocPtr doc, xmlNodePtr node) if (strcmp(node->name, "chroot") == 0) { tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); _configuration.chroot = atoi(tmp); - if (tmp) free(tmp); + if (tmp) xmlFree(tmp); } else if (strcmp(node->name, "changeowner") == 0) { _configuration.chuid = 1; oldnode = node; @@ -330,10 +340,10 @@ static void _parse_security(xmlDocPtr doc, xmlNodePtr node) if(node == NULL) break; if(xmlIsBlankNode(node)) continue; if(strcmp(node->name, "user") == 0) { - if(_configuration.user) free(_configuration.user); + if(_configuration.user) xmlFree(_configuration.user); _configuration.user = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } else if(strcmp(node->name, "group") == 0) { - if(_configuration.group) free(_configuration.group); + if(_configuration.group) xmlFree(_configuration.group); _configuration.group = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); } } while((node = node->next)); @@ -363,7 +373,7 @@ static void _add_server(xmlDocPtr doc, xmlNodePtr node) } else if (strcmp(node->name, "touch-freq") == 0) { tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); server->touch_freq = atoi(tmp); - if (tmp) free(tmp); + if (tmp) xmlFree(tmp); } server->next = NULL; } while ((node = node->next)); @@ -382,11 +392,6 @@ static void _add_server(xmlDocPtr doc, xmlNodePtr node) addnode = 0; } - if (server) { - if (server->host) free(server->host); - free(server); - server = NULL; - } }