summaryrefslogtreecommitdiff
blob: 3914e65608ed2094a0c3d2ea7a4b490e74ec6cb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
diff -Nuar a/logrotate.c b/logrotate.c
--- a/logrotate.c	2016-01-25 19:55:46.740039999 +0100
+++ b/logrotate.c	2016-01-25 20:03:15.290049030 +0100
@@ -367,15 +367,18 @@
 int createOutputFile(char *fileName, int flags, struct stat *sb, acl_type acl, int force_mode)
 {
     int fd;
-	struct stat sb_create;
-	int acl_set = 0;
-
-	fd = open(fileName, (flags | O_EXCL | O_NOFOLLOW),
-		(S_IRUSR | S_IWUSR) & sb->st_mode);
+    int acl_set = 0;
+    struct stat sb_create;
+    char template[PATH_MAX + 1];
+    mode_t umask_value;
+    snprintf(template, PATH_MAX, "%s/logrotate_temp.XXXXXX", ourDirName(fileName));
+    umask_value = umask(0000);
+    fd = mkostemp(template, (flags | O_EXCL | O_NOFOLLOW));
+    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)) {
@@ -425,6 +428,12 @@
 		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;
 }