All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Jeremy Sowden <jeremy@azazel.net>
Cc: Netfilter Devel <netfilter-devel@vger.kernel.org>
Subject: Re: [PATCH ulogd2 v3 1/2] pcap: simplify opening of output file
Date: Thu, 16 Mar 2023 20:02:15 +0100	[thread overview]
Message-ID: <ZBNnt5POeEw1sr0v@salvia> (raw)
In-Reply-To: <20230316110754.260967-2-jeremy@azazel.net>

On Thu, Mar 16, 2023 at 11:07:53AM +0000, Jeremy Sowden wrote:
> 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>
> ---
>  output/pcap/ulogd_output_PCAP.c | 42 ++++++++++++---------------------
>  1 file changed, 15 insertions(+), 27 deletions(-)
> 
> diff --git a/output/pcap/ulogd_output_PCAP.c b/output/pcap/ulogd_output_PCAP.c
> index e7798f20c8fc..220fc6dec5fe 100644
> --- a/output/pcap/ulogd_output_PCAP.c
> +++ b/output/pcap/ulogd_output_PCAP.c
> @@ -220,33 +220,21 @@ 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) {
> +	    if (!write_pcap_header(pi)) {
        ^^^^
coding style nitpick, it can be fixed before applying it.

> +		    ulogd_log(ULOGD_ERROR, "can't write pcap header: %s\n",
> +			      strerror(errno));
> +		    return -ENOSPC;
> +	    }
>  	}
>  
>  	return 0;
> -- 
> 2.39.2
> 

  parent reply	other threads:[~2023-03-16 19:02 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-16 11:07 [PATCH ulogd2 v3 0/2] pcap: prevent crashes when output `FILE *` is null Jeremy Sowden
2023-03-16 11:07 ` [PATCH ulogd2 v3 1/2] pcap: simplify opening of output file Jeremy Sowden
2023-03-16 11:24   ` Florian Westphal
2023-03-16 11:32     ` Florian Westphal
2023-03-16 19:02   ` Pablo Neira Ayuso [this message]
2023-03-16 19:09     ` Jeremy Sowden
2023-03-16 11:07 ` [PATCH ulogd2 v3 2/2] pcap: prevent crashes when output `FILE *` is null Jeremy Sowden
2023-03-16 11:36 ` [PATCH ulogd2 v3 0/2] " Florian Westphal
2023-03-16 23:36 ` Florian Westphal
2023-03-17 11:34   ` Jeremy Sowden

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZBNnt5POeEw1sr0v@salvia \
    --to=pablo@netfilter.org \
    --cc=jeremy@azazel.net \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.