autofs-5.0.8 - fix macro_addvar() and move init to main thread From: Ian Kent - move macro table initialization to the main thread. - change to using malloc() instead of realloc() in macro_global_addvar() and macro_addvar(). - fix possible incorrect return from macro_addvar(). --- CHANGELOG | 1 + daemon/automount.c | 16 ++++++++++++++++ lib/macros.c | 25 ++++++++++++++++++++----- modules/parse_sun.c | 9 --------- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d966260..cf31fb4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -26,6 +26,7 @@ - check for bind onto self in mount_bind.c. - fix symlink expire. - dont clobber mapent for negative cache. +- fix macro_addvar() and move init to main thread. 17/10/2013 autofs-5.0.8 ======================= diff --git a/daemon/automount.c b/daemon/automount.c index 0cb8ca4..a21d061 100644 --- a/daemon/automount.c +++ b/daemon/automount.c @@ -2129,6 +2129,9 @@ int main(int argc, char *argv[]) program); #endif + /* Get processor information for predefined escapes */ + macro_init(); + if (dumpmaps) { struct master_mapent *entry; struct list_head *head, *p; @@ -2152,6 +2155,7 @@ int main(int argc, char *argv[]) master_list = master_new(master, timeout, ghost); if (!master_list) { printf("%s: can't create master map", program); + macro_free_global_table(); exit(1); } @@ -2163,6 +2167,7 @@ int main(int argc, char *argv[]) if (!nc) { printf("%s: failed to init null map cache for %s", program, master_list->name); + macro_free_global_table(); exit(1); } master_list->nc = nc; @@ -2187,6 +2192,7 @@ int main(int argc, char *argv[]) master_free_mapent(entry); } master_kill(master_list); + macro_free_global_table(); exit(0); } @@ -2198,6 +2204,7 @@ int main(int argc, char *argv[]) if (!master_list) { printf("%s: can't create master map %s", program, argv[0]); + macro_free_global_table(); exit(1); } @@ -2209,6 +2216,7 @@ int main(int argc, char *argv[]) res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat)); close(start_pipefd[1]); release_flag_file(); + macro_free_global_table(); exit(1); } @@ -2218,6 +2226,7 @@ int main(int argc, char *argv[]) res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat)); close(start_pipefd[1]); release_flag_file(); + macro_free_global_table(); exit(1); } @@ -2228,6 +2237,7 @@ int main(int argc, char *argv[]) res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat)); close(start_pipefd[1]); release_flag_file(); + macro_free_global_table(); exit(1); } @@ -2239,6 +2249,7 @@ int main(int argc, char *argv[]) res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat)); close(start_pipefd[1]); release_flag_file(); + macro_free_global_table(); exit(1); } #endif @@ -2257,6 +2268,7 @@ int main(int argc, char *argv[]) res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat)); close(start_pipefd[1]); release_flag_file(); + macro_free_global_table(); exit(1); } @@ -2268,6 +2280,7 @@ int main(int argc, char *argv[]) res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat)); close(start_pipefd[1]); release_flag_file(); + macro_free_global_table(); exit(1); } @@ -2277,6 +2290,7 @@ int main(int argc, char *argv[]) res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat)); close(start_pipefd[1]); release_flag_file(); + macro_free_global_table(); exit(1); } @@ -2299,6 +2313,7 @@ int main(int argc, char *argv[]) res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat)); close(start_pipefd[1]); release_flag_file(); + macro_free_global_table(); exit(3); } @@ -2323,6 +2338,7 @@ int main(int argc, char *argv[]) } closelog(); release_flag_file(); + macro_free_global_table(); #ifdef TIRPC_WORKAROUND if (dh_tirpc) diff --git a/lib/macros.c b/lib/macros.c index c858f41..5109abc 100644 --- a/lib/macros.c +++ b/lib/macros.c @@ -35,6 +35,7 @@ static struct substvar }; static struct substvar *system_table = &sv_osvers; +static unsigned int macro_init_done = 0; static pthread_mutex_t table_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t macro_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -62,6 +63,12 @@ void dump_table(struct substvar *table) /* Get processor information for predefined macro definitions */ void macro_init(void) { + macro_lock(); + if (macro_init_done) { + macro_unlock(); + return; + } + uname(&un); /* * uname -p is not defined on Linux. Make it the same as @@ -71,6 +78,10 @@ void macro_init(void) if (processor[0] == 'i' && processor[1] >= '3' && !strcmp(processor + 2, "86")) processor[1] = '3'; + + macro_init_done = 1; + macro_unlock(); + return; } int macro_is_systemvar(const char *str, int len) @@ -118,10 +129,11 @@ int macro_global_addvar(const char *str, int len, const char *value) } if (sv && !sv->readonly) { - char *this = realloc(sv->val, strlen(value) + 1); + char *this = malloc(strlen(value) + 1); if (!this) goto done; - strcat(this, value); + strcpy(this, value); + free(sv->val); sv->val = this; ret = 1; } else { @@ -213,13 +225,16 @@ macro_addvar(struct substvar *table, const char *str, int len, const char *value } if (lv) { - char *this = realloc(lv->val, strlen(value) + 1); + char *this = malloc(strlen(value) + 1); if (!this) { lv = table; goto done; } - strcat(this, value); + strcpy(this, value); + free(lv->val); lv->val = this; + if (lv != table) + lv = table; } else { struct substvar *new; char *def, *val; @@ -413,7 +428,7 @@ macro_findvar(const struct substvar *table, const char *str, int len) etmp[len]='\0'; if ((value=getenv(etmp)) != NULL) { - lv_var = macro_addvar(table, str, len, value); + lv_var = macro_addvar((struct substvar *) table, str, len, value); return(lv_var); } #endif diff --git a/modules/parse_sun.c b/modules/parse_sun.c index 9a877c8..8e5c60c 100644 --- a/modules/parse_sun.c +++ b/modules/parse_sun.c @@ -41,7 +41,6 @@ int parse_version = AUTOFS_PARSE_VERSION; /* Required by protocol */ static struct mount_mod *mount_nfs = NULL; static int init_ctr = 0; -static int macro_init_done = 0; static pthread_mutex_t instance_mutex = PTHREAD_MUTEX_INITIALIZER; static void instance_mutex_lock(void) @@ -243,14 +242,6 @@ int parse_init(int argc, const char *const *argv, void **context) int i, bval; unsigned int append_options; - /* Get processor information for predefined escapes */ - macro_lock(); - if (!macro_init_done) { - macro_init_done = 1; - macro_init(); - } - macro_unlock(); - /* Set up context and escape chain */ if (!(ctxt = (struct parse_context *) malloc(sizeof(struct parse_context)))) {