summaryrefslogtreecommitdiffstats
path: root/meta/recipes-extended/logrotate/logrotate/act-as-mv-when-rotate.patch
diff options
context:
space:
mode:
Diffstat (limited to 'meta/recipes-extended/logrotate/logrotate/act-as-mv-when-rotate.patch')
-rw-r--r--meta/recipes-extended/logrotate/logrotate/act-as-mv-when-rotate.patch134
1 files changed, 134 insertions, 0 deletions
diff --git a/meta/recipes-extended/logrotate/logrotate/act-as-mv-when-rotate.patch b/meta/recipes-extended/logrotate/logrotate/act-as-mv-when-rotate.patch
new file mode 100644
index 0000000000..ce64040d5f
--- /dev/null
+++ b/meta/recipes-extended/logrotate/logrotate/act-as-mv-when-rotate.patch
@@ -0,0 +1,134 @@
1Act as the "mv" command when rotate log
2
3Act as the "mv" command when rotate log, first rename, if failed, then
4read and write.
5
6Upstream-Status: Submitted
7
8Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
9---
10 logrotate.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++---------
11 1 file changed, 56 insertions(+), 9 deletions(-)
12
13diff --git a/logrotate.c b/logrotate.c
14index 174a26b..b18b629 100644
15--- a/logrotate.c
16+++ b/logrotate.c
17@@ -906,6 +906,53 @@ int findNeedRotating(struct logInfo *log, int logNum, int force)
18 return 0;
19 }
20
21+/* Act as the "mv" command, if rename failed, then read the old file and
22+ * write to new file. The function which invokes the mvFile will use
23+ * the strerror(errorno) to handle the error message, so we don't have
24+ * to print the error message here */
25+
26+int mvFile (char *oldName, char *newName, struct logInfo *log, acl_type acl)
27+{
28+ struct stat sbprev;
29+ int fd_old, fd_new, n;
30+ char buf[BUFSIZ];
31+
32+ /* Do the rename first */
33+ if (!rename(oldName, newName))
34+ return 0;
35+
36+ /* If the errno is EXDEV, then read old file, write newfile and
37+ * remove the oldfile */
38+ if (errno == EXDEV) {
39+ /* Open the old file to read */
40+ if ((fd_old = open(oldName, O_RDONLY)) < 0)
41+ return 1;
42+
43+ /* Create the file to write, keep the same attribute as the old file */
44+ if (stat(oldName, &sbprev))
45+ return 1;
46+ else {
47+ if ((fd_new = createOutputFile(newName,
48+ O_WRONLY | O_CREAT | O_TRUNC, &sbprev, acl, 0)) < 0 )
49+ return 1;
50+ }
51+
52+ /* Read and write */
53+ while ((n = read(fd_old, buf, BUFSIZ)) > 0)
54+ if (write(fd_new, buf, n) != n)
55+ return 1;
56+
57+ if ((close(fd_old) < 0) ||
58+ removeLogFile(oldName, log) ||
59+ (close(fd_new) < 0))
60+ return 1;
61+
62+ return 0;
63+ }
64+
65+ return 1;
66+}
67+
68 int prerotateSingleLog(struct logInfo *log, int logNum, struct logState *state,
69 struct logNames *rotNames)
70 {
71@@ -1268,15 +1315,15 @@ int prerotateSingleLog(struct logInfo *log, int logNum, struct logState *state,
72 }
73
74 message(MESS_DEBUG,
75- "renaming %s to %s (rotatecount %d, logstart %d, i %d), \n",
76+ "moving %s to %s (rotatecount %d, logstart %d, i %d), \n",
77 oldName, newName, rotateCount, logStart, i);
78
79- if (!debug && rename(oldName, newName)) {
80+ if (!debug && mvFile(oldName, newName, log, prev_acl)) {
81 if (errno == ENOENT) {
82 message(MESS_DEBUG, "old log %s does not exist\n",
83 oldName);
84 } else {
85- message(MESS_ERROR, "error renaming %s to %s: %s\n",
86+ message(MESS_ERROR, "error moving %s to %s: %s\n",
87 oldName, newName, strerror(errno));
88 hasErrors = 1;
89 }
90@@ -1408,11 +1455,11 @@ int rotateSingleLog(struct logInfo *log, int logNum, struct logState *state,
91 }
92 }
93 #endif /* WITH_ACL */
94- message(MESS_DEBUG, "renaming %s to %s\n", log->files[logNum],
95+ message(MESS_DEBUG, "moving %s to %s\n", log->files[logNum],
96 rotNames->finalName);
97 if (!debug && !hasErrors &&
98- rename(log->files[logNum], rotNames->finalName)) {
99- message(MESS_ERROR, "failed to rename %s to %s: %s\n",
100+ mvFile(log->files[logNum], rotNames->finalName, log, prev_acl)) {
101+ message(MESS_ERROR, "failed to move %s to %s: %s\n",
102 log->files[logNum], rotNames->finalName,
103 strerror(errno));
104 hasErrors = 1;
105@@ -1775,7 +1822,7 @@ int rotateLogSet(struct logInfo *log, int force)
106 return hasErrors;
107 }
108
109-static int writeState(char *stateFilename)
110+static int writeState(struct logInfo *log, char *stateFilename)
111 {
112 struct logState *p;
113 FILE *f;
114@@ -1939,7 +1986,7 @@ static int writeState(char *stateFilename)
115 fclose(f);
116
117 if (error == 0) {
118- if (rename(tmpFilename, stateFilename)) {
119+ if (mvFile(tmpFilename, stateFilename, log, prev_acl)) {
120 unlink(tmpFilename);
121 error = 1;
122 message(MESS_ERROR, "error renaming temp state file %s\n",
123@@ -2223,7 +2270,7 @@ int main(int argc, const char **argv)
124 rc |= rotateLogSet(log, force);
125
126 if (!debug)
127- rc |= writeState(stateFile);
128+ rc |= writeState(log, stateFile);
129
130 return (rc != 0);
131 }
132--
1331.7.10.4
134