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 15extern "C" { 16#endif 17 18struct event_queue; 19struct file_descriptor; 20struct io_context; 21struct net_socket; 22struct selectsync; 23struct select_info; 24 25struct 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 49struct 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 68enum 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 85extern struct file_descriptor *alloc_fd(void); 86extern int new_fd_etc(struct io_context *, struct file_descriptor *, 87 int firstIndex); 88extern int new_fd(struct io_context *, struct file_descriptor *); 89extern struct file_descriptor *get_fd(struct io_context *, int); 90extern struct file_descriptor *get_open_fd(struct io_context *, int); 91extern void close_fd(struct io_context *context, 92 struct file_descriptor *descriptor); 93extern status_t close_fd_index(struct io_context *context, int fd); 94extern void put_fd(struct file_descriptor *descriptor); 95extern void disconnect_fd(struct file_descriptor *descriptor); 96extern void inc_fd_ref_count(struct file_descriptor *descriptor); 97extern int dup_foreign_fd(team_id fromTeam, int fd, bool kernel); 98extern status_t select_fd(int32 fd, struct select_info *info, bool kernel); 99extern status_t deselect_fd(int32 fd, struct select_info *info, bool kernel); 100extern bool fd_is_valid(int fd, bool kernel); 101extern struct vnode *fd_vnode(struct file_descriptor *descriptor); 102 103extern bool fd_close_on_exec(struct io_context *context, int fd); 104extern void fd_set_close_on_exec(struct io_context *context, int fd, 105 bool closeFD); 106 107static struct io_context *get_current_io_context(bool kernel); 108 109extern 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 117static inline struct io_context * 118get_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