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