From: Stefan Hajnoczi <stefanha@gmail.com>
To: Zhi Yong Wu <zwu.kernel@gmail.com>
Cc: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 2/2] net: take ownership of fd in socket init functions
Date: Thu, 8 Dec 2011 12:57:48 +0000 [thread overview]
Message-ID: <CAJSP0QVHZYswe73PkJ8Hw9z0ekcd53N-p2_spu_2URL1vh2CDw@mail.gmail.com> (raw)
In-Reply-To: <CAEH94Li-03UjVMt7UTuCUPkXqumpCX0799KcfprzR93OO66AwA@mail.gmail.com>
On Thu, Dec 8, 2011 at 12:29 PM, Zhi Yong Wu <zwu.kernel@gmail.com> wrote:
> On Wed, Dec 7, 2011 at 11:01 PM, Stefan Hajnoczi
> <stefanha@linux.vnet.ibm.com> wrote:
>> Today net/socket.c has no consistent policy for closing the socket file
>> descriptor when initialization fails. This means we leak the file
>> descriptor in some cases or we could also try to close it twice.
>>
>> Make error paths consistent by taking ownership of the file descriptor
>> and closing it on error.
>>
>> Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
>> ---
>> net/socket.c | 17 +++++++++--------
>> 1 files changed, 9 insertions(+), 8 deletions(-)
>>
>> diff --git a/net/socket.c b/net/socket.c
>> index 613a7ef..f999c26 100644
>> --- a/net/socket.c
>> +++ b/net/socket.c
>> @@ -266,14 +266,13 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
>> if (saddr.sin_addr.s_addr == 0) {
>> fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, "
>> "cannot setup multicast dst addr\n", fd);
>> - return NULL;
>> + goto err;
>> }
>> /* clone dgram socket */
>> newfd = net_socket_mcast_create(&saddr, NULL);
>> if (newfd < 0) {
>> /* error already reported by net_socket_mcast_create() */
>> - close(fd);
>> - return NULL;
>> + goto err;
>> }
>> /* clone newfd to fd, close newfd */
>> dup2(newfd, fd);
>> @@ -283,7 +282,7 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
>> fprintf(stderr,
>> "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
>> fd, strerror(errno));
>> - return NULL;
>> + goto err;
>> }
>> }
>>
>> @@ -304,6 +303,10 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
>> if (is_connected) s->dgram_dst=saddr;
>>
>> return s;
>> +
>> +err:
>> + closesocket(fd);
>> + return NULL;
>> }
>>
>> static void net_socket_connect(void *opaque)
>> @@ -353,6 +356,7 @@ static NetSocketState *net_socket_fd_init(VLANState *vlan,
>> (socklen_t *)&optlen)< 0) {
>> fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
>> fd);
>> + closesocket(fd);
>> return NULL;
>> }
>> switch(so_type) {
>> @@ -386,9 +390,7 @@ static void net_socket_accept(void *opaque)
>> }
>> }
>> s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
>> - if (!s1) {
>> - closesocket(fd);
>> - } else {
> Why is it not handled when s1 is NULL?
The point of the patch is to introduce consistent error behavior -
net_socket_fd_init() will close the socket on error so we no longer
have to do that. If you look at net_socket_accept() there is nothing
else to do on failure it was possible to just remove the if (!s1)
check.
Stefan
next prev parent reply other threads:[~2011-12-08 12:57 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-07 15:01 [Qemu-devel] [PATCH 0/2] net: clean up net/socket.c Stefan Hajnoczi
2011-12-07 15:01 ` [Qemu-devel] [PATCH 1/2] net: expand tabs in net/socket.c Stefan Hajnoczi
2011-12-08 12:22 ` Zhi Yong Wu
2011-12-07 15:01 ` [Qemu-devel] [PATCH 2/2] net: take ownership of fd in socket init functions Stefan Hajnoczi
2011-12-08 12:29 ` Zhi Yong Wu
2011-12-08 12:57 ` Stefan Hajnoczi [this message]
2011-12-09 12:35 ` Zhi Yong Wu
2011-12-12 23:34 ` [Qemu-devel] [PATCH 0/2] net: clean up net/socket.c Anthony Liguori
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=CAJSP0QVHZYswe73PkJ8Hw9z0ekcd53N-p2_spu_2URL1vh2CDw@mail.gmail.com \
--to=stefanha@gmail.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@linux.vnet.ibm.com \
--cc=zwu.kernel@gmail.com \
/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).