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
7#include "mos/ipc/ipc.h"
8#include "mos/ipc/ipc_io.h"
11#include "mos/tasks/process.h"
12
13#include <mos_stdlib.h>
14#include <mos_string.h>
15
22
25
26static 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 {
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
54static 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
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
87static 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
97static 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
111 .open = vfs_open_ipc,
112 .read = vfs_ipc_file_read,
113 .write = vfs_ipc_file_write,
114 .release = vfs_ipc_file_release,
115};
#define MOS_ASSERT(cond)
Definition assert.h:14
#define MOS_PAGE_SIZE
Definition autoconf.h:6
@ FD_FLAGS_NONE
Definition fs_types.h:48
void dentry_detach(dentry_t *d)
Detach the inode from a dentry.
Definition dentry.c:318
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
slab_t * ipc_vfs_private_slab
Definition ipc_sysfs.c:23
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
static void vfs_ipc_file_release(file_t *file)
Definition ipc_sysfs.c:97
const file_ops_t ipc_sysfs_file_ops
Definition ipc_sysfs.c:110
static ssize_t vfs_ipc_file_read(const file_t *file, void *buf, size_t size, off_t offset)
Definition ipc_sysfs.c:54
static bool vfs_open_ipc(inode_t *ino, file_t *file, bool created)
Definition ipc_sysfs.c:26
void ipc_client_close_channel(ipc_t *ipc)
Definition ipc.c:140
size_t ipc_client_write(ipc_t *ipc, const void *buffer, size_t size)
Definition ipc.c:125
void ipc_server_close(ipc_server_t *server)
Definition ipc.c:73
ipc_server_t * ipc_get_server(const char *name)
Definition ipc.c:220
ipc_t * ipc_server_accept(ipc_server_t *server)
Definition ipc.c:236
size_t ipc_client_read(ipc_t *ipc, void *buffer, size_t size)
Definition ipc.c:120
ipc_t * ipc_connect_to_server(const char *name, size_t buffer_size)
Definition ipc.c:291
#define MOS_UNUSED(x)
Definition mos_global.h:64
#define IS_ERR_VALUE(x)
Definition mos_global.h:126
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
#define NULL
Definition pb_syshdr.h:46
#define current_process
Definition platform.h:31
fd_t process_attach_ref_fd(process_t *process, io_t *file, fd_flags_t flags)
Definition process.c:171
size_t size
Definition slab.c:30
#define SLAB_AUTOINIT(name, var, type)
const char * name
Definition vfs_types.h:118
void * private_data
Definition vfs_types.h:203
dentry_t * dentry
Definition vfs_types.h:200
Definition ipc.c:30
ipc_t * client_ipc
Definition ipc_sysfs.c:20
bool server_control_file
Definition ipc_sysfs.c:18
ipc_server_t * server
Definition ipc_sysfs.c:19
Definition slab.h:45
ssize_t off_t
Definition types.h:84
s32 fd_t
Definition types.h:81
signed long ssize_t
Definition types.h:83