2011-04-07 09:44:11 +02:00
|
|
|
/*
|
|
|
|
* This file implements handling of socket-related requests from VFS
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <minix/ipc.h>
|
|
|
|
#include <minix/com.h>
|
|
|
|
#include <minix/callnr.h>
|
|
|
|
#include <minix/sysutil.h>
|
2012-02-24 19:02:32 +01:00
|
|
|
#include <minix/netsock.h>
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
#include <lwip/tcp.h>
|
|
|
|
|
2011-09-08 15:57:03 +02:00
|
|
|
#include <sys/ioc_net.h>
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
char * netsock_user_name = NULL;
|
|
|
|
#define NETSOCK_USER_NAME (netsock_user_name ? netsock_user_name : "NETSOCK")
|
|
|
|
|
|
|
|
#define debug_print(str, ...) printf("%s : %s:%d : " str "\n", \
|
|
|
|
NETSOCK_USER_NAME, __func__, __LINE__, ##__VA_ARGS__)
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
#if 0
|
2012-02-24 19:02:32 +01:00
|
|
|
#define debug_sock_print(...) debug_print(__VA_ARGS__)
|
2011-04-07 09:44:11 +02:00
|
|
|
#else
|
2012-02-24 19:02:32 +01:00
|
|
|
#define debug_sock_print(...)
|
2011-04-07 09:44:11 +02:00
|
|
|
#endif
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
#if 0
|
|
|
|
#define debug_sock_select_print(...) debug_print(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define debug_sock_select_print(...) debug_sock_print(__VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define netsock_panic(str, ...) panic("%s : " str, NETSOCK_USER_NAME, \
|
|
|
|
##__VA_ARGS__)
|
|
|
|
#define netsock_error(str, ...) printf("%s : " str, NETSOCK_USER_NAME, \
|
|
|
|
##__VA_ARGS__)
|
|
|
|
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
struct socket socket[MAX_SOCKETS];
|
|
|
|
|
2013-09-04 17:42:36 +02:00
|
|
|
static int netsock_open(devminor_t minor, int access, endpoint_t user_endpt);
|
|
|
|
static int netsock_close(devminor_t minor);
|
|
|
|
static ssize_t netsock_read(devminor_t minor, u64_t position, endpoint_t endpt,
|
|
|
|
cp_grant_id_t grant, size_t size, int flags, cdev_id_t id);
|
|
|
|
static ssize_t netsock_write(devminor_t minor, u64_t position,
|
|
|
|
endpoint_t endpt, cp_grant_id_t grant, size_t size, int flags,
|
|
|
|
cdev_id_t id);
|
|
|
|
static int netsock_ioctl(devminor_t minor, unsigned long request,
|
|
|
|
endpoint_t endpt, cp_grant_id_t grant, int flags,
|
|
|
|
endpoint_t user_endpt, cdev_id_t id);
|
|
|
|
static int netsock_cancel(devminor_t minor, endpoint_t endpt, cdev_id_t id);
|
|
|
|
static int netsock_select(devminor_t minor, unsigned int ops,
|
|
|
|
endpoint_t endpt);
|
|
|
|
|
|
|
|
static struct chardriver netsock_tab = {
|
|
|
|
.cdr_open = netsock_open,
|
|
|
|
.cdr_close = netsock_close,
|
|
|
|
.cdr_read = netsock_read,
|
|
|
|
.cdr_write = netsock_write,
|
|
|
|
.cdr_ioctl = netsock_ioctl,
|
|
|
|
.cdr_cancel = netsock_cancel,
|
|
|
|
.cdr_select = netsock_select
|
|
|
|
};
|
|
|
|
|
2011-04-07 09:44:11 +02:00
|
|
|
#define recv_q_alloc() debug_malloc(sizeof(struct recv_q))
|
|
|
|
#define recv_q_free debug_free
|
|
|
|
|
|
|
|
struct mq {
|
2013-09-11 00:50:36 +02:00
|
|
|
struct sock_req req;
|
2011-04-07 09:44:11 +02:00
|
|
|
struct mq * prev;
|
|
|
|
struct mq * next;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define mq_alloc() debug_malloc(sizeof(struct mq))
|
|
|
|
#define mq_free debug_free
|
|
|
|
|
|
|
|
static struct mq * mq_head, *mq_tail;
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
int mq_enqueue(struct sock_req * req)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
struct mq * mq;
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
debug_sock_print("sock %d op %d", req->minor, req->type);
|
2011-04-07 09:44:11 +02:00
|
|
|
mq = mq_alloc();
|
|
|
|
|
|
|
|
if (mq == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
mq->next = NULL;
|
2013-09-11 00:50:36 +02:00
|
|
|
mq->req = *req;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
if (mq_head) {
|
|
|
|
mq->prev = mq_tail;
|
|
|
|
mq_tail->next = mq;
|
|
|
|
mq_tail = mq;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mq->prev = NULL;
|
|
|
|
mq_head = mq_tail = mq;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__unused static struct mq * mq_dequeue_head(void)
|
|
|
|
{
|
|
|
|
struct mq * ret;
|
|
|
|
|
|
|
|
if (!mq_head)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = mq_head;
|
|
|
|
|
|
|
|
if (mq_head != mq_tail) {
|
|
|
|
mq_head = mq_head->next;
|
|
|
|
mq_head->prev = NULL;
|
|
|
|
} else
|
|
|
|
mq_head = mq_tail = NULL;
|
2012-02-24 19:02:32 +01:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
debug_sock_print("socket %d\n", ret->req.minor);
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mq_dequeue(struct mq * mq)
|
|
|
|
{
|
|
|
|
if (mq_head == mq_tail)
|
|
|
|
mq_head = mq_tail = NULL;
|
|
|
|
else {
|
|
|
|
if (mq->prev == NULL) {
|
|
|
|
mq_head = mq->next;
|
|
|
|
mq_head->prev = NULL;
|
|
|
|
} else
|
|
|
|
mq->prev->next = mq->next;
|
|
|
|
if (mq->next == NULL) {
|
|
|
|
mq_tail = mq->prev;
|
|
|
|
mq_tail->next = NULL;
|
|
|
|
} else
|
|
|
|
mq->next->prev = mq->prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
static int mq_cancel(devminor_t minor, endpoint_t endpt, cdev_id_t id)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
struct mq * mq;
|
|
|
|
|
|
|
|
for (mq = mq_tail; mq; mq = mq->prev) {
|
2013-09-11 00:50:36 +02:00
|
|
|
if (minor == mq->req.minor && endpt == mq->req.endpt &&
|
|
|
|
id == mq->req.id) {
|
|
|
|
debug_sock_print("socket %d\n", minor);
|
2011-04-07 09:44:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
if (mq) {
|
|
|
|
mq_dequeue(mq);
|
|
|
|
mq_free(mq);
|
|
|
|
}
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
/* FIXME: shouldn't this return (!!mq) ? */
|
2011-04-07 09:44:11 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sock_enqueue_data(struct socket * sock, void * data, unsigned size)
|
|
|
|
{
|
|
|
|
struct recv_q * r;
|
|
|
|
|
|
|
|
if (!(r = recv_q_alloc()))
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
r->data = data;
|
|
|
|
r->next = NULL;
|
|
|
|
|
|
|
|
if (sock->recv_head) {
|
|
|
|
sock->recv_tail->next = r;
|
|
|
|
sock->recv_tail = r;
|
|
|
|
} else {
|
|
|
|
sock->recv_head = sock->recv_tail = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(size > 0);
|
|
|
|
sock->recv_data_size += size;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void * sock_dequeue_data(struct socket * sock)
|
|
|
|
{
|
|
|
|
void * data;
|
|
|
|
struct recv_q * r;
|
|
|
|
|
|
|
|
if ((r = sock->recv_head)) {
|
|
|
|
data = r->data;
|
|
|
|
if (!(sock->recv_head = r->next))
|
|
|
|
sock->recv_tail = NULL;
|
|
|
|
recv_q_free(r);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sock_dequeue_data_all(struct socket * sock,
|
|
|
|
recv_data_free_fn data_free)
|
|
|
|
{
|
|
|
|
void * data;
|
|
|
|
|
|
|
|
while ((data = sock_dequeue_data(sock)))
|
|
|
|
data_free(data);
|
|
|
|
sock->recv_data_size = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
void send_req_reply(struct sock_req * req, int status)
|
|
|
|
{
|
|
|
|
if (status == EDONTREPLY)
|
|
|
|
return;
|
|
|
|
|
2013-09-04 17:42:36 +02:00
|
|
|
chardriver_reply_task(req->endpt, req->id, status);
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void sock_select_notify(struct socket * sock)
|
|
|
|
{
|
2013-09-04 17:42:36 +02:00
|
|
|
unsigned int ops;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
debug_sock_select_print("socket num %ld", get_sock_num(sock));
|
2011-04-07 09:44:11 +02:00
|
|
|
assert(sock->select_ep != NONE);
|
|
|
|
|
2013-09-04 17:42:36 +02:00
|
|
|
ops = sock->ops->select_reply(sock);
|
|
|
|
if (ops == 0) {
|
2012-02-24 19:02:32 +01:00
|
|
|
debug_sock_select_print("called from %p sflags 0x%x TXsz %d RXsz %d\n",
|
|
|
|
__builtin_return_address(0), sock->flags,
|
|
|
|
sock->buf_size, sock->recv_data_size);
|
2011-04-07 09:44:11 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-02-24 19:02:32 +01:00
|
|
|
|
2013-09-04 17:42:36 +02:00
|
|
|
chardriver_reply_select(sock->select_ep, get_sock_num(sock), ops);
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
sock_clear_select(sock);
|
|
|
|
sock->select_ep = NONE;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
struct socket * get_unused_sock(void)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
for (i = SOCK_TYPES + MAX_DEVS; i < MAX_SOCKETS; i++) {
|
|
|
|
if (socket[i].ops == NULL) {
|
|
|
|
/* clear it all */
|
|
|
|
memset(&socket[i], 0, sizeof(struct socket));
|
|
|
|
return &socket[i];
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
return NULL;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
static int socket_request_socket(struct socket * sock, struct sock_req * req)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
2013-09-10 20:25:01 +02:00
|
|
|
int r, blocking = (req->flags & CDEV_NONBLOCK) ? 0 : 1;
|
2012-02-24 19:02:32 +01:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
switch (req->type) {
|
|
|
|
case SOCK_REQ_READ:
|
2012-08-07 13:10:36 +02:00
|
|
|
if (sock->ops && sock->ops->read)
|
2013-09-11 00:50:36 +02:00
|
|
|
r = sock->ops->read(sock, req, blocking);
|
2011-04-07 09:44:11 +02:00
|
|
|
else
|
2013-09-11 00:50:36 +02:00
|
|
|
r = EINVAL;
|
|
|
|
break;
|
|
|
|
case SOCK_REQ_WRITE:
|
2012-08-07 13:10:36 +02:00
|
|
|
if (sock->ops && sock->ops->write)
|
2013-09-11 00:50:36 +02:00
|
|
|
r = sock->ops->write(sock, req, blocking);
|
2011-04-07 09:44:11 +02:00
|
|
|
else
|
2013-09-11 00:50:36 +02:00
|
|
|
r = EINVAL;
|
|
|
|
break;
|
|
|
|
case SOCK_REQ_IOCTL:
|
2012-08-07 13:10:36 +02:00
|
|
|
if (sock->ops && sock->ops->ioctl)
|
2013-09-11 00:50:36 +02:00
|
|
|
r = sock->ops->ioctl(sock, req, blocking);
|
2011-04-07 09:44:11 +02:00
|
|
|
else
|
2013-09-11 00:50:36 +02:00
|
|
|
r = EINVAL;
|
|
|
|
break;
|
2011-04-07 09:44:11 +02:00
|
|
|
default:
|
2012-02-24 19:02:32 +01:00
|
|
|
netsock_panic("cannot happen!");
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
2013-09-11 00:50:36 +02:00
|
|
|
|
|
|
|
return r;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2013-09-04 17:42:36 +02:00
|
|
|
static int netsock_open(devminor_t minor, int UNUSED(access),
|
|
|
|
endpoint_t UNUSED(user_endpt))
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
2013-09-10 20:25:01 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = socket_open(minor)) < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return CDEV_CLONED | r;
|
2013-09-04 17:42:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int netsock_close(devminor_t minor)
|
|
|
|
{
|
|
|
|
struct socket *sock;
|
|
|
|
|
|
|
|
if (!(sock = get_sock(minor)))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (sock->ops && sock->ops->close) {
|
|
|
|
sock->flags &= ~SOCK_FLG_OP_PENDING;
|
|
|
|
|
|
|
|
return sock->ops->close(sock);
|
|
|
|
} else
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int netsock_request(struct socket *sock, struct sock_req *req)
|
|
|
|
{
|
|
|
|
char *o;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If an operation is pending (blocking operation) or writing is
|
|
|
|
* still going on and we're reading, suspend the new operation
|
|
|
|
*/
|
|
|
|
if ((sock->flags & SOCK_FLG_OP_PENDING) ||
|
|
|
|
(req->type == SOCK_REQ_READ &&
|
|
|
|
sock->flags & SOCK_FLG_OP_WRITING)) {
|
|
|
|
if (sock->flags & SOCK_FLG_OP_READING)
|
|
|
|
o = "READ";
|
|
|
|
else if (sock->flags & SOCK_FLG_OP_WRITING)
|
|
|
|
o = "WRITE";
|
|
|
|
else
|
|
|
|
o = "non R/W op";
|
|
|
|
debug_sock_print("socket %ld is busy by %s flgs 0x%x\n",
|
|
|
|
get_sock_num(sock), o, sock->flags);
|
|
|
|
|
|
|
|
if (mq_enqueue(req) != 0) {
|
|
|
|
debug_sock_print("Enqueuing suspended call failed");
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EDONTREPLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return socket_request_socket(sock, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t netsock_read(devminor_t minor, u64_t UNUSED(position),
|
|
|
|
endpoint_t endpt, cp_grant_id_t grant, size_t size, int flags,
|
|
|
|
cdev_id_t id)
|
|
|
|
{
|
|
|
|
struct socket *sock;
|
|
|
|
struct sock_req req;
|
|
|
|
|
|
|
|
if (!(sock = get_sock(minor)))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* Build a request record for this request. */
|
|
|
|
req.type = SOCK_REQ_READ;
|
|
|
|
req.minor = minor;
|
|
|
|
req.endpt = endpt;
|
|
|
|
req.grant = grant;
|
|
|
|
req.size = size;
|
|
|
|
req.flags = flags;
|
|
|
|
req.id = id;
|
|
|
|
|
|
|
|
/* Process the request. */
|
|
|
|
return netsock_request(sock, &req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t netsock_write(devminor_t minor, u64_t UNUSED(position),
|
|
|
|
endpoint_t endpt, cp_grant_id_t grant, size_t size, int flags,
|
|
|
|
cdev_id_t id)
|
|
|
|
{
|
|
|
|
struct socket *sock;
|
|
|
|
struct sock_req req;
|
|
|
|
|
|
|
|
if (!(sock = get_sock(minor)))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* Build a request record for this request. */
|
|
|
|
req.type = SOCK_REQ_WRITE;
|
|
|
|
req.minor = minor;
|
|
|
|
req.endpt = endpt;
|
|
|
|
req.grant = grant;
|
|
|
|
req.size = size;
|
|
|
|
req.flags = flags;
|
|
|
|
req.id = id;
|
|
|
|
|
|
|
|
/* Process the request. */
|
|
|
|
return netsock_request(sock, &req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int netsock_ioctl(devminor_t minor, unsigned long request,
|
|
|
|
endpoint_t endpt, cp_grant_id_t grant, int flags,
|
|
|
|
endpoint_t UNUSED(user_endpt), cdev_id_t id)
|
|
|
|
{
|
|
|
|
struct socket *sock;
|
2013-09-11 00:50:36 +02:00
|
|
|
struct sock_req req;
|
2013-09-04 17:42:36 +02:00
|
|
|
|
|
|
|
if (!(sock = get_sock(minor)))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* Build a request record for this request. */
|
|
|
|
req.type = SOCK_REQ_IOCTL;
|
|
|
|
req.minor = minor;
|
|
|
|
req.req = request;
|
|
|
|
req.endpt = endpt;
|
|
|
|
req.grant = grant;
|
|
|
|
req.flags = flags;
|
|
|
|
req.id = id;
|
|
|
|
|
|
|
|
/* Process the request. */
|
|
|
|
return netsock_request(sock, &req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int netsock_cancel(devminor_t minor, endpoint_t endpt, cdev_id_t id)
|
|
|
|
{
|
|
|
|
struct socket *sock;
|
|
|
|
|
|
|
|
if (!(sock = get_sock(minor)))
|
|
|
|
return EDONTREPLY;
|
|
|
|
|
|
|
|
debug_sock_print("socket num %ld", get_sock_num(sock));
|
|
|
|
|
|
|
|
/* Cancel the last operation in the queue */
|
|
|
|
if (mq_cancel(minor, endpt, id))
|
|
|
|
return EINTR;
|
|
|
|
|
|
|
|
/* Cancel any ongoing blocked read */
|
|
|
|
if ((sock->flags & SOCK_FLG_OP_PENDING) &&
|
|
|
|
(sock->flags & SOCK_FLG_OP_READING) &&
|
|
|
|
endpt == sock->req.endpt && id == sock->req.id) {
|
|
|
|
sock->flags &= ~SOCK_FLG_OP_PENDING;
|
|
|
|
return EINTR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The request may not be found. This is OK. Do not reply. */
|
|
|
|
return EDONTREPLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int netsock_select(devminor_t minor, unsigned int ops, endpoint_t endpt)
|
|
|
|
{
|
|
|
|
struct socket *sock;
|
2013-09-11 00:50:36 +02:00
|
|
|
int r;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2013-09-04 17:42:36 +02:00
|
|
|
/*
|
|
|
|
* Select is always executed immediately and is never suspended.
|
|
|
|
* Although, it sets actions which must be monitored
|
|
|
|
*/
|
|
|
|
if (!(sock = get_sock(minor)))
|
|
|
|
return EBADF;
|
|
|
|
|
|
|
|
assert(sock->select_ep == NONE || sock->select_ep == endpt);
|
|
|
|
|
|
|
|
if (sock->ops && sock->ops->select) {
|
|
|
|
sock->select_ep = endpt;
|
|
|
|
r = sock->ops->select(sock, ops);
|
|
|
|
if (!sock_select_set(sock))
|
|
|
|
sock->select_ep = NONE;
|
|
|
|
} else
|
|
|
|
r = EINVAL;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void socket_request(message * m, int ipc_status)
|
|
|
|
{
|
2012-02-24 19:02:32 +01:00
|
|
|
debug_sock_print("request %d", m->m_type);
|
2013-09-11 00:50:36 +02:00
|
|
|
|
2013-09-04 17:42:36 +02:00
|
|
|
/* Let the chardriver library decode the request for us. */
|
|
|
|
chardriver_process(&netsock_tab, m, ipc_status);
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void mq_process(void)
|
|
|
|
{
|
|
|
|
struct mq * mq;
|
|
|
|
struct socket * sock;
|
2013-09-11 00:50:36 +02:00
|
|
|
int r;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
mq = mq_head;
|
|
|
|
|
|
|
|
while(mq) {
|
|
|
|
struct mq * next = mq->next;
|
2012-02-24 19:02:32 +01:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
sock = get_sock(mq->req.minor);
|
2012-02-24 19:02:32 +01:00
|
|
|
if (!(sock->flags & SOCK_FLG_OP_PENDING) &&
|
2013-09-11 00:50:36 +02:00
|
|
|
!(mq->req.type == SOCK_REQ_READ &&
|
2012-02-24 19:02:32 +01:00
|
|
|
sock->flags & SOCK_FLG_OP_WRITING)) {
|
2011-04-07 09:44:11 +02:00
|
|
|
debug_sock_print("resuming op on sock %ld\n",
|
|
|
|
get_sock_num(sock));
|
2013-09-11 00:50:36 +02:00
|
|
|
sock->req = mq->req;
|
|
|
|
r = socket_request_socket(sock, &sock->req);
|
|
|
|
send_req_reply(&sock->req, r);
|
2011-04-07 09:44:11 +02:00
|
|
|
mq_dequeue(mq);
|
|
|
|
mq_free(mq);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mq = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
int generic_op_select(struct socket * sock, unsigned int sel)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
2013-09-11 00:50:36 +02:00
|
|
|
int retsel = 0;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
debug_sock_print("socket num %ld 0x%x", get_sock_num(sock), sel);
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
/* in this case any operation would block, no error */
|
|
|
|
if (sock->flags & SOCK_FLG_OP_PENDING) {
|
2013-09-10 20:25:01 +02:00
|
|
|
if (sel & CDEV_NOTIFY) {
|
|
|
|
if (sel & CDEV_OP_RD)
|
2011-04-07 09:44:11 +02:00
|
|
|
sock->flags |= SOCK_FLG_SEL_READ;
|
2013-09-10 20:25:01 +02:00
|
|
|
if (sel & CDEV_OP_WR)
|
2011-04-07 09:44:11 +02:00
|
|
|
sock->flags |= SOCK_FLG_SEL_WRITE;
|
|
|
|
/* FIXME we do not monitor error */
|
|
|
|
}
|
2013-09-11 00:50:36 +02:00
|
|
|
return 0;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2013-09-10 20:25:01 +02:00
|
|
|
if (sel & CDEV_OP_RD) {
|
2011-04-07 09:44:11 +02:00
|
|
|
if (sock->recv_head)
|
2013-09-10 20:25:01 +02:00
|
|
|
retsel |= CDEV_OP_RD;
|
|
|
|
else if (sel & CDEV_NOTIFY)
|
2011-04-07 09:44:11 +02:00
|
|
|
sock->flags |= SOCK_FLG_SEL_READ;
|
|
|
|
}
|
|
|
|
/* FIXME generic packet socket never blocks on write */
|
2013-09-10 20:25:01 +02:00
|
|
|
if (sel & CDEV_OP_WR)
|
|
|
|
retsel |= CDEV_OP_WR;
|
|
|
|
/* FIXME CDEV_OP_ERR is ignored, we do not generate exceptions */
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
return retsel;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
int generic_op_select_reply(struct socket * sock)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
2013-09-11 00:50:36 +02:00
|
|
|
unsigned int sel = 0;
|
|
|
|
|
2011-04-07 09:44:11 +02:00
|
|
|
assert(sock->select_ep != NONE);
|
2012-02-24 19:02:32 +01:00
|
|
|
debug_sock_print("socket num %ld", get_sock_num(sock));
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
/* unused for generic packet socket, see generic_op_select() */
|
|
|
|
assert((sock->flags & (SOCK_FLG_SEL_WRITE | SOCK_FLG_SEL_ERROR)) == 0);
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
if (sock->flags & SOCK_FLG_OP_PENDING) {
|
|
|
|
debug_sock_print("WARNING socket still blocking!");
|
2013-09-11 00:50:36 +02:00
|
|
|
return 0;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->flags & SOCK_FLG_SEL_READ && sock->recv_head)
|
2013-09-10 20:25:01 +02:00
|
|
|
sel |= CDEV_OP_RD;
|
2012-02-24 19:02:32 +01:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
if (sel)
|
2011-04-07 09:44:11 +02:00
|
|
|
sock->flags &= ~(SOCK_FLG_SEL_WRITE | SOCK_FLG_SEL_READ |
|
|
|
|
SOCK_FLG_SEL_ERROR);
|
2013-09-11 00:50:36 +02:00
|
|
|
|
|
|
|
return sel;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|