diff '--exclude-from=/home/dang/.scripts/diffrc' -up -ruN logrotate-3.8.0.orig/logrotate.c logrotate-3.8.0/logrotate.c --- logrotate-3.8.0.orig/logrotate.c 2011-06-21 04:12:02.000000000 -0400 +++ logrotate-3.8.0/logrotate.c 2011-07-18 11:40:48.775713520 -0400 @@ -260,33 +260,50 @@ static int runScript(char *logfn, char * int createOutputFile(char *fileName, int flags, struct stat *sb) { int fd; + char template[PATH_MAX + 1]; + char *fname; + mode_t umask_value; + snprintf(template, PATH_MAX, "%s/logrotate_temp.XXXXXX", ourDirName(fileName)); - fd = open(fileName, (flags | O_EXCL | O_NOFOLLOW), - (S_IRUSR | S_IWUSR) & sb->st_mode); + umask_value = umask(0000); + fname = mktemp(template); + fd = open(fname, (flags | O_EXCL | O_NOFOLLOW), (S_IRUSR | S_IWUSR) & sb->st_mode); + umask(umask_value); - if (fd < 0) { - message(MESS_ERROR, "error creating output file %s: %s\n", - fileName, strerror(errno)); - return -1; - } - if (fchmod(fd, (S_IRUSR | S_IWUSR) & sb->st_mode)) { - message(MESS_ERROR, "error setting mode of %s: %s\n", - fileName, strerror(errno)); - close(fd); - return -1; - } - if (fchown(fd, sb->st_uid, sb->st_gid)) { - message(MESS_ERROR, "error setting owner of %s: %s\n", - fileName, strerror(errno)); - close(fd); - return -1; - } - if (fchmod(fd, sb->st_mode)) { - message(MESS_ERROR, "error setting mode of %s: %s\n", - fileName, strerror(errno)); - close(fd); - return -1; + if (fd < 0) { + message(MESS_ERROR, "error creating unique temp file: %s\n", + strerror(errno)); + return -1; + } + + if (fchmod(fd, (S_IRUSR | S_IWUSR) & sb->st_mode)) { + message(MESS_ERROR, "error setting mode of %s: %s\n", + fileName, strerror(errno)); + close(fd); + return -1; } + + if (fchown(fd, sb->st_uid, sb->st_gid)) { + message(MESS_ERROR, "error setting owner of %s: %s\n", + fileName, strerror(errno)); + close(fd); + return -1; + } + + if (fchmod(fd, sb->st_mode)) { + message(MESS_ERROR, "error setting mode of %s: %s\n", + fileName, strerror(errno)); + close(fd); + return -1; + } + + if (rename(template, fileName)) { + message(MESS_ERROR, "error renaming temp file to %s: %s\n", + fileName, strerror(errno)); + close(fd); + return -1; + } + return fd; }