summaryrefslogtreecommitdiffstats
path: root/output/pcap/ulogd_output_PCAP.c
diff options
context:
space:
mode:
authorJeremy Sowden <jeremy@azazel.net>2023-03-16 11:07:53 +0000
committerFlorian Westphal <fw@strlen.de>2023-03-16 23:24:55 +0100
commit3994535c1d54028ae0a3268e001c9598c7c335ed (patch)
treedacc10a8799cc842ed65966717b6d68d1f3e94b8 /output/pcap/ulogd_output_PCAP.c
parent9fa2184c303a237917b76da74f7271dca70d0d90 (diff)
pcap: simplify opening of output file
Instead of statting the file, and choosing the mode with which to open it and whether to write the PCAP header based on the result, always open it with mode "a" and _then_ stat it. This simplifies the flow-control and avoids a race between statting and opening. Signed-off-by: Jeremy Sowden <jeremy@azazel.net> Signed-off-by: Florian Westphal <fw@strlen.de>
Diffstat (limited to 'output/pcap/ulogd_output_PCAP.c')
-rw-r--r--output/pcap/ulogd_output_PCAP.c41
1 files changed, 14 insertions, 27 deletions
diff --git a/output/pcap/ulogd_output_PCAP.c b/output/pcap/ulogd_output_PCAP.c
index e7798f2..0daa2c9 100644
--- a/output/pcap/ulogd_output_PCAP.c
+++ b/output/pcap/ulogd_output_PCAP.c
@@ -220,33 +220,20 @@ static int append_create_outfile(struct ulogd_pluginstance *upi)
{
struct pcap_instance *pi = (struct pcap_instance *) &upi->private;
char *filename = upi->config_kset->ces[0].u.string;
- struct stat st_dummy;
- int exist = 0;
-
- if (stat(filename, &st_dummy) == 0 && st_dummy.st_size > 0)
- exist = 1;
-
- if (!exist) {
- pi->of = fopen(filename, "w");
- if (!pi->of) {
- ulogd_log(ULOGD_ERROR, "can't open pcap file %s: %s\n",
- filename,
- strerror(errno));
- return -EPERM;
- }
- if (!write_pcap_header(pi)) {
- ulogd_log(ULOGD_ERROR, "can't write pcap header: %s\n",
- strerror(errno));
- return -ENOSPC;
- }
- } else {
- pi->of = fopen(filename, "a");
- if (!pi->of) {
- ulogd_log(ULOGD_ERROR, "can't open pcap file %s: %s\n",
- filename,
- strerror(errno));
- return -EPERM;
- }
+ struct stat st_of;
+
+ pi->of = fopen(filename, "a");
+ if (!pi->of) {
+ ulogd_log(ULOGD_ERROR, "can't open pcap file %s: %s\n",
+ filename,
+ strerror(errno));
+ return -EPERM;
+ }
+ if (fstat(fileno(pi->of), &st_of) == 0 && st_of.st_size == 0 &&
+ !write_pcap_header(pi)) {
+ ulogd_log(ULOGD_ERROR, "can't write pcap header: %s\n",
+ strerror(errno));
+ return -ENOSPC;
}
return 0;