Message ID | 1339080860-16036-1-git-send-email-zwu.kernel@gmail.com |
---|---|
State | New |
Headers | show |
On Thu, Jun 7, 2012 at 3:54 PM, <zwu.kernel@gmail.com> wrote: > @@ -86,7 +82,16 @@ static void net_socket_send(void *opaque) > /* end of connection */ > eoc: > qemu_set_fd_handler(s->fd, NULL, NULL, NULL); > + qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); What happens when this is not a listen socket? I suggest setting listen_fd to -1 during creation and not calling qemu_set_fd_handler() when listen_fd is -1 here. If listen_fd is 0 then we'll register net_socket_accept when standard input becomes ready! > closesocket(s->fd); > + > + s->fd = 0; -1 should be used since 0 is a valid file descriptor (standard input). Stefan
On Fri, Jun 8, 2012 at 6:31 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: > On Thu, Jun 7, 2012 at 3:54 PM, <zwu.kernel@gmail.com> wrote: >> @@ -86,7 +82,16 @@ static void net_socket_send(void *opaque) >> /* end of connection */ >> eoc: >> qemu_set_fd_handler(s->fd, NULL, NULL, NULL); >> + qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); > > What happens when this is not a listen socket? I suggest setting > listen_fd to -1 during creation and not calling qemu_set_fd_handler() listen_fd isn't -1 here, and is one valid value when this function is executed. > when listen_fd is -1 here. If listen_fd is 0 then we'll register > net_socket_accept when standard input becomes ready! > >> closesocket(s->fd); >> + >> + s->fd = 0; > > -1 should be used since 0 is a valid file descriptor (standard input). OK. done, but In fact, some other places default fd to zero. > > Stefan >
On Fri, Jun 8, 2012 at 3:15 PM, Zhi Yong Wu <zwu.kernel@gmail.com> wrote: > On Fri, Jun 8, 2012 at 6:31 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: >> On Thu, Jun 7, 2012 at 3:54 PM, <zwu.kernel@gmail.com> wrote: >>> @@ -86,7 +82,16 @@ static void net_socket_send(void *opaque) >>> /* end of connection */ >>> eoc: >>> qemu_set_fd_handler(s->fd, NULL, NULL, NULL); >>> + qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); >> >> What happens when this is not a listen socket? I suggest setting >> listen_fd to -1 during creation and not calling qemu_set_fd_handler() > listen_fd isn't -1 here, and is one valid value when this function is executed. >> when listen_fd is -1 here. If listen_fd is 0 then we'll register >> net_socket_accept when standard input becomes ready! >> >>> closesocket(s->fd); >>> + >>> + s->fd = 0; >> >> -1 should be used since 0 is a valid file descriptor (standard input). > OK. done, but In fact, some other places default fd to zero. Where? Maybe those places need to be fixed too. The danger with fd=0 is that we call functions like read()/write()/close() on standard input by mistake. Stefan
On Fri, Jun 8, 2012 at 10:20 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: > On Fri, Jun 8, 2012 at 3:15 PM, Zhi Yong Wu <zwu.kernel@gmail.com> wrote: >> On Fri, Jun 8, 2012 at 6:31 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: >>> On Thu, Jun 7, 2012 at 3:54 PM, <zwu.kernel@gmail.com> wrote: >>>> @@ -86,7 +82,16 @@ static void net_socket_send(void *opaque) >>>> /* end of connection */ >>>> eoc: >>>> qemu_set_fd_handler(s->fd, NULL, NULL, NULL); >>>> + qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); >>> >>> What happens when this is not a listen socket? I suggest setting >>> listen_fd to -1 during creation and not calling qemu_set_fd_handler() >> listen_fd isn't -1 here, and is one valid value when this function is executed. >>> when listen_fd is -1 here. If listen_fd is 0 then we'll register >>> net_socket_accept when standard input becomes ready! >>> >>>> closesocket(s->fd); >>>> + >>>> + s->fd = 0; >>> >>> -1 should be used since 0 is a valid file descriptor (standard input). >> OK. done, but In fact, some other places default fd to zero. > > Where? Maybe those places need to be fixed too. The danger with fd=0 In qemu_new_net_client(), you can see "vc = g_malloc0(info->size);". If it will create one socket net client, it will default fd to zero. > is that we call functions like read()/write()/close() on standard > input by mistake. > > Stefan
On Fri, Jun 8, 2012 at 10:20 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: > On Fri, Jun 8, 2012 at 3:15 PM, Zhi Yong Wu <zwu.kernel@gmail.com> wrote: >> On Fri, Jun 8, 2012 at 6:31 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: >>> On Thu, Jun 7, 2012 at 3:54 PM, <zwu.kernel@gmail.com> wrote: >>>> @@ -86,7 +82,16 @@ static void net_socket_send(void *opaque) >>>> /* end of connection */ >>>> eoc: >>>> qemu_set_fd_handler(s->fd, NULL, NULL, NULL); >>>> + qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); >>> >>> What happens when this is not a listen socket? I suggest setting >>> listen_fd to -1 during creation and not calling qemu_set_fd_handler() >> listen_fd isn't -1 here, and is one valid value when this function is executed. >>> when listen_fd is -1 here. If listen_fd is 0 then we'll register >>> net_socket_accept when standard input becomes ready! >>> >>>> closesocket(s->fd); >>>> + >>>> + s->fd = 0; >>> >>> -1 should be used since 0 is a valid file descriptor (standard input). >> OK. done, but In fact, some other places default fd to zero. > > Where? Maybe those places need to be fixed too. The danger with fd=0 > is that we call functions like read()/write()/close() on standard > input by mistake. When net_socket_accept() is registered, fd/listen_fd has been not zero in our codes. So that case you said will not happen for our codes. > > Stefan
On Fri, Jun 8, 2012 at 6:31 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: > On Thu, Jun 7, 2012 at 3:54 PM, <zwu.kernel@gmail.com> wrote: >> @@ -86,7 +82,16 @@ static void net_socket_send(void *opaque) >> /* end of connection */ >> eoc: >> qemu_set_fd_handler(s->fd, NULL, NULL, NULL); >> + qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); > > What happens when this is not a listen socket? I suggest setting > listen_fd to -1 during creation and not calling qemu_set_fd_handler() > when listen_fd is -1 here. If listen_fd is 0 then we'll register > net_socket_accept when standard input becomes ready! > >> closesocket(s->fd); >> + >> + s->fd = 0; > > -1 should be used since 0 is a valid file descriptor (standard input). I think that s->fd = 0 doesn't cause every issue. When it is zero, this fd hasn't been registered with every handler. You can see that "qemu_set_fd_handler(s->fd, NULL, NULL, NULL);" before "s->fd = 0". > > Stefan >
On Fri, Jun 8, 2012 at 3:54 PM, Zhi Yong Wu <zwu.kernel@gmail.com> wrote: > On Fri, Jun 8, 2012 at 6:31 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: >> On Thu, Jun 7, 2012 at 3:54 PM, <zwu.kernel@gmail.com> wrote: >>> @@ -86,7 +82,16 @@ static void net_socket_send(void *opaque) >>> /* end of connection */ >>> eoc: >>> qemu_set_fd_handler(s->fd, NULL, NULL, NULL); >>> + qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); >> >> What happens when this is not a listen socket? I suggest setting >> listen_fd to -1 during creation and not calling qemu_set_fd_handler() >> when listen_fd is -1 here. If listen_fd is 0 then we'll register >> net_socket_accept when standard input becomes ready! >> >>> closesocket(s->fd); >>> + >>> + s->fd = 0; >> >> -1 should be used since 0 is a valid file descriptor (standard input). > I think that s->fd = 0 doesn't cause every issue. When it is zero, > this fd hasn't been registered with every handler. You can see that > "qemu_set_fd_handler(s->fd, NULL, NULL, NULL);" before "s->fd = 0". If s->fd = 0 because we are still listening and net_socket_cleanup() is called we will close() standard input. Stefan
On Fri, Jun 8, 2012 at 11:59 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: > On Fri, Jun 8, 2012 at 3:54 PM, Zhi Yong Wu <zwu.kernel@gmail.com> wrote: >> On Fri, Jun 8, 2012 at 6:31 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: >>> On Thu, Jun 7, 2012 at 3:54 PM, <zwu.kernel@gmail.com> wrote: >>>> @@ -86,7 +82,16 @@ static void net_socket_send(void *opaque) >>>> /* end of connection */ >>>> eoc: >>>> qemu_set_fd_handler(s->fd, NULL, NULL, NULL); >>>> + qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); >>> >>> What happens when this is not a listen socket? I suggest setting >>> listen_fd to -1 during creation and not calling qemu_set_fd_handler() >>> when listen_fd is -1 here. If listen_fd is 0 then we'll register >>> net_socket_accept when standard input becomes ready! >>> >>>> closesocket(s->fd); >>>> + >>>> + s->fd = 0; >>> >>> -1 should be used since 0 is a valid file descriptor (standard input). >> I think that s->fd = 0 doesn't cause every issue. When it is zero, >> this fd hasn't been registered with every handler. You can see that >> "qemu_set_fd_handler(s->fd, NULL, NULL, NULL);" before "s->fd = 0". > > If s->fd = 0 because we are still listening and net_socket_cleanup() OK, done. any other issue? > is called we will close() standard input. > > Stefan
On Sat, Jun 09, 2012 at 10:09:44AM +0800, Zhi Yong Wu wrote: > On Fri, Jun 8, 2012 at 11:59 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: > > On Fri, Jun 8, 2012 at 3:54 PM, Zhi Yong Wu <zwu.kernel@gmail.com> wrote: > >> On Fri, Jun 8, 2012 at 6:31 PM, Stefan Hajnoczi <stefanha@gmail.com> wrote: > >>> On Thu, Jun 7, 2012 at 3:54 PM, <zwu.kernel@gmail.com> wrote: > >>>> @@ -86,7 +82,16 @@ static void net_socket_send(void *opaque) > >>>> /* end of connection */ > >>>> eoc: > >>>> qemu_set_fd_handler(s->fd, NULL, NULL, NULL); > >>>> + qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); > >>> > >>> What happens when this is not a listen socket? I suggest setting > >>> listen_fd to -1 during creation and not calling qemu_set_fd_handler() > >>> when listen_fd is -1 here. If listen_fd is 0 then we'll register > >>> net_socket_accept when standard input becomes ready! > >>> > >>>> closesocket(s->fd); > >>>> + > >>>> + s->fd = 0; > >>> > >>> -1 should be used since 0 is a valid file descriptor (standard input). > >> I think that s->fd = 0 doesn't cause every issue. When it is zero, > >> this fd hasn't been registered with every handler. You can see that > >> "qemu_set_fd_handler(s->fd, NULL, NULL, NULL);" before "s->fd = 0". > > > > If s->fd = 0 because we are still listening and net_socket_cleanup() > OK, done. any other issue? I haven't seen your new code yet so I'm not sure. Basically if you have introduced -1 states for s->fd and s->listen_fd as well as checks before calling qemu_set_fd_handler() or close(), then the code is fine. Stefan
diff --git a/net/socket.c b/net/socket.c index 7194345..27e8c4e 100644 --- a/net/socket.c +++ b/net/socket.c @@ -35,6 +35,7 @@ typedef struct NetSocketState { VLANClientState nc; + int listen_fd; int fd; int state; /* 0 = getting length, 1 = getting data */ unsigned int index; @@ -43,12 +44,7 @@ typedef struct NetSocketState { struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ } NetSocketState; -typedef struct NetSocketListenState { - VLANState *vlan; - char *model; - char *name; - int fd; -} NetSocketListenState; +static void net_socket_accept(void *opaque); /* XXX: we consider we can send the whole packet without blocking */ static ssize_t net_socket_receive(VLANClientState *nc, const uint8_t *buf, size_t size) @@ -86,7 +82,16 @@ static void net_socket_send(void *opaque) /* end of connection */ eoc: qemu_set_fd_handler(s->fd, NULL, NULL, NULL); + qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); closesocket(s->fd); + + s->fd = 0; + s->state = 0; + s->index = 0; + s->packet_len = 0; + memset(s->buf, 0, sizeof(s->buf)); + memset(s->nc.info_str, 0, sizeof(s->nc.info_str)); + return; } buf = buf1; @@ -377,27 +382,28 @@ static NetSocketState *net_socket_fd_init(VLANState *vlan, static void net_socket_accept(void *opaque) { - NetSocketListenState *s = opaque; - NetSocketState *s1; + NetSocketState *s = opaque; struct sockaddr_in saddr; socklen_t len; int fd; for(;;) { len = sizeof(saddr); - fd = qemu_accept(s->fd, (struct sockaddr *)&saddr, &len); + fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len); if (fd < 0 && errno != EINTR) { return; } else if (fd >= 0) { + qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); break; } } - s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); - if (s1) { - snprintf(s1->nc.info_str, sizeof(s1->nc.info_str), - "socket: connection from %s:%d", - inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); - } + + s->fd = fd; + s->nc.link_down = false; + net_socket_connect(s); + snprintf(s->nc.info_str, sizeof(s->nc.info_str), + "socket: connection from %s:%d", + inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); } static int net_socket_listen_init(VLANState *vlan, @@ -405,19 +411,17 @@ static int net_socket_listen_init(VLANState *vlan, const char *name, const char *host_str) { - NetSocketListenState *s; - int fd, val, ret; + VLANClientState *nc; + NetSocketState *s; struct sockaddr_in saddr; + int fd, val, ret; if (parse_host_port(&saddr, host_str) < 0) return -1; - s = g_malloc0(sizeof(NetSocketListenState)); - fd = qemu_socket(PF_INET, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); - g_free(s); return -1; } socket_set_nonblock(fd); @@ -429,22 +433,22 @@ static int net_socket_listen_init(VLANState *vlan, ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); if (ret < 0) { perror("bind"); - g_free(s); closesocket(fd); return -1; } ret = listen(fd, 0); if (ret < 0) { perror("listen"); - g_free(s); closesocket(fd); return -1; } - s->vlan = vlan; - s->model = g_strdup(model); - s->name = name ? g_strdup(name) : NULL; - s->fd = fd; - qemu_set_fd_handler(fd, net_socket_accept, NULL, s); + + nc = qemu_new_net_client(&net_socket_info, vlan, NULL, model, name); + s = DO_UPCAST(NetSocketState, nc, nc); + s->listen_fd = fd; + s->nc.link_down = true; + + qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); return 0; }