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];
|
|
|
|
|
|
|
|
#define recv_q_alloc() debug_malloc(sizeof(struct recv_q))
|
|
|
|
#define recv_q_free debug_free
|
|
|
|
|
|
|
|
struct mq {
|
|
|
|
message m;
|
|
|
|
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;
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
int mq_enqueue(message * m)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
struct mq * mq;
|
|
|
|
|
|
|
|
debug_sock_print("sock %d op %d", m->DEVICE, m->m_type);
|
|
|
|
mq = mq_alloc();
|
|
|
|
|
|
|
|
if (mq == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
mq->next = NULL;
|
|
|
|
mq->m = *m;
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-04-07 09:44:11 +02:00
|
|
|
debug_sock_print("socket %d\n", ret->m.DEVICE);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mq_cancel(message * m)
|
|
|
|
{
|
|
|
|
struct mq * mq;
|
|
|
|
|
|
|
|
for (mq = mq_tail; mq; mq = mq->prev) {
|
|
|
|
if (m->DEVICE == mq->m.DEVICE &&
|
Server/driver protocols: no longer allow third-party copies.
Before safecopies, the IO_ENDPT and DL_ENDPT message fields were needed
to know which actual process to copy data from/to, as that process may
not always be the caller. Now that we have full safecopy support, these
fields have become useless for that purpose: the owner of the grant is
*always* the caller. Allowing the caller to supply another endpoint is
in fact dangerous, because the callee may then end up using a grant
from a third party. One could call this a variant of the confused
deputy problem.
From now on, safecopy calls should always use the caller's endpoint as
grant owner. This fully obsoletes the DL_ENDPT field in the
inet/ethernet protocol. IO_ENDPT has other uses besides identifying the
grant owner though. This patch renames IO_ENDPT to USER_ENDPT, not only
because that is a more fitting name (it should never be used for I/O
after all), but also in order to intentionally break any old system
source code outside the base system. If this patch breaks your code,
fixing it is fairly simple:
- DL_ENDPT should be replaced with m_source;
- IO_ENDPT should be replaced with m_source when used for safecopies;
- IO_ENDPT should be replaced with USER_ENDPT for any other use, e.g.
when setting REP_ENDPT, matching requests in CANCEL calls, getting
DEV_SELECT flags, and retrieving of the real user process's endpoint
in DEV_OPEN.
The changes in this patch are binary backward compatible.
2011-04-11 19:35:05 +02:00
|
|
|
m->USER_ENDPT == mq->m.USER_ENDPT &&
|
2011-04-07 09:44:11 +02:00
|
|
|
m->IO_GRANT == mq->m.IO_GRANT) {
|
|
|
|
debug_sock_print("socket %d\n", mq->m.DEVICE);
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_reply_msg(message * m, int status)
|
|
|
|
{
|
|
|
|
int proc, ref;
|
|
|
|
|
Server/driver protocols: no longer allow third-party copies.
Before safecopies, the IO_ENDPT and DL_ENDPT message fields were needed
to know which actual process to copy data from/to, as that process may
not always be the caller. Now that we have full safecopy support, these
fields have become useless for that purpose: the owner of the grant is
*always* the caller. Allowing the caller to supply another endpoint is
in fact dangerous, because the callee may then end up using a grant
from a third party. One could call this a variant of the confused
deputy problem.
From now on, safecopy calls should always use the caller's endpoint as
grant owner. This fully obsoletes the DL_ENDPT field in the
inet/ethernet protocol. IO_ENDPT has other uses besides identifying the
grant owner though. This patch renames IO_ENDPT to USER_ENDPT, not only
because that is a more fitting name (it should never be used for I/O
after all), but also in order to intentionally break any old system
source code outside the base system. If this patch breaks your code,
fixing it is fairly simple:
- DL_ENDPT should be replaced with m_source;
- IO_ENDPT should be replaced with m_source when used for safecopies;
- IO_ENDPT should be replaced with USER_ENDPT for any other use, e.g.
when setting REP_ENDPT, matching requests in CANCEL calls, getting
DEV_SELECT flags, and retrieving of the real user process's endpoint
in DEV_OPEN.
The changes in this patch are binary backward compatible.
2011-04-11 19:35:05 +02:00
|
|
|
proc= m->USER_ENDPT;
|
2011-04-07 09:44:11 +02:00
|
|
|
ref= (int)m->IO_GRANT;
|
|
|
|
|
|
|
|
m->REP_ENDPT= proc;
|
|
|
|
m->REP_STATUS= status;
|
|
|
|
m->REP_IO_GRANT= ref;
|
|
|
|
}
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
void send_reply_type(message * m, int type, int status)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
set_reply_msg(m, status);
|
2012-02-24 19:02:32 +01:00
|
|
|
|
|
|
|
m->m_type = type;
|
2011-04-07 09:44:11 +02:00
|
|
|
result = send(m->m_source, m);
|
|
|
|
if (result != OK)
|
2012-02-24 19:02:32 +01:00
|
|
|
netsock_panic("unable to send (err %d)", result);
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
void send_reply(message * m, int status)
|
|
|
|
{
|
|
|
|
debug_sock_print("status %d", status);
|
|
|
|
send_reply_type(m, DEV_REVIVE, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_reply_open(message * m, int status)
|
|
|
|
{
|
|
|
|
debug_sock_print("status %d", status);
|
|
|
|
send_reply_type(m, DEV_OPEN_REPL, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_reply_close(message * m, int status)
|
|
|
|
{
|
|
|
|
debug_sock_print("status %d", status);
|
|
|
|
send_reply_type(m, DEV_CLOSE_REPL, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sock_reply_select(struct socket * sock, unsigned selops)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
int result;
|
2012-02-24 19:02:32 +01:00
|
|
|
message msg;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
debug_sock_select_print("selops %d", selops);
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
msg.m_type = DEV_SEL_REPL1;
|
|
|
|
msg.DEV_MINOR = get_sock_num(sock);
|
|
|
|
msg.DEV_SEL_OPS = selops;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
result = send(sock->select_ep, &msg);
|
|
|
|
if (result != OK)
|
|
|
|
netsock_panic("unable to send (err %d)", result);
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void sock_select_notify(struct socket * sock)
|
|
|
|
{
|
|
|
|
int result;
|
2012-02-24 19:02:32 +01:00
|
|
|
message msg;
|
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);
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
msg.DEV_SEL_OPS = 0;
|
|
|
|
sock->ops->select_reply(sock, &msg);
|
|
|
|
if (msg.DEV_SEL_OPS == 0) {
|
|
|
|
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
|
|
|
|
|
|
|
msg.m_type = DEV_SEL_REPL2;
|
|
|
|
msg.DEV_MINOR = get_sock_num(sock);
|
|
|
|
|
|
|
|
debug_sock_select_print("socket num %d select result 0x%x sent",
|
|
|
|
msg.DEV_MINOR, msg.DEV_SEL_OPS);
|
|
|
|
result = send(sock->select_ep, &msg);
|
2011-04-07 09:44:11 +02:00
|
|
|
if (result != OK)
|
2012-02-24 19:02:32 +01:00
|
|
|
netsock_panic("unable to send (err %d)", result);
|
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
|
|
|
static void sock_reply_type(struct socket * sock, int type, int status)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
2012-02-24 19:02:32 +01:00
|
|
|
sock->mess.m_type = type;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
send_reply_type(&sock->mess, type, status);
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
void sock_reply_close(struct socket * sock, int status)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
2012-02-24 19:02:32 +01:00
|
|
|
debug_sock_print("sock %ld status %d", get_sock_num(sock), status);
|
|
|
|
sock_reply_type(sock, DEV_CLOSE_REPL, status);
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
void sock_reply(struct socket * sock, int status)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
2012-02-24 19:02:32 +01:00
|
|
|
debug_sock_print("sock %ld status %d", get_sock_num(sock), status);
|
|
|
|
sock_reply_type(sock, DEV_REVIVE, status);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void socket_request_socket(struct socket * sock, message * m)
|
|
|
|
{
|
2012-02-24 19:02:32 +01:00
|
|
|
int blocking = m->FLAGS & FLG_OP_NONBLOCK ? 0 : 1;
|
|
|
|
|
2011-04-07 09:44:11 +02:00
|
|
|
switch (m->m_type) {
|
|
|
|
case DEV_READ_S:
|
2012-08-07 13:10:36 +02:00
|
|
|
if (sock->ops && sock->ops->read)
|
2012-02-24 19:02:32 +01:00
|
|
|
sock->ops->read(sock, m, blocking);
|
2011-04-07 09:44:11 +02:00
|
|
|
else
|
|
|
|
send_reply(m, EINVAL);
|
|
|
|
return;
|
|
|
|
case DEV_WRITE_S:
|
2012-08-07 13:10:36 +02:00
|
|
|
if (sock->ops && sock->ops->write)
|
2012-02-24 19:02:32 +01:00
|
|
|
sock->ops->write(sock, m, blocking);
|
2011-04-07 09:44:11 +02:00
|
|
|
else
|
|
|
|
send_reply(m, EINVAL);
|
|
|
|
return;
|
|
|
|
case DEV_IOCTL_S:
|
2012-08-07 13:10:36 +02:00
|
|
|
if (sock->ops && sock->ops->ioctl)
|
2012-02-24 19:02:32 +01:00
|
|
|
sock->ops->ioctl(sock, m, blocking);
|
2011-04-07 09:44:11 +02:00
|
|
|
else
|
|
|
|
send_reply(m, EINVAL);
|
|
|
|
return;
|
|
|
|
default:
|
2012-02-24 19:02:32 +01:00
|
|
|
netsock_panic("cannot happen!");
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void socket_request(message * m)
|
|
|
|
{
|
|
|
|
struct socket * sock;
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
debug_sock_print("request %d", m->m_type);
|
2011-04-07 09:44:11 +02:00
|
|
|
switch (m->m_type) {
|
|
|
|
case DEV_OPEN:
|
|
|
|
socket_open(m);
|
|
|
|
return;
|
|
|
|
case DEV_CLOSE:
|
|
|
|
sock = get_sock(m->DEVICE);
|
2012-08-07 13:10:36 +02:00
|
|
|
if (sock->ops && sock->ops->close) {
|
2011-04-07 09:44:11 +02:00
|
|
|
sock->flags &= ~SOCK_FLG_OP_PENDING;
|
|
|
|
sock->mess = *m;
|
|
|
|
sock->ops->close(sock, m);
|
|
|
|
} else
|
2012-02-24 19:02:32 +01:00
|
|
|
send_reply_close(m, EINVAL);
|
2011-04-07 09:44:11 +02:00
|
|
|
return;
|
|
|
|
case DEV_READ_S:
|
|
|
|
case DEV_WRITE_S:
|
|
|
|
case DEV_IOCTL_S:
|
|
|
|
sock = get_sock(m->DEVICE);
|
|
|
|
if (!sock) {
|
|
|
|
send_reply(m, EINVAL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If an operation is pending (blocking operation) or writing is
|
|
|
|
* still going and we want to read, suspend the new operation
|
|
|
|
*/
|
2012-02-24 19:02:32 +01:00
|
|
|
if ((sock->flags & SOCK_FLG_OP_PENDING) ||
|
2011-04-07 09:44:11 +02:00
|
|
|
(m->m_type == DEV_READ_S &&
|
|
|
|
sock->flags & SOCK_FLG_OP_WRITING)) {
|
|
|
|
char * o = "\0";
|
|
|
|
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";
|
2012-02-24 19:02:32 +01:00
|
|
|
debug_sock_print("socket %ld is busy by %s flgs 0x%x\n",
|
|
|
|
get_sock_num(sock), o, sock->flags);
|
|
|
|
if (mq_enqueue(m) != 0) {
|
2011-04-07 09:44:11 +02:00
|
|
|
debug_sock_print("Enqueuing suspended "
|
|
|
|
"call failed");
|
|
|
|
send_reply(m, ENOMEM);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sock->mess = *m;
|
|
|
|
socket_request_socket(sock, m);
|
|
|
|
return;
|
|
|
|
case CANCEL:
|
|
|
|
sock = get_sock(m->DEVICE);
|
2012-02-24 19:02:32 +01:00
|
|
|
printf("socket num %ld\n", get_sock_num(sock));
|
2011-04-07 09:44:11 +02:00
|
|
|
debug_sock_print("socket num %ld", get_sock_num(sock));
|
|
|
|
/* Cancel the last operation in the queue */
|
|
|
|
if (mq_cancel(m)) {
|
|
|
|
send_reply(m, EINTR);
|
|
|
|
return;
|
|
|
|
/* ... or a blocked read */
|
|
|
|
} else if (sock->flags & SOCK_FLG_OP_PENDING &&
|
|
|
|
sock->flags & SOCK_FLG_OP_READING) {
|
|
|
|
sock->flags &= ~SOCK_FLG_OP_PENDING;
|
|
|
|
send_reply(m, EINTR);
|
|
|
|
return;
|
|
|
|
} else
|
2012-02-24 19:02:32 +01:00
|
|
|
netsock_panic("no operation to cancel");
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
case DEV_SELECT:
|
2012-02-24 19:02:32 +01:00
|
|
|
/*
|
2011-04-07 09:44:11 +02:00
|
|
|
* Select is always executed immediately and is never suspended.
|
|
|
|
* Although, it sets actions which must be monitored
|
|
|
|
*/
|
|
|
|
sock = get_sock(m->DEVICE);
|
|
|
|
assert(sock->select_ep == NONE || sock->select_ep == m->m_source);
|
2012-02-24 19:02:32 +01:00
|
|
|
|
2012-08-07 13:10:36 +02:00
|
|
|
if (sock->ops && sock->ops->select) {
|
2012-02-24 19:02:32 +01:00
|
|
|
sock->select_ep = m->m_source;
|
2011-04-07 09:44:11 +02:00
|
|
|
sock->ops->select(sock, m);
|
2012-02-24 19:02:32 +01:00
|
|
|
if (!sock_select_set(sock))
|
|
|
|
sock->select_ep = NONE;
|
2011-04-07 09:44:11 +02:00
|
|
|
} else
|
|
|
|
send_reply(m, EINVAL);
|
|
|
|
return;
|
|
|
|
default:
|
2012-02-24 19:02:32 +01:00
|
|
|
netsock_error("unknown message from VFS, type %d\n",
|
2011-04-07 09:44:11 +02:00
|
|
|
m->m_type);
|
|
|
|
}
|
|
|
|
send_reply(m, EGENERIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mq_process(void)
|
|
|
|
{
|
|
|
|
struct mq * mq;
|
|
|
|
struct socket * sock;
|
|
|
|
|
|
|
|
mq = mq_head;
|
|
|
|
|
|
|
|
while(mq) {
|
|
|
|
struct mq * next = mq->next;
|
2012-02-24 19:02:32 +01:00
|
|
|
|
2011-04-07 09:44:11 +02:00
|
|
|
sock = get_sock(mq->m.DEVICE);
|
2012-02-24 19:02:32 +01:00
|
|
|
if (!(sock->flags & SOCK_FLG_OP_PENDING) &&
|
2011-04-07 09:44:11 +02:00
|
|
|
!(mq->m.m_type == DEV_READ_S &&
|
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));
|
|
|
|
sock->mess = mq->m;
|
|
|
|
socket_request_socket(sock, &sock->mess);
|
|
|
|
mq_dequeue(mq);
|
|
|
|
mq_free(mq);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mq = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void generic_op_select(struct socket * sock, message * m)
|
|
|
|
{
|
|
|
|
int retsel = 0, sel;
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
debug_sock_print("socket num %ld 0x%x", get_sock_num(sock), m->USER_ENDPT);
|
2011-04-07 09:44:11 +02:00
|
|
|
|
Server/driver protocols: no longer allow third-party copies.
Before safecopies, the IO_ENDPT and DL_ENDPT message fields were needed
to know which actual process to copy data from/to, as that process may
not always be the caller. Now that we have full safecopy support, these
fields have become useless for that purpose: the owner of the grant is
*always* the caller. Allowing the caller to supply another endpoint is
in fact dangerous, because the callee may then end up using a grant
from a third party. One could call this a variant of the confused
deputy problem.
From now on, safecopy calls should always use the caller's endpoint as
grant owner. This fully obsoletes the DL_ENDPT field in the
inet/ethernet protocol. IO_ENDPT has other uses besides identifying the
grant owner though. This patch renames IO_ENDPT to USER_ENDPT, not only
because that is a more fitting name (it should never be used for I/O
after all), but also in order to intentionally break any old system
source code outside the base system. If this patch breaks your code,
fixing it is fairly simple:
- DL_ENDPT should be replaced with m_source;
- IO_ENDPT should be replaced with m_source when used for safecopies;
- IO_ENDPT should be replaced with USER_ENDPT for any other use, e.g.
when setting REP_ENDPT, matching requests in CANCEL calls, getting
DEV_SELECT flags, and retrieving of the real user process's endpoint
in DEV_OPEN.
The changes in this patch are binary backward compatible.
2011-04-11 19:35:05 +02:00
|
|
|
sel = m->USER_ENDPT;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
/* in this case any operation would block, no error */
|
|
|
|
if (sock->flags & SOCK_FLG_OP_PENDING) {
|
|
|
|
if (sel & SEL_NOTIFY) {
|
|
|
|
if (sel & SEL_RD)
|
|
|
|
sock->flags |= SOCK_FLG_SEL_READ;
|
|
|
|
if (sel & SEL_WR)
|
|
|
|
sock->flags |= SOCK_FLG_SEL_WRITE;
|
|
|
|
/* FIXME we do not monitor error */
|
|
|
|
}
|
2012-02-24 19:02:32 +01:00
|
|
|
sock_reply_select(sock, 0);
|
2011-04-07 09:44:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sel & SEL_RD) {
|
|
|
|
if (sock->recv_head)
|
|
|
|
retsel |= SEL_RD;
|
|
|
|
else if (sel & SEL_NOTIFY)
|
|
|
|
sock->flags |= SOCK_FLG_SEL_READ;
|
|
|
|
}
|
|
|
|
/* FIXME generic packet socket never blocks on write */
|
|
|
|
if (sel & SEL_WR)
|
|
|
|
retsel |= SEL_WR;
|
|
|
|
/* FIXME SEL_ERR is ignored, we do not generate exceptions */
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
sock_reply_select(sock, retsel);
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void generic_op_select_reply(struct socket * sock, __unused message * m)
|
|
|
|
{
|
|
|
|
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!");
|
2011-04-07 09:44:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->flags & SOCK_FLG_SEL_READ && sock->recv_head)
|
|
|
|
m->DEV_SEL_OPS |= SEL_RD;
|
2012-02-24 19:02:32 +01:00
|
|
|
|
|
|
|
if (m->DEV_SEL_OPS)
|
2011-04-07 09:44:11 +02:00
|
|
|
sock->flags &= ~(SOCK_FLG_SEL_WRITE | SOCK_FLG_SEL_READ |
|
|
|
|
SOCK_FLG_SEL_ERROR);
|
|
|
|
}
|