mirror of
https://github.com/troglobit/finit.git
synced 2026-11-20 19:41:51 +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:
1 parent
77aa941e84
commit
790a316607
3 files changed
+37
-23
No files matched your search
@@ -21,6 +21,8 @@ if LOGIT
|
|||||||
pkglibexec_PROGRAMS = logit
|
pkglibexec_PROGRAMS = logit
|
||||||
logit_SOURCES = logit.c
|
logit_SOURCES = logit.c
|
||||||
logit_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99
|
logit_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99
|
||||||
|
logit_CFLAGS += $(lite_CFLAGS)
|
||||||
|
logit_LDADD = $(lite_LIBS)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
finit_SOURCES = api.c cgroup.c cgroup.h \
|
finit_SOURCES = api.c cgroup.c cgroup.h \
|
||||||
|
|||||||
+18
-13
@@ -30,6 +30,7 @@
|
|||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <lite/lite.h>
|
||||||
|
|
||||||
static const char version_info[] = PACKAGE_NAME " v" PACKAGE_VERSION;
|
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);
|
snprintf(nfile, len, "%s.%d.gz", file, cnt);
|
||||||
|
|
||||||
/* May fail because ofile doesn't exist yet, ignore. */
|
/* 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--) {
|
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);
|
snprintf(nfile, len, "%s.%d", file, cnt);
|
||||||
|
|
||||||
/* May fail because ofile doesn't exist yet, ignore. */
|
/* 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)) {
|
if (cnt == 2 && fexist(nfile)) {
|
||||||
size_t len = 5 + strlen(nfile) + 1;
|
if (systemf("gzip %s", nfile))
|
||||||
char cmd[len];
|
continue; /* no gzip, probably */
|
||||||
|
|
||||||
snprintf(cmd, len, "gzip %s", nfile);
|
(void)remove(nfile);
|
||||||
system(cmd);
|
|
||||||
|
|
||||||
remove(nfile);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rename(file, nfile))
|
if (rename(file, nfile))
|
||||||
(void)truncate(file, 0);
|
goto fallback;
|
||||||
else
|
create(file, st.st_mode, st.st_uid, st.st_gid);
|
||||||
create(file, st.st_mode, st.st_uid, st.st_gid);
|
|
||||||
} else {
|
} else {
|
||||||
|
fallback:
|
||||||
if (truncate(file, 0))
|
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);
|
snprintf(nfile, len, "%s.%d.gz", file, cnt);
|
||||||
|
|
||||||
/* May fail because ofile doesn't exist yet, ignore. */
|
/* 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--) {
|
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);
|
snprintf(nfile, len, "%s.%d", file, cnt);
|
||||||
|
|
||||||
/* May fail because ofile doesn't exist yet, ignore. */
|
/* 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)) {
|
if (cnt == 2 && fexist(nfile)) {
|
||||||
size_t len = 5 + strlen(nfile) + 1;
|
if (systemf("gzip %s", nfile))
|
||||||
char cmd[len];
|
continue; /* no gzip, probably */
|
||||||
|
|
||||||
snprintf(cmd, len, "gzip %s", nfile);
|
(void)remove(nfile);
|
||||||
run(cmd);
|
|
||||||
|
|
||||||
remove(nfile);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(void)rename(file, nfile);
|
if (rename(file, nfile))
|
||||||
|
goto fallback;
|
||||||
create(file, st.st_mode, st.st_uid, st.st_gid);
|
create(file, st.st_mode, st.st_uid, st.st_gid);
|
||||||
} else {
|
} 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