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>
This commit is contained in:
Joshua Judson Rosen 2014-05-20 01:02:20 -04:00 committed by Denys Vlasenko
parent b905d6c2ea
commit 9aa6ffb22b

View File

@ -648,12 +648,16 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file)
} }
/* newFile == "f.0" now */ /* newFile == "f.0" now */
rename(log_file->path, newFile); rename(log_file->path, newFile);
/* Incredibly, if F and F.0 are hardlinks, POSIX }
* _demands_ that rename returns 0 but does not
* remove F!!! /* We may or may not have just renamed the file away;
* if we didn't rename because we aren't keeping any backlog,
* then it's time to clobber the file. If we did rename it...,
* incredibly, if F and F.0 are hardlinks, POSIX _demands_
* that rename returns 0 but does not remove F!!!
* (hardlinked F/F.0 pair was observed after * (hardlinked F/F.0 pair was observed after
* power failure during rename()). * power failure during rename()).
* Ensure old file is gone: * So ensure old file is gone in any case:
*/ */
unlink(log_file->path); unlink(log_file->path);
#ifdef SYSLOGD_WRLOCK #ifdef SYSLOGD_WRLOCK
@ -663,18 +667,6 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file)
close(log_file->fd); close(log_file->fd);
goto reopen; goto reopen;
} }
/* We don't get here unless G.logFileRotate == 0;
* in which case don't bother unlinking and reopening,
* just truncate and reset size to match:
*/
ftruncate(log_file->fd, 0);
log_file->size = 0;
#ifdef SYSLOGD_WRLOCK
fl.l_type = F_UNLCK;
fcntl(log_file->fd, F_SETLKW, &fl);
#endif
}
log_file->size += log_file->size +=
#endif #endif
full_write(log_file->fd, msg, len); full_write(log_file->fd, msg, len);