MOS Source Code
Loading...
Searching...
No Matches
ipc_sysfs.c
Go to the documentation of this file.
1
// SPDX-License-Identifier: GPL-3.0-or-later
2
3
// ! /sys/ipc/<server_name> operations to connect to an IPC server
4
5
#include "
mos/filesystem/dentry.h
"
6
#include "
mos/filesystem/vfs_types.h
"
7
#include "
mos/ipc/ipc.h
"
8
#include "
mos/ipc/ipc_io.h
"
9
#include "
mos/mm/slab_autoinit.h
"
10
#include "
mos/platform/platform.h
"
11
#include "
mos/tasks/process.h
"
12
13
#include <
mos_stdlib.h
>
14
#include <
mos_string.h
>
15
16
typedef
struct
17
{
18
bool
server_control_file
;
19
ipc_server_t
*
server
;
20
ipc_t
*
client_ipc
;
21
}
ipc_vfs_private_t
;
22
23
slab_t
*
ipc_vfs_private_slab
=
NULL
;
24
SLAB_AUTOINIT
(
"ipc_vfs_private"
,
ipc_vfs_private_slab
,
ipc_vfs_private_t
);
25
26
static
bool
vfs_open_ipc
(
inode_t
*ino,
file_t
*file,
bool
created)
27
{
28
MOS_UNUSED
(ino);
29
30
ipc_t
*ipc =
NULL
;
31
ipc_server_t
*server =
NULL
;
32
33
if
(created)
34
{
35
server =
ipc_get_server
(file->
dentry
->
name
);
36
MOS_ASSERT
(server);
37
}
38
else
39
{
40
ipc =
ipc_connect_to_server
(file->
dentry
->
name
,
MOS_PAGE_SIZE
);
41
if
(IS_ERR(ipc))
42
return
false
;
43
}
44
45
ipc_vfs_private_t
*
private
= kmalloc(
ipc_vfs_private_slab
);
46
private
->server_control_file = created;
47
private
->client_ipc = ipc;
48
private
->server = server;
49
50
file->
private_data
=
private
;
51
return
true
;
52
}
53
54
static
ssize_t
vfs_ipc_file_read
(
const
file_t
*file,
void
*buf,
size_t
size
,
off_t
offset)
55
{
56
MOS_UNUSED
(offset);
57
58
ipc_vfs_private_t
*
private
= file->
private_data
;
59
if
(private->server_control_file)
60
{
61
// reading from the server's control file accepts and returns a new connection, as a fd
62
if
(
size
<
sizeof
(
fd_t
))
63
return
-EINVAL;
64
65
ipc_t
*ipc =
ipc_server_accept
(private->server);
66
if
(IS_ERR(ipc))
67
return
PTR_ERR(ipc);
68
69
ipc_conn_io_t
*connio =
ipc_conn_io_create
(ipc,
true
);
70
if
(IS_ERR(connio))
71
return
PTR_ERR(connio);
72
73
const
fd_t
fd =
process_attach_ref_fd
(
current_process
, &connio->
io
,
FD_FLAGS_NONE
);
74
if
(
IS_ERR_VALUE
(fd))
75
return
fd;
76
77
// return the fd to the client
78
memcpy
(buf, &fd,
sizeof
(
fd_t
));
79
return
sizeof
(
fd_t
);
80
}
81
else
82
{
83
return
ipc_client_read
(private->client_ipc, buf,
size
);
84
}
85
}
86
87
static
ssize_t
vfs_ipc_file_write
(
const
file_t
*file,
const
void
*buf,
size_t
size
,
off_t
offset)
88
{
89
MOS_UNUSED
(offset);
90
ipc_vfs_private_t
*
private
= file->
private_data
;
91
if
(private->server_control_file)
92
return
-EBADF;
// writing to the server's control file is not supported
93
94
return
ipc_client_write
(private->client_ipc, buf,
size
);
95
}
96
97
static
void
vfs_ipc_file_release
(
file_t
*file)
98
{
99
ipc_vfs_private_t
*
private
= file->
private_data
;
100
if
(private->server_control_file)
101
{
102
ipc_server_close
(private->server);
103
dentry_detach
(file->
dentry
);
104
}
105
else
106
ipc_client_close_channel
(private->client_ipc);
107
kfree(
private
);
108
}
109
110
const
file_ops_t
ipc_sysfs_file_ops
= {
111
.open =
vfs_open_ipc
,
112
.read =
vfs_ipc_file_read
,
113
.write =
vfs_ipc_file_write
,
114
.release =
vfs_ipc_file_release
,
115
};
MOS_ASSERT
#define MOS_ASSERT(cond)
Definition
assert.h:14
MOS_PAGE_SIZE
#define MOS_PAGE_SIZE
Definition
autoconf.h:6
dentry.h
FD_FLAGS_NONE
@ FD_FLAGS_NONE
Definition
fs_types.h:48
dentry_detach
void dentry_detach(dentry_t *d)
Detach the inode from a dentry.
Definition
dentry.c:318
ipc_io.h
ipc_conn_io_create
ipc_conn_io_t * ipc_conn_io_create(ipc_t *ipc, bool is_server_side)
Create a new IPC connection io descriptor.
Definition
ipc_io.c:96
ipc_vfs_private_slab
slab_t * ipc_vfs_private_slab
Definition
ipc_sysfs.c:23
vfs_ipc_file_write
static ssize_t vfs_ipc_file_write(const file_t *file, const void *buf, size_t size, off_t offset)
Definition
ipc_sysfs.c:87
vfs_ipc_file_release
static void vfs_ipc_file_release(file_t *file)
Definition
ipc_sysfs.c:97
ipc_sysfs_file_ops
const file_ops_t ipc_sysfs_file_ops
Definition
ipc_sysfs.c:110
vfs_ipc_file_read
static ssize_t vfs_ipc_file_read(const file_t *file, void *buf, size_t size, off_t offset)
Definition
ipc_sysfs.c:54
vfs_open_ipc
static bool vfs_open_ipc(inode_t *ino, file_t *file, bool created)
Definition
ipc_sysfs.c:26
ipc.h
ipc_client_close_channel
void ipc_client_close_channel(ipc_t *ipc)
Definition
ipc.c:140
ipc_client_write
size_t ipc_client_write(ipc_t *ipc, const void *buffer, size_t size)
Definition
ipc.c:125
ipc_server_close
void ipc_server_close(ipc_server_t *server)
Definition
ipc.c:73
ipc_get_server
ipc_server_t * ipc_get_server(const char *name)
Definition
ipc.c:220
ipc_server_accept
ipc_t * ipc_server_accept(ipc_server_t *server)
Definition
ipc.c:236
ipc_client_read
size_t ipc_client_read(ipc_t *ipc, void *buffer, size_t size)
Definition
ipc.c:120
ipc_connect_to_server
ipc_t * ipc_connect_to_server(const char *name, size_t buffer_size)
Definition
ipc.c:291
MOS_UNUSED
#define MOS_UNUSED(x)
Definition
mos_global.h:64
IS_ERR_VALUE
#define IS_ERR_VALUE(x)
Definition
mos_global.h:126
mos_stdlib.h
mos_string.h
memcpy
static void * memcpy(void *s1, const void *s2, size_t n)
Definition
pb_syshdr.h:90
NULL
#define NULL
Definition
pb_syshdr.h:46
platform.h
current_process
#define current_process
Definition
platform.h:31
process.h
process_attach_ref_fd
fd_t process_attach_ref_fd(process_t *process, io_t *file, fd_flags_t flags)
Definition
process.c:171
size
size_t size
Definition
slab.c:30
slab_autoinit.h
SLAB_AUTOINIT
#define SLAB_AUTOINIT(name, var, type)
Definition
slab_autoinit.h:14
dentry_t::name
const char * name
Definition
vfs_types.h:118
file_ops_t
Definition
vfs_types.h:89
file_t
Definition
vfs_types.h:198
file_t::private_data
void * private_data
Definition
vfs_types.h:203
file_t::dentry
dentry_t * dentry
Definition
vfs_types.h:200
inode_t
Definition
vfs_types.h:156
ipc_conn_io_t
Definition
ipc_io.h:10
ipc_conn_io_t::io
io_t io
Definition
ipc_io.h:11
ipc_server_t
Definition
ipc.c:51
ipc_t
Definition
ipc.c:30
ipc_vfs_private_t
Definition
ipc_sysfs.c:17
ipc_vfs_private_t::client_ipc
ipc_t * client_ipc
Definition
ipc_sysfs.c:20
ipc_vfs_private_t::server_control_file
bool server_control_file
Definition
ipc_sysfs.c:18
ipc_vfs_private_t::server
ipc_server_t * server
Definition
ipc_sysfs.c:19
slab_t
Definition
slab.h:45
off_t
ssize_t off_t
Definition
types.h:84
fd_t
s32 fd_t
Definition
types.h:81
ssize_t
signed long ssize_t
Definition
types.h:83
vfs_types.h
kernel
ipc
ipc_sysfs.c
Generated on Sun Sep 1 2024 18:22:52 for MOS Source Code by
1.12.0