mirror of
https://github.com/troglobit/finit.git
synced 2026-10-13 10:14:47 +00:00
Log rotate: improved handling of return values from syscalls
- Align the two implementations in logit.c and utmp-api.c - Use libite APIs to handle common constructs - If gzip fails, don't remove rotated-to file - Don't try gzip if rename fails - Issues found by Coverity Scan Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
@@ -21,6 +21,8 @@ if LOGIT
|
||||
pkglibexec_PROGRAMS = logit
|
||||
logit_SOURCES = logit.c
|
||||
logit_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99
|
||||
logit_CFLAGS += $(lite_CFLAGS)
|
||||
logit_LDADD = $(lite_LIBS)
|
||||
endif
|
||||
|
||||
finit_SOURCES = api.c cgroup.c cgroup.h \
|
||||
|
||||
+18
-13
@@ -30,6 +30,7 @@
|
||||
#include <syslog.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <lite/lite.h>
|
||||
|
||||
static const char version_info[] = PACKAGE_NAME " v" PACKAGE_VERSION;
|
||||
|
||||
@@ -65,7 +66,9 @@ static int logrotate(char *file, int num, off_t sz)
|
||||
snprintf(nfile, len, "%s.%d.gz", file, cnt);
|
||||
|
||||
/* May fail because ofile doesn't exist yet, ignore. */
|
||||
(void)rename(ofile, nfile);
|
||||
if (rename(ofile, nfile) && errno != ENOENT)
|
||||
syslog(LOG_ERR, "Failed logrotate %s: %s",
|
||||
ofile, strerror(errno));
|
||||
}
|
||||
|
||||
for (cnt = num; cnt > 0; cnt--) {
|
||||
@@ -73,26 +76,28 @@ static int logrotate(char *file, int num, off_t sz)
|
||||
snprintf(nfile, len, "%s.%d", file, cnt);
|
||||
|
||||
/* May fail because ofile doesn't exist yet, ignore. */
|
||||
(void)rename(ofile, nfile);
|
||||
if (rename(ofile, nfile) && errno != ENOENT) {
|
||||
syslog(LOG_ERR, "Failed logrotate %s: %s",
|
||||
ofile, strerror(errno));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cnt == 2 && !access(nfile, F_OK)) {
|
||||
size_t len = 5 + strlen(nfile) + 1;
|
||||
char cmd[len];
|
||||
if (cnt == 2 && fexist(nfile)) {
|
||||
if (systemf("gzip %s", nfile))
|
||||
continue; /* no gzip, probably */
|
||||
|
||||
snprintf(cmd, len, "gzip %s", nfile);
|
||||
system(cmd);
|
||||
|
||||
remove(nfile);
|
||||
(void)remove(nfile);
|
||||
}
|
||||
}
|
||||
|
||||
if (rename(file, nfile))
|
||||
(void)truncate(file, 0);
|
||||
else
|
||||
create(file, st.st_mode, st.st_uid, st.st_gid);
|
||||
goto fallback;
|
||||
create(file, st.st_mode, st.st_uid, st.st_gid);
|
||||
} else {
|
||||
fallback:
|
||||
if (truncate(file, 0))
|
||||
syslog(LOG_ERR | LOG_PERROR, "Failed truncating %s during logrotate: %s", file, strerror(errno));
|
||||
syslog(LOG_ERR, "Failed truncating %s during logrotate: %s",
|
||||
file, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+17
-10
@@ -73,7 +73,9 @@ static int logrotate(char *file, int num, off_t sz)
|
||||
snprintf(nfile, len, "%s.%d.gz", file, cnt);
|
||||
|
||||
/* May fail because ofile doesn't exist yet, ignore. */
|
||||
(void)rename(ofile, nfile);
|
||||
if (rename(ofile, nfile) && errno != ENOENT)
|
||||
logit(LOG_ERR, "Failed logrotate %s: %s",
|
||||
ofile, strerror(errno));
|
||||
}
|
||||
|
||||
for (cnt = num; cnt > 0; cnt--) {
|
||||
@@ -81,23 +83,28 @@ static int logrotate(char *file, int num, off_t sz)
|
||||
snprintf(nfile, len, "%s.%d", file, cnt);
|
||||
|
||||
/* May fail because ofile doesn't exist yet, ignore. */
|
||||
(void)rename(ofile, nfile);
|
||||
if (rename(ofile, nfile) && errno != ENOENT) {
|
||||
logit(LOG_ERR, "Failed logrotate %s: %s",
|
||||
ofile, strerror(errno));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cnt == 2 && fexist(nfile)) {
|
||||
size_t len = 5 + strlen(nfile) + 1;
|
||||
char cmd[len];
|
||||
if (systemf("gzip %s", nfile))
|
||||
continue; /* no gzip, probably */
|
||||
|
||||
snprintf(cmd, len, "gzip %s", nfile);
|
||||
run(cmd);
|
||||
|
||||
remove(nfile);
|
||||
(void)remove(nfile);
|
||||
}
|
||||
}
|
||||
|
||||
(void)rename(file, nfile);
|
||||
if (rename(file, nfile))
|
||||
goto fallback;
|
||||
create(file, st.st_mode, st.st_uid, st.st_gid);
|
||||
} else {
|
||||
(void)truncate(file, 0);
|
||||
fallback:
|
||||
if (truncate(file, 0))
|
||||
logit(LOG_ERR, "Failed truncating %s during logrotate: %s",
|
||||
file, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user