summaryrefslogtreecommitdiffstats
path: root/main/logrotate/logrotate-3.7.9-atomic-create.patch
blob: b888dc0231a4d29d2d14c0cf4be1a25ca895531a (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
diff --git a/logrotate.c b/logrotate.c
index 3748918..fbe232a 100644
--- a/logrotate.c
+++ b/logrotate.c
@@ -194,31 +194,41 @@ static int runScript(char *logfn, char *script)
 int createOutputFile(char *fileName, int flags, struct stat *sb)
 {
     int fd;
+	char template[PATH_MAX + 1];
+	mode_t umask_value;
+	snprintf(template, PATH_MAX, "%s/logrotate_temp.XXXXXX", ourDirName(fileName));
+
+	umask_value = umask(0000);
+	fd = mkstemp(template);
+	umask(umask_value);
+
+	if (fd < 0) {
+		message(MESS_ERROR, "error creating unique temp file: %s\n",
+				strerror(errno));
+		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;
+	}
 
-    fd = open(fileName, flags, sb->st_mode);
-    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;
-    }
     return fd;
 }