| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <dirent.h> |
| 5 |
#include <fcntl.h> |
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
#include "basic-forward.h" |
| 9 |
|
| 10 |
/* maximum length of fdname */ |
| 11 |
#define FDNAME_MAX 255 |
| 12 |
|
| 13 |
/* Make sure we can distinguish fd 0 and NULL */ |
| 14 |
#define FD_TO_PTR(fd) INT_TO_PTR((fd)+1) |
| 15 |
#define PTR_TO_FD(p) (PTR_TO_INT(p)-1) |
| 16 |
|
| 17 |
/* Useful helpers for initializing pipe(), socketpair() or stdio fd arrays */ |
| 18 |
#define EBADF_PAIR { -EBADF, -EBADF } |
| 19 |
#define EBADF_TRIPLET { -EBADF, -EBADF, -EBADF } |
| 20 |
|
| 21 |
/* So O_LARGEFILE is generally implied by glibc, and defined to zero hence, because we only build in LFS |
| 22 |
* mode. However, when invoking fcntl(F_GETFL) the flag is ORed into the result anyway — glibc does not mask |
| 23 |
* it away. Which sucks. Let's define the actual value here, so that we can mask it ourselves. |
| 24 |
* |
| 25 |
* The precise definition is arch specific, so we use the values defined in the kernel (note that some |
| 26 |
* are hexa and others are octal; duplicated as-is from the kernel definitions): |
| 27 |
* - alpha, arm, arm64, m68k, mips, parisc, powerpc, sparc: each has a specific value; |
| 28 |
* - others: they use the "generic" value (defined in include/uapi/asm-generic/fcntl.h) */ |
| 29 |
#if O_LARGEFILE != 0 |
| 30 |
# define RAW_O_LARGEFILE O_LARGEFILE |
| 31 |
#else |
| 32 |
# if defined(__alpha__) || defined(__arm__) || defined(__aarch64__) || defined(__m68k__) |
| 33 |
# define RAW_O_LARGEFILE 0400000 |
| 34 |
# elif defined(__mips__) |
| 35 |
# define RAW_O_LARGEFILE 0x2000 |
| 36 |
# elif defined(__parisc__) || defined(__hppa__) |
| 37 |
# define RAW_O_LARGEFILE 000004000 |
| 38 |
# elif defined(__powerpc__) |
| 39 |
# define RAW_O_LARGEFILE 0200000 |
| 40 |
# elif defined(__sparc__) |
| 41 |
# define RAW_O_LARGEFILE 0x40000 |
| 42 |
# else |
| 43 |
# define RAW_O_LARGEFILE 00100000 |
| 44 |
# endif |
| 45 |
#endif |
| 46 |
|
| 47 |
/* On musl, O_ACCMODE is defined as (03|O_SEARCH), unlike glibc which defines it as |
| 48 |
* (O_RDONLY|O_WRONLY|O_RDWR). Additionally, O_SEARCH is simply defined as O_PATH. This changes the behaviour |
| 49 |
* of O_ACCMODE in certain situations, which we don't want. This definition is copied from glibc and works |
| 50 |
* around the problems with musl's definition. */ |
| 51 |
#define O_ACCMODE_STRICT (O_RDONLY|O_WRONLY|O_RDWR) |
| 52 |
|
| 53 |
/* The default, minimum, and maximum values of /proc/sys/fs/nr_open. See kernel's fs/file.c. |
| 54 |
* These values have been unchanged since kernel-2.6.26: |
| 55 |
* https://github.com/torvalds/linux/commit/eceea0b3df05ed262ae32e0c6340cc7a3626632d */ |
| 56 |
#define NR_OPEN_DEFAULT ((unsigned) (1024 * 1024)) |
| 57 |
#define NR_OPEN_MINIMUM ((unsigned) (sizeof(long) * 8)) |
| 58 |
#define NR_OPEN_MAXIMUM ((unsigned) (CONST_MIN((size_t) INT_MAX, SIZE_MAX / __SIZEOF_POINTER__) & ~(sizeof(long) * 8 - 1))) |
| 59 |
|
| 60 |
/* A special fd that can be passed in various helpers instead of an fd indicating the root dir. Inspired by, |
| 61 |
* and an alternative to AT_FDCWD. We use specific negative value that is outside of the negative errno |
| 62 |
* range, to avoid any potential ambiguities. */ |
| 63 |
#define XAT_FDROOT -8192 |
| 64 |
assert_cc(XAT_FDROOT != AT_FDCWD); |
| 65 |
assert_cc(XAT_FDROOT < -ERRNO_MAX); |
| 66 |
|
| 67 |
/* Checks whether the specified fd is acceptable as a *at() directory fd that supports the two "wildcard" |
| 68 |
* values: it's either a regular, valid fd (i.e. >= 0), or one of the special AT_FDCWD/XAT_FDROOT values. */ |
| 69 |
static inline bool wildcard_fd_is_valid(int fd) { |
| 70 |
return fd >= 0 || IN_SET(fd, AT_FDCWD, XAT_FDROOT); |
| 71 |
} |
| 72 |
|
| 73 |
int close_nointr(int fd); |
| 74 |
int safe_close(int fd); |
| 75 |
void safe_close_pair(int p[static 2]); |
| 76 |
|
| 77 |
static inline int safe_close_above_stdio(int fd) { |
| 78 |
if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -EBADF. */ |
| 79 |
return -EBADF; |
| 80 |
|
| 81 |
return safe_close(fd); |
| 82 |
} |
| 83 |
|
| 84 |
static inline void* close_fd_ptr(void *p) { |
| 85 |
safe_close(PTR_TO_FD(p)); |
| 86 |
return NULL; |
| 87 |
} |
| 88 |
|
| 89 |
void close_many(const int fds[], size_t n_fds); |
| 90 |
void close_many_unset(int fds[], size_t n_fds); |
| 91 |
void close_many_and_free(int *fds, size_t n_fds); |
| 92 |
|
| 93 |
int fclose_nointr(FILE *f); |
| 94 |
FILE* safe_fclose(FILE *f); |
| 95 |
DIR* safe_closedir(DIR *d); |
| 96 |
|
| 97 |
static inline void closep(int *fd) { |
| 98 |
safe_close(*fd); |
| 99 |
} |
| 100 |
|
| 101 |
static inline void close_pairp(int (*p)[2]) { |
| 102 |
safe_close_pair(*p); |
| 103 |
} |
| 104 |
|
| 105 |
static inline void fclosep(FILE **f) { |
| 106 |
safe_fclose(*f); |
| 107 |
} |
| 108 |
|
| 109 |
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose, NULL); |
| 110 |
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL); |
| 111 |
|
| 112 |
#define _cleanup_close_ _cleanup_(closep) |
| 113 |
#define _cleanup_fclose_ _cleanup_(fclosep) |
| 114 |
#define _cleanup_pclose_ _cleanup_(pclosep) |
| 115 |
#define _cleanup_closedir_ _cleanup_(closedirp) |
| 116 |
#define _cleanup_close_pair_ _cleanup_(close_pairp) |
| 117 |
|
| 118 |
int fd_nonblock(int fd, bool nonblock); |
| 119 |
int stdio_disable_nonblock(void); |
| 120 |
|
| 121 |
void nonblock_resetp(int *fd); |
| 122 |
|
| 123 |
int fd_cloexec(int fd, bool cloexec); |
| 124 |
int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec); |
| 125 |
|
| 126 |
int get_max_fd(void); |
| 127 |
|
| 128 |
int close_all_fds(const int except[], size_t n_except); |
| 129 |
int close_all_fds_without_malloc(const int except[], size_t n_except); |
| 130 |
int close_all_fds_frugal(const int except[], size_t n_except); |
| 131 |
|
| 132 |
int pack_fds(int fds[], size_t n); |
| 133 |
|
| 134 |
int fd_validate(int fd); |
| 135 |
int same_fd(int a, int b); |
| 136 |
|
| 137 |
bool fdname_is_valid(const char *s); |
| 138 |
|
| 139 |
int fd_get_path(int fd, char **ret); |
| 140 |
|
| 141 |
int move_fd(int from, int to, int cloexec); |
| 142 |
|
| 143 |
int fd_move_above_stdio(int fd); |
| 144 |
|
| 145 |
int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd); |
| 146 |
|
| 147 |
static inline int make_null_stdio(void) { |
| 148 |
return rearrange_stdio(-EBADF, -EBADF, -EBADF); |
| 149 |
} |
| 150 |
|
| 151 |
/* Like TAKE_PTR() but for file descriptors, resetting them to -EBADF */ |
| 152 |
#define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF) |
| 153 |
|
| 154 |
/* Like free_and_replace(), but for file descriptors */ |
| 155 |
#define close_and_replace(a, b) \ |
| 156 |
({ \ |
| 157 |
int *_fdp_ = &(a); \ |
| 158 |
safe_close(*_fdp_); \ |
| 159 |
*_fdp_ = TAKE_FD(b); \ |
| 160 |
0; \ |
| 161 |
}) |
| 162 |
|
| 163 |
int fd_reopen(int fd, int flags); |
| 164 |
int fd_reopen_propagate_append_and_position(int fd, int flags); |
| 165 |
int fd_reopen_condition(int fd, int flags, int mask, int *ret_new_fd); |
| 166 |
|
| 167 |
int fd_is_opath(int fd); |
| 168 |
int fd_vet_accmode(int fd, int mode); |
| 169 |
int fd_is_writable(int fd); |
| 170 |
|
| 171 |
int fd_verify_safe_flags_full(int fd, int extra_flags); |
| 172 |
static inline int fd_verify_safe_flags(int fd) { |
| 173 |
return fd_verify_safe_flags_full(fd, 0); |
| 174 |
} |
| 175 |
|
| 176 |
unsigned read_nr_open(void); |
| 177 |
int fd_get_diskseq(int fd, uint64_t *ret); |
| 178 |
|
| 179 |
int path_is_root_at(int dir_fd, const char *path); |
| 180 |
static inline int path_is_root(const char *path) { |
| 181 |
return path_is_root_at(AT_FDCWD, path); |
| 182 |
} |
| 183 |
static inline int dir_fd_is_root(int dir_fd) { |
| 184 |
return dir_fd == XAT_FDROOT ? true : path_is_root_at(dir_fd, NULL); |
| 185 |
} |
| 186 |
static inline int dir_fd_is_root_or_cwd(int dir_fd) { |
| 187 |
return IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT) ? true : path_is_root_at(dir_fd, NULL); |
| 188 |
} |
| 189 |
|
| 190 |
int fds_inode_and_mount_same(int fd1, int fd2); |
| 191 |
|
| 192 |
int resolve_xat_fdroot(int *fd, const char **path, char **ret_buffer); |
| 193 |
|
| 194 |
/* The maximum length a buffer for a /proc/self/fd/<fd> path needs */ |
| 195 |
#define PROC_FD_PATH_MAX \ |
| 196 |
(STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)) |
| 197 |
|
| 198 |
char* format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd); |
| 199 |
|
| 200 |
#define FORMAT_PROC_FD_PATH(fd) \ |
| 201 |
format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd)) |
| 202 |
|
| 203 |
/* The maximum length a buffer for a /proc/<pid>/fd/<fd> path needs */ |
| 204 |
#define PROC_PID_FD_PATH_MAX \ |
| 205 |
(STRLEN("/proc//fd/") + DECIMAL_STR_MAX(pid_t) + DECIMAL_STR_MAX(int)) |
| 206 |
|
| 207 |
char* format_proc_pid_fd_path(char buf[static PROC_PID_FD_PATH_MAX], pid_t pid, int fd); |
| 208 |
|
| 209 |
/* Kinda the same as FORMAT_PROC_FD_PATH(), but goes by PID rather than "self" symlink */ |
| 210 |
#define FORMAT_PROC_PID_FD_PATH(pid, fd) \ |
| 211 |
format_proc_pid_fd_path((char[PROC_PID_FD_PATH_MAX]) {}, (pid), (fd)) |
| 212 |
|
| 213 |
int proc_fd_enoent_errno(void); |
| 214 |
|
| 215 |
const char* accmode_to_string(int flags); |
| 216 |
|
| 217 |
/* Like ASSERT_PTR, but for fds */ |
| 218 |
#define ASSERT_FD(fd) \ |
| 219 |
({ \ |
| 220 |
int _fd_ = (fd); \ |
| 221 |
assert(_fd_ >= 0); \ |
| 222 |
_fd_; \ |
| 223 |
}) |