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-09-03 02:00:20 +02:00
|
|
|
* uds_request: process a character device request
|
2010-07-15 15:39:57 +02:00
|
|
|
*
|
|
|
|
* Also See...
|
|
|
|
*
|
2013-09-03 02:00:20 +02:00
|
|
|
* uds.c, uds.h
|
2010-07-15 15:39:57 +02:00
|
|
|
*
|
|
|
|
* Overview
|
|
|
|
*
|
|
|
|
* The interface to unix domain sockets is similar to the
|
|
|
|
* the interface to network sockets. There is a character
|
|
|
|
* device (/dev/uds) that uses STYLE_CLONE and this server
|
|
|
|
* is a 'driver' for that device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
|
|
|
|
#include "inc.h"
|
|
|
|
#include "const.h"
|
|
|
|
#include "glo.h"
|
|
|
|
#include "uds.h"
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
static ssize_t uds_perform_read(devminor_t minor, endpoint_t endpt,
|
|
|
|
cp_grant_id_t grant, size_t size, int pretend);
|
|
|
|
static ssize_t uds_perform_write(devminor_t minor, endpoint_t endpt,
|
|
|
|
cp_grant_id_t grant, size_t size, int pretend);
|
|
|
|
|
|
|
|
static int uds_open(devminor_t orig_minor, int access, endpoint_t user_endpt);
|
|
|
|
static int uds_close(devminor_t minor);
|
|
|
|
static ssize_t uds_read(devminor_t minor, u64_t position, endpoint_t endpt,
|
|
|
|
cp_grant_id_t grant, size_t size, int flags, cdev_id_t id);
|
|
|
|
static ssize_t uds_write(devminor_t minor, u64_t position, endpoint_t endpt,
|
|
|
|
cp_grant_id_t grant, size_t size, int flags, cdev_id_t id);
|
|
|
|
static int uds_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
|
|
|
|
cp_grant_id_t grant, int flags, endpoint_t user_endpt, cdev_id_t id);
|
|
|
|
static int uds_cancel(devminor_t minor, endpoint_t endpt, cdev_id_t id);
|
|
|
|
static int uds_select(devminor_t minor, unsigned int ops, endpoint_t endpt);
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
void uds_request(message *m_ptr, int ipc_status)
|
|
|
|
{
|
|
|
|
/* Use libchardriver to process character device requests. */
|
|
|
|
chardriver_process(&uds_tab, m_ptr, ipc_status);
|
|
|
|
}
|
2010-07-21 15:39:46 +02:00
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
static int uds_open(devminor_t UNUSED(orig_minor), int access,
|
|
|
|
endpoint_t user_endpt)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
|
|
|
message fs_m_in, fs_m_out;
|
2013-04-18 11:07:44 +02:00
|
|
|
struct uucred ucred;
|
2013-09-03 02:00:20 +02:00
|
|
|
devminor_t minor;
|
2010-07-15 15:39:57 +02:00
|
|
|
int rc, i;
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We found a slot in uds_fd_table, now initialize the descriptor
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
/* initialize the data pointer (pos) to the start of the PIPE */
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
uds_fd_table[minor].mode = S_IRUSR|S_IWUSR;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* get the effective user id and effective group id from the endpoint */
|
|
|
|
/* this is needed in the REQ_NEWNODE request to PFS. */
|
2013-09-03 02:00:20 +02:00
|
|
|
rc = getnucred(user_endpt, &ucred);
|
2010-07-15 15:39:57 +02:00
|
|
|
if (rc == -1) {
|
|
|
|
/* roll back the changes we made to the descriptor */
|
|
|
|
memset(&(uds_fd_table[minor]), '\0', sizeof(uds_fd_t));
|
|
|
|
|
|
|
|
/* likely error: invalid endpoint / proc doesn't exist */
|
2013-09-03 02:00:20 +02:00
|
|
|
return EIO;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare Request to the FS side of PFS */
|
|
|
|
|
|
|
|
fs_m_in.m_type = REQ_NEWNODE;
|
|
|
|
fs_m_in.REQ_MODE = I_NAMED_PIPE;
|
|
|
|
fs_m_in.REQ_DEV = NO_DEV;
|
2013-04-18 11:07:44 +02:00
|
|
|
fs_m_in.REQ_UID = ucred.cr_uid;
|
|
|
|
fs_m_in.REQ_GID = ucred.cr_gid;
|
2010-07-15 15:39:57 +02:00
|
|
|
|
|
|
|
/* Request a new inode on the pipe file system */
|
|
|
|
|
|
|
|
rc = fs_newnode(&fs_m_in, &fs_m_out);
|
|
|
|
if (rc != OK) {
|
|
|
|
/* roll back the changes we made to the descriptor */
|
|
|
|
memset(&(uds_fd_table[minor]), '\0', sizeof(uds_fd_t));
|
|
|
|
|
|
|
|
/* likely error: get_block() failed */
|
2011-03-25 11:52:53 +01:00
|
|
|
return rc;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process the response */
|
|
|
|
uds_fd_table[minor].inode_nr = fs_m_out.RES_INODE_NR;
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
return minor; /* cloned! */
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
static int uds_close(devminor_t minor)
|
2010-07-15 15:39:57 +02:00
|
|
|
{
|
|
|
|
message fs_m_in, fs_m_out;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
#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-03-05 11:10:41 +01:00
|
|
|
int 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
|
|
|
}
|
|
|
|
|
2010-07-15 15:39:57 +02:00
|
|
|
/* Prepare Request to the FS side of PFS */
|
|
|
|
|
|
|
|
fs_m_in.m_type = REQ_PUTNODE;
|
|
|
|
fs_m_in.REQ_INODE_NR = uds_fd_table[minor].inode_nr;
|
|
|
|
fs_m_in.REQ_COUNT = 1;
|
|
|
|
|
|
|
|
/* set the socket back to its original UDS_FREE state */
|
|
|
|
memset(&(uds_fd_table[minor]), '\0', sizeof(uds_fd_t));
|
|
|
|
|
|
|
|
/* Request the removal of the inode from the pipe file system */
|
|
|
|
|
|
|
|
rc = fs_putnode(&fs_m_in, &fs_m_out);
|
|
|
|
if (rc != OK) {
|
2013-09-03 02:00:20 +02:00
|
|
|
printf("PFS: fs_putnode returned %d\n", rc);
|
|
|
|
|
2010-07-15 15:39:57 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-09-03 02:00:20 +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-03 02:00:20 +02:00
|
|
|
watch = (ops & SEL_NOTIFY);
|
|
|
|
ops &= (SEL_RD | SEL_WR | SEL_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-03 02:00:20 +02:00
|
|
|
if (ops & SEL_RD) {
|
|
|
|
bytes = uds_perform_read(minor, NONE, GRANT_INVALID, 1, 1);
|
|
|
|
if (bytes > 0) {
|
|
|
|
/* there is data in the pipe for us to read */
|
|
|
|
ready_ops |= SEL_RD;
|
|
|
|
} 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) {
|
|
|
|
ready_ops |= SEL_RD;
|
|
|
|
break;
|
|
|
|
}
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
2013-09-03 02:00:20 +02:00
|
|
|
} else if (bytes != SUSPEND) {
|
|
|
|
ready_ops |= SEL_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-03 02:00:20 +02:00
|
|
|
if (ops & SEL_WR) {
|
|
|
|
bytes = uds_perform_write(minor, NONE, GRANT_INVALID, PIPE_BUF,
|
|
|
|
1);
|
|
|
|
if (bytes != 0 && bytes != SUSPEND) {
|
|
|
|
/* There is room to write or there is an error
|
|
|
|
* condition.
|
|
|
|
*/
|
|
|
|
ready_ops |= SEL_WR;
|
|
|
|
}
|
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-09-03 02:00:20 +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-03-05 11:10:41 +01:00
|
|
|
int rc, peer;
|
2010-07-15 15:39:57 +02:00
|
|
|
message fs_m_in;
|
|
|
|
message fs_m_out;
|
|
|
|
|
|
|
|
#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 */
|
|
|
|
if (!(uds_fd_table[minor].mode & S_IRUSR)) {
|
|
|
|
/* 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) {
|
|
|
|
uds_fd_table[minor].err = 0;
|
|
|
|
return ECONNRESET;
|
|
|
|
} else {
|
|
|
|
return ENOTCONN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if process is reading from a closed pipe */
|
|
|
|
if (peer != -1 && !(uds_fd_table[peer].mode & S_IWUSR) &&
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (pretend) {
|
|
|
|
return (size > uds_fd_table[minor].size) ?
|
|
|
|
uds_fd_table[minor].size : size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare Request to the FS side of PFS */
|
2013-09-03 02:00:20 +02:00
|
|
|
fs_m_in.m_source = endpt;
|
2010-07-15 15:39:57 +02:00
|
|
|
fs_m_in.m_type = REQ_READ;
|
|
|
|
fs_m_in.REQ_INODE_NR = uds_fd_table[minor].inode_nr;
|
2013-09-03 02:00:20 +02:00
|
|
|
fs_m_in.REQ_GRANT = grant;
|
2010-07-15 15:39:57 +02:00
|
|
|
fs_m_in.REQ_SEEK_POS_HI = 0;
|
|
|
|
fs_m_in.REQ_SEEK_POS_LO = uds_fd_table[minor].pos;
|
|
|
|
fs_m_in.REQ_NBYTES = (size > uds_fd_table[minor].size) ?
|
|
|
|
uds_fd_table[minor].size : size;
|
|
|
|
|
|
|
|
/* perform the read */
|
|
|
|
rc = fs_readwrite(&fs_m_in, &fs_m_out);
|
|
|
|
if (rc != OK) {
|
2013-09-03 02:00:20 +02:00
|
|
|
printf("PFS: fs_readwrite returned %d\n", rc);
|
2010-07-15 15:39:57 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process the response */
|
|
|
|
#if DEBUG == 1
|
|
|
|
printf("(uds) [%d] read complete\n", minor);
|
|
|
|
#endif
|
|
|
|
|
2012-02-13 16:28:04 +01:00
|
|
|
/* move the position of the data pointer up to data we haven't
|
2010-07-15 15:39:57 +02:00
|
|
|
* read yet
|
|
|
|
*/
|
|
|
|
uds_fd_table[minor].pos += fs_m_out.RES_NBYTES;
|
|
|
|
|
|
|
|
/* decrease the number of unread bytes */
|
|
|
|
uds_fd_table[minor].size -= fs_m_out.RES_NBYTES;
|
|
|
|
|
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-03 02:00:20 +02:00
|
|
|
if (peer != -1 && (uds_fd_table[peer].sel_ops & SEL_WR) &&
|
2013-03-05 11:10:41 +01:00
|
|
|
(uds_fd_table[minor].size+uds_fd_table[minor].pos + 1 < PIPE_BUF)){
|
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,
|
|
|
|
SEL_WR);
|
|
|
|
uds_fd_table[peer].sel_ops &= ~SEL_WR;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return fs_m_out.RES_NBYTES; /* return number of bytes read */
|
|
|
|
}
|
|
|
|
|
2013-09-03 02:00:20 +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
|
|
|
{
|
|
|
|
int rc, peer, i;
|
|
|
|
message fs_m_in;
|
|
|
|
message fs_m_out;
|
|
|
|
|
|
|
|
#if DEBUG == 1
|
|
|
|
static int call_count = 0;
|
|
|
|
printf("(uds) [%d] uds_perform_write() call_count=%d\n", minor,
|
|
|
|
++call_count);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* skip reads and writes of 0 (or less!) bytes */
|
|
|
|
if (size <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if we are allowed to write */
|
|
|
|
if (!(uds_fd_table[minor].mode & S_IWUSR)) {
|
|
|
|
/* 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 */
|
|
|
|
if (!(uds_fd_table[peer].mode & S_IRUSR)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:28:04 +01:00
|
|
|
/* check if write would overrun buffer. check if message
|
2013-06-11 01:47:42 +02:00
|
|
|
* SEQPACKET wouldn't write to an empty buffer. check if
|
|
|
|
* connectionless sockets have a target to write to.
|
2010-07-15 15:39:57 +02:00
|
|
|
*/
|
|
|
|
if ((uds_fd_table[peer].pos+uds_fd_table[peer].size+size > PIPE_BUF) ||
|
2013-06-11 01:47:42 +02:00
|
|
|
((uds_fd_table[minor].type == SOCK_SEQPACKET) &&
|
2012-07-18 12:05:50 +02:00
|
|
|
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
|
|
|
|
printf("(uds) [%d] suspending write 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
|
|
|
}
|
|
|
|
|
|
|
|
if (pretend) {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare Request to the FS side of PFS */
|
2013-09-03 02:00:20 +02:00
|
|
|
fs_m_in.m_source = endpt;
|
2010-07-15 15:39:57 +02:00
|
|
|
fs_m_in.m_type = REQ_WRITE;
|
|
|
|
fs_m_in.REQ_INODE_NR = uds_fd_table[peer].inode_nr;
|
2013-09-03 02:00:20 +02:00
|
|
|
fs_m_in.REQ_GRANT = grant;
|
2010-07-15 15:39:57 +02:00
|
|
|
fs_m_in.REQ_SEEK_POS_HI = 0;
|
|
|
|
fs_m_in.REQ_SEEK_POS_LO = uds_fd_table[peer].pos +
|
|
|
|
uds_fd_table[peer].size;
|
|
|
|
fs_m_in.REQ_NBYTES = size;
|
|
|
|
|
|
|
|
/* Request the write */
|
|
|
|
rc = fs_readwrite(&fs_m_in, &fs_m_out);
|
|
|
|
if (rc != OK) {
|
2013-09-03 02:00:20 +02:00
|
|
|
printf("PFS: fs_readwrite returned %d\n", rc);
|
2010-07-15 15:39:57 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process the response */
|
|
|
|
#if DEBUG == 1
|
|
|
|
printf("(uds) [%d] write complete\n", minor);
|
|
|
|
#endif
|
|
|
|
/* increase the count of unread bytes */
|
|
|
|
uds_fd_table[peer].size += fs_m_out.RES_NBYTES;
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
if ((uds_fd_table[peer].sel_ops & SEL_RD) && fs_m_out.RES_NBYTES > 0) {
|
|
|
|
/* a read on peer is possible now */
|
|
|
|
chardriver_reply_select(uds_fd_table[peer].sel_endpt, peer,
|
|
|
|
SEL_RD);
|
|
|
|
uds_fd_table[peer].sel_ops &= ~SEL_RD;
|
2010-07-15 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return fs_m_out.RES_NBYTES; /* return number of bytes written */
|
|
|
|
}
|
|
|
|
|
2013-09-03 02:00:20 +02:00
|
|
|
static ssize_t uds_read(devminor_t minor, u64_t position, endpoint_t endpt,
|
|
|
|
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. */
|
|
|
|
if (flags & FLG_OP_NONBLOCK) {
|
|
|
|
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-09-03 02:00:20 +02:00
|
|
|
static ssize_t uds_write(devminor_t minor, u64_t position, endpoint_t endpt,
|
|
|
|
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. */
|
|
|
|
if (flags & FLG_OP_NONBLOCK) {
|
|
|
|
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-09-03 02:00:20 +02:00
|
|
|
static int uds_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
|
|
|
|
cp_grant_id_t grant, int flags, endpoint_t user_endpt, cdev_id_t id)
|
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. */
|
|
|
|
if (flags & FLG_OP_NONBLOCK) {
|
|
|
|
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-09-03 02:00:20 +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-09-03 02:00:20 +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) {
|
|
|
|
printf("PFS: cancel request for closed minor %d\n", minor);
|
|
|
|
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
|
|
|
}
|