Skip to content

Commit d0ca309

Browse files
author
张世争
committed
[lwp_ipc] support file descriptor transmit
1 parent 1c886a8 commit d0ca309

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

components/lwp/lwp_ipc.c

+61
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,53 @@ static void sender_timeout(void *parameter)
356356
rt_schedule();
357357
}
358358

359+
/**
360+
* Get file vnode from fd.
361+
*/
362+
static void *_ipc_msg_get_file(int fd)
363+
{
364+
struct dfs_file *d;
365+
366+
d = fd_get(fd);
367+
if (d == RT_NULL)
368+
return RT_NULL;
369+
370+
if (!d->vnode)
371+
return RT_NULL;
372+
373+
d->vnode->ref_count++;
374+
return (void *)d->vnode;
375+
}
376+
377+
/**
378+
* Get fd from file vnode.
379+
*/
380+
static int _ipc_msg_fd_new(void *file)
381+
{
382+
int fd;
383+
struct dfs_file *d;
384+
385+
fd = fd_new();
386+
if (fd < 0)
387+
{
388+
return -1;
389+
}
390+
391+
d = fd_get(fd);
392+
if (!d)
393+
{
394+
fd_release(fd);
395+
return -1;
396+
}
397+
398+
d->vnode = (struct dfs_vnode *)file;
399+
d->flags = O_RDWR; /* set flags as read and write */
400+
d->vnode->size = 0;
401+
d->pos = 0;
402+
403+
return fd;
404+
}
405+
359406
/**
360407
* Send data through an IPC channel, wait for the reply or not.
361408
*/
@@ -398,6 +445,12 @@ static rt_err_t _rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_ms
398445
return -RT_ENOMEM;
399446
}
400447

448+
/* IPC message : file descriptor */
449+
if (data->type == RT_CHANNEL_FD)
450+
{
451+
data->u.fd.file = _ipc_msg_get_file(data->u.fd.fd);
452+
}
453+
401454
rt_ipc_msg_init(msg, data, need_reply);
402455

403456
if (need_reply)
@@ -686,6 +739,10 @@ static rt_err_t _rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t d
686739
ch->stat = RT_IPC_STAT_ACTIVE; /* no valid suspened receivers */
687740
}
688741
*data = msg_ret->msg; /* extract the transferred data */
742+
if (data->type == RT_CHANNEL_FD)
743+
{
744+
data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
745+
}
689746
_ipc_msg_free(msg_ret); /* put back the message to kernel */
690747
}
691748
else
@@ -740,6 +797,10 @@ static rt_err_t _rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t d
740797
}
741798
/* If waked up, the received message has been store into the thread. */
742799
*data = ((rt_ipc_msg_t)(thread->msg_ret))->msg; /* extract data */
800+
if (data->type == RT_CHANNEL_FD)
801+
{
802+
data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
803+
}
743804
_ipc_msg_free(thread->msg_ret); /* put back the message to kernel */
744805
thread->msg_ret = RT_NULL;
745806
}

components/lwp/lwp_ipc.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ extern "C" {
1818
enum
1919
{
2020
RT_CHANNEL_RAW,
21-
RT_CHANNEL_BUFFER
21+
RT_CHANNEL_BUFFER,
22+
RT_CHANNEL_FD
2223
};
2324

2425
struct rt_channel_msg
@@ -32,6 +33,11 @@ struct rt_channel_msg
3233
void *buf;
3334
size_t length;
3435
} b;
36+
struct chfd
37+
{
38+
void *file;
39+
int fd;
40+
} fd;
3541
void* d;
3642
} u;
3743
};

0 commit comments

Comments
 (0)