xref: /haiku/headers/private/kernel/fs/fd.h (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 /*
2  * Copyright 2002-2005, 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 select_sync;
20 
21 struct fd_ops {
22 	status_t	(*fd_read)(struct file_descriptor *, off_t pos, void *buffer, size_t *length);
23 	status_t	(*fd_write)(struct file_descriptor *, off_t pos, const void *buffer, size_t *length);
24 	off_t		(*fd_seek)(struct file_descriptor *, off_t pos, int seekType);
25 	status_t	(*fd_ioctl)(struct file_descriptor *, ulong op, void *buffer, size_t length);
26 	status_t	(*fd_select)(struct file_descriptor *, uint8 event, uint32 ref, struct select_sync *sync);
27 	status_t	(*fd_deselect)(struct file_descriptor *, uint8 event, struct select_sync *sync);
28 	status_t	(*fd_read_dir)(struct file_descriptor *, struct dirent *buffer, size_t bufferSize, uint32 *_count);
29 	status_t	(*fd_rewind_dir)(struct file_descriptor *);
30 	status_t	(*fd_read_stat)(struct file_descriptor *, struct stat *);
31 	status_t	(*fd_write_stat)(struct file_descriptor *, const struct stat *, int statMask);
32 	status_t	(*fd_close)(struct file_descriptor *);
33 	void		(*fd_free)(struct file_descriptor *);
34 };
35 
36 struct file_descriptor {
37 	int32	type;               /* descriptor type */
38 	int32	ref_count;
39 	int32	open_count;
40 	struct fd_ops *ops;
41 	union {
42 		struct vnode *vnode;
43 		struct fs_mount *mount;
44 	} u;
45 	void	*cookie;
46 	int32	open_mode;
47 	off_t	pos;
48 };
49 
50 
51 /* Types of file descriptors we can create */
52 
53 enum fd_types {
54 	FDTYPE_FILE	= 1,
55 	FDTYPE_ATTR,
56 	FDTYPE_DIR,
57 	FDTYPE_ATTR_DIR,
58 	FDTYPE_INDEX,
59 	FDTYPE_INDEX_DIR,
60 	FDTYPE_QUERY,
61 	FDTYPE_SOCKET
62 };
63 
64 
65 /* Prototypes */
66 
67 extern struct file_descriptor *alloc_fd(void);
68 extern int new_fd_etc(struct io_context *, struct file_descriptor *, int firstIndex);
69 extern int new_fd(struct io_context *, struct file_descriptor *);
70 extern struct file_descriptor *get_fd(struct io_context *, int);
71 extern void close_fd(struct file_descriptor *descriptor);
72 extern void put_fd(struct file_descriptor *descriptor);
73 extern status_t select_fd(int fd, uint8 event, uint32 ref, struct select_sync *sync, bool kernel);
74 extern status_t deselect_fd(int fd, uint8 event, struct select_sync *sync, bool kernel);
75 extern bool fd_is_valid(int fd, bool kernel);
76 
77 extern bool fd_close_on_exec(struct io_context *context, int fd);
78 extern void fd_set_close_on_exec(struct io_context *context, int fd, bool closeFD);
79 
80 static struct io_context *get_current_io_context(bool kernel);
81 
82 /* The prototypes of the (sys|user)_ functions are currently defined in vfs.h */
83 
84 
85 /* Inlines */
86 
87 static inline struct io_context *
88 get_current_io_context(bool kernel)
89 {
90 	if (kernel)
91 		return (struct io_context *)team_get_kernel_team()->io_context;
92 
93 	return (struct io_context *)thread_get_current_thread()->team->io_context;
94 }
95 
96 #ifdef __cplusplus
97 }
98 #endif
99 
100 #endif /* _FD_H */
101