Index: main/config.c =================================================================== --- main/config.c (revision 154022) +++ main/config.c (working copy) @@ -1005,13 +1005,13 @@ { FILE *f = NULL; int fd = -1; - char fn[256], fntmp[256]; + char fn[256], fntmp[256], fnpath[256], *slashptr; char date[256]=""; time_t t; struct ast_variable *var; struct ast_category *cat; struct ast_comment *cmt; - struct stat s; + struct stat s, s2; int blanklines = 0; int stat_result = 0; @@ -1022,6 +1022,16 @@ snprintf(fntmp, sizeof(fntmp), "%s/%s.XXXXXX", ast_config_AST_CONFIG_DIR, configfile); snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile); } + + ast_copy_string(fnpath, fn, sizeof(fnpath)); + if ((slashptr = strrchr(fnpath, '/'))) { + *slashptr = '\0'; + } + if (ast_lock_path(fnpath) != AST_LOCK_SUCCESS) { + ast_log(LOG_ERROR, "Unable to lock path '%s'\n", fnpath); + return -1; + } + time(&t); ast_copy_string(date, ctime(&t), sizeof(date)); if ((fd = mkstemp(fntmp)) > 0 && (f = fdopen(fd, "w")) != NULL) { @@ -1123,11 +1133,18 @@ ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno)); if (fd > -1) close(fd); + ast_unlock_path(fnpath); return -1; } if (!(stat_result = stat(fn, &s))) { fchmod(fd, s.st_mode); + } else { + ast_log(LOG_WARNING, "Unable to stat config file: %s: %s\n", fn, strerror(errno)); } + if (fstat(fd, &s2) || s2.st_size == 0) { + ast_log(LOG_ERROR, "Resulting file %s is of size 0. This is a hard error. I cannot continue.\n", fntmp); + stat_result = -1; + } fclose(f); if ((!stat_result && unlink(fn)) || link(fntmp, fn)) { if (option_debug) @@ -1135,9 +1152,11 @@ if (option_verbose > 1) ast_verbose(VERBOSE_PREFIX_2 "Unable to write %s (%s)", fn, strerror(errno)); unlink(fntmp); + ast_unlock_path(fnpath); return -1; } unlink(fntmp); + ast_unlock_path(fnpath); return 0; }