diff options
| author | Joshua Judson Rosen <jrosen@harvestai.com> | 2014-05-20 01:02:20 -0400 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2014-06-02 03:11:40 +0200 |
| commit | 9aa6ffb22b712d4e928604e291f954b02237e8cd (patch) | |
| tree | ad606fe29c75f2acfdad5c2b328d56f911c3047e | |
| parent | b905d6c2eaaf7ad92a50dccc7b91ee19dd9424b7 (diff) | |
| download | busybox-w32-9aa6ffb22b712d4e928604e291f954b02237e8cd.tar.gz busybox-w32-9aa6ffb22b712d4e928604e291f954b02237e8cd.tar.bz2 busybox-w32-9aa6ffb22b712d4e928604e291f954b02237e8cd.zip | |
syslogd: Unify unlink/truncate + unlock log-rotation logic
Always unlink + reopen, rather than sometimes using ftruncate();
using a single code-path reduces the opportunity for either
mistakes or duplicate code.
Signed-off-by: Joshua Judson Rosen <jrosen@harvestai.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | sysklogd/syslogd.c | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index d77fc9475..f75851085 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c | |||
| @@ -648,32 +648,24 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file) | |||
| 648 | } | 648 | } |
| 649 | /* newFile == "f.0" now */ | 649 | /* newFile == "f.0" now */ |
| 650 | rename(log_file->path, newFile); | 650 | rename(log_file->path, newFile); |
| 651 | /* Incredibly, if F and F.0 are hardlinks, POSIX | ||
| 652 | * _demands_ that rename returns 0 but does not | ||
| 653 | * remove F!!! | ||
| 654 | * (hardlinked F/F.0 pair was observed after | ||
| 655 | * power failure during rename()). | ||
| 656 | * Ensure old file is gone: | ||
| 657 | */ | ||
| 658 | unlink(log_file->path); | ||
| 659 | #ifdef SYSLOGD_WRLOCK | ||
| 660 | fl.l_type = F_UNLCK; | ||
| 661 | fcntl(log_file->fd, F_SETLKW, &fl); | ||
| 662 | #endif | ||
| 663 | close(log_file->fd); | ||
| 664 | goto reopen; | ||
| 665 | } | 651 | } |
| 666 | 652 | ||
| 667 | /* We don't get here unless G.logFileRotate == 0; | 653 | /* We may or may not have just renamed the file away; |
| 668 | * in which case don't bother unlinking and reopening, | 654 | * if we didn't rename because we aren't keeping any backlog, |
| 669 | * just truncate and reset size to match: | 655 | * then it's time to clobber the file. If we did rename it..., |
| 656 | * incredibly, if F and F.0 are hardlinks, POSIX _demands_ | ||
| 657 | * that rename returns 0 but does not remove F!!! | ||
| 658 | * (hardlinked F/F.0 pair was observed after | ||
| 659 | * power failure during rename()). | ||
| 660 | * So ensure old file is gone in any case: | ||
| 670 | */ | 661 | */ |
| 671 | ftruncate(log_file->fd, 0); | 662 | unlink(log_file->path); |
| 672 | log_file->size = 0; | ||
| 673 | #ifdef SYSLOGD_WRLOCK | 663 | #ifdef SYSLOGD_WRLOCK |
| 674 | fl.l_type = F_UNLCK; | 664 | fl.l_type = F_UNLCK; |
| 675 | fcntl(log_file->fd, F_SETLKW, &fl); | 665 | fcntl(log_file->fd, F_SETLKW, &fl); |
| 676 | #endif | 666 | #endif |
| 667 | close(log_file->fd); | ||
| 668 | goto reopen; | ||
| 677 | } | 669 | } |
| 678 | log_file->size += | 670 | log_file->size += |
| 679 | #endif | 671 | #endif |
