]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | /* |
2 | * This file is open source software, licensed to you under the terms | |
3 | * of the Apache License, Version 2.0 (the "License"). See the NOTICE file | |
4 | * distributed with this work for additional information regarding copyright | |
5 | * ownership. You may not use this file except in compliance with the License. | |
6 | * | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, | |
12 | * software distributed under the License is distributed on an | |
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | * KIND, either express or implied. See the License for the | |
15 | * specific language governing permissions and limitations | |
16 | * under the License. | |
17 | */ | |
18 | ||
19 | /* | |
20 | * Copyright (C) 2015 Cloudius Systems, Ltd. | |
21 | */ | |
22 | ||
23 | #pragma once | |
24 | ||
25 | /// \mainpage | |
26 | /// | |
27 | /// Seastar is a high performance C++ application framework for high | |
28 | /// concurrency server applications. | |
29 | /// | |
f67539c2 | 30 | /// A good place to start is the [Tutorial](tutorial.html) or [Multi-page version](split/). |
11fdf7f2 TL |
31 | /// |
32 | /// Please see: | |
33 | /// - \ref future-module Documentation on futures and promises, which are | |
34 | /// the seastar building blocks. | |
35 | /// - \ref future-util Utililty functions for working with futures | |
36 | /// - \ref memory-module Memory management | |
37 | /// - \ref networking-module TCP/IP networking | |
38 | /// - \ref fileio-module File Input/Output | |
39 | /// - \ref smp-module Multicore support | |
40 | /// - \ref fiber-module Utilities for managing loosely coupled chains of | |
41 | /// continuations, also known as fibers | |
42 | /// - \ref thread-module Support for traditional threaded execution | |
f67539c2 | 43 | /// - \ref rpc Build high-level communication protocols |
1e59de90 TL |
44 | /// - \ref websocket (experimental) Implement a WebSocket-based server |
45 | /// - \ref fsnotifier (experimental) Implement a filesystem modification notifier. | |
f67539c2 TL |
46 | /// |
47 | /// View the [Seastar compatibility statement](./md_compatibility.html) for | |
48 | /// information about library evolution. | |
11fdf7f2 TL |
49 | |
50 | #include <seastar/core/sstring.hh> | |
51 | #include <seastar/core/future.hh> | |
9f95a23c | 52 | #include <seastar/core/file-types.hh> |
1e59de90 | 53 | #include <seastar/core/posix.hh> |
9f95a23c | 54 | #include <seastar/util/bool_class.hh> |
f67539c2 | 55 | #include <seastar/util/std-compat.hh> |
f51cf556 | 56 | #include <seastar/util/modules.hh> |
9f95a23c | 57 | #include "./internal/api-level.hh" |
f51cf556 TL |
58 | #ifndef SEASTAR_MODULE |
59 | #include <cstdint> | |
60 | #include <filesystem> | |
61 | #include <optional> | |
62 | #include <string_view> | |
63 | #endif | |
11fdf7f2 TL |
64 | |
65 | namespace seastar { | |
66 | ||
f51cf556 TL |
67 | SEASTAR_MODULE_EXPORT_BEGIN |
68 | ||
11fdf7f2 TL |
69 | // iostream.hh |
70 | template <class CharType> class input_stream; | |
71 | template <class CharType> class output_stream; | |
72 | ||
f67539c2 TL |
73 | class server_socket; |
74 | class socket; | |
11fdf7f2 TL |
75 | class connected_socket; |
76 | class socket_address; | |
9f95a23c | 77 | struct listen_options; |
11fdf7f2 TL |
78 | enum class transport; |
79 | ||
80 | // file.hh | |
81 | class file; | |
9f95a23c TL |
82 | struct file_open_options; |
83 | struct stat_data; | |
11fdf7f2 | 84 | |
f67539c2 TL |
85 | namespace net { |
86 | ||
f51cf556 | 87 | using udp_channel = class datagram_channel; |
f67539c2 TL |
88 | |
89 | } | |
90 | ||
1e59de90 TL |
91 | namespace experimental { |
92 | // process.hh | |
93 | class process; | |
94 | struct spawn_parameters; | |
95 | } | |
96 | ||
11fdf7f2 TL |
97 | // Networking API |
98 | ||
99 | /// \defgroup networking-module Networking | |
100 | /// | |
101 | /// Seastar provides a simple networking API, backed by two | |
102 | /// TCP/IP stacks: the POSIX stack, utilizing the kernel's | |
103 | /// BSD socket APIs, and the native stack, implement fully | |
104 | /// within seastar and able to drive network cards directly. | |
105 | /// The native stack supports zero-copy on both transmit | |
106 | /// and receive, and is implemented using seastar's high | |
107 | /// performance, lockless sharded design. The network stack | |
108 | /// can be selected with the \c \--network-stack command-line | |
109 | /// parameter. | |
110 | ||
111 | /// \addtogroup networking-module | |
112 | /// @{ | |
113 | ||
114 | /// Listen for connections on a given port | |
115 | /// | |
116 | /// Starts listening on a given address for incoming connections. | |
117 | /// | |
118 | /// \param sa socket address to listen on | |
119 | /// | |
120 | /// \return \ref server_socket object ready to accept connections. | |
121 | /// | |
122 | /// \see listen(socket_address sa, listen_options opts) | |
123 | server_socket listen(socket_address sa); | |
124 | ||
125 | /// Listen for connections on a given port | |
126 | /// | |
127 | /// Starts listening on a given address for incoming connections. | |
128 | /// | |
129 | /// \param sa socket address to listen on | |
130 | /// \param opts options controlling the listen operation | |
131 | /// | |
132 | /// \return \ref server_socket object ready to accept connections. | |
133 | /// | |
134 | /// \see listen(socket_address sa) | |
135 | server_socket listen(socket_address sa, listen_options opts); | |
136 | ||
137 | /// Establishes a connection to a given address | |
138 | /// | |
139 | /// Attempts to connect to the given address. | |
140 | /// | |
141 | /// \param sa socket address to connect to | |
142 | /// | |
143 | /// \return a \ref connected_socket object, or an exception | |
144 | future<connected_socket> connect(socket_address sa); | |
145 | ||
146 | /// Establishes a connection to a given address | |
147 | /// | |
148 | /// Attempts to connect to the given address with a defined local endpoint | |
149 | /// | |
150 | /// \param sa socket address to connect to | |
151 | /// \param local socket address for local endpoint | |
152 | /// \param proto transport protocol (TCP or SCTP) | |
153 | /// | |
154 | /// \return a \ref connected_socket object, or an exception | |
155 | future<connected_socket> connect(socket_address sa, socket_address local, transport proto); | |
156 | ||
f67539c2 TL |
157 | |
158 | /// Creates a socket object suitable for establishing stream-oriented connections | |
159 | /// | |
160 | /// \return a \ref socket object that can be used for establishing connections | |
161 | socket make_socket(); | |
162 | ||
163 | /// Creates a udp_channel object suitable for sending UDP packets | |
164 | /// | |
165 | /// The channel is not bound to a local address, and thus can only be used | |
166 | /// for sending. | |
167 | /// | |
168 | /// \return a \ref net::udp_channel object that can be used for UDP transfers. | |
f51cf556 | 169 | [[deprecated("Use `make_unbound_datagram_channel` instead")]] |
f67539c2 TL |
170 | net::udp_channel make_udp_channel(); |
171 | ||
172 | ||
173 | /// Creates a udp_channel object suitable for sending and receiving UDP packets | |
174 | /// | |
175 | /// \param local local address to bind to | |
176 | /// | |
177 | /// \return a \ref net::udp_channel object that can be used for UDP transfers. | |
f51cf556 | 178 | [[deprecated("Use `make_bound_datagram_channel` instead")]] |
f67539c2 TL |
179 | net::udp_channel make_udp_channel(const socket_address& local); |
180 | ||
f51cf556 TL |
181 | /// Creates a datagram_channel object suitable for sending datagrams to |
182 | /// destinations that belong to the provided address family. | |
183 | /// Supported address families: AF_INET, AF_INET6 and AF_UNIX. | |
184 | /// | |
185 | /// Setting family to AF_INET or AF_INET6 creates a datagram_channel that uses | |
186 | /// UDP protocol. AF_UNIX creates a datagram_channel that uses UNIX domain | |
187 | /// sockets. | |
188 | /// | |
189 | /// The channel is not bound to a local address, and thus can only be used | |
190 | /// for sending. | |
191 | /// | |
192 | /// \param family address family in which the \ref datagram_channel will operate | |
193 | /// | |
194 | /// \return a \ref net::datagram_channel object for sending datagrams in a | |
195 | /// specified address family. | |
196 | net::datagram_channel make_unbound_datagram_channel(sa_family_t family); | |
197 | ||
198 | /// Creates a datagram_channel object suitable for sending and receiving | |
199 | /// datagrams to/from destinations that belong to the provided address family. | |
200 | /// Supported address families: AF_INET, AF_INET6 and AF_UNIX. | |
201 | /// | |
202 | /// \param local local address to bind to | |
203 | /// | |
204 | /// \return a \ref net::datagram_channel object for sending/receiving datagrams | |
205 | /// in a specified address family. | |
206 | net::datagram_channel make_bound_datagram_channel(const socket_address& local); | |
207 | ||
11fdf7f2 TL |
208 | /// @} |
209 | ||
210 | /// \defgroup fileio-module File Input/Output | |
211 | /// | |
212 | /// Seastar provides a file API to deal with persistent storage. | |
213 | /// Unlike most file APIs, seastar offers unbuffered file I/O | |
214 | /// (similar to, and based on, \c O_DIRECT). Unbuffered I/O means | |
215 | /// that the application is required to do its own caching, but | |
216 | /// delivers better performance if this caching is done correctly. | |
217 | /// | |
218 | /// For random I/O or sequential unbuffered I/O, the \ref file | |
219 | /// class provides a set of methods for reading, writing, discarding, | |
220 | /// or otherwise manipulating a file. For buffered sequential I/O, | |
221 | /// see \ref make_file_input_stream() and \ref make_file_output_stream(). | |
222 | ||
223 | /// \addtogroup fileio-module | |
224 | /// @{ | |
225 | ||
226 | /// Opens or creates a file. The "dma" in the name refers to the fact | |
227 | /// that data transfers are unbuffered and uncached. | |
228 | /// | |
229 | /// \param name the name of the file to open or create | |
230 | /// \param flags various flags controlling the open process | |
231 | /// \return a \ref file object, as a future | |
232 | /// | |
233 | /// \note | |
234 | /// The file name is not guaranteed to be stable on disk, unless the | |
235 | /// containing directory is sync'ed. | |
236 | /// | |
237 | /// \relates file | |
f67539c2 | 238 | future<file> open_file_dma(std::string_view name, open_flags flags) noexcept; |
11fdf7f2 TL |
239 | |
240 | /// Opens or creates a file. The "dma" in the name refers to the fact | |
241 | /// that data transfers are unbuffered and uncached. | |
242 | /// | |
243 | /// \param name the name of the file to open or create | |
244 | /// \param flags various flags controlling the open process | |
245 | /// \param options options for opening the file | |
246 | /// \return a \ref file object, as a future | |
247 | /// | |
248 | /// \note | |
249 | /// The file name is not guaranteed to be stable on disk, unless the | |
250 | /// containing directory is sync'ed. | |
251 | /// | |
252 | /// \relates file | |
f67539c2 | 253 | future<file> open_file_dma(std::string_view name, open_flags flags, file_open_options options) noexcept; |
11fdf7f2 TL |
254 | |
255 | /// Checks if a given directory supports direct io | |
256 | /// | |
257 | /// Seastar bypasses the Operating System caches and issues direct io to the | |
258 | /// underlying block devices. Projects using seastar should check if the directory | |
259 | /// lies in a filesystem that support such operations. This function can be used | |
260 | /// to do that. | |
261 | /// | |
262 | /// It will return if direct io can be used, or throw an std::system_error | |
263 | /// exception, with the EINVAL error code. | |
264 | /// | |
f67539c2 | 265 | /// A std::system_error with the respective error code is also thrown if \c path is |
11fdf7f2 TL |
266 | /// not a directory. |
267 | /// | |
268 | /// \param path the directory we need to verify. | |
f67539c2 | 269 | future<> check_direct_io_support(std::string_view path) noexcept; |
11fdf7f2 TL |
270 | |
271 | /// Opens a directory. | |
272 | /// | |
273 | /// \param name name of the directory to open | |
274 | /// | |
275 | /// \return a \ref file object representing a directory. The only | |
276 | /// legal operations are \ref file::list_directory(), | |
f67539c2 | 277 | /// \ref file::flush(), and \ref file::close(). |
11fdf7f2 TL |
278 | /// |
279 | /// \relates file | |
f67539c2 | 280 | future<file> open_directory(std::string_view name) noexcept; |
11fdf7f2 TL |
281 | |
282 | /// Creates a new directory. | |
283 | /// | |
284 | /// \param name name of the directory to create | |
9f95a23c | 285 | /// \param permissions optional file permissions of the directory to create. |
11fdf7f2 TL |
286 | /// |
287 | /// \note | |
288 | /// The directory is not guaranteed to be stable on disk, unless the | |
289 | /// containing directory is sync'ed. | |
f67539c2 | 290 | future<> make_directory(std::string_view name, file_permissions permissions = file_permissions::default_dir_permissions) noexcept; |
11fdf7f2 TL |
291 | |
292 | /// Ensures a directory exists | |
293 | /// | |
294 | /// Checks whether a directory exists, and if not, creates it. Only | |
295 | /// the last component of the directory name is created. | |
296 | /// | |
297 | /// \param name name of the directory to potentially create | |
9f95a23c | 298 | /// \param permissions optional file permissions of the directory to create. |
11fdf7f2 TL |
299 | /// |
300 | /// \note | |
301 | /// The directory is not guaranteed to be stable on disk, unless the | |
302 | /// containing directory is sync'ed. | |
9f95a23c | 303 | /// If the directory exists, the provided permissions are not applied. |
f67539c2 | 304 | future<> touch_directory(std::string_view name, file_permissions permissions = file_permissions::default_dir_permissions) noexcept; |
11fdf7f2 TL |
305 | |
306 | /// Recursively ensures a directory exists | |
307 | /// | |
308 | /// Checks whether each component of a directory exists, and if not, creates it. | |
309 | /// | |
310 | /// \param name name of the directory to potentially create | |
9f95a23c | 311 | /// \param permissions optional file permissions of the directory to create. |
11fdf7f2 TL |
312 | /// |
313 | /// \note | |
314 | /// This function fsyncs each component created, and is therefore guaranteed to be stable on disk. | |
9f95a23c TL |
315 | /// The provided permissions are applied only on the last component in the path, if it needs to be created, |
316 | /// if intermediate directories do not exist, they are created with the default_dir_permissions. | |
317 | /// If any directory exists, the provided permissions are not applied. | |
f67539c2 | 318 | future<> recursive_touch_directory(std::string_view name, file_permissions permissions = file_permissions::default_dir_permissions) noexcept; |
11fdf7f2 TL |
319 | |
320 | /// Synchronizes a directory to disk | |
321 | /// | |
322 | /// Makes sure the modifications in a directory are synchronized in disk. | |
323 | /// This is useful, for instance, after creating or removing a file inside the | |
324 | /// directory. | |
325 | /// | |
326 | /// \param name name of the directory to potentially create | |
f67539c2 | 327 | future<> sync_directory(std::string_view name) noexcept; |
11fdf7f2 TL |
328 | |
329 | ||
9f95a23c | 330 | /// Removes (unlinks) a file or an empty directory |
11fdf7f2 | 331 | /// |
9f95a23c | 332 | /// \param name name of the file or the directory to remove |
11fdf7f2 TL |
333 | /// |
334 | /// \note | |
335 | /// The removal is not guaranteed to be stable on disk, unless the | |
336 | /// containing directory is sync'ed. | |
f67539c2 | 337 | future<> remove_file(std::string_view name) noexcept; |
11fdf7f2 TL |
338 | |
339 | /// Renames (moves) a file. | |
340 | /// | |
341 | /// \param old_name existing file name | |
342 | /// \param new_name new file name | |
343 | /// | |
344 | /// \note | |
345 | /// The rename is not guaranteed to be stable on disk, unless the | |
346 | /// both containing directories are sync'ed. | |
f67539c2 | 347 | future<> rename_file(std::string_view old_name, std::string_view new_name) noexcept; |
11fdf7f2 | 348 | |
9f95a23c TL |
349 | struct follow_symlink_tag { }; |
350 | using follow_symlink = bool_class<follow_symlink_tag>; | |
351 | ||
352 | /// Return stat information about a file. | |
353 | /// | |
354 | /// \param name name of the file to return its stat information | |
f67539c2 | 355 | /// \param fs a follow_symlink flag to follow symbolic links. |
9f95a23c TL |
356 | /// |
357 | /// \return stat_data of the file identified by name. | |
358 | /// If name identifies a symbolic link then stat_data is returned either for the target of the link, | |
359 | /// with follow_symlink::yes, or for the link itself, with follow_symlink::no. | |
f67539c2 | 360 | future<stat_data> file_stat(std::string_view name, follow_symlink fs = follow_symlink::yes) noexcept; |
9f95a23c | 361 | |
11fdf7f2 TL |
362 | /// Return the size of a file. |
363 | /// | |
364 | /// \param name name of the file to return the size | |
9f95a23c TL |
365 | /// |
366 | /// Note that file_size of a symlink is NOT the size of the symlink - | |
367 | /// which is the length of the pathname it contains - | |
368 | /// but rather the size of the file to which it points. | |
f67539c2 | 369 | future<uint64_t> file_size(std::string_view name) noexcept; |
11fdf7f2 | 370 | |
9f95a23c TL |
371 | /// Check file access. |
372 | /// | |
373 | /// \param name name of the file to check | |
374 | /// \param flags bit pattern containing type of access to check (read/write/execute or exists). | |
375 | /// | |
376 | /// If only access_flags::exists is queried, returns true if the file exists, or false otherwise. | |
f67539c2 | 377 | /// Throws a std::filesystem::filesystem_error exception if any error other than ENOENT is encountered. |
9f95a23c TL |
378 | /// |
379 | /// If any of the access_flags (read/write/execute) is set, returns true if the file exists and is | |
380 | /// accessible with the requested flags, or false if the file exists and is not accessible | |
381 | /// as queried. | |
f67539c2 | 382 | /// Throws a std::filesystem::filesystem_error exception if any error other than EACCES is encountered. |
9f95a23c TL |
383 | /// Note that if any path component leading to the file is not searchable, the file is considered inaccessible |
384 | /// with the requested mode and false will be returned. | |
f67539c2 | 385 | future<bool> file_accessible(std::string_view name, access_flags flags) noexcept; |
9f95a23c | 386 | |
11fdf7f2 TL |
387 | /// check if a file exists. |
388 | /// | |
389 | /// \param name name of the file to check | |
f67539c2 TL |
390 | future<bool> file_exists(std::string_view name) noexcept; |
391 | ||
392 | /// Determine the type of a file (regular file, directory, etc.) | |
393 | /// | |
394 | /// \param name name of the file for which type information is requested | |
395 | /// \param follow a follow_symlink flag that determines whether a trailing symbolic link should be followed or not | |
396 | /// | |
397 | /// \return a engaged optional with the file type if lookup was successful; a disengaged optional | |
398 | /// if the file (or one of its parent directories) does not exist; an exceptional future on | |
399 | /// other errors. | |
400 | future<std::optional<directory_entry_type>> file_type(std::string_view name, follow_symlink follow = follow_symlink::yes) noexcept; | |
401 | ||
11fdf7f2 TL |
402 | |
403 | /// Creates a hard link for a file | |
404 | /// | |
405 | /// \param oldpath existing file name | |
406 | /// \param newpath name of link | |
407 | /// | |
f67539c2 | 408 | future<> link_file(std::string_view oldpath, std::string_view newpath) noexcept; |
11fdf7f2 | 409 | |
9f95a23c TL |
410 | /// Changes the permissions mode of a file or directory |
411 | /// | |
412 | /// \param name name of the file ot directory to change | |
413 | /// \param permissions permissions to set | |
414 | /// | |
f67539c2 | 415 | future<> chmod(std::string_view name, file_permissions permissions) noexcept; |
9f95a23c | 416 | |
11fdf7f2 TL |
417 | /// Return information about the filesystem where a file is located. |
418 | /// | |
419 | /// \param name name of the file to inspect | |
f67539c2 | 420 | future<fs_type> file_system_at(std::string_view name) noexcept; |
11fdf7f2 TL |
421 | |
422 | /// Return space available to unprivileged users in filesystem where a file is located, in bytes. | |
423 | /// | |
424 | /// \param name name of the file to inspect | |
f67539c2 | 425 | future<uint64_t> fs_avail(std::string_view name) noexcept; |
11fdf7f2 TL |
426 | |
427 | /// Return free space in filesystem where a file is located, in bytes. | |
428 | /// | |
429 | /// \param name name of the file to inspect | |
f67539c2 | 430 | future<uint64_t> fs_free(std::string_view name) noexcept; |
11fdf7f2 TL |
431 | /// @} |
432 | ||
1e59de90 TL |
433 | namespace experimental { |
434 | /// \defgroup interprocess-module Interprocess Communication | |
435 | /// | |
436 | /// Seastar provides a set of APIs for interprocess communicate | |
437 | ||
438 | /// \addtogroup interprocess-module | |
439 | /// @{ | |
440 | ||
441 | /// Create a pipe using \c pipe2 | |
442 | /// | |
443 | /// \return a tuple of \c file_desc, the first one for reading from the pipe, the second | |
444 | /// for writing to it. | |
445 | future<std::tuple<file_desc, file_desc>> make_pipe(); | |
446 | ||
447 | /// Spawn a subprocess | |
448 | /// | |
449 | /// \param pathname the path to the executable | |
450 | /// \param params parameters for spawning the subprocess | |
451 | /// | |
452 | /// \return a process representing the spawned subprocess | |
453 | /// \note | |
454 | /// the subprocess is spawned with \c posix_spawn() system call, so the | |
455 | /// pathname should be relative or absolute path of the executable. | |
456 | future<process> spawn_process(const std::filesystem::path& pathname, | |
457 | spawn_parameters params); | |
458 | /// Spawn a subprocess | |
459 | /// | |
460 | /// \param pathname the path to the executable | |
461 | /// | |
462 | /// \return a process representing the spawned subprocess | |
463 | /// \note | |
464 | /// the this overload does not specify a \c params parameters for spawning the | |
465 | /// subprocess. Instead, it uses the pathname for the \c argv[0] in the params. | |
466 | future<process> spawn_process(const std::filesystem::path& pathname); | |
467 | /// @} | |
468 | } | |
f51cf556 TL |
469 | |
470 | SEASTAR_MODULE_EXPORT_END | |
471 | ||
11fdf7f2 | 472 | } |