2010-07-15 15:39:57 +02:00
|
|
|
/*
|
|
|
|
* Unix Domain Sockets Implementation (PF_UNIX, PF_LOCAL)
|
|
|
|
* This code handles ioctl(2) commands to implement the socket API.
|
|
|
|
* Some helper functions are also present.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uds.h"
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
perform_connection(devminor_t minorx, devminor_t minory,
|
2013-09-03 02:00:20 +02:00
|
|
|
struct sockaddr_un *addr)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* There are several places were a connection is established, the
|
|
|
|
* initiating call being one of accept(2), connect(2), socketpair(2).
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: perform_connection(%d, %d)\n", minorx, minory));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* Only connection-oriented types are acceptable and only equal
|
|
|
|
* types can connect to each other.
|
|
|
|
*/
|
|
|
|
if ((uds_fd_table[minorx].type != SOCK_SEQPACKET &&
|
|
|
|
uds_fd_table[minorx].type != SOCK_STREAM) ||
|
|
|
|
uds_fd_table[minorx].type != uds_fd_table[minory].type)
|
2010-07-15 15:39:57 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Connect the pair of sockets. */
|
2010-07-15 15:39:57 +02:00
|
|
|
uds_fd_table[minorx].peer = minory;
|
|
|
|
uds_fd_table[minory].peer = minorx;
|
|
|
|
|
|
|
|
/* Set the address of both sockets */
|
2013-10-04 16:46:18 +02:00
|
|
|
memcpy(&uds_fd_table[minorx].addr, addr, sizeof(struct sockaddr_un));
|
|
|
|
memcpy(&uds_fd_table[minory].addr, addr, sizeof(struct sockaddr_un));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_accept(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-09-03 02:00:20 +02:00
|
|
|
devminor_t minorparent; /* minor number of parent (server) */
|
|
|
|
devminor_t minorpeer;
|
2010-07-15 15:39:57 +02:00
|
|
|
int rc, i;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_accept(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* Somewhat weird logic is used in this function, so here's an
|
2012-02-13 16:28:04 +01:00
|
|
|
* overview... The minor number is the server's client socket
|
|
|
|
* (the socket to be returned by accept()). The data waiting
|
|
|
|
* for us in the IO Grant is the address that the server is
|
|
|
|
* listening on. This function uses the address to find the
|
|
|
|
* server's descriptor. From there we can perform the
|
2010-07-15 15:39:57 +02:00
|
|
|
* connection or suspend and wait for a connect().
|
|
|
|
*/
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* This IOCTL must be called on a 'fresh' socket. */
|
|
|
|
if (uds_fd_table[minor].type != -1)
|
2010-07-15 15:39:57 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* Get the server's address */
|
2013-10-04 16:46:18 +02:00
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &addr,
|
|
|
|
sizeof(struct sockaddr_un))) != OK)
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Locate the server socket. */
|
2010-07-15 15:39:57 +02:00
|
|
|
for (i = 0; i < NR_FDS; i++) {
|
|
|
|
if (uds_fd_table[i].addr.sun_family == AF_UNIX &&
|
2013-10-04 16:46:18 +02:00
|
|
|
!strncmp(addr.sun_path, uds_fd_table[i].addr.sun_path,
|
|
|
|
UNIX_PATH_MAX) && uds_fd_table[i].listening == 1)
|
2010-07-15 15:39:57 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if (i == NR_FDS)
|
2010-07-15 15:39:57 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
minorparent = i; /* parent */
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* We are the parent's child. */
|
2010-07-15 15:39:57 +02:00
|
|
|
uds_fd_table[minorparent].child = minor;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* The peer has the same type as the parent. we need to be that
|
2010-07-15 15:39:57 +02:00
|
|
|
* type too.
|
|
|
|
*/
|
|
|
|
uds_fd_table[minor].type = uds_fd_table[minorparent].type;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Locate the peer to accept in the parent's backlog. */
|
|
|
|
minorpeer = -1;
|
2010-07-15 15:39:57 +02:00
|
|
|
for (i = 0; i < uds_fd_table[minorparent].backlog_size; i++) {
|
|
|
|
if (uds_fd_table[minorparent].backlog[i] != -1) {
|
|
|
|
minorpeer = uds_fd_table[minorparent].backlog[i];
|
|
|
|
uds_fd_table[minorparent].backlog[i] = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minorpeer == -1) {
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_accept(%d): suspend\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* There are no peers in the backlog, suspend and wait for one
|
|
|
|
* to show up.
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
|
|
|
uds_fd_table[minor].suspended = UDS_SUSPENDED_ACCEPT;
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
return EDONTREPLY;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: connecting %d to %d -- parent is %d\n", minor,
|
|
|
|
minorpeer, minorparent));
|
|
|
|
|
|
|
|
if ((rc = perform_connection(minor, minorpeer, &addr)) != OK) {
|
|
|
|
dprintf(("UDS: do_accept(%d): connection failed\n", minor));
|
|
|
|
|
2011-03-25 11:52:53 +01:00
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uds_fd_table[minorparent].child = -1;
|
|
|
|
|
2013-10-04 17:57:42 +02:00
|
|
|
/* If the peer is blocked on connect() or write(), revive the peer. */
|
|
|
|
if (uds_fd_table[minorpeer].suspended == UDS_SUSPENDED_CONNECT ||
|
|
|
|
uds_fd_table[minorpeer].suspended == UDS_SUSPENDED_WRITE) {
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_accept(%d): revive %d\n", minor, minorpeer));
|
2013-09-03 02:00:20 +02:00
|
|
|
uds_unsuspend(minorpeer);
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 17:57:42 +02:00
|
|
|
/* See if we can satisfy an ongoing select. */
|
|
|
|
if ((uds_fd_table[minorpeer].sel_ops & CDEV_OP_WR) &&
|
|
|
|
uds_fd_table[minorpeer].size < UDS_BUF) {
|
|
|
|
/* A write on the peer is possible now. */
|
|
|
|
chardriver_reply_select(uds_fd_table[minorpeer].sel_endpt,
|
|
|
|
minorpeer, CDEV_OP_WR);
|
|
|
|
uds_fd_table[minorpeer].sel_ops &= ~CDEV_OP_WR;
|
|
|
|
}
|
|
|
|
|
2010-07-15 15:39:57 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_connect(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 17:57:42 +02:00
|
|
|
int child, peer;
|
2010-07-15 15:39:57 +02:00
|
|
|
struct sockaddr_un addr;
|
|
|
|
int rc, i, j;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_connect(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Only connection oriented sockets can connect. */
|
2010-07-15 15:39:57 +02:00
|
|
|
if (uds_fd_table[minor].type != SOCK_STREAM &&
|
2013-10-04 16:46:18 +02:00
|
|
|
uds_fd_table[minor].type != SOCK_SEQPACKET)
|
2010-07-15 15:39:57 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
2013-10-04 17:57:42 +02:00
|
|
|
/* The socket must not be connecting or connected already. */
|
|
|
|
peer = uds_fd_table[minor].peer;
|
|
|
|
if (peer != -1) {
|
|
|
|
if (uds_fd_table[peer].peer == -1)
|
|
|
|
return EALREADY; /* connecting */
|
|
|
|
else
|
|
|
|
return EISCONN; /* connected */
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &addr,
|
|
|
|
sizeof(struct sockaddr_un))) != OK)
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-11-03 22:33:44 +01:00
|
|
|
if ((rc = checkperms(uds_fd_table[minor].owner, addr.sun_path,
|
|
|
|
UNIX_PATH_MAX)) != OK)
|
|
|
|
return rc;
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* Look for a socket of the same type that is listening on the
|
|
|
|
* address we want to connect to.
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
|
|
|
for (i = 0; i < NR_FDS; i++) {
|
2013-10-04 16:46:18 +02:00
|
|
|
if (uds_fd_table[minor].type != uds_fd_table[i].type)
|
|
|
|
continue;
|
|
|
|
if (!uds_fd_table[i].listening)
|
|
|
|
continue;
|
|
|
|
if (uds_fd_table[i].addr.sun_family != AF_UNIX)
|
|
|
|
continue;
|
|
|
|
if (strncmp(addr.sun_path, uds_fd_table[i].addr.sun_path,
|
|
|
|
UNIX_PATH_MAX))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Found a matching socket. */
|
|
|
|
break;
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if (i == NR_FDS)
|
|
|
|
return ECONNREFUSED;
|
|
|
|
|
|
|
|
/* If the server is blocked on an accept, perform the connection. */
|
|
|
|
if ((child = uds_fd_table[i].child) != -1) {
|
|
|
|
rc = perform_connection(minor, child, &addr);
|
|
|
|
|
|
|
|
if (rc != OK)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
uds_fd_table[i].child = -1;
|
|
|
|
|
|
|
|
dprintf(("UDS: do_connect(%d): revive %d\n", minor, child));
|
|
|
|
|
|
|
|
/* Wake up the accepting party. */
|
|
|
|
uds_unsuspend(child);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf(("UDS: adding %d to %d's backlog\n", minor, i));
|
|
|
|
|
|
|
|
/* Look for a free slot in the backlog. */
|
|
|
|
rc = -1;
|
|
|
|
for (j = 0; j < uds_fd_table[i].backlog_size; j++) {
|
|
|
|
if (uds_fd_table[i].backlog[j] == -1) {
|
|
|
|
uds_fd_table[i].backlog[j] = minor;
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
break;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if (rc == -1)
|
|
|
|
return ECONNREFUSED; /* backlog is full */
|
|
|
|
|
|
|
|
/* See if the server is blocked on select(). */
|
|
|
|
if (uds_fd_table[i].sel_ops & CDEV_OP_RD) {
|
|
|
|
/* Satisfy a read-type select on the server. */
|
|
|
|
chardriver_reply_select(uds_fd_table[i].sel_endpt, i,
|
|
|
|
CDEV_OP_RD);
|
|
|
|
|
|
|
|
uds_fd_table[i].sel_ops &= ~CDEV_OP_RD;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* We found our server. */
|
|
|
|
uds_fd_table[minor].peer = i;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
memcpy(&uds_fd_table[minor].addr, &addr, sizeof(struct sockaddr_un));
|
|
|
|
|
|
|
|
dprintf(("UDS: do_connect(%d): suspend\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Suspend until the server side accepts the connection. */
|
2010-07-15 15:39:57 +02:00
|
|
|
uds_fd_table[minor].suspended = UDS_SUSPENDED_CONNECT;
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
return EDONTREPLY;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_listen(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int backlog_size;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_listen(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Ensure the socket has a type and is bound. */
|
2010-07-15 15:39:57 +02:00
|
|
|
if (uds_fd_table[minor].type == -1 ||
|
2013-10-04 16:46:18 +02:00
|
|
|
uds_fd_table[minor].addr.sun_family != AF_UNIX)
|
2010-07-15 15:39:57 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* listen(2) supports only two socket types. */
|
2010-07-15 15:39:57 +02:00
|
|
|
if (uds_fd_table[minor].type != SOCK_STREAM &&
|
2013-10-04 16:46:18 +02:00
|
|
|
uds_fd_table[minor].type != SOCK_SEQPACKET)
|
2010-07-15 15:39:57 +02:00
|
|
|
return EOPNOTSUPP;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* The POSIX standard doesn't say what to do if listen() has
|
|
|
|
* already been called. Well, there isn't an errno. We silently
|
2012-02-13 16:28:04 +01:00
|
|
|
* let it happen, but if listen() has already been called, we
|
2013-10-04 16:46:18 +02:00
|
|
|
* don't allow the backlog to shrink.
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
2013-10-04 16:46:18 +02:00
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &backlog_size,
|
|
|
|
sizeof(backlog_size))) != OK)
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
if (uds_fd_table[minor].listening == 0) {
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Set the backlog size to a reasonable value. */
|
|
|
|
if (backlog_size <= 0 || backlog_size > UDS_SOMAXCONN)
|
|
|
|
backlog_size = UDS_SOMAXCONN;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
uds_fd_table[minor].backlog_size = backlog_size;
|
2010-07-15 15:39:57 +02:00
|
|
|
} else {
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Allow the user to expand the backlog size. */
|
2010-07-15 15:39:57 +02:00
|
|
|
if (backlog_size > uds_fd_table[minor].backlog_size &&
|
2013-10-04 16:46:18 +02:00
|
|
|
backlog_size < UDS_SOMAXCONN)
|
2010-07-15 15:39:57 +02:00
|
|
|
uds_fd_table[minor].backlog_size = backlog_size;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* Don't let the user shrink the backlog_size, as we might
|
|
|
|
* have clients waiting in those slots.
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* This socket is now listening. */
|
2010-07-15 15:39:57 +02:00
|
|
|
uds_fd_table[minor].listening = 1;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_socket(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 16:46:18 +02:00
|
|
|
int rc, type;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_socket(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* The socket type can only be set once. */
|
|
|
|
if (uds_fd_table[minor].type != -1)
|
2010-07-15 15:39:57 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Get the requested type. */
|
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &type,
|
|
|
|
sizeof(type))) != OK)
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Assign the type if it is valid only. */
|
|
|
|
switch (type) {
|
|
|
|
case SOCK_STREAM:
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
case SOCK_SEQPACKET:
|
|
|
|
uds_fd_table[minor].type = type;
|
|
|
|
return OK;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
default:
|
|
|
|
return EINVAL;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_bind(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int rc, i;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_bind(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* If the type hasn't been set by do_socket() yet, OR an attempt
|
|
|
|
* to re-bind() a non-SOCK_DGRAM socket is made, fail the call.
|
|
|
|
*/
|
2010-07-15 15:39:57 +02:00
|
|
|
if ((uds_fd_table[minor].type == -1) ||
|
2013-10-04 16:46:18 +02:00
|
|
|
(uds_fd_table[minor].addr.sun_family == AF_UNIX &&
|
|
|
|
uds_fd_table[minor].type != SOCK_DGRAM))
|
2010-07-15 15:39:57 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &addr,
|
|
|
|
sizeof(struct sockaddr_un))) != OK)
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Do some basic sanity checks on the address. */
|
|
|
|
if (addr.sun_family != AF_UNIX)
|
2010-08-30 15:46:44 +02:00
|
|
|
return EAFNOSUPPORT;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if (addr.sun_path[0] == '\0')
|
2010-08-30 15:46:44 +02:00
|
|
|
return ENOENT;
|
|
|
|
|
2013-11-03 22:33:44 +01:00
|
|
|
if ((rc = checkperms(uds_fd_table[minor].owner, addr.sun_path,
|
|
|
|
UNIX_PATH_MAX)) != OK)
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Make sure the address isn't already in use by another socket. */
|
2010-07-15 15:39:57 +02:00
|
|
|
for (i = 0; i < NR_FDS; i++) {
|
2013-10-04 16:46:18 +02:00
|
|
|
if (uds_fd_table[i].addr.sun_family == AF_UNIX &&
|
|
|
|
!strncmp(addr.sun_path, uds_fd_table[i].addr.sun_path,
|
|
|
|
UNIX_PATH_MAX)) {
|
|
|
|
/* Another socket is bound to this sun_path. */
|
2010-07-15 15:39:57 +02:00
|
|
|
return EADDRINUSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Looks good, perform the bind(). */
|
|
|
|
memcpy(&uds_fd_table[minor].addr, &addr, sizeof(struct sockaddr_un));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_getsockname(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_getsockname(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* Unconditionally send the address we have assigned to this socket.
|
|
|
|
* The POSIX standard doesn't say what to do if the address hasn't been
|
|
|
|
* set. If the address isn't currently set, then the user will get
|
|
|
|
* NULL bytes. Note: libc depends on this behavior.
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
2013-10-04 16:46:18 +02:00
|
|
|
return sys_safecopyto(endpt, grant, 0,
|
|
|
|
(vir_bytes) &uds_fd_table[minor].addr, sizeof(struct sockaddr_un));
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_getpeername(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 17:57:42 +02:00
|
|
|
int peer_minor;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_getpeername(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Check that the socket is connected with a valid peer. */
|
2010-07-15 15:39:57 +02:00
|
|
|
if (uds_fd_table[minor].peer != -1) {
|
|
|
|
peer_minor = uds_fd_table[minor].peer;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Copy the address from the peer. */
|
|
|
|
return sys_safecopyto(endpt, grant, 0,
|
|
|
|
(vir_bytes) &uds_fd_table[peer_minor].addr,
|
|
|
|
sizeof(struct sockaddr_un));
|
|
|
|
} else if (uds_fd_table[minor].err == ECONNRESET) {
|
|
|
|
uds_fd_table[minor].err = 0;
|
2011-03-25 11:52:53 +01:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
return ECONNRESET;
|
|
|
|
} else
|
|
|
|
return ENOTCONN;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_shutdown(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
|
|
|
int rc, how;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_shutdown(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* The socket must be connection oriented. */
|
2010-07-15 15:39:57 +02:00
|
|
|
if (uds_fd_table[minor].type != SOCK_STREAM &&
|
2013-10-04 16:46:18 +02:00
|
|
|
uds_fd_table[minor].type != SOCK_SEQPACKET)
|
2010-07-15 15:39:57 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (uds_fd_table[minor].peer == -1) {
|
2013-10-04 16:46:18 +02:00
|
|
|
/* shutdown(2) is only valid for connected sockets. */
|
|
|
|
if (uds_fd_table[minor].err == ECONNRESET)
|
2011-03-25 11:52:53 +01:00
|
|
|
return ECONNRESET;
|
2013-10-04 16:46:18 +02:00
|
|
|
else
|
2011-03-25 11:52:53 +01:00
|
|
|
return ENOTCONN;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Get the 'how' parameter from the caller. */
|
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &how,
|
|
|
|
sizeof(how))) != OK)
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
switch (how) {
|
2013-10-04 16:46:18 +02:00
|
|
|
case SHUT_RD: /* Take away read permission. */
|
|
|
|
uds_fd_table[minor].mode &= ~UDS_R;
|
|
|
|
break;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
case SHUT_WR: /* Take away write permission. */
|
|
|
|
uds_fd_table[minor].mode &= ~UDS_W;
|
|
|
|
break;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
case SHUT_RDWR: /* Shut down completely. */
|
|
|
|
uds_fd_table[minor].mode = 0;
|
|
|
|
break;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
default:
|
|
|
|
return EINVAL;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_socketpair(devminor_t minorx, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
dev_t minorin;
|
2013-09-03 02:00:20 +02:00
|
|
|
devminor_t minory;
|
2010-07-15 15:39:57 +02:00
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_socketpair(%d)\n", minorx));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* The ioctl argument is the minor number of the second socket. */
|
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &minorin,
|
|
|
|
sizeof(minorin))) != OK)
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2011-08-31 15:38:28 +02:00
|
|
|
minory = minor(minorin);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: socketpair(%d, %d,)\n", minorx, minory));
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Security check: both sockets must have the same owner endpoint. */
|
|
|
|
if (uds_fd_table[minorx].owner != uds_fd_table[minory].owner)
|
2010-07-15 15:39:57 +02:00
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
addr.sun_path[0] = 'X';
|
|
|
|
addr.sun_path[1] = '\0';
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
return perform_connection(minorx, minory, &addr);
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_getsockopt_sotype(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_getsockopt_sotype(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* If the type hasn't been set yet, we fail the call. */
|
|
|
|
if (uds_fd_table[minor].type == -1)
|
2010-07-15 15:39:57 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
return sys_safecopyto(endpt, grant, 0,
|
|
|
|
(vir_bytes) &uds_fd_table[minor].type, sizeof(int));
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_getsockopt_peercred(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
|
|
|
int peer_minor;
|
|
|
|
int rc;
|
2013-04-18 11:07:44 +02:00
|
|
|
struct uucred cred;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_getsockopt_peercred(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
if (uds_fd_table[minor].peer == -1) {
|
|
|
|
if (uds_fd_table[minor].err == ECONNRESET) {
|
|
|
|
uds_fd_table[minor].err = 0;
|
2011-03-25 11:52:53 +01:00
|
|
|
|
|
|
|
return ECONNRESET;
|
2013-10-04 16:46:18 +02:00
|
|
|
} else
|
2011-03-25 11:52:53 +01:00
|
|
|
return ENOTCONN;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
peer_minor = uds_fd_table[minor].peer;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Obtain the peer's credentials and copy them out. */
|
|
|
|
if ((rc = getnucred(uds_fd_table[peer_minor].owner, &cred)) < 0)
|
2013-11-03 22:33:44 +01:00
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
return sys_safecopyto(endpt, grant, 0, (vir_bytes) &cred,
|
|
|
|
sizeof(struct uucred));
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_getsockopt_sndbuf(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 16:46:18 +02:00
|
|
|
size_t sndbuf = UDS_BUF;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_getsockopt_sndbuf(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
return sys_safecopyto(endpt, grant, 0, (vir_bytes) &sndbuf,
|
|
|
|
sizeof(sndbuf));
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_setsockopt_sndbuf(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
size_t sndbuf;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_setsockopt_sndbuf(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &sndbuf,
|
|
|
|
sizeof(sndbuf))) != OK)
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* The send buffer is limited to 32KB at the moment. */
|
|
|
|
if (sndbuf > UDS_BUF)
|
2010-07-15 15:39:57 +02:00
|
|
|
return ENOSYS;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* FIXME: actually shrink the buffer. */
|
2010-07-15 15:39:57 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_getsockopt_rcvbuf(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 16:46:18 +02:00
|
|
|
size_t rcvbuf = UDS_BUF;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_getsockopt_rcvbuf(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
return sys_safecopyto(endpt, grant, 0, (vir_bytes) &rcvbuf,
|
|
|
|
sizeof(rcvbuf));
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_setsockopt_rcvbuf(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
size_t rcvbuf;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_setsockopt_rcvbuf(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &rcvbuf,
|
|
|
|
sizeof(rcvbuf))) != OK)
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* The receive buffer is limited to 32KB at the moment. */
|
|
|
|
if (rcvbuf > UDS_BUF)
|
2010-07-15 15:39:57 +02:00
|
|
|
return ENOSYS;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* FIXME: actually shrink the buffer. */
|
2010-07-15 15:39:57 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_sendto(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_sendto(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* This IOCTL is only for SOCK_DGRAM sockets. */
|
|
|
|
if (uds_fd_table[minor].type != SOCK_DGRAM)
|
2010-07-15 15:39:57 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &addr,
|
|
|
|
sizeof(struct sockaddr_un))) != OK)
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Do some basic sanity checks on the address. */
|
|
|
|
if (addr.sun_family != AF_UNIX || addr.sun_path[0] == '\0')
|
2010-07-15 15:39:57 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
2013-11-03 22:33:44 +01:00
|
|
|
if ((rc = checkperms(uds_fd_table[minor].owner, addr.sun_path,
|
|
|
|
UNIX_PATH_MAX)) != OK)
|
|
|
|
return rc;
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
memcpy(&uds_fd_table[minor].target, &addr, sizeof(struct sockaddr_un));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_recvfrom(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_recvfrom(%d)\n", minor));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
return sys_safecopyto(endpt, grant, 0,
|
|
|
|
(vir_bytes) &uds_fd_table[minor].source,
|
|
|
|
sizeof(struct sockaddr_un));
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
2013-10-05 16:31:35 +02:00
|
|
|
send_fds(devminor_t minor, struct msg_control *msg_ctrl,
|
|
|
|
struct ancillary *data)
|
2010-08-30 15:46:44 +02:00
|
|
|
{
|
2013-10-04 16:46:18 +02:00
|
|
|
int i, rc, nfds, totalfds;
|
2013-10-05 16:31:35 +02:00
|
|
|
endpoint_t from_ep;
|
2010-08-30 15:46:44 +02:00
|
|
|
struct msghdr msghdr;
|
|
|
|
struct cmsghdr *cmsg = NULL;
|
|
|
|
|
2013-10-05 16:31:35 +02:00
|
|
|
dprintf(("UDS: send_fds(%d)\n", minor));
|
|
|
|
|
|
|
|
from_ep = uds_fd_table[minor].owner;
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Obtain this socket's credentials. */
|
2013-10-05 16:31:35 +02:00
|
|
|
if ((rc = getnucred(from_ep, &data->cred)) < 0)
|
2013-11-03 22:33:44 +01:00
|
|
|
return rc;
|
2013-10-04 16:46:18 +02:00
|
|
|
|
|
|
|
dprintf(("UDS: minor=%d cred={%d,%d,%d}\n", minor, data->cred.pid,
|
|
|
|
data->cred.uid, data->cred.gid));
|
|
|
|
|
|
|
|
totalfds = data->nfiledes;
|
2010-08-30 15:46:44 +02:00
|
|
|
|
|
|
|
memset(&msghdr, '\0', sizeof(struct msghdr));
|
|
|
|
msghdr.msg_control = msg_ctrl->msg_control;
|
|
|
|
msghdr.msg_controllen = msg_ctrl->msg_controllen;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
|
|
|
|
cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
|
|
|
|
if (cmsg->cmsg_level != SOL_SOCKET ||
|
|
|
|
cmsg->cmsg_type != SCM_RIGHTS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nfds = MIN((cmsg->cmsg_len-CMSG_LEN(0))/sizeof(int), OPEN_MAX);
|
|
|
|
|
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
if (totalfds == OPEN_MAX)
|
|
|
|
return EOVERFLOW;
|
|
|
|
|
|
|
|
data->fds[totalfds] = ((int *) CMSG_DATA(cmsg))[i];
|
|
|
|
dprintf(("UDS: minor=%d fd[%d]=%d\n", minor, totalfds,
|
|
|
|
data->fds[totalfds]));
|
|
|
|
totalfds++;
|
2010-08-30 15:46:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-05 16:31:35 +02:00
|
|
|
for (i = data->nfiledes; i < totalfds; i++) {
|
|
|
|
if ((rc = copyfd(from_ep, data->fds[i], COPYFD_FROM)) < 0) {
|
|
|
|
printf("UDS: copyfd(COPYFD_FROM) failed: %d\n", rc);
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2013-10-05 16:31:35 +02:00
|
|
|
/* Revert the successful copyfd() calls made so far. */
|
|
|
|
for (i--; i >= data->nfiledes; i--)
|
|
|
|
close(data->fds[i]);
|
2010-08-30 15:46:44 +02:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2013-10-05 16:31:35 +02:00
|
|
|
|
|
|
|
dprintf(("UDS: send_fds(): %d -> %d\n", data->fds[i], rc));
|
|
|
|
|
|
|
|
data->fds[i] = rc; /* this is now the local FD */
|
2010-08-30 15:46:44 +02:00
|
|
|
}
|
|
|
|
|
2013-10-05 16:31:35 +02:00
|
|
|
data->nfiledes = totalfds;
|
2013-10-04 21:10:01 +02:00
|
|
|
|
2010-08-30 15:46:44 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
/*
|
2013-10-05 16:31:35 +02:00
|
|
|
* This function calls close() for all of the FDs in flight. This is used
|
2013-10-04 16:46:18 +02:00
|
|
|
* when a Unix Domain Socket is closed and there exists references to file
|
|
|
|
* descriptors that haven't been received with recvmsg().
|
2010-08-30 15:46:44 +02:00
|
|
|
*/
|
2013-10-04 16:29:40 +02:00
|
|
|
int
|
|
|
|
uds_clear_fds(devminor_t minor, struct ancillary *data)
|
|
|
|
{
|
2010-08-30 15:46:44 +02:00
|
|
|
int i;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: uds_clear_fds(%d)\n", minor));
|
2010-08-30 15:46:44 +02:00
|
|
|
|
|
|
|
for (i = 0; i < data->nfiledes; i++) {
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: uds_clear_fds() => %d\n", data->fds[i]));
|
|
|
|
|
2013-10-05 16:31:35 +02:00
|
|
|
close(data->fds[i]);
|
2013-10-04 16:46:18 +02:00
|
|
|
|
2010-08-30 15:46:44 +02:00
|
|
|
data->fds[i] = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->nfiledes = 0;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
recv_fds(devminor_t minor, struct ancillary *data,
|
|
|
|
struct msg_control *msg_ctrl)
|
2010-08-30 15:46:44 +02:00
|
|
|
{
|
2013-10-05 16:31:35 +02:00
|
|
|
int rc, i, j, fds[OPEN_MAX];
|
2010-08-30 15:46:44 +02:00
|
|
|
struct msghdr msghdr;
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
endpoint_t to_ep;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: recv_fds(%d)\n", minor));
|
2010-08-30 15:46:44 +02:00
|
|
|
|
|
|
|
msghdr.msg_control = msg_ctrl->msg_control;
|
|
|
|
msghdr.msg_controllen = msg_ctrl->msg_controllen;
|
|
|
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msghdr);
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(int) * data->nfiledes);
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
|
|
|
|
to_ep = uds_fd_table[minor].owner;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Copy to the target endpoint. */
|
2010-08-30 15:46:44 +02:00
|
|
|
for (i = 0; i < data->nfiledes; i++) {
|
2013-10-05 16:31:35 +02:00
|
|
|
if ((rc = copyfd(to_ep, data->fds[i], COPYFD_TO)) < 0) {
|
|
|
|
printf("UDS: copyfd(COPYFD_TO) failed: %d\n", rc);
|
2013-10-04 16:46:18 +02:00
|
|
|
|
2013-10-05 16:31:35 +02:00
|
|
|
/* Revert the successful copyfd() calls made so far. */
|
|
|
|
for (i--; i >= 0; i--)
|
|
|
|
(void) copyfd(to_ep, fds[i], COPYFD_CLOSE);
|
2013-10-04 16:46:18 +02:00
|
|
|
|
2010-08-30 15:46:44 +02:00
|
|
|
return rc;
|
|
|
|
}
|
2013-10-05 16:31:35 +02:00
|
|
|
|
|
|
|
fds[i] = rc; /* this is now the remote FD */
|
2010-08-30 15:46:44 +02:00
|
|
|
}
|
|
|
|
|
2013-10-05 16:31:35 +02:00
|
|
|
/* Close the local copies only once the entire procedure succeeded. */
|
2010-08-30 15:46:44 +02:00
|
|
|
for (i = 0; i < data->nfiledes; i++) {
|
2013-10-05 16:31:35 +02:00
|
|
|
dprintf(("UDS: recv_fds(): %d -> %d\n", data->fds[i], fds[i]));
|
2013-10-04 16:46:18 +02:00
|
|
|
|
2013-10-05 16:31:35 +02:00
|
|
|
((int *)CMSG_DATA(cmsg))[i] = fds[i];
|
|
|
|
|
|
|
|
close(data->fds[i]);
|
2013-10-04 16:46:18 +02:00
|
|
|
|
2010-08-30 15:46:44 +02:00
|
|
|
data->fds[i] = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->nfiledes = 0;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
recv_cred(devminor_t minor, struct ancillary *data,
|
|
|
|
struct msg_control *msg_ctrl)
|
2010-08-30 15:46:44 +02:00
|
|
|
{
|
|
|
|
struct msghdr msghdr;
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: recv_cred(%d)\n", minor));
|
2010-08-30 15:46:44 +02:00
|
|
|
|
|
|
|
msghdr.msg_control = msg_ctrl->msg_control;
|
|
|
|
msghdr.msg_controllen = msg_ctrl->msg_controllen;
|
|
|
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msghdr);
|
2013-10-04 16:46:18 +02:00
|
|
|
if (cmsg->cmsg_len > 0)
|
2010-08-30 15:46:44 +02:00
|
|
|
cmsg = CMSG_NXTHDR(&msghdr, cmsg);
|
|
|
|
|
2013-04-18 11:07:44 +02:00
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(struct uucred));
|
2010-08-30 15:46:44 +02:00
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_CREDENTIALS;
|
2013-10-04 16:46:18 +02:00
|
|
|
memcpy(CMSG_DATA(cmsg), &data->cred, sizeof(struct uucred));
|
2010-08-30 15:46:44 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_sendmsg(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-08-30 15:46:44 +02:00
|
|
|
{
|
2013-10-05 16:31:35 +02:00
|
|
|
int peer, rc, i;
|
2010-08-30 15:46:44 +02:00
|
|
|
struct msg_control msg_ctrl;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: do_sendmsg(%d)\n", minor));
|
2010-08-30 15:46:44 +02:00
|
|
|
|
|
|
|
memset(&msg_ctrl, '\0', sizeof(struct msg_control));
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &msg_ctrl,
|
|
|
|
sizeof(struct msg_control))) != OK)
|
|
|
|
return rc;
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Locate the peer. */
|
2010-08-30 15:46:44 +02:00
|
|
|
peer = -1;
|
|
|
|
if (uds_fd_table[minor].type == SOCK_DGRAM) {
|
|
|
|
if (uds_fd_table[minor].target.sun_path[0] == '\0' ||
|
2013-10-04 16:46:18 +02:00
|
|
|
uds_fd_table[minor].target.sun_family != AF_UNIX)
|
2010-08-30 15:46:44 +02:00
|
|
|
return EDESTADDRREQ;
|
|
|
|
|
|
|
|
for (i = 0; i < NR_FDS; i++) {
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* Look for a SOCK_DGRAM socket that is bound on the
|
|
|
|
* target address.
|
2010-08-30 15:46:44 +02:00
|
|
|
*/
|
|
|
|
if (uds_fd_table[i].type == SOCK_DGRAM &&
|
2013-10-04 16:46:18 +02:00
|
|
|
uds_fd_table[i].addr.sun_family == AF_UNIX &&
|
|
|
|
!strncmp(uds_fd_table[minor].target.sun_path,
|
|
|
|
uds_fd_table[i].addr.sun_path, UNIX_PATH_MAX)) {
|
2010-08-30 15:46:44 +02:00
|
|
|
peer = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if (peer == -1)
|
2010-08-30 15:46:44 +02:00
|
|
|
return ENOENT;
|
|
|
|
} else {
|
|
|
|
peer = uds_fd_table[minor].peer;
|
2013-10-04 16:46:18 +02:00
|
|
|
if (peer == -1)
|
2010-08-30 15:46:44 +02:00
|
|
|
return ENOTCONN;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
dprintf(("UDS: sendmsg(%d) -- peer=%d\n", minor, peer));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: it's possible that there is already some file descriptors in
|
|
|
|
* ancillary_data if the peer didn't call recvmsg() yet. That's okay.
|
|
|
|
* The receiver will get the current file descriptors plus the new
|
|
|
|
* ones.
|
2010-08-30 15:46:44 +02:00
|
|
|
*/
|
2013-10-05 16:31:35 +02:00
|
|
|
return send_fds(minor, &msg_ctrl, &uds_fd_table[peer].ancillary_data);
|
2010-08-30 15:46:44 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
do_recvmsg(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
2010-08-30 15:46:44 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct msg_control msg_ctrl;
|
2013-10-04 16:46:18 +02:00
|
|
|
socklen_t clen_avail = 0;
|
|
|
|
socklen_t clen_needed = 0;
|
|
|
|
socklen_t clen_desired = 0;
|
|
|
|
|
|
|
|
dprintf(("UDS: do_recvmsg(%d)\n", minor));
|
|
|
|
dprintf(("UDS: minor=%d credentials={pid:%d,uid:%d,gid:%d}\n", minor,
|
|
|
|
uds_fd_table[minor].ancillary_data.cred.pid,
|
|
|
|
uds_fd_table[minor].ancillary_data.cred.uid,
|
|
|
|
uds_fd_table[minor].ancillary_data.cred.gid));
|
2010-08-30 15:46:44 +02:00
|
|
|
|
|
|
|
memset(&msg_ctrl, '\0', sizeof(struct msg_control));
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* Get the msg_control from the user. It will include the
|
2010-08-30 15:46:44 +02:00
|
|
|
* amount of space the user has allocated for control data.
|
|
|
|
*/
|
2013-10-04 16:46:18 +02:00
|
|
|
if ((rc = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &msg_ctrl,
|
|
|
|
sizeof(struct msg_control))) != OK)
|
|
|
|
return rc;
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
clen_avail = MIN(msg_ctrl.msg_controllen, MSG_CONTROL_MAX);
|
2010-08-30 15:46:44 +02:00
|
|
|
|
|
|
|
if (uds_fd_table[minor].ancillary_data.nfiledes > 0) {
|
2013-10-04 21:10:01 +02:00
|
|
|
clen_needed = CMSG_SPACE(sizeof(int) *
|
2013-10-04 16:46:18 +02:00
|
|
|
uds_fd_table[minor].ancillary_data.nfiledes);
|
2010-08-30 15:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if there is room we also include credentials */
|
2013-10-04 21:10:01 +02:00
|
|
|
clen_desired = clen_needed + CMSG_SPACE(sizeof(struct uucred));
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if (clen_needed > clen_avail)
|
2010-08-30 15:46:44 +02:00
|
|
|
return EOVERFLOW;
|
|
|
|
|
2013-10-04 21:10:01 +02:00
|
|
|
if (uds_fd_table[minor].ancillary_data.nfiledes > 0) {
|
|
|
|
if ((rc = recv_fds(minor, &uds_fd_table[minor].ancillary_data,
|
|
|
|
&msg_ctrl)) != OK)
|
|
|
|
return rc;
|
|
|
|
}
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
if (clen_desired <= clen_avail) {
|
2010-08-30 15:46:44 +02:00
|
|
|
rc = recv_cred(minor, &uds_fd_table[minor].ancillary_data,
|
2013-10-04 16:46:18 +02:00
|
|
|
&msg_ctrl);
|
|
|
|
if (rc != OK)
|
2010-08-30 15:46:44 +02:00
|
|
|
return rc;
|
2013-10-04 21:10:01 +02:00
|
|
|
msg_ctrl.msg_controllen = clen_desired;
|
|
|
|
} else
|
|
|
|
msg_ctrl.msg_controllen = clen_needed;
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Send the control data to the user. */
|
|
|
|
return sys_safecopyto(endpt, grant, 0, (vir_bytes) &msg_ctrl,
|
|
|
|
sizeof(struct msg_control));
|
2010-08-30 15:46:44 +02:00
|
|
|
}
|
2013-09-03 02:00:20 +02:00
|
|
|
|
2013-10-04 18:41:21 +02:00
|
|
|
static int
|
|
|
|
do_fionread(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = uds_perform_read(minor, NONE, GRANT_INVALID, UDS_BUF, 1);
|
|
|
|
|
|
|
|
/* What should we do on error? Just set to zero for now. */
|
|
|
|
if (rc < 0)
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
return sys_safecopyto(endpt, grant, 0, (vir_bytes) &rc, sizeof(rc));
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
int
|
|
|
|
uds_do_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
|
2013-09-03 02:00:20 +02:00
|
|
|
cp_grant_id_t grant)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2013-10-04 16:46:18 +02:00
|
|
|
switch (request) {
|
2013-09-03 02:00:20 +02:00
|
|
|
case NWIOSUDSCONN:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Connect to a listening socket -- connect(). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_connect(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOSUDSACCEPT:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Accept an incoming connection -- accept(). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_accept(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOSUDSBLOG:
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* Set the backlog_size and put the socket into the listening
|
|
|
|
* state -- listen().
|
2013-09-03 02:00:20 +02:00
|
|
|
*/
|
|
|
|
rc = do_listen(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOSUDSTYPE:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Set the SOCK_ type for this socket -- socket(). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_socket(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOSUDSADDR:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Set the address for this socket -- bind(). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_bind(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOGUDSADDR:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Get the address for this socket -- getsockname(). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_getsockname(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOGUDSPADDR:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Get the address for the peer -- getpeername(). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_getpeername(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOSUDSSHUT:
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* Shut down a socket for reading, writing, or both --
|
|
|
|
* shutdown().
|
2013-09-03 02:00:20 +02:00
|
|
|
*/
|
|
|
|
rc = do_shutdown(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOSUDSPAIR:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Connect two sockets -- socketpair(). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_socketpair(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOGUDSSOTYPE:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Get socket type -- getsockopt(SO_TYPE). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_getsockopt_sotype(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOGUDSPEERCRED:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Get peer endpoint -- getsockopt(SO_PEERCRED). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_getsockopt_peercred(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOSUDSTADDR:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Set target address -- sendto(). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_sendto(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOGUDSFADDR:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Get from address -- recvfrom(). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_recvfrom(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOGUDSSNDBUF:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Get the send buffer size -- getsockopt(SO_SNDBUF). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_getsockopt_sndbuf(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOSUDSSNDBUF:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Set the send buffer size -- setsockopt(SO_SNDBUF). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_setsockopt_sndbuf(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOGUDSRCVBUF:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Get the send buffer size -- getsockopt(SO_SNDBUF). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_getsockopt_rcvbuf(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOSUDSRCVBUF:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Set the send buffer size -- setsockopt(SO_SNDBUF). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_setsockopt_rcvbuf(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOSUDSCTRL:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Set the control data -- sendmsg(). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_sendmsg(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NWIOGUDSCTRL:
|
2013-10-04 16:46:18 +02:00
|
|
|
/* Set the control data -- recvmsg(). */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = do_recvmsg(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-10-04 18:41:21 +02:00
|
|
|
case FIONREAD:
|
|
|
|
/*
|
|
|
|
* Get the number of bytes immediately available for reading.
|
|
|
|
*/
|
|
|
|
rc = do_fionread(minor, endpt, grant);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
default:
|
2013-10-04 16:46:18 +02:00
|
|
|
/*
|
|
|
|
* The IOCTL command is not valid for /dev/uds -- this happens
|
|
|
|
* a lot and is normal. A lot of libc functions determine the
|
|
|
|
* socket type with IOCTLs. Any unrecognized requests simply
|
|
|
|
* get an ENOTTY response.
|
2013-09-03 02:00:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
rc = ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|