1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <fuchsia/io/c/fidl.h>
8 #include <lib/fdio/limits.h>
9 #include <lib/fdio/vfs.h>
10 #include <errno.h>
11 #include <stdarg.h>
12 #include <stdatomic.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <threads.h>
19 #include <zircon/types.h>
20 
21 typedef struct fdio fdio_t;
22 typedef struct fdio_namespace fdio_ns_t;
23 
24 // FDIO provides open/close/read/write io over various transports
25 // via the fdio_t interface abstraction.
26 //
27 // The PIPE protocol uses message ports as simple, no-flow-control
28 // io pipes with a maximum message size of ZX_PIPE_SIZE.
29 //
30 // The REMOTEIO protocol uses message ports to implement simple
31 // synchronous remoting of read/write/close operations.
32 //
33 // The NULL protocol absorbs writes and is never readable.
34 
35 typedef struct fdio_ops {
36     ssize_t (*read)(fdio_t* io, void* data, size_t len);
37     ssize_t (*read_at)(fdio_t* io, void* data, size_t len, off_t offset);
38     ssize_t (*write)(fdio_t* io, const void* data, size_t len);
39     ssize_t (*write_at)(fdio_t* io, const void* data, size_t len, off_t offset);
40     off_t (*seek)(fdio_t* io, off_t offset, int whence);
41     zx_status_t (*misc)(fdio_t* io, uint32_t op, int64_t off, uint32_t maxreply,
42                         void* data, size_t len);
43     zx_status_t (*close)(fdio_t* io);
44     zx_status_t (*open)(fdio_t* io, const char* path, uint32_t flags,
45                         uint32_t mode, fdio_t** out);
46     zx_status_t (*clone)(fdio_t* io, zx_handle_t* out_handles, uint32_t* out_types);
47     zx_status_t (*unwrap)(fdio_t* io, zx_handle_t* out_handles, uint32_t* out_types);
48     void (*wait_begin)(fdio_t* io, uint32_t events, zx_handle_t* handle,
49                        zx_signals_t* signals);
50     void (*wait_end)(fdio_t* io, zx_signals_t signals, uint32_t* events);
51     ssize_t (*ioctl)(fdio_t* io, uint32_t op, const void* in_buf, size_t in_len,
52                      void* out_buf, size_t out_len);
53     ssize_t (*posix_ioctl)(fdio_t* io, int req, va_list va);
54     zx_status_t (*get_vmo)(fdio_t* io, int flags, zx_handle_t* out);
55     zx_status_t (*get_token)(fdio_t* io, zx_handle_t* out);
56     zx_status_t (*get_attr)(fdio_t* io, fuchsia_io_NodeAttributes* out);
57     zx_status_t (*set_attr)(fdio_t* io, uint32_t flags, const fuchsia_io_NodeAttributes* attr);
58     zx_status_t (*sync)(fdio_t* io);
59     zx_status_t (*readdir)(fdio_t* io, void* ptr, size_t max, size_t* actual);
60     zx_status_t (*rewind)(fdio_t* io);
61     zx_status_t (*unlink)(fdio_t* io, const char* path, size_t len);
62     zx_status_t (*truncate)(fdio_t* io, off_t off);
63     zx_status_t (*rename)(fdio_t* io, const char* src, size_t srclen,
64                           zx_handle_t dst_token, const char* dst, size_t dstlen);
65     zx_status_t (*link)(fdio_t* io, const char* src, size_t srclen,
66                         zx_handle_t dst_token, const char* dst, size_t dstlen);
67     zx_status_t (*get_flags)(fdio_t* io, uint32_t* out_flags);
68     zx_status_t (*set_flags)(fdio_t* io, uint32_t flags);
69     ssize_t (*recvfrom)(fdio_t* io, void* data, size_t len, int flags,
70                         struct sockaddr* restrict addr, socklen_t* restrict addrlen);
71     ssize_t (*sendto)(fdio_t* io, const void* data, size_t len, int flags,
72                       const struct sockaddr* addr, socklen_t addrlen);
73     ssize_t (*recvmsg)(fdio_t* io, struct msghdr* msg, int flags);
74     ssize_t (*sendmsg)(fdio_t* io, const struct msghdr* msg, int flags);
75     zx_status_t (*shutdown)(fdio_t* io, int how);
76 } fdio_ops_t;
77 
78 // fdio_t ioflag values
79 #define IOFLAG_CLOEXEC              (1 << 0)
80 #define IOFLAG_SOCKET               (1 << 1)
81 #define IOFLAG_EPOLL                (1 << 2)
82 #define IOFLAG_WAITABLE             (1 << 3)
83 #define IOFLAG_SOCKET_CONNECTING    (1 << 4)
84 #define IOFLAG_SOCKET_CONNECTED     (1 << 5)
85 #define IOFLAG_NONBLOCK             (1 << 6)
86 
87 // The subset of fdio_t per-fd flags queryable via fcntl.
88 // Static assertions in unistd.c ensure we aren't colliding.
89 #define IOFLAG_FD_FLAGS IOFLAG_CLOEXEC
90 
91 typedef struct fdio {
92     fdio_ops_t* ops;
93     uint32_t magic;
94     atomic_int_fast32_t refcount;
95     int32_t dupcount;
96     uint32_t ioflag;
97 } fdio_t;
98 
99 // Lifecycle notes:
100 //
101 // Upon creation, fdio objects have a refcount of 1.
102 // fdio_acquire() and fdio_release() are used to upref
103 // and downref, respectively.  Upon downref to 0,
104 // fdio_free() is called, which poisons the object and
105 // free()s it.
106 //
107 // The close hook must be called before free and should
108 // only be called once.  In normal use, fdio objects are
109 // accessed through the fdio_fdtab, and when close is
110 // called they are removed from the fdtab and the reference
111 // that the fdtab itself is holding is released, at which
112 // point they will be free()'d unless somebody is holding
113 // a ref due to an ongoing io transaction, which will
114 // certainly fail doe to underlying handles being closed
115 // at which point a downref will happen and destruction
116 // will follow.
117 //
118 // dupcount tracks how many fdtab entries an fdio object
119 // is in.  close() reduces the dupcount, and only actually
120 // closes the underlying object when it reaches zero.
121 
122 #define FDIO_MAGIC 0x4f49584d // FDIO
123 
124 zx_status_t fdio_close(fdio_t* io);
125 zx_status_t fdio_wait(fdio_t* io, uint32_t events, zx_time_t deadline,
126                       uint32_t* out_pending);
127 
128 // Creates a pipe backed by a socket.
129 //
130 // Takes ownership of |socket|.
131 fdio_t* fdio_pipe_create(zx_handle_t socket);
132 
133 // Wraps a socket with an fdio_t using socketpair io.
134 fdio_t* fdio_socketpair_create(zx_handle_t h);
135 
136 // Creates an |fdio_t| for a VMO file.
137 //
138 // * |control| is an handle to the control channel for the VMO file.
139 // * |vmo| is the VMO that contains the contents of the file.
140 // * |offset| is the index of the first byte of the file in the VMO.
141 // * |length| is the number of bytes in the file.
142 // * |seek| is the initial seek offset within the file (i.e., relative to
143 //   |offset| within the underlying VMO).
144 //
145 // Always consumes |h| and |vmo|.
146 fdio_t* fdio_vmofile_create(zx_handle_t control, zx_handle_t vmo,
147                             zx_off_t offset, zx_off_t length, zx_off_t seek);
148 
149 // Creates an |fdio_t| from a Zircon socket object.
150 //
151 // Examines |socket| and determines whether to create a pipe, stream socket, or
152 // datagram socket.
153 //
154 // Always consumes |socket|.
155 zx_status_t fdio_from_socket(zx_handle_t socket, fdio_t** out_io);
156 
157 // Creates an |fdio_t| from a Zircon channel object.
158 //
159 // The |channel| must implement the |fuchsia.io.Node| protocol. Uses the
160 // |Describe| method from the |fuchsia.io.Node| protocol to determine whether to
161 // create a remoteio or a vmofile.
162 //
163 // Always consumes |channel|.
164 zx_status_t fdio_from_channel(zx_handle_t channel, fdio_t** out_io);
165 
166 // Wraps a socket with an fdio_t using socket io.
167 fdio_t* fdio_socket_create_stream(zx_handle_t s, int flags);
168 fdio_t* fdio_socket_create_datagram(zx_handle_t s, int flags);
169 
170 // creates a message port and pair of simple io fdio_t's
171 int fdio_pipe_pair(fdio_t** a, fdio_t** b);
172 
173 void fdio_free(fdio_t* io);
174 
175 fdio_t* fdio_ns_open_root(fdio_ns_t* ns);
176 
177 // io will be consumed by this and must not be shared
178 void fdio_chdir(fdio_t* io, const char* path);
179 
180 // Wraps an arbitrary handle with a fdio_t that works with wait hooks.
181 // Takes ownership of handle unless shared_handle is true.
182 fdio_t* fdio_waitable_create(zx_handle_t h, zx_signals_t signals_in,
183                              zx_signals_t signals_out, bool shared_handle);
184 
185 // unsupported / do-nothing hooks shared by implementations
186 zx_status_t fdio_default_get_token(fdio_t* io, zx_handle_t* out);
187 zx_status_t fdio_default_set_attr(fdio_t* io, uint32_t flags, const fuchsia_io_NodeAttributes* attr);
188 zx_status_t fdio_default_sync(fdio_t* io);
189 zx_status_t fdio_default_readdir(fdio_t* io, void* ptr, size_t max, size_t* actual);
190 zx_status_t fdio_default_rewind(fdio_t* io);
191 zx_status_t fdio_default_unlink(fdio_t* io, const char* path, size_t len);
192 zx_status_t fdio_default_truncate(fdio_t* io, off_t off);
193 zx_status_t fdio_default_rename(fdio_t* io, const char* src, size_t srclen,
194                                 zx_handle_t dst_token, const char* dst, size_t dstlen);
195 zx_status_t fdio_default_link(fdio_t* io, const char* src, size_t srclen,
196                               zx_handle_t dst_token, const char* dst, size_t dstlen);
197 zx_status_t fdio_default_get_flags(fdio_t* io, uint32_t* out_flags);
198 zx_status_t fdio_default_set_flags(fdio_t* io, uint32_t flags);
199 ssize_t fdio_default_read(fdio_t* io, void* _data, size_t len);
200 ssize_t fdio_default_read_at(fdio_t* io, void* _data, size_t len, off_t offset);
201 ssize_t fdio_default_write(fdio_t* io, const void* _data, size_t len);
202 ssize_t fdio_default_write_at(fdio_t* io, const void* _data, size_t len, off_t offset);
203 ssize_t fdio_default_recvfrom(fdio_t* io, void* _data, size_t len, int flags,
204                               struct sockaddr* restrict addr,
205                               socklen_t* restrict addrlen);
206 ssize_t fdio_default_sendto(fdio_t* io, const void* _data, size_t len,
207                             int flags, const struct sockaddr* addr,
208                             socklen_t addrlen);
209 ssize_t fdio_default_recvmsg(fdio_t* io, struct msghdr* msg, int flags);
210 ssize_t fdio_default_sendmsg(fdio_t* io, const struct msghdr* msg, int flags);
211 off_t fdio_default_seek(fdio_t* io, off_t offset, int whence);
212 zx_status_t fdio_default_get_attr(fdio_t* io, fuchsia_io_NodeAttributes* out);
213 zx_status_t fdio_default_misc(fdio_t* io, uint32_t op, int64_t off,
214                               uint32_t arg, void* data, size_t len);
215 zx_status_t fdio_default_close(fdio_t* io);
216 zx_status_t fdio_default_open(fdio_t* io, const char* path, uint32_t flags,
217                               uint32_t mode, fdio_t** out);
218 zx_status_t fdio_default_clone(fdio_t* io, zx_handle_t* handles, uint32_t* types);
219 ssize_t fdio_default_ioctl(fdio_t* io, uint32_t op, const void* in_buf,
220                            size_t in_len, void* out_buf, size_t out_len);
221 void fdio_default_wait_begin(fdio_t* io, uint32_t events, zx_handle_t* handle,
222                              zx_signals_t* _signals);
223 void fdio_default_wait_end(fdio_t* io, zx_signals_t signals, uint32_t* _events);
224 zx_status_t fdio_default_unwrap(fdio_t* io, zx_handle_t* handles, uint32_t* types);
225 zx_status_t fdio_default_shutdown(fdio_t* io, int how);
226 ssize_t fdio_default_posix_ioctl(fdio_t* io, int req, va_list va);
227 zx_status_t fdio_default_get_vmo(fdio_t* io, int flags, zx_handle_t* out);
228 
229 typedef struct {
230     mtx_t lock;
231     mtx_t cwd_lock;
232     bool init;
233     mode_t umask;
234     fdio_t* root;
235     fdio_t* cwd;
236     // fdtab contains either NULL, or a reference to fdio_reserved_io, or a
237     // valid fdio_t pointer. fdio_reserved_io must never be returned for
238     // operations.
239     fdio_t* fdtab[FDIO_MAX_FD];
240     fdio_ns_t* ns;
241     char cwd_path[PATH_MAX];
242 } fdio_state_t;
243 
244 extern fdio_state_t __fdio_global_state;
245 
246 #define fdio_lock (__fdio_global_state.lock)
247 #define fdio_root_handle (__fdio_global_state.root)
248 #define fdio_cwd_handle (__fdio_global_state.cwd)
249 #define fdio_cwd_lock (__fdio_global_state.cwd_lock)
250 #define fdio_cwd_path (__fdio_global_state.cwd_path)
251 #define fdio_fdtab (__fdio_global_state.fdtab)
252 #define fdio_root_init (__fdio_global_state.init)
253 #define fdio_root_ns (__fdio_global_state.ns)
254 
255 
256 // Enable low level debug chatter, which requires a kernel that
257 // doesn't check the resource argument to zx_debuglog_create()
258 //
259 // The value is the default debug level (0 = none)
260 // The environment variable FDIODEBUG will override this on fdio init
261 //
262 // #define FDIO_LLDEBUG 1
263 
264 #ifdef FDIO_LLDEBUG
265 void fdio_lldebug_printf(unsigned level, const char* fmt, ...);
266 #define LOG(level, fmt...) fdio_lldebug_printf(level, fmt)
267 #else
268 #define LOG(level, fmt...) do {} while (0)
269 #endif
270 
271 void fdio_set_debug_level(unsigned level);
272 
273 // Enable intrusive allocation debugging
274 //
275 //#define FDIO_ALLOCDEBUG
276 
fdio_acquire(fdio_t * io)277 static inline void fdio_acquire(fdio_t* io) {
278     LOG(6, "fdio: acquire: %p\n", io);
279     atomic_fetch_add(&io->refcount, 1);
280 }
281 
fdio_release(fdio_t * io)282 static inline void fdio_release(fdio_t* io) {
283     LOG(6, "fdio: release: %p\n", io);
284     if (atomic_fetch_sub(&io->refcount, 1) == 1) {
285         fdio_free(io);
286     }
287 }
288 
289 #ifdef FDIO_ALLOCDEBUG
290 void* fdio_alloc(size_t sz);
291 #else
fdio_alloc(size_t sz)292 static inline void* fdio_alloc(size_t sz) {
293     void* ptr = calloc(1, sz);
294     LOG(5, "fdio: io: alloc: %p\n", ptr);
295     return ptr;
296 }
297 #endif
298 
299 // Returns an fd number greater than or equal to |starting_fd|, following the
300 // same rules as fdio_bind_fd. If there are no free file descriptors, -1 is
301 // returned and |errno| is set to EMFILE. The returned |fd| is bound to
302 // fdio_reserved_io that has no ops table, and must not be consumed outside of
303 // fdio, nor allowed to be used for operations.
304 int fdio_reserve_fd(int starting_fd);
305 
306 // Assign the given |io| to the reserved |fd|. If |fd| is not reserved, then -1
307 // is returned and errno is set to EINVAL.
308 int fdio_assign_reserved(int fd, fdio_t *io);
309 
310 // Unassign the reservation at |fd|. If |fd| does not resolve to a reservation
311 // then -1 is returned and errno is set to EINVAL, otherwise |fd| is returned.
312 int fdio_release_reserved(int fd);