blob: 28fd0375ac320c65fc12069408ebc78fbc5a69e9 [file] [log] [blame]
diff -Nur a/logrotate.c b/logrotate.c
--- a/logrotate.c 2013-05-10 21:06:07.459903230 +0200
+++ b/logrotate.c 2013-05-10 21:15:15.849900150 +0200
@@ -300,14 +300,20 @@
int createOutputFile(char *fileName, int flags, struct stat *sb, acl_type acl, int force_mode)
{
int fd;
- struct stat sb_create;
- fd = open(fileName, (flags | O_EXCL | O_NOFOLLOW),
- (S_IRUSR | S_IWUSR) & sb->st_mode);
+ struct stat sb_create;
+ char template[PATH_MAX + 1];
+ char *fname;
+ mode_t umask_value;
+ snprintf(template, PATH_MAX, "%s/logrotate_temp.XXXXXX", ourDirName(fileName));
+ 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));
+ message(MESS_ERROR, "error creating unique temp file: %s\n",
+ strerror(errno));
return -1;
}
if (fchmod(fd, (S_IRUSR | S_IWUSR) & sb->st_mode)) {
@@ -352,6 +358,13 @@
}
#endif
+ if (rename(template, fileName)) {
+ message(MESS_ERROR, "error renaming temp file to %s: %s\n",
+ fileName, strerror(errno));
+ close(fd);
+ return -1;
+ }
+
return fd;
}