1 /* 2 * Copyright 2002-2018, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _FD_H 6 #define _FD_H 7 8 9 #include <vfs.h> 10 #include <team.h> 11 #include <thread.h> 12 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 struct event_queue; 19 struct file_descriptor; 20 struct io_context; 21 struct net_socket; 22 struct selectsync; 23 struct select_info; 24 25 struct fd_ops { 26 status_t (*fd_read)(struct file_descriptor *, off_t pos, void *buffer, 27 size_t *length); 28 status_t (*fd_write)(struct file_descriptor *, off_t pos, 29 const void *buffer, size_t *length); 30 off_t (*fd_seek)(struct file_descriptor *, off_t pos, int seekType); 31 status_t (*fd_ioctl)(struct file_descriptor *, ulong op, void *buffer, 32 size_t length); 33 status_t (*fd_set_flags)(struct file_descriptor *, int flags); 34 status_t (*fd_select)(struct file_descriptor *, uint8 event, 35 struct selectsync *sync); 36 status_t (*fd_deselect)(struct file_descriptor *, uint8 event, 37 struct selectsync *sync); 38 status_t (*fd_read_dir)(struct io_context* ioContext, 39 struct file_descriptor *, struct dirent *buffer, 40 size_t bufferSize, uint32 *_count); 41 status_t (*fd_rewind_dir)(struct file_descriptor *); 42 status_t (*fd_read_stat)(struct file_descriptor *, struct stat *); 43 status_t (*fd_write_stat)(struct file_descriptor *, const struct stat *, 44 int statMask); 45 status_t (*fd_close)(struct file_descriptor *); 46 void (*fd_free)(struct file_descriptor *); 47 }; 48 49 struct file_descriptor { 50 int32 type; /* descriptor type */ 51 int32 ref_count; 52 int32 open_count; 53 struct fd_ops *ops; 54 union { 55 struct vnode *vnode; 56 struct fs_mount *mount; 57 struct net_socket *socket; 58 struct event_queue *queue; 59 } u; 60 void *cookie; 61 int32 open_mode; 62 off_t pos; 63 }; 64 65 66 /* Types of file descriptors we can create */ 67 68 enum fd_types { 69 FDTYPE_FILE = 1, 70 FDTYPE_ATTR, 71 FDTYPE_DIR, 72 FDTYPE_ATTR_DIR, 73 FDTYPE_INDEX, 74 FDTYPE_INDEX_DIR, 75 FDTYPE_QUERY, 76 FDTYPE_SOCKET, 77 FDTYPE_EVENT_QUEUE 78 }; 79 80 // additional open mode - kernel special 81 #define O_DISCONNECTED 0x80000000 82 83 /* Prototypes */ 84 85 extern struct file_descriptor *alloc_fd(void); 86 extern int new_fd_etc(struct io_context *, struct file_descriptor *, 87 int firstIndex); 88 extern int new_fd(struct io_context *, struct file_descriptor *); 89 extern struct file_descriptor *get_fd(struct io_context *, int); 90 extern struct file_descriptor *get_open_fd(struct io_context *, int); 91 extern void close_fd(struct io_context *context, 92 struct file_descriptor *descriptor); 93 extern status_t close_fd_index(struct io_context *context, int fd); 94 extern void put_fd(struct file_descriptor *descriptor); 95 extern void disconnect_fd(struct file_descriptor *descriptor); 96 extern void inc_fd_ref_count(struct file_descriptor *descriptor); 97 extern int dup_foreign_fd(team_id fromTeam, int fd, bool kernel); 98 extern status_t select_fd(int32 fd, struct select_info *info, bool kernel); 99 extern status_t deselect_fd(int32 fd, struct select_info *info, bool kernel); 100 extern bool fd_is_valid(int fd, bool kernel); 101 extern struct vnode *fd_vnode(struct file_descriptor *descriptor); 102 103 extern bool fd_close_on_exec(struct io_context *context, int fd); 104 extern void fd_set_close_on_exec(struct io_context *context, int fd, 105 bool closeFD); 106 107 static struct io_context *get_current_io_context(bool kernel); 108 109 extern status_t user_fd_kernel_ioctl(int fd, ulong op, void *buffer, 110 size_t length); 111 112 /* The prototypes of the (sys|user)_ functions are currently defined in vfs.h */ 113 114 115 /* Inlines */ 116 117 static inline struct io_context * 118 get_current_io_context(bool kernel) 119 { 120 if (kernel) 121 return (struct io_context *)team_get_kernel_team()->io_context; 122 123 return (struct io_context *)thread_get_current_thread()->team->io_context; 124 } 125 126 #ifdef __cplusplus 127 } 128 #endif 129 130 #endif /* _FD_H */ 131