1 /* 2 * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU) 3 * Licensed under the Mulan PSL v2. 4 * You can use this software according to the terms and conditions of the Mulan PSL v2. 5 * You may obtain a copy of Mulan PSL v2 at: 6 * http://license.coscl.org.cn/MulanPSL2 7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR 8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR 9 * PURPOSE. 10 * See the Mulan PSL v2 for more details. 11 */ 12 13 #pragma once 14 15 #include "chcore/container/hashtable.h" 16 #include "chcore/ipc.h" 17 #include <termios.h> 18 #include <sys/uio.h> 19 20 #include "poll.h" 21 #include "fs_client_defs.h" 22 23 #define MAX_FD 1024 24 #define MIN_FD 0 25 26 #define warn(fmt, ...) printf("[WARN] " fmt, ##__VA_ARGS__) 27 /* Type of fd */ 28 enum fd_type { 29 FD_TYPE_FILE = 0, 30 FD_TYPE_PIPE, 31 FD_TYPE_SOCK, 32 FD_TYPE_STDIN, 33 FD_TYPE_STDOUT, 34 FD_TYPE_STDERR, 35 FD_TYPE_EVENT, 36 FD_TYPE_TIMER, 37 FD_TYPE_EPOLL, 38 }; 39 40 struct fd_ops { 41 ssize_t (*read)(int fd, void *buf, size_t count); 42 ssize_t (*write)(int fd, void *buf, size_t count); 43 int (*close)(int fd); 44 int (*poll)(int fd, struct pollarg *arg); 45 int (*ioctl)(int fd, unsigned long request, void *arg); 46 int (*fcntl)(int fd, int cmd, int arg); 47 }; 48 49 extern struct fd_ops epoll_ops; 50 extern struct fd_ops socket_ops; 51 extern struct fd_ops file_ops; 52 extern struct fd_ops event_op; 53 extern struct fd_ops timer_op; 54 extern struct fd_ops pipe_op; 55 extern struct fd_ops stdin_ops; 56 extern struct fd_ops stdout_ops; 57 extern struct fd_ops stderr_ops; 58 59 /* 60 * Each fd will have a fd structure `fd_desc` which can be found from 61 * the `fd_dic`. `fd_desc` structure contains the basic information of 62 * the fd. 63 */ 64 struct fd_desc { 65 /* Identification used by corresponding service */ 66 union { 67 int conn_id; 68 int fd; 69 }; 70 /* Baisc informantion of fd */ 71 int flags; /* Flags of the file */ 72 cap_t cap; /* Service's cap of fd, 0 if no service */ 73 enum fd_type type; /* Type for debug use */ 74 struct fd_ops *fd_op; 75 76 /* stored termios */ 77 struct termios termios; 78 79 /* Private data of fd */ 80 void *private_data; 81 }; 82 83 extern struct fd_desc *fd_dic[MAX_FD]; 84 85 /* fd */ 86 int alloc_fd(void); 87 int alloc_fd_since(int min); 88 void free_fd(int fd); 89 90 /* fd operation */ 91 ssize_t chcore_read(int fd, void *buf, size_t count); 92 ssize_t chcore_write(int fd, void *buf, size_t count); 93 int chcore_close(int fd); 94 int chcore_ioctl(int fd, unsigned long request, void *arg); 95 ssize_t chcore_readv(int fd, const struct iovec *iov, int iovcnt); 96 ssize_t chcore_writev(int fd, const struct iovec *iov, int iovcnt); 97 int dup_fd_content(int fd, int arg);