From: Phil Sutter <phil@nwl.cc>
To: Hangbin Liu <liuhangbin@gmail.com>
Cc: netdev@vger.kernel.org,
Stephen Hemminger <stephen@networkplumber.org>,
Michal Kubecek <mkubecek@suse.cz>
Subject: Re: [PATCH iproute2 1/2] lib/libnetlink: re malloc buff if size is not enough
Date: Fri, 8 Sep 2017 13:02:47 +0200 [thread overview]
Message-ID: <20170908110247.GS2399@orbyte.nwl.cc> (raw)
In-Reply-To: <1504865697-27274-2-git-send-email-liuhangbin@gmail.com>
Hi Hangbin,
On Fri, Sep 08, 2017 at 06:14:56PM +0800, Hangbin Liu wrote:
[...]
> diff --git a/lib/libnetlink.c b/lib/libnetlink.c
> index be7ac86..37cfb5a 100644
> --- a/lib/libnetlink.c
> +++ b/lib/libnetlink.c
> @@ -402,6 +402,59 @@ static void rtnl_dump_error(const struct rtnl_handle *rth,
> }
> }
>
> +static int rtnl_recvmsg(int fd, struct msghdr *msg, char **buf)
> +{
> + struct iovec *iov;
> + int len = -1, buf_len = 32768;
> + char *buffer = *buf;
Isn't it possible to make 'buffer' static instead of the two 'buf'
variables in rtnl_dump_filter_l() and __rtnl_talk()? Then we would have
only a single buffer which is shared between both functions instead of
two which are independently allocated.
> +
> + int flag = MSG_PEEK | MSG_TRUNC;
> +
> + if (buffer == NULL)
> +re_malloc:
> + buffer = malloc(buf_len);
I think using realloc() here is more appropriate since there is no need
to free the buffer in beforehand and calling realloc(NULL, len) is
equivalent to calling malloc(len). I think 'realloc' is also a better
name for the goto label.
> + if (buffer == NULL) {
> + fprintf(stderr, "malloc error: no enough buffer\n");
Minor typo here: s/no/not/
> + return -1;
Return -ENOMEM?
> + }
> +
> + iov = msg->msg_iov;
> + iov->iov_base = buffer;
> + iov->iov_len = buf_len;
> +
> +re_recv:
Just call this 'recv'? (Not really important though.)
> + len = recvmsg(fd, msg, flag);
> +
> + if (len < 0) {
> + if (errno == EINTR || errno == EAGAIN)
> + return 0;
Instead of returning 0 (which makes callers retry), goto re_recv?
> + fprintf(stderr, "netlink receive error %s (%d)\n",
> + strerror(errno), errno);
> + return len;
> + }
> +
> + if (len == 0) {
> + fprintf(stderr, "EOF on netlink\n");
> + return -1;
Return -ENODATA here? (Initially I though about -EOF, but EOF is -1 so
that would be incorrect).
> + }
> +
> + if (len > buf_len) {
> + free(buffer);
If you use realloc() above, this can be dropped.
> + buf_len = len;
For this to work you have to make buf_len static too, otherwise you will
unnecessarily reallocate the buffer. Oh, and that also requires the
single buffer (as pointed out above) because you will otherwise use a
common buf_len for both static buffers passed to this function.
> + flag = 0;
> + goto re_malloc;
> + }
> +
> + if (flag != 0) {
> + flag = 0;
> + goto re_recv;
> + }
> +
> + *buf = buffer;
> + return len;
> +}
> +
> int rtnl_dump_filter_l(struct rtnl_handle *rth,
> const struct rtnl_dump_filter_arg *arg)
> {
> @@ -413,31 +466,20 @@ int rtnl_dump_filter_l(struct rtnl_handle *rth,
> .msg_iov = &iov,
> .msg_iovlen = 1,
> };
> - char buf[32768];
> + static char *buf = NULL;
If you keep the static buffer in rtnl_recvmsg(), there is no need to
assign NULL here.
> int dump_intr = 0;
>
> - iov.iov_base = buf;
> while (1) {
> int status;
> const struct rtnl_dump_filter_arg *a;
> int found_done = 0;
> int msglen = 0;
>
> - iov.iov_len = sizeof(buf);
> - status = recvmsg(rth->fd, &msg, 0);
> -
> - if (status < 0) {
> - if (errno == EINTR || errno == EAGAIN)
> - continue;
> - fprintf(stderr, "netlink receive error %s (%d)\n",
> - strerror(errno), errno);
> - return -1;
> - }
> -
> - if (status == 0) {
> - fprintf(stderr, "EOF on netlink\n");
> - return -1;
> - }
> + status = rtnl_recvmsg(rth->fd, &msg, &buf);
> + if (status < 0)
> + return status;
> + else if (status == 0)
> + continue;
When retrying inside rtnl_recvmsg(), it won't return 0 anymore. I
believe the whole 'while (1)' loop could go away then.
Everything noted for rtnl_dump_filter_l() applies to __rtnl_talk() as
well.
Thanks, Phil
next prev parent reply other threads:[~2017-09-08 11:02 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-08 10:14 [PATCH iproute2 0/2] malloc correct buff at run time Hangbin Liu
2017-09-08 10:14 ` [PATCH iproute2 1/2] lib/libnetlink: re malloc buff if size is not enough Hangbin Liu
2017-09-08 11:02 ` Phil Sutter [this message]
2017-09-08 12:32 ` Michal Kubecek
2017-09-08 14:01 ` Hangbin Liu
2017-09-08 14:51 ` Phil Sutter
2017-09-11 7:19 ` Hangbin Liu
2017-09-12 8:47 ` Michal Kubecek
2017-09-12 9:09 ` Michal Kubecek
2017-09-13 9:26 ` Hangbin Liu
2017-09-08 10:14 ` [PATCH iproute2 2/2] lib/libnetlink: update rtnl_talk to support malloc buff at run time Hangbin Liu
2017-09-08 11:06 ` Phil Sutter
2017-09-08 13:26 ` Hangbin Liu
2017-09-08 12:03 ` [PATCH iproute2 0/2] malloc correct " Michal Kubecek
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=20170908110247.GS2399@orbyte.nwl.cc \
--to=phil@nwl.cc \
--cc=liuhangbin@gmail.com \
--cc=mkubecek@suse.cz \
--cc=netdev@vger.kernel.org \
--cc=stephen@networkplumber.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).