|
| 1 | +/* |
| 2 | + * Copyright (c) Bruce ROSIER |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/posix/fcntl.h> |
| 8 | +#include <zephyr/sys/fdtable.h> |
| 9 | +#include <zephyr/fs/fs.h> |
| 10 | + |
| 11 | +static ssize_t pipe_read_vmeth(void *obj, void *buffer, size_t count); |
| 12 | +static ssize_t pipe_write_vmeth(void *obj, const void *buffer, size_t count); |
| 13 | +static int pipe_close_vmeth(void *obj); |
| 14 | +static int pipe_ioctl_vmeth(void *obj, unsigned int request, va_list args); |
| 15 | + |
| 16 | +struct pipe_desc { |
| 17 | + unsigned char __aligned(4) ring_buffer[CONFIG_POSIX_PIPE_BUF]; |
| 18 | + struct k_pipe pipe; |
| 19 | + int flags_read; |
| 20 | + int flags_write; |
| 21 | + atomic_t read_opened; |
| 22 | + atomic_t write_opened; |
| 23 | + atomic_t is_used; |
| 24 | + struct k_sem sync; |
| 25 | +}; |
| 26 | + |
| 27 | +static struct pipe_desc pipe_desc_array[CONFIG_POSIX_PIPES_MAX]; |
| 28 | + |
| 29 | +static struct fd_op_vtable fs_fd_op_vtable = { |
| 30 | + .read = pipe_read_vmeth, |
| 31 | + .write = pipe_write_vmeth, |
| 32 | + .close = pipe_close_vmeth, |
| 33 | + .ioctl = pipe_ioctl_vmeth, |
| 34 | +}; |
| 35 | + |
| 36 | +static ssize_t pipe_read_vmeth(void *obj, void *buffer, size_t count) |
| 37 | +{ |
| 38 | + ssize_t total_read = 0; |
| 39 | + int *ptr = (int *)obj; |
| 40 | + unsigned char *read_buf = (unsigned char *)buffer; |
| 41 | + int rc; |
| 42 | + |
| 43 | + struct pipe_desc *p_pipe_desc = |
| 44 | + (struct pipe_desc *)((char *)ptr - offsetof(struct pipe_desc, flags_read)); |
| 45 | + |
| 46 | + if (((*ptr & FS_O_READ) == 0) || atomic_get(&p_pipe_desc->read_opened) == 0) { |
| 47 | + errno = EACCES; |
| 48 | + return -1; |
| 49 | + } |
| 50 | + |
| 51 | + if ((*ptr & O_NONBLOCK) == O_NONBLOCK) { |
| 52 | + rc = k_pipe_get(&(p_pipe_desc->pipe), read_buf, count, &total_read, 1, K_NO_WAIT); |
| 53 | + if (rc == -EIO) { |
| 54 | + errno = EAGAIN; |
| 55 | + return -1; |
| 56 | + } |
| 57 | + |
| 58 | + } else { |
| 59 | + while (count > 0 && atomic_get(&p_pipe_desc->write_opened) == 1) { |
| 60 | + ssize_t read = 0; |
| 61 | + |
| 62 | + rc = k_pipe_get(&(p_pipe_desc->pipe), read_buf, count, &read, 1, K_NO_WAIT); |
| 63 | + |
| 64 | + if (rc != -EIO) { |
| 65 | + k_sem_give(&p_pipe_desc->sync); |
| 66 | + } |
| 67 | + |
| 68 | + if (read != count) { |
| 69 | + k_sem_take(&p_pipe_desc->sync, K_FOREVER); |
| 70 | + } |
| 71 | + |
| 72 | + read_buf += read; |
| 73 | + count -= read; |
| 74 | + total_read += read; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return total_read; |
| 79 | +} |
| 80 | + |
| 81 | +static ssize_t pipe_write_vmeth(void *obj, const void *buffer, size_t count) |
| 82 | +{ |
| 83 | + ssize_t total_written = 0; |
| 84 | + int *ptr = (int *)obj; |
| 85 | + unsigned char *write_buf = (unsigned char *)buffer; |
| 86 | + int rc; |
| 87 | + |
| 88 | + struct pipe_desc *p_pipe_desc = |
| 89 | + (struct pipe_desc *)((char *)ptr - offsetof(struct pipe_desc, flags_write)); |
| 90 | + |
| 91 | + if (((*ptr & FS_O_WRITE) == 0) || atomic_get(&p_pipe_desc->write_opened) == 0) { |
| 92 | + errno = EACCES; |
| 93 | + return -1; |
| 94 | + } |
| 95 | + |
| 96 | + if ((*ptr & O_NONBLOCK) == O_NONBLOCK) { |
| 97 | + rc = k_pipe_put(&(p_pipe_desc->pipe), write_buf, count, &total_written, 1, |
| 98 | + K_NO_WAIT); |
| 99 | + if (rc == -EIO) { |
| 100 | + errno = EAGAIN; |
| 101 | + return -1; |
| 102 | + } |
| 103 | + } else { |
| 104 | + while (count > 0 && atomic_get(&p_pipe_desc->read_opened) == 1) { |
| 105 | + ssize_t written = 0; |
| 106 | + |
| 107 | + rc = k_pipe_put(&(p_pipe_desc->pipe), write_buf, count, &written, 1, |
| 108 | + K_NO_WAIT); |
| 109 | + |
| 110 | + if (rc != -EIO) { |
| 111 | + k_sem_give(&p_pipe_desc->sync); |
| 112 | + } |
| 113 | + |
| 114 | + if (written != count) { |
| 115 | + k_sem_take(&p_pipe_desc->sync, K_FOREVER); |
| 116 | + } |
| 117 | + |
| 118 | + write_buf += written; |
| 119 | + count -= written; |
| 120 | + total_written += written; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return total_written; |
| 125 | +} |
| 126 | + |
| 127 | +static int pipe_close_vmeth(void *obj) |
| 128 | +{ |
| 129 | + int *ptr = (int *)obj; |
| 130 | + struct pipe_desc *p_pipe_desc; |
| 131 | + |
| 132 | + if ((*ptr & FS_O_WRITE)) { |
| 133 | + p_pipe_desc = |
| 134 | + (struct pipe_desc *)((char *)ptr - offsetof(struct pipe_desc, flags_write)); |
| 135 | + atomic_clear(&p_pipe_desc->write_opened); |
| 136 | + k_sem_give(&p_pipe_desc->sync); |
| 137 | + } else if ((*ptr & FS_O_READ) == FS_O_READ) { |
| 138 | + p_pipe_desc = |
| 139 | + (struct pipe_desc *)((char *)ptr - offsetof(struct pipe_desc, flags_read)); |
| 140 | + atomic_clear(&p_pipe_desc->read_opened); |
| 141 | + k_sem_give(&p_pipe_desc->sync); |
| 142 | + } else { |
| 143 | + errno = EINVAL; |
| 144 | + return -1; |
| 145 | + } |
| 146 | + |
| 147 | + if (atomic_get(&p_pipe_desc->read_opened) == 0 && |
| 148 | + atomic_get(&p_pipe_desc->write_opened) == 0) { |
| 149 | + k_pipe_flush(&p_pipe_desc->pipe); |
| 150 | + atomic_clear(&p_pipe_desc->is_used); |
| 151 | + } |
| 152 | + |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
| 156 | +static int pipe_ioctl_vmeth(void *obj, unsigned int request, va_list args) |
| 157 | +{ |
| 158 | + int *ptr = (int *)obj; |
| 159 | + struct pipe_desc *p_pipe_desc; |
| 160 | + |
| 161 | + if ((*ptr & FS_O_WRITE)) { |
| 162 | + p_pipe_desc = |
| 163 | + (struct pipe_desc *)((char *)ptr - offsetof(struct pipe_desc, flags_write)); |
| 164 | + } else if ((*ptr & FS_O_READ)) { |
| 165 | + p_pipe_desc = |
| 166 | + (struct pipe_desc *)((char *)ptr - offsetof(struct pipe_desc, flags_read)); |
| 167 | + } else { |
| 168 | + errno = EINVAL; |
| 169 | + return -1; |
| 170 | + } |
| 171 | + |
| 172 | + int fd; |
| 173 | + int flags; |
| 174 | + int ret = 0; |
| 175 | + |
| 176 | + switch (request) { |
| 177 | + case F_DUPFD: |
| 178 | + fd = va_arg(args, int); |
| 179 | + ret = zvfs_reserve_fd(); |
| 180 | + if (ret == -1) { |
| 181 | + errno = ENFILE; |
| 182 | + return -1; |
| 183 | + } |
| 184 | + zvfs_finalize_fd(ret, obj, &fs_fd_op_vtable); |
| 185 | + break; |
| 186 | + |
| 187 | + case F_GETFL: |
| 188 | + if ((*ptr & (FS_O_READ | FS_O_WRITE)) == FS_O_READ) { |
| 189 | + ret = O_RDONLY; |
| 190 | + } else if ((*ptr & (FS_O_READ | FS_O_WRITE)) == FS_O_WRITE) { |
| 191 | + ret = O_WRONLY; |
| 192 | + } else { |
| 193 | + errno = EINVAL; |
| 194 | + ret = -1; |
| 195 | + } |
| 196 | + if (*ptr & O_NONBLOCK) { |
| 197 | + ret |= O_NONBLOCK; |
| 198 | + } |
| 199 | + break; |
| 200 | + |
| 201 | + case F_SETFL: |
| 202 | + flags = va_arg(args, int); |
| 203 | + if (flags & O_NONBLOCK) { |
| 204 | + (*ptr) = (*ptr) | O_NONBLOCK; |
| 205 | + } else { |
| 206 | + *ptr &= ~O_NONBLOCK; |
| 207 | + } |
| 208 | + ret = 0; |
| 209 | + break; |
| 210 | + |
| 211 | + default: |
| 212 | + errno = EINVAL; |
| 213 | + ret = -1; |
| 214 | + break; |
| 215 | + } |
| 216 | + |
| 217 | + return ret; |
| 218 | +} |
| 219 | + |
| 220 | +int pipe(int fildes[2]) |
| 221 | +{ |
| 222 | + int fd1, fd2, i; |
| 223 | + |
| 224 | + fd1 = zvfs_reserve_fd(); |
| 225 | + if (fd1 == -1) { |
| 226 | + errno = ENFILE; |
| 227 | + return -1; |
| 228 | + } |
| 229 | + |
| 230 | + fd2 = zvfs_reserve_fd(); |
| 231 | + if (fd2 == -1) { |
| 232 | + errno = ENFILE; |
| 233 | + zvfs_free_fd(fd1); |
| 234 | + return -1; |
| 235 | + } |
| 236 | + |
| 237 | + fildes[0] = fd1; |
| 238 | + fildes[1] = fd2; |
| 239 | + |
| 240 | + for (i = 0; i < CONFIG_POSIX_PIPES_MAX; i++) { |
| 241 | + if (atomic_cas(&pipe_desc_array[i].is_used, 0, 1)) { |
| 242 | + break; |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + if (i == CONFIG_POSIX_PIPES_MAX) { |
| 247 | + errno = EMFILE; |
| 248 | + return -1; |
| 249 | + } |
| 250 | + |
| 251 | + atomic_set(&pipe_desc_array[i].read_opened, 1); |
| 252 | + atomic_set(&pipe_desc_array[i].write_opened, 1); |
| 253 | + |
| 254 | + k_pipe_init(&(pipe_desc_array[i].pipe), pipe_desc_array[i].ring_buffer, |
| 255 | + sizeof(pipe_desc_array[i].ring_buffer)); |
| 256 | + |
| 257 | + pipe_desc_array[i].flags_read = FS_O_READ; |
| 258 | + pipe_desc_array[i].flags_write = FS_O_WRITE; |
| 259 | + |
| 260 | + k_sem_init(&pipe_desc_array[i].sync, 0, 1); |
| 261 | + |
| 262 | + zvfs_finalize_fd(fd1, &(pipe_desc_array[i].flags_read), &fs_fd_op_vtable); |
| 263 | + zvfs_finalize_fd(fd2, &(pipe_desc_array[i].flags_write), &fs_fd_op_vtable); |
| 264 | + |
| 265 | + return 0; |
| 266 | +} |
0 commit comments