Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[smart][lwp_ipc] support file descriptor transmit #7318

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions components/lwp/lwp_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,51 @@ static void sender_timeout(void *parameter)
rt_schedule();
}

/**
* Get file vnode from fd.
*/
static void *_ipc_msg_get_file(int fd)
{
struct dfs_file *d;

d = fd_get(fd);
if (d == RT_NULL)
return RT_NULL;

if (!d->vnode)
return RT_NULL;

d->vnode->ref_count++;
return (void *)d->vnode;
}

/**
* Get fd from file vnode.
*/
static int _ipc_msg_fd_new(void *file)
{
int fd;
struct dfs_file *d;

fd = fd_new();
if (fd < 0)
{
return -1;
}

d = fd_get(fd);
if (!d)
{
fd_release(fd);
return -1;
}

d->vnode = (struct dfs_vnode *)file;
d->flags = O_RDWR; /* set flags as read and write */

return fd;
}

/**
* Send data through an IPC channel, wait for the reply or not.
*/
Expand Down Expand Up @@ -398,6 +443,12 @@ static rt_err_t _rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_ms
return -RT_ENOMEM;
}

/* IPC message : file descriptor */
if (data->type == RT_CHANNEL_FD)
{
data->u.fd.file = _ipc_msg_get_file(data->u.fd.fd);
}

rt_ipc_msg_init(msg, data, need_reply);

if (need_reply)
Expand Down Expand Up @@ -686,6 +737,10 @@ static rt_err_t _rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t d
ch->stat = RT_IPC_STAT_ACTIVE; /* no valid suspened receivers */
}
*data = msg_ret->msg; /* extract the transferred data */
if (data->type == RT_CHANNEL_FD)
{
data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
}
_ipc_msg_free(msg_ret); /* put back the message to kernel */
}
else
Expand Down Expand Up @@ -740,6 +795,10 @@ static rt_err_t _rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t d
}
/* If waked up, the received message has been store into the thread. */
*data = ((rt_ipc_msg_t)(thread->msg_ret))->msg; /* extract data */
if (data->type == RT_CHANNEL_FD)
{
data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
}
_ipc_msg_free(thread->msg_ret); /* put back the message to kernel */
thread->msg_ret = RT_NULL;
}
Expand Down
8 changes: 7 additions & 1 deletion components/lwp/lwp_ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ extern "C" {
enum
{
RT_CHANNEL_RAW,
RT_CHANNEL_BUFFER
RT_CHANNEL_BUFFER,
RT_CHANNEL_FD
};

struct rt_channel_msg
Expand All @@ -32,6 +33,11 @@ struct rt_channel_msg
void *buf;
size_t length;
} b;
struct chfd
{
void *file;
int fd;
} fd;
void* d;
} u;
};
Expand Down