Commit be01d7b0 authored by Niels Möller's avatar Niels Möller

Fixed bug in UNLINK_FD.

Rev: src/io.c:1.5
parent 85ccf21a
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h>
#include "io.h" #include "io.h"
#include "werror.h" #include "werror.h"
...@@ -52,11 +53,12 @@ static int do_read(struct fd_read *closure, UINT8 *buffer, UINT32 length) ...@@ -52,11 +53,12 @@ static int do_read(struct fd_read *closure, UINT8 *buffer, UINT32 length)
{ \ { \
type **(_fd); \ type **(_fd); \
type *(fd); \ type *(fd); \
for(_fd = &(list); ((fd) = *_fd); _fd = &(*_fd)->next, (extra)) for(_fd = &(list); ((fd) = *_fd); (extra)) {
#define END_FOR_FDS } #define END_FOR_FDS _fd = &(*_fd)->next; } }
/* UNLINK_FD must be followed by a continue, to avoid updating _fd */
#define UNLINK_FD (*_fd = (*_fd)->next) #define UNLINK_FD (*_fd = (*_fd)->next)
void io_run(struct io_backend *b) void io_run(struct io_backend *b)
...@@ -170,13 +172,15 @@ void io_run(struct io_backend *b) ...@@ -170,13 +172,15 @@ void io_run(struct io_backend *b)
case EAGAIN: case EAGAIN:
break; break;
default: default:
werror("io.c: write failed, %s\n", strerror(errno));
CALLBACK(fd->close_callback); CALLBACK(fd->close_callback);
/* FIXME: Must do this later. Perhaps add a
* closed flag to th io_fd struct? */
fd->please_close = 1; fd->please_close = 1;
break; break;
} }
else if (!res)
fatal("What now?");
else else
fd->buffer->start += res; fd->buffer->start += res;
} }
...@@ -211,6 +215,7 @@ void io_run(struct io_backend *b) ...@@ -211,6 +215,7 @@ void io_run(struct io_backend *b)
UNLINK_FD; UNLINK_FD;
free(fd->buffer); free(fd->buffer);
free(fd); free(fd);
continue;
} }
} }
END_FOR_FDS; END_FOR_FDS;
...@@ -221,8 +226,10 @@ void io_run(struct io_backend *b) ...@@ -221,8 +226,10 @@ void io_run(struct io_backend *b)
{ {
/* FIXME: Do something with the peer address? */ /* FIXME: Do something with the peer address? */
struct sockaddr_in peer; struct sockaddr_in peer;
size_t addr_len = sizeof(peer);
int conn = accept(fd->fd, &peer, sizeof(peer)); int conn = accept(fd->fd,
(struct sockaddr *) &peer, &addr_len);
if (conn < 0) if (conn < 0)
{ {
werror("io.c: accept() failed, %s", strerror(errno)); werror("io.c: accept() failed, %s", strerror(errno));
...@@ -233,6 +240,7 @@ void io_run(struct io_backend *b) ...@@ -233,6 +240,7 @@ void io_run(struct io_backend *b)
/* FIXME: Should fd be closed here? */ /* FIXME: Should fd be closed here? */
UNLINK_FD; UNLINK_FD;
free(fd); free(fd);
continue;
} }
} }
} }
...@@ -240,12 +248,14 @@ void io_run(struct io_backend *b) ...@@ -240,12 +248,14 @@ void io_run(struct io_backend *b)
FOR_FDS(struct connect_fd, fd, b->connect, i++) FOR_FDS(struct connect_fd, fd, b->connect, i++)
{ {
if (fds[i]->revents & POLLOUT) if (fds[i].revents & POLLOUT)
{ {
if (!FD_CALLBACK(fd->callback, fd->fd)) if (!FD_CALLBACK(fd->callback, fd->fd))
fatal("What now?"); fatal("What now?");
b->nconnect--;
UNLINK_FD; UNLINK_FD;
free(fd); free(fd);
continue;
} }
} }
END_FOR_FDS; END_FOR_FDS;
...@@ -363,7 +373,7 @@ struct connect_fd *io_connect(struct io_backend *b, ...@@ -363,7 +373,7 @@ struct connect_fd *io_connect(struct io_backend *b,
fd = xalloc(sizeof(struct connect_fd)); fd = xalloc(sizeof(struct connect_fd));
fd->fd = s; fd->fd = s;
fd->callback = callback; fd->callback = f;
fd->next = b->connect; fd->next = b->connect;
b->connect = fd; b->connect = fd;
...@@ -416,22 +426,22 @@ struct listen_fd *io_listen(struct io_backend *b, ...@@ -416,22 +426,22 @@ struct listen_fd *io_listen(struct io_backend *b,
struct abstract_write *io_read_write(struct io_backend *b, struct abstract_write *io_read_write(struct io_backend *b,
int fd, int fd,
struct read_callback *read_callback, struct read_handler *read_callback,
UINT32 block_size, UINT32 block_size,
struct callback *close_callback) struct callback *close_callback)
{ {
struct io_fd = xalloc(sizeof(struct io_fd)); struct io_fd *f= xalloc(sizeof(struct io_fd));
struct write_buffer *buffer = write_buffer_alloc(block_size); struct write_buffer *buffer = write_buffer_alloc(block_size);
fd->fd = fd; f->fd = fd;
fd->please_close = 0; f->please_close = 0;
fd->read_callback = read_callback; f->handler = read_callback;
fd->close_callback = close_callback; f->close_callback = close_callback;
fd->buffer = buffer; f->buffer = buffer;
fd->next = b->io; f->next = b->io;
b->io = fd; b->io = f;
b->nio++; b->nio++;
return (struct abstract_write *) buffer; return (struct abstract_write *) buffer;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment