Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 1 | From 17d57a2a923a4af53c8910a9999aebeab3f5d83a Mon Sep 17 00:00:00 2001 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | From: Robert Yang <liezhi.yang@windriver.com> |
| 3 | Date: Tue, 17 Feb 2015 21:08:07 -0800 |
| 4 | Subject: [PATCH] Act as the "mv" command when rotate log |
| 5 | |
| 6 | Act as the "mv" command when rotate log, first rename, if failed, then |
| 7 | read and write. |
| 8 | |
| 9 | Upstream-Status: Pending |
| 10 | |
| 11 | Signed-off-by: Robert Yang <liezhi.yang@windriver.com> |
Andrew Geissler | 4b740dc | 2020-05-05 08:54:39 -0500 | [diff] [blame] | 12 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | --- |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 14 | logrotate.c | 71 ++++++++++++++++++++++++++++++++++++++++++++--------- |
| 15 | 1 file changed, 59 insertions(+), 12 deletions(-) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | |
| 17 | diff --git a/logrotate.c b/logrotate.c |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 18 | index 45b3eb6..231371a 100644 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | --- a/logrotate.c |
| 20 | +++ b/logrotate.c |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 21 | @@ -1463,6 +1463,53 @@ static int findNeedRotating(const struct logInfo *log, unsigned logNum, int forc |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | +/* Act as the "mv" command, if rename failed, then read the old file and |
| 26 | + * write to new file. The function which invokes the mvFile will use |
| 27 | + * the strerror(errorno) to handle the error message, so we don't have |
| 28 | + * to print the error message here */ |
| 29 | + |
| 30 | +int mvFile (char *oldName, char *newName, struct logInfo *log, acl_type acl) |
| 31 | +{ |
| 32 | + struct stat sbprev; |
| 33 | + int fd_old, fd_new, n; |
| 34 | + char buf[BUFSIZ]; |
| 35 | + |
| 36 | + /* Do the rename first */ |
| 37 | + if (!rename(oldName, newName)) |
| 38 | + return 0; |
| 39 | + |
| 40 | + /* If the errno is EXDEV, then read old file, write newfile and |
| 41 | + * remove the oldfile */ |
| 42 | + if (errno == EXDEV) { |
| 43 | + /* Open the old file to read */ |
| 44 | + if ((fd_old = open(oldName, O_RDONLY)) < 0) |
| 45 | + return 1; |
| 46 | + |
| 47 | + /* Create the file to write, keep the same attribute as the old file */ |
| 48 | + if (stat(oldName, &sbprev)) |
| 49 | + return 1; |
| 50 | + else { |
| 51 | + if ((fd_new = createOutputFile(newName, |
| 52 | + O_WRONLY | O_CREAT | O_TRUNC, &sbprev, acl, 0)) < 0 ) |
| 53 | + return 1; |
| 54 | + } |
| 55 | + |
| 56 | + /* Read and write */ |
| 57 | + while ((n = read(fd_old, buf, BUFSIZ)) > 0) |
| 58 | + if (write(fd_new, buf, n) != n) |
| 59 | + return 1; |
| 60 | + |
| 61 | + if ((close(fd_old) < 0) || |
| 62 | + removeLogFile(oldName, log) || |
| 63 | + (close(fd_new) < 0)) |
| 64 | + return 1; |
| 65 | + |
| 66 | + return 0; |
| 67 | + } |
| 68 | + |
| 69 | + return 1; |
| 70 | +} |
| 71 | + |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 72 | /* find the rotated file with the highest index */ |
| 73 | static int findLastRotated(const struct logNames *rotNames, |
| 74 | const char *fileext, const char *compext) |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 75 | @@ -1958,15 +2005,15 @@ static int prerotateSingleLog(const struct logInfo *log, unsigned logNum, |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 76 | } |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 78 | message(MESS_DEBUG, |
| 79 | - "renaming %s to %s (rotatecount %d, logstart %d, i %d), \n", |
| 80 | + "moving %s to %s (rotatecount %d, logstart %d, i %d), \n", |
| 81 | oldName, newName, rotateCount, logStart, i); |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 83 | - if (!debug && rename(oldName, newName)) { |
| 84 | + if (!debug && mvFile(oldName, newName, log, prev_acl)) { |
| 85 | if (errno == ENOENT) { |
| 86 | message(MESS_DEBUG, "old log %s does not exist\n", |
| 87 | oldName); |
| 88 | } else { |
| 89 | - message(MESS_ERROR, "error renaming %s to %s: %s\n", |
| 90 | + message(MESS_ERROR, "error moving %s to %s: %s\n", |
| 91 | oldName, newName, strerror(errno)); |
| 92 | hasErrors = 1; |
| 93 | } |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 94 | @@ -2051,10 +2098,10 @@ static int rotateSingleLog(const struct logInfo *log, unsigned logNum, |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 95 | return 1; |
| 96 | } |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 98 | - message(MESS_DEBUG, "renaming %s to %s\n", log->files[logNum], |
| 99 | + message(MESS_DEBUG, "moving %s to %s\n", log->files[logNum], |
| 100 | tmpFilename); |
| 101 | - if (!debug && !hasErrors && rename(log->files[logNum], tmpFilename)) { |
| 102 | - message(MESS_ERROR, "failed to rename %s to %s: %s\n", |
| 103 | + if (!debug && !hasErrors && mvFile(log->files[logNum], rotNames->finalName, log, prev_acl)) { |
| 104 | + message(MESS_ERROR, "failed to move %s to %s: %s\n", |
| 105 | log->files[logNum], tmpFilename, |
| 106 | strerror(errno)); |
| 107 | hasErrors = 1; |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 108 | @@ -2063,11 +2110,11 @@ static int rotateSingleLog(const struct logInfo *log, unsigned logNum, |
Andrew Geissler | 4b740dc | 2020-05-05 08:54:39 -0500 | [diff] [blame] | 109 | free(tmpFilename); |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 110 | } |
| 111 | else { |
| 112 | - message(MESS_DEBUG, "renaming %s to %s\n", log->files[logNum], |
| 113 | + message(MESS_DEBUG, "moving %s to %s\n", log->files[logNum], |
| 114 | rotNames->finalName); |
| 115 | if (!debug && !hasErrors && |
| 116 | - rename(log->files[logNum], rotNames->finalName)) { |
| 117 | - message(MESS_ERROR, "failed to rename %s to %s: %s\n", |
| 118 | + mvFile(log->files[logNum], rotNames->finalName, log, prev_acl)) { |
| 119 | + message(MESS_ERROR, "failed to move %s to %s: %s\n", |
| 120 | log->files[logNum], rotNames->finalName, |
| 121 | strerror(errno)); |
| 122 | hasErrors = 1; |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 123 | @@ -2480,7 +2527,7 @@ static int rotateLogSet(const struct logInfo *log, int force) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | return hasErrors; |
| 125 | } |
| 126 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 127 | -static int writeState(const char *stateFilename) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | +static int writeState(struct logInfo *log, char *stateFilename) |
| 129 | { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 130 | struct logState *p; |
| 131 | FILE *f; |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 132 | @@ -2659,7 +2706,7 @@ static int writeState(const char *stateFilename) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 133 | fclose(f); |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 134 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 135 | if (error == 0) { |
| 136 | - if (rename(tmpFilename, stateFilename)) { |
| 137 | + if (mvFile(tmpFilename, stateFilename, log, prev_acl)) { |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 138 | message(MESS_ERROR, "error renaming temp state file %s to %s: %s\n", |
| 139 | tmpFilename, stateFilename, strerror(errno)); |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 140 | unlink(tmpFilename); |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 141 | @@ -3073,7 +3120,7 @@ int main(int argc, const char **argv) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 142 | rc |= rotateLogSet(log, force); |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 143 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 144 | if (!debug) |
| 145 | - rc |= writeState(stateFile); |
| 146 | + rc |= writeState(log, stateFile); |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 147 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 148 | return (rc != 0); |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 149 | } |