2010-07-15 15:39:57 +02:00
|
|
|
/*
|
|
|
|
* Unix Domain Sockets Implementation (PF_UNIX, PF_LOCAL)
|
|
|
|
* This code handles requests generated by operations on /dev/uds
|
|
|
|
*
|
|
|
|
* The entry points into this file are...
|
|
|
|
*
|
2013-10-04 16:29:40 +02:00
|
|
|
* uds_unsuspend: resume a previously suspended socket call
|
|
|
|
* main: driver main loop
|
2010-07-15 15:39:57 +02:00
|
|
|
*
|
2013-09-10 20:25:01 +02:00
|
|
|
* The interface to unix domain sockets is similar to the interface to network
|
|
|
|
* sockets. There is a character device (/dev/uds) and this server is a
|
|
|
|
* 'driver' for that device.
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uds.h"
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static ssize_t uds_perform_read(devminor_t, endpoint_t, cp_grant_id_t, size_t,
|
|
|
|
int);
|
|
|
|
static ssize_t uds_perform_write(devminor_t, endpoint_t, cp_grant_id_t, size_t,
|
|
|
|
int);
|
|
|
|
|
|
|
|
static int uds_open(devminor_t, int, endpoint_t);
|
|
|
|
static int uds_close(devminor_t);
|
|
|
|
static ssize_t uds_read(devminor_t, u64_t, endpoint_t, cp_grant_id_t, size_t,
|
|
|
|
int, cdev_id_t);
|
|
|
|
static ssize_t uds_write(devminor_t, u64_t, endpoint_t, cp_grant_id_t, size_t,
|
|
|
|
int, cdev_id_t);
|
|
|
|
static int uds_ioctl(devminor_t, unsigned long, endpoint_t, cp_grant_id_t, int,
|
|
|
|
endpoint_t, cdev_id_t);
|
|
|
|
static int uds_cancel(devminor_t, endpoint_t, cdev_id_t);
|
|
|
|
static int uds_select(devminor_t, unsigned int, endpoint_t);
|
2013-09-03 02:00:20 +02:00
|
|
|
|
|
|
|
static struct chardriver uds_tab = {
|
|
|
|
.cdr_open = uds_open,
|
|
|
|
.cdr_close = uds_close,
|
|
|
|
.cdr_read = uds_read,
|
|
|
|
.cdr_write = uds_write,
|
|
|
|
.cdr_ioctl = uds_ioctl,
|
|
|
|
.cdr_cancel = uds_cancel,
|
|
|
|
.cdr_select = uds_select
|
|
|
|
};
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
/* File Descriptor Table */
|
|
|
|
uds_fd_t uds_fd_table[NR_FDS];
|
|
|
|
|
|
|
|
static unsigned int uds_exit_left;
|
2010-07-21 15:39:46 +02:00
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
uds_open(devminor_t UNUSED(orig_minor), int access,
|
2013-09-03 02:00:20 +02:00
|
|
|
endpoint_t user_endpt)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-09-03 02:00:20 +02:00
|
|
|
devminor_t minor;
|
2013-10-04 16:29:40 +02:00
|
|
|
int i;
|
|
|
|
char *buf;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
#if DEBUG == 1
|
|
|
|
static int call_count = 0;
|
2013-09-03 02:00:20 +02:00
|
|
|
printf("(uds) [-] uds_open() call_count=%d\n", ++call_count);
|
|
|
|
printf("Endpoint: 0x%x\n", user_endpt);
|
2010-07-15 15:39:57 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a slot in the descriptor table for the new descriptor.
|
|
|
|
* The index of the descriptor in the table will be returned.
|
|
|
|
* Subsequent calls to read/write/close/ioctl/etc will use this
|
2010-08-30 15:46:44 +02:00
|
|
|
* minor number. The minor number must be different from the
|
|
|
|
* the /dev/uds device's minor number (currently 0).
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
|
|
|
minor = -1; /* to trap error */
|
|
|
|
|
2010-08-30 15:46:44 +02:00
|
|
|
for (i = 1; i < NR_FDS; i++) {
|
2010-07-15 15:39:57 +02:00
|
|
|
if (uds_fd_table[i].state == UDS_FREE) {
|
|
|
|
minor = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
if (minor == -1)
|
2010-07-15 15:39:57 +02:00
|
|
|
return ENFILE;
|
|
|
|
|
|
|
|
/*
|
2013-10-04 16:29:40 +02:00
|
|
|
* Allocate memory for the ringer buffer. In order to save on memory
|
|
|
|
* in the common case, the buffer is allocated only when the socket is
|
|
|
|
* in use. We use mmap instead of malloc to allow the memory to be
|
|
|
|
* actually freed later.
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
2013-10-04 16:29:40 +02:00
|
|
|
if ((buf = minix_mmap(NULL, PIPE_BUF, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_ANON | MAP_PRIVATE, -1, 0)) == MAP_FAILED)
|
|
|
|
return ENOMEM;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
/* mark this one as 'in use' so that it doesn't get assigned to
|
|
|
|
* another socket
|
|
|
|
*/
|
|
|
|
uds_fd_table[minor].state = UDS_INUSE;
|
|
|
|
|
|
|
|
/* set the socket owner */
|
2013-09-03 02:00:20 +02:00
|
|
|
uds_fd_table[minor].owner = user_endpt;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
/* setup select(2) framework */
|
2013-09-03 02:00:20 +02:00
|
|
|
uds_fd_table[minor].sel_endpt = NONE;
|
|
|
|
uds_fd_table[minor].sel_ops = 0;
|
2013-10-04 16:29:40 +02:00
|
|
|
uds_fd_table[minor].buf = buf;
|
2010-07-15 15:39:57 +02:00
|
|
|
uds_fd_table[minor].pos = 0;
|
|
|
|
|
|
|
|
/* the PIPE is initially empty */
|
|
|
|
uds_fd_table[minor].size = 0;
|
|
|
|
|
|
|
|
/* the default for a new socket is to allow reading and writing.
|
|
|
|
* shutdown(2) will remove one or both flags.
|
|
|
|
*/
|
2013-10-04 16:29:40 +02:00
|
|
|
uds_fd_table[minor].mode = R_BIT | W_BIT;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
/* In libc socket(2) sets this to the actual value later with the
|
|
|
|
* NWIOSUDSTYPE ioctl().
|
|
|
|
*/
|
|
|
|
uds_fd_table[minor].type = -1;
|
|
|
|
|
|
|
|
/* Clear the backlog by setting each entry to -1 */
|
|
|
|
for (i = 0; i < UDS_SOMAXCONN; i++) {
|
|
|
|
/* initially no connections are pending */
|
|
|
|
uds_fd_table[minor].backlog[i] = -1;
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:28:04 +01:00
|
|
|
memset(&uds_fd_table[minor].ancillary_data, '\0', sizeof(struct
|
2010-08-30 15:46:44 +02:00
|
|
|
ancillary));
|
|
|
|
for (i = 0; i < OPEN_MAX; i++) {
|
|
|
|
uds_fd_table[minor].ancillary_data.fds[i] = -1;
|
|
|
|
}
|
|
|
|
|
2010-07-15 15:39:57 +02:00
|
|
|
/* default the size to UDS_SOMAXCONN */
|
|
|
|
uds_fd_table[minor].backlog_size = UDS_SOMAXCONN;
|
|
|
|
|
|
|
|
/* the socket isn't listening for incoming connections until
|
|
|
|
* listen(2) is called
|
|
|
|
*/
|
|
|
|
uds_fd_table[minor].listening = 0;
|
|
|
|
|
|
|
|
/* initially the socket is not connected to a peer */
|
|
|
|
uds_fd_table[minor].peer = -1;
|
|
|
|
|
|
|
|
/* there isn't a child waiting to be accept(2)'d */
|
|
|
|
uds_fd_table[minor].child = -1;
|
|
|
|
|
|
|
|
/* initially the socket is not bound or listening on an address */
|
|
|
|
memset(&(uds_fd_table[minor].addr), '\0', sizeof(struct sockaddr_un));
|
2010-08-30 15:46:44 +02:00
|
|
|
memset(&(uds_fd_table[minor].source), '\0', sizeof(struct sockaddr_un));
|
|
|
|
memset(&(uds_fd_table[minor].target), '\0', sizeof(struct sockaddr_un));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
/* Initially the socket isn't suspended. */
|
|
|
|
uds_fd_table[minor].suspended = UDS_NOT_SUSPENDED;
|
|
|
|
|
2013-09-10 20:25:01 +02:00
|
|
|
return CDEV_CLONED | minor;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
uds_close(devminor_t minor)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 16:29:40 +02:00
|
|
|
int peer;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
#if DEBUG == 1
|
|
|
|
static int call_count = 0;
|
2013-09-03 02:00:20 +02:00
|
|
|
printf("(uds) [%d] uds_close() call_count=%d\n", minor, ++call_count);
|
2010-07-15 15:39:57 +02:00
|
|
|
#endif
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
if (minor < 0 || minor >= NR_FDS) return ENXIO;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
if (uds_fd_table[minor].state != UDS_INUSE) {
|
2012-02-13 16:28:04 +01:00
|
|
|
/* attempted to close a socket that hasn't been opened --
|
2010-07-15 15:39:57 +02:00
|
|
|
* something is very wrong :(
|
|
|
|
*/
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the socket is connected, disconnect it */
|
|
|
|
if (uds_fd_table[minor].peer != -1) {
|
2013-10-04 16:29:40 +02:00
|
|
|
peer = uds_fd_table[minor].peer;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
/* set peer of this peer to -1 */
|
2013-03-05 11:10:41 +01:00
|
|
|
uds_fd_table[peer].peer = -1;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
/* error to pass to peer */
|
2013-03-05 11:10:41 +01:00
|
|
|
uds_fd_table[peer].err = ECONNRESET;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
/* if peer was blocked on I/O revive peer */
|
2013-09-03 02:00:20 +02:00
|
|
|
if (uds_fd_table[peer].suspended != UDS_NOT_SUSPENDED)
|
|
|
|
uds_unsuspend(peer);
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2010-08-30 15:46:44 +02:00
|
|
|
if (uds_fd_table[minor].ancillary_data.nfiledes > 0) {
|
2013-09-03 02:00:20 +02:00
|
|
|
uds_clear_fds(minor, &uds_fd_table[minor].ancillary_data);
|
2010-08-30 15:46:44 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
/* Release the memory for the ring buffer. */
|
|
|
|
minix_munmap(uds_fd_table[minor].buf, PIPE_BUF);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
/* Set the socket back to its original UDS_FREE state. */
|
|
|
|
memset(&uds_fd_table[minor], '\0', sizeof(uds_fd_t));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
/* If terminating, and this was the last open socket, exit now. */
|
|
|
|
if (uds_exit_left > 0) {
|
|
|
|
if (--uds_exit_left == 0)
|
|
|
|
chardriver_terminate();
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
uds_select(devminor_t minor, unsigned int ops, endpoint_t endpt)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-09-03 02:00:20 +02:00
|
|
|
unsigned int ready_ops;
|
|
|
|
int i, bytes, watch;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
#if DEBUG == 1
|
|
|
|
static int call_count = 0;
|
2013-09-03 02:00:20 +02:00
|
|
|
printf("(uds) [%d] uds_select() call_count=%d\n", minor, ++call_count);
|
2010-07-15 15:39:57 +02:00
|
|
|
#endif
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
if (minor < 0 || minor >= NR_FDS) return ENXIO;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
if (uds_fd_table[minor].state != UDS_INUSE) {
|
2013-09-03 02:00:20 +02:00
|
|
|
/* attempted to select on a socket that hasn't been opened --
|
2010-07-15 15:39:57 +02:00
|
|
|
* something is very wrong :(
|
|
|
|
*/
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-09-10 20:25:01 +02:00
|
|
|
watch = (ops & CDEV_NOTIFY);
|
|
|
|
ops &= (CDEV_OP_RD | CDEV_OP_WR | CDEV_OP_ERR);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
ready_ops = 0;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
/* check if there is data available to read */
|
2013-09-10 20:25:01 +02:00
|
|
|
if (ops & CDEV_OP_RD) {
|
2013-09-03 02:00:20 +02:00
|
|
|
bytes = uds_perform_read(minor, NONE, GRANT_INVALID, 1, 1);
|
|
|
|
if (bytes > 0) {
|
|
|
|
/* there is data in the pipe for us to read */
|
2013-09-10 20:25:01 +02:00
|
|
|
ready_ops |= CDEV_OP_RD;
|
2013-09-03 02:00:20 +02:00
|
|
|
} else if (uds_fd_table[minor].listening == 1) {
|
|
|
|
/* check for pending connections */
|
|
|
|
for (i = 0; i < uds_fd_table[minor].backlog_size; i++)
|
|
|
|
{
|
|
|
|
if (uds_fd_table[minor].backlog[i] != -1) {
|
2013-09-10 20:25:01 +02:00
|
|
|
ready_ops |= CDEV_OP_RD;
|
2013-09-03 02:00:20 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
2013-09-03 02:00:20 +02:00
|
|
|
} else if (bytes != SUSPEND) {
|
2013-09-10 20:25:01 +02:00
|
|
|
ready_ops |= CDEV_OP_RD;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-30 15:46:44 +02:00
|
|
|
/* check if we can write without blocking */
|
2013-09-10 20:25:01 +02:00
|
|
|
if (ops & CDEV_OP_WR) {
|
2013-10-04 16:29:40 +02:00
|
|
|
bytes = uds_perform_write(minor, NONE, GRANT_INVALID, 1, 1);
|
2013-09-03 02:00:20 +02:00
|
|
|
if (bytes != 0 && bytes != SUSPEND) {
|
|
|
|
/* There is room to write or there is an error
|
|
|
|
* condition.
|
|
|
|
*/
|
2013-09-10 20:25:01 +02:00
|
|
|
ready_ops |= CDEV_OP_WR;
|
2013-09-03 02:00:20 +02:00
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* If not all requested ops were ready, and the caller requests to be
|
|
|
|
* notified about changes, we add the remaining ops to the saved set.
|
|
|
|
*/
|
|
|
|
ops &= ~ready_ops;
|
|
|
|
if (ops && watch) {
|
|
|
|
uds_fd_table[minor].sel_endpt = endpt;
|
|
|
|
uds_fd_table[minor].sel_ops |= ops;
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
return ready_ops;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static ssize_t
|
|
|
|
uds_perform_read(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant,
|
|
|
|
size_t size, int pretend)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 16:29:40 +02:00
|
|
|
size_t pos, subsize;
|
|
|
|
int r, peer;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
#if DEBUG == 1
|
|
|
|
static int call_count = 0;
|
|
|
|
printf("(uds) [%d] uds_perform_read() call_count=%d\n", minor,
|
|
|
|
++call_count);
|
|
|
|
#endif
|
|
|
|
|
2013-03-05 11:10:41 +01:00
|
|
|
peer = uds_fd_table[minor].peer;
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* skip reads and writes of 0 (or less!) bytes */
|
2010-07-15 15:39:57 +02:00
|
|
|
if (size <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if we are allowed to read */
|
2013-10-04 16:29:40 +02:00
|
|
|
if (!(uds_fd_table[minor].mode & R_BIT)) {
|
2010-07-15 15:39:57 +02:00
|
|
|
/* socket is shutdown for reading */
|
2011-03-25 11:52:53 +01:00
|
|
|
return EPIPE;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uds_fd_table[minor].size == 0) {
|
2013-03-05 11:10:41 +01:00
|
|
|
if (peer == -1) {
|
|
|
|
/* We're not connected. That's only a problem when this
|
|
|
|
* socket is connection oriented. */
|
|
|
|
if (uds_fd_table[minor].type == SOCK_STREAM ||
|
|
|
|
uds_fd_table[minor].type == SOCK_SEQPACKET) {
|
|
|
|
if (uds_fd_table[minor].err == ECONNRESET) {
|
2013-10-04 16:29:40 +02:00
|
|
|
if (!pretend)
|
|
|
|
uds_fd_table[minor].err = 0;
|
2013-03-05 11:10:41 +01:00
|
|
|
return ECONNRESET;
|
|
|
|
} else {
|
|
|
|
return ENOTCONN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if process is reading from a closed pipe */
|
2013-10-04 16:29:40 +02:00
|
|
|
if (peer != -1 && !(uds_fd_table[peer].mode & W_BIT) &&
|
2013-03-05 11:10:41 +01:00
|
|
|
uds_fd_table[minor].size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-15 15:39:57 +02:00
|
|
|
if (pretend) {
|
|
|
|
return SUSPEND;
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:28:04 +01:00
|
|
|
/* maybe a process is blocked waiting to write? if
|
2010-07-15 15:39:57 +02:00
|
|
|
* needed revive the writer
|
|
|
|
*/
|
2013-09-03 02:00:20 +02:00
|
|
|
if (peer != -1 &&
|
|
|
|
uds_fd_table[peer].suspended == UDS_SUSPENDED_WRITE)
|
|
|
|
uds_unsuspend(peer);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
#if DEBUG == 1
|
|
|
|
printf("(uds) [%d] suspending read request\n", minor);
|
|
|
|
#endif
|
2012-02-13 16:28:04 +01:00
|
|
|
/* Process is reading from an empty pipe,
|
2010-07-15 15:39:57 +02:00
|
|
|
* suspend it so some bytes can be written
|
|
|
|
*/
|
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
|
|
|
/* How much can we get from the ring buffer? */
|
|
|
|
if (size > uds_fd_table[minor].size)
|
|
|
|
size = uds_fd_table[minor].size;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
if (pretend)
|
|
|
|
return size;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
/* Get the data from the tail of the ring buffer. */
|
|
|
|
pos = uds_fd_table[minor].pos;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
subsize = PIPE_BUF - pos;
|
|
|
|
if (subsize > size)
|
|
|
|
subsize = size;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
if ((r = sys_safecopyto(endpt, grant, 0,
|
|
|
|
(vir_bytes) &uds_fd_table[minor].buf[pos], subsize)) != OK)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (subsize < size) {
|
|
|
|
if ((r = sys_safecopyto(endpt, grant, subsize,
|
|
|
|
(vir_bytes) uds_fd_table[minor].buf,
|
|
|
|
size - subsize)) != OK)
|
|
|
|
return r;
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
/* Advance the buffer tail. */
|
|
|
|
uds_fd_table[minor].pos = (pos + size) % PIPE_BUF;
|
|
|
|
uds_fd_table[minor].size -= size;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2012-02-13 16:28:04 +01:00
|
|
|
/* if we have 0 unread bytes, move the data pointer back to the
|
2010-07-15 15:39:57 +02:00
|
|
|
* start of the buffer
|
|
|
|
*/
|
|
|
|
if (uds_fd_table[minor].size == 0) {
|
|
|
|
uds_fd_table[minor].pos = 0;
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:28:04 +01:00
|
|
|
/* maybe a big write was waiting for us to read some data, if
|
2010-07-15 15:39:57 +02:00
|
|
|
* needed revive the writer
|
|
|
|
*/
|
2013-09-03 02:00:20 +02:00
|
|
|
if (peer != -1 && uds_fd_table[peer].suspended == UDS_SUSPENDED_WRITE)
|
|
|
|
uds_unsuspend(peer);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* see if peer is blocked on select() and a write is possible (from
|
|
|
|
* peer to minor); if the peer wants to know about write being possible
|
|
|
|
* and it doesn't know about it already, then let the peer know.
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
2013-09-10 20:25:01 +02:00
|
|
|
if (peer != -1 && (uds_fd_table[peer].sel_ops & CDEV_OP_WR) &&
|
2013-10-04 16:29:40 +02:00
|
|
|
size > 0) {
|
2013-09-03 02:00:20 +02:00
|
|
|
/* a write on peer is possible now */
|
|
|
|
chardriver_reply_select(uds_fd_table[peer].sel_endpt, peer,
|
2013-09-10 20:25:01 +02:00
|
|
|
CDEV_OP_WR);
|
|
|
|
uds_fd_table[peer].sel_ops &= ~CDEV_OP_WR;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
return size; /* return number of bytes read */
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static ssize_t
|
|
|
|
uds_perform_write(devminor_t minor, endpoint_t endpt, cp_grant_id_t grant,
|
|
|
|
size_t size, int pretend)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-10-04 16:29:40 +02:00
|
|
|
size_t subsize, pos;
|
|
|
|
int i, r, peer;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
#if DEBUG == 1
|
|
|
|
static int call_count = 0;
|
|
|
|
printf("(uds) [%d] uds_perform_write() call_count=%d\n", minor,
|
|
|
|
++call_count);
|
|
|
|
#endif
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
/* Skip writes of zero bytes. */
|
|
|
|
if (size == 0)
|
2010-07-15 15:39:57 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* check if we are allowed to write */
|
2013-10-04 16:29:40 +02:00
|
|
|
if (!(uds_fd_table[minor].mode & W_BIT)) {
|
2010-07-15 15:39:57 +02:00
|
|
|
/* socket is shutdown for writing */
|
2011-03-25 11:52:53 +01:00
|
|
|
return EPIPE;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (size > PIPE_BUF) {
|
|
|
|
/* message is too big to ever write to the PIPE */
|
2011-03-25 11:52:53 +01:00
|
|
|
return EMSGSIZE;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uds_fd_table[minor].type == SOCK_STREAM ||
|
|
|
|
uds_fd_table[minor].type == SOCK_SEQPACKET) {
|
2012-02-13 16:28:04 +01:00
|
|
|
/* if we're writing with a connection oriented socket,
|
2010-07-15 15:39:57 +02:00
|
|
|
* then it needs a peer to write to
|
|
|
|
*/
|
|
|
|
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;
|
2010-07-15 15:39:57 +02:00
|
|
|
} else {
|
2011-03-25 11:52:53 +01:00
|
|
|
return ENOTCONN;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
peer = uds_fd_table[minor].peer;
|
|
|
|
}
|
|
|
|
} else /* uds_fd_table[minor].type == SOCK_DGRAM */ {
|
|
|
|
peer = -1;
|
|
|
|
|
|
|
|
/* locate the "peer" we want to write to */
|
|
|
|
for (i = 0; i < NR_FDS; i++) {
|
|
|
|
|
2012-02-13 16:28:04 +01:00
|
|
|
/* look for a SOCK_DGRAM socket that is bound on
|
2010-07-15 15:39:57 +02:00
|
|
|
* the target address
|
|
|
|
*/
|
|
|
|
if (uds_fd_table[i].type == SOCK_DGRAM &&
|
|
|
|
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)) {
|
|
|
|
|
|
|
|
peer = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-07-18 12:05:50 +02:00
|
|
|
}
|
2010-08-30 15:46:44 +02:00
|
|
|
|
2012-07-18 12:05:50 +02:00
|
|
|
if (peer == -1) {
|
|
|
|
if (pretend)
|
|
|
|
return SUSPEND;
|
|
|
|
|
|
|
|
return ENOENT;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-03-05 11:10:41 +01:00
|
|
|
/* check if we write to a closed pipe */
|
2013-10-04 16:29:40 +02:00
|
|
|
if (!(uds_fd_table[peer].mode & R_BIT)) {
|
2013-03-05 11:10:41 +01:00
|
|
|
return EPIPE;
|
|
|
|
}
|
|
|
|
|
2013-06-11 01:47:42 +02:00
|
|
|
/* we have to preserve the boundary for DGRAM. if there's
|
|
|
|
* already a packet waiting, discard it silently and pretend
|
|
|
|
* it was written.
|
|
|
|
*/
|
|
|
|
if(uds_fd_table[minor].type == SOCK_DGRAM &&
|
|
|
|
uds_fd_table[peer].size > 0) {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
/*
|
|
|
|
* Check if the ring buffer is already full, and if the SEQPACKET
|
|
|
|
* message wouldn't write to an empty buffer.
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
2013-10-04 16:29:40 +02:00
|
|
|
if (uds_fd_table[peer].size == PIPE_BUF ||
|
|
|
|
(uds_fd_table[minor].type == SOCK_SEQPACKET &&
|
|
|
|
uds_fd_table[peer].size > 0)) {
|
2010-07-15 15:39:57 +02:00
|
|
|
if (pretend) {
|
|
|
|
return SUSPEND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if needed revive the reader */
|
2013-09-03 02:00:20 +02:00
|
|
|
if (uds_fd_table[peer].suspended == UDS_SUSPENDED_READ)
|
|
|
|
uds_unsuspend(peer);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
#if DEBUG == 1
|
2013-10-04 16:29:40 +02:00
|
|
|
printf("(uds) [%d] suspending write request\n", minor);
|
2010-07-15 15:39:57 +02:00
|
|
|
#endif
|
|
|
|
|
2012-02-13 16:28:04 +01:00
|
|
|
/* Process is reading from an empty pipe,
|
2010-07-15 15:39:57 +02:00
|
|
|
* suspend it so some bytes can be written
|
|
|
|
*/
|
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
|
|
|
/* How much can we add to the ring buffer? */
|
|
|
|
if (size > PIPE_BUF - uds_fd_table[peer].size)
|
|
|
|
size = PIPE_BUF - uds_fd_table[peer].size;
|
|
|
|
|
|
|
|
if (pretend)
|
2010-07-15 15:39:57 +02:00
|
|
|
return size;
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
/* Put the data at the head of the ring buffer. */
|
|
|
|
pos = (uds_fd_table[peer].pos + uds_fd_table[peer].size) % PIPE_BUF;
|
|
|
|
|
|
|
|
subsize = PIPE_BUF - pos;
|
|
|
|
if (subsize > size)
|
|
|
|
subsize = size;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
if ((r = sys_safecopyfrom(endpt, grant, 0,
|
|
|
|
(vir_bytes) &uds_fd_table[peer].buf[pos], subsize)) != OK)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (subsize < size) {
|
|
|
|
if ((r = sys_safecopyfrom(endpt, grant, subsize,
|
|
|
|
(vir_bytes) uds_fd_table[peer].buf, size - subsize)) != OK)
|
|
|
|
return r;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
/* Advance the buffer head. */
|
|
|
|
uds_fd_table[peer].size += size;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
/* fill in the source address to be returned by recvfrom & recvmsg */
|
|
|
|
if (uds_fd_table[minor].type == SOCK_DGRAM) {
|
|
|
|
memcpy(&uds_fd_table[peer].source, &uds_fd_table[minor].addr,
|
|
|
|
sizeof(struct sockaddr_un));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* revive peer that was waiting for us to write */
|
2013-09-03 02:00:20 +02:00
|
|
|
if (uds_fd_table[peer].suspended == UDS_SUSPENDED_READ)
|
|
|
|
uds_unsuspend(peer);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* see if peer is blocked on select(); if the peer wants to know about
|
|
|
|
* data ready to read and it doesn't know about it already, then let
|
|
|
|
* the peer know we have data for it.
|
|
|
|
*/
|
2013-10-04 16:29:40 +02:00
|
|
|
if ((uds_fd_table[peer].sel_ops & CDEV_OP_RD) && size > 0) {
|
2013-09-03 02:00:20 +02:00
|
|
|
/* a read on peer is possible now */
|
|
|
|
chardriver_reply_select(uds_fd_table[peer].sel_endpt, peer,
|
2013-09-10 20:25:01 +02:00
|
|
|
CDEV_OP_RD);
|
|
|
|
uds_fd_table[peer].sel_ops &= ~CDEV_OP_RD;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
return size; /* return number of bytes written */
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static ssize_t
|
|
|
|
uds_read(devminor_t minor, u64_t position, endpoint_t endpt,
|
2013-09-03 02:00:20 +02:00
|
|
|
cp_grant_id_t grant, size_t size, int flags, cdev_id_t id)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-09-03 02:00:20 +02:00
|
|
|
ssize_t rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
#if DEBUG == 1
|
|
|
|
static int call_count = 0;
|
2013-09-03 02:00:20 +02:00
|
|
|
printf("(uds) [%d] uds_read() call_count=%d\n", minor, ++call_count);
|
2010-07-15 15:39:57 +02:00
|
|
|
#endif
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
if (minor < 0 || minor >= NR_FDS) return ENXIO;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
if (uds_fd_table[minor].state != UDS_INUSE) {
|
2013-09-03 02:00:20 +02:00
|
|
|
/* attempted to read from a socket that hasn't been opened --
|
2010-07-15 15:39:57 +02:00
|
|
|
* something is very wrong :(
|
|
|
|
*/
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = uds_perform_read(minor, endpt, grant, size, 0);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* If the call couldn't complete, suspend the caller. */
|
|
|
|
if (rc == EDONTREPLY) {
|
|
|
|
uds_fd_table[minor].suspended = UDS_SUSPENDED_READ;
|
|
|
|
uds_fd_table[minor].susp_endpt = endpt;
|
|
|
|
uds_fd_table[minor].susp_grant = grant;
|
|
|
|
uds_fd_table[minor].susp_size = size;
|
|
|
|
uds_fd_table[minor].susp_id = id;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* If the call wasn't supposed to block, cancel immediately. */
|
2013-09-10 20:25:01 +02:00
|
|
|
if (flags & CDEV_NONBLOCK) {
|
2013-09-03 02:00:20 +02:00
|
|
|
uds_cancel(minor, endpt, id);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = EAGAIN;
|
|
|
|
}
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static ssize_t
|
|
|
|
uds_write(devminor_t minor, u64_t position, endpoint_t endpt,
|
2013-09-03 02:00:20 +02:00
|
|
|
cp_grant_id_t grant, size_t size, int flags, cdev_id_t id)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-09-03 02:00:20 +02:00
|
|
|
ssize_t rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
#if DEBUG == 1
|
|
|
|
static int call_count = 0;
|
2013-09-03 02:00:20 +02:00
|
|
|
printf("(uds) [%d] uds_write() call_count=%d\n", minor, ++call_count);
|
2010-07-15 15:39:57 +02:00
|
|
|
#endif
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
if (minor < 0 || minor >= NR_FDS) return ENXIO;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
if (uds_fd_table[minor].state != UDS_INUSE) {
|
2013-03-05 11:10:41 +01:00
|
|
|
/* attempted to write to a socket that hasn't been opened --
|
2010-07-15 15:39:57 +02:00
|
|
|
* something is very wrong :(
|
|
|
|
*/
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = uds_perform_write(minor, endpt, grant, size, 0);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* If the call couldn't complete, suspend the caller. */
|
|
|
|
if (rc == EDONTREPLY) {
|
|
|
|
uds_fd_table[minor].suspended = UDS_SUSPENDED_WRITE;
|
|
|
|
uds_fd_table[minor].susp_endpt = endpt;
|
|
|
|
uds_fd_table[minor].susp_grant = grant;
|
|
|
|
uds_fd_table[minor].susp_size = size;
|
|
|
|
uds_fd_table[minor].susp_id = id;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* If the call wasn't supposed to block, cancel immediately. */
|
2013-09-10 20:25:01 +02:00
|
|
|
if (flags & CDEV_NONBLOCK) {
|
2013-09-03 02:00:20 +02:00
|
|
|
uds_cancel(minor, endpt, id);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = EAGAIN;
|
|
|
|
}
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
uds_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
|
2013-09-03 02:00:20 +02:00
|
|
|
cp_grant_id_t grant, int flags, endpoint_t user_endpt, cdev_id_t id)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-09-03 02:00:20 +02:00
|
|
|
int rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
#if DEBUG == 1
|
|
|
|
static int call_count = 0;
|
2013-09-03 02:00:20 +02:00
|
|
|
printf("(uds) [%d] uds_ioctl() call_count=%d\n", minor, ++call_count);
|
2010-07-15 15:39:57 +02:00
|
|
|
#endif
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
if (minor < 0 || minor >= NR_FDS) return ENXIO;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
if (uds_fd_table[minor].state != UDS_INUSE) {
|
2013-09-03 02:00:20 +02:00
|
|
|
/* attempted to perform I/O control on a socket that hasn't
|
|
|
|
* been opened -- something is very wrong :(
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* update the owner endpoint */
|
|
|
|
uds_fd_table[minor].owner = user_endpt;
|
2011-03-25 11:52:53 +01:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* let the UDS subsystem handle the actual request */
|
|
|
|
rc = uds_do_ioctl(minor, request, endpt, grant);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* If the call couldn't complete, suspend the caller. */
|
|
|
|
if (rc == EDONTREPLY) {
|
|
|
|
/* The suspension type is already set by the IOCTL handler. */
|
|
|
|
if (uds_fd_table[minor].suspended == UDS_NOT_SUSPENDED)
|
|
|
|
panic("IOCTL did not actually suspend?");
|
|
|
|
uds_fd_table[minor].susp_endpt = endpt;
|
|
|
|
uds_fd_table[minor].susp_grant = grant;
|
|
|
|
uds_fd_table[minor].susp_size = 0; /* irrelevant */
|
|
|
|
uds_fd_table[minor].susp_id = id;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* If the call wasn't supposed to block, cancel immediately. */
|
2013-09-10 20:25:01 +02:00
|
|
|
if (flags & CDEV_NONBLOCK) {
|
2013-09-03 02:00:20 +02:00
|
|
|
uds_cancel(minor, endpt, id);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = EAGAIN;
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
2011-03-25 11:52:53 +01:00
|
|
|
|
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
void
|
|
|
|
uds_unsuspend(devminor_t minor)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-09-03 02:00:20 +02:00
|
|
|
int r;
|
2012-02-13 16:28:04 +01:00
|
|
|
uds_fd_t *fdp;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2012-02-13 16:28:04 +01:00
|
|
|
fdp = &uds_fd_table[minor];
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
switch (fdp->suspended) {
|
|
|
|
case UDS_SUSPENDED_READ:
|
|
|
|
r = uds_perform_read(minor, fdp->susp_endpt, fdp->susp_grant,
|
|
|
|
fdp->susp_size, 0);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
if (r == EDONTREPLY)
|
|
|
|
return;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
break;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
case UDS_SUSPENDED_WRITE:
|
|
|
|
r = uds_perform_write(minor, fdp->susp_endpt, fdp->susp_grant,
|
|
|
|
fdp->susp_size, 0);
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
if (r == EDONTREPLY)
|
|
|
|
return;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
break;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
case UDS_SUSPENDED_CONNECT:
|
|
|
|
case UDS_SUSPENDED_ACCEPT:
|
|
|
|
/* In both cases, the caller already set up the connection.
|
|
|
|
* The only thing to do here is unblock.
|
|
|
|
*/
|
|
|
|
r = OK;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
break;
|
2012-02-13 16:28:04 +01:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
default:
|
|
|
|
panic("unknown suspension type %d", fdp->suspended);
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
chardriver_reply_task(fdp->susp_endpt, fdp->susp_id, r);
|
|
|
|
|
|
|
|
fdp->suspended = UDS_NOT_SUSPENDED;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:29:40 +02:00
|
|
|
static int
|
|
|
|
uds_cancel(devminor_t minor, endpoint_t endpt, cdev_id_t id)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
2013-09-03 02:00:20 +02:00
|
|
|
uds_fd_t *fdp;
|
2010-07-15 15:39:57 +02:00
|
|
|
int i, j;
|
2013-09-03 02:00:20 +02:00
|
|
|
|
2010-07-15 15:39:57 +02:00
|
|
|
#if DEBUG == 1
|
|
|
|
static int call_count = 0;
|
2013-09-03 02:00:20 +02:00
|
|
|
printf("(uds) [%d] uds_cancel() call_count=%d\n", minor, ++call_count);
|
2010-07-15 15:39:57 +02:00
|
|
|
#endif
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
if (minor < 0 || minor >= NR_FDS) return EDONTREPLY;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
fdp = &uds_fd_table[minor];
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
if (fdp->state != UDS_INUSE) {
|
2013-10-04 16:29:40 +02:00
|
|
|
printf("UDS: cancel request for closed minor %d\n", minor);
|
2013-09-03 02:00:20 +02:00
|
|
|
return EDONTREPLY;
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* Make sure the cancel request is for a request we're hanging on. */
|
|
|
|
if (fdp->suspended == UDS_NOT_SUSPENDED || fdp->susp_endpt != endpt ||
|
|
|
|
fdp->susp_id != id) {
|
|
|
|
return EDONTREPLY; /* this happens. */
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* The system call was cancelled, so the socket is not suspended
|
|
|
|
* anymore.
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
2013-09-03 02:00:20 +02:00
|
|
|
switch (fdp->suspended) {
|
|
|
|
case UDS_SUSPENDED_ACCEPT: /* accept() */
|
|
|
|
/* partial accept() only changes
|
|
|
|
* uds_fd_table[minorparent].child
|
|
|
|
*/
|
|
|
|
for (i = 0; i < NR_FDS; i++) {
|
|
|
|
if (uds_fd_table[i].child == minor) {
|
|
|
|
uds_fd_table[i].child = -1;
|
|
|
|
}
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
break;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
case UDS_SUSPENDED_CONNECT: /* connect() */
|
|
|
|
/* partial connect() sets addr and adds minor to server backlog
|
|
|
|
*/
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
for (i = 0; i < NR_FDS; i++) {
|
|
|
|
/* find a socket that is in use. */
|
|
|
|
if (uds_fd_table[i].state != UDS_INUSE)
|
|
|
|
continue;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* see if minor is in the backlog */
|
2010-07-15 15:39:57 +02:00
|
|
|
for (j = 0; j < uds_fd_table[i].backlog_size; j++) {
|
|
|
|
if (uds_fd_table[i].backlog[j] == minor) {
|
|
|
|
|
|
|
|
/* remove from backlog */
|
|
|
|
uds_fd_table[i].backlog[j] = -1;
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 02:00:20 +02:00
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
/* clear the address */
|
|
|
|
memset(&(uds_fd_table[minor].addr), '\0',
|
|
|
|
sizeof(struct sockaddr_un));
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
break;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
case UDS_SUSPENDED_READ:
|
|
|
|
case UDS_SUSPENDED_WRITE:
|
|
|
|
/* Nothing more to do. */
|
|
|
|
break;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
default:
|
|
|
|
panic("unknown suspension type %d", fdp->suspended);
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
fdp->suspended = UDS_NOT_SUSPENDED;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
return EINTR; /* reply to the original request */
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
2013-10-04 16:29:40 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the server.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
uds_init(int UNUSED(type), sef_init_info_t *UNUSED(info))
|
|
|
|
{
|
|
|
|
/* Setting everything to NULL implicitly sets the state to UDS_FREE. */
|
|
|
|
memset(uds_fd_table, '\0', sizeof(uds_fd_t) * NR_FDS);
|
|
|
|
|
|
|
|
uds_exit_left = 0;
|
|
|
|
|
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
uds_signal(int signo)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Only check for termination signal, ignore anything else. */
|
|
|
|
if (signo != SIGTERM) return;
|
|
|
|
|
|
|
|
/* Only exit once all sockets have been closed. */
|
|
|
|
uds_exit_left = 0;
|
|
|
|
for (i = 0; i < NR_FDS; i++)
|
|
|
|
if (uds_fd_table[i].state == UDS_INUSE)
|
|
|
|
uds_exit_left++;
|
|
|
|
|
|
|
|
if (uds_exit_left == 0)
|
|
|
|
chardriver_terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
uds_startup(void)
|
|
|
|
{
|
|
|
|
/* Register init callbacks. */
|
|
|
|
sef_setcb_init_fresh(uds_init);
|
|
|
|
|
|
|
|
/* No live update support for now. */
|
|
|
|
|
|
|
|
/* Register signal callbacks. */
|
|
|
|
sef_setcb_signal_handler(uds_signal);
|
|
|
|
|
|
|
|
/* Let SEF perform startup. */
|
|
|
|
sef_startup();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The UNIX domain sockets driver.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
uds_startup();
|
|
|
|
|
|
|
|
chardriver_task(&uds_tab);
|
|
|
|
|
|
|
|
return(OK);
|
|
|
|
}
|