Skip to content

Commit eaa34e3

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

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

components/lwp/lwp_ipc.c

+59
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,51 @@ 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+
401+
return fd;
402+
}
403+
359404
/**
360405
* Send data through an IPC channel, wait for the reply or not.
361406
*/
@@ -398,6 +443,12 @@ static rt_err_t _rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_ms
398443
return -RT_ENOMEM;
399444
}
400445

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

403454
if (need_reply)
@@ -686,6 +737,10 @@ static rt_err_t _rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t d
686737
ch->stat = RT_IPC_STAT_ACTIVE; /* no valid suspened receivers */
687738
}
688739
*data = msg_ret->msg; /* extract the transferred data */
740+
if (data->type == RT_CHANNEL_FD)
741+
{
742+
data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
743+
}
689744
_ipc_msg_free(msg_ret); /* put back the message to kernel */
690745
}
691746
else
@@ -740,6 +795,10 @@ static rt_err_t _rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t d
740795
}
741796
/* If waked up, the received message has been store into the thread. */
742797
*data = ((rt_ipc_msg_t)(thread->msg_ret))->msg; /* extract data */
798+
if (data->type == RT_CHANNEL_FD)
799+
{
800+
data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
801+
}
743802
_ipc_msg_free(thread->msg_ret); /* put back the message to kernel */
744803
thread->msg_ret = RT_NULL;
745804
}

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)