| |
| |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <sys/resource.h> |
| #include <sys/socket.h> |
| #include <sys/stat.h> |
| #include <unistd.h> |
| |
| #include "alloc-util.h" |
| #include "copy.h" |
| #include "dirent-util.h" |
| #include "fd-util.h" |
| #include "fileio.h" |
| #include "fs-util.h" |
| #include "io-util.h" |
| #include "macro.h" |
| #include "memfd-util.h" |
| #include "missing.h" |
| #include "parse-util.h" |
| #include "path-util.h" |
| #include "process-util.h" |
| #include "socket-util.h" |
| #include "stdio-util.h" |
| #include "util.h" |
| |
| int close_nointr(int fd) { |
| assert(fd >= 0); |
| |
| if (close(fd) >= 0) |
| return 0; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (errno == EINTR) |
| return 0; |
| |
| return -errno; |
| } |
| |
| int safe_close(int fd) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (fd >= 0) { |
| PROTECT_ERRNO; |
| |
| |
| |
| |
| |
| |
| assert_se(close_nointr(fd) != -EBADF); |
| } |
| |
| return -1; |
| } |
| |
| void safe_close_pair(int p[]) { |
| assert(p); |
| |
| if (p[0] == p[1]) { |
| |
| |
| p[0] = p[1] = safe_close(p[0]); |
| return; |
| } |
| |
| p[0] = safe_close(p[0]); |
| p[1] = safe_close(p[1]); |
| } |
| |
| void close_many(const int fds[], size_t n_fd) { |
| size_t i; |
| |
| assert(fds || n_fd <= 0); |
| |
| for (i = 0; i < n_fd; i++) |
| safe_close(fds[i]); |
| } |
| |
| int fclose_nointr(FILE *f) { |
| assert(f); |
| |
| |
| |
| if (fclose(f) == 0) |
| return 0; |
| |
| if (errno == EINTR) |
| return 0; |
| |
| return -errno; |
| } |
| |
| FILE* safe_fclose(FILE *f) { |
| |
| |
| |
| if (f) { |
| PROTECT_ERRNO; |
| |
| assert_se(fclose_nointr(f) != EBADF); |
| } |
| |
| return NULL; |
| } |
| |
| DIR* safe_closedir(DIR *d) { |
| |
| if (d) { |
| PROTECT_ERRNO; |
| |
| assert_se(closedir(d) >= 0 || errno != EBADF); |
| } |
| |
| return NULL; |
| } |
| |
| int fd_nonblock(int fd, bool nonblock) { |
| int flags, nflags; |
| |
| assert(fd >= 0); |
| |
| flags = fcntl(fd, F_GETFL, 0); |
| if (flags < 0) |
| return -errno; |
| |
| if (nonblock) |
| nflags = flags | O_NONBLOCK; |
| else |
| nflags = flags & ~O_NONBLOCK; |
| |
| if (nflags == flags) |
| return 0; |
| |
| if (fcntl(fd, F_SETFL, nflags) < 0) |
| return -errno; |
| |
| return 0; |
| } |
| |
| int fd_cloexec(int fd, bool cloexec) { |
| int flags, nflags; |
| |
| assert(fd >= 0); |
| |
| flags = fcntl(fd, F_GETFD, 0); |
| if (flags < 0) |
| return -errno; |
| |
| if (cloexec) |
| nflags = flags | FD_CLOEXEC; |
| else |
| nflags = flags & ~FD_CLOEXEC; |
| |
| if (nflags == flags) |
| return 0; |
| |
| if (fcntl(fd, F_SETFD, nflags) < 0) |
| return -errno; |
| |
| return 0; |
| } |
| |
| _pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) { |
| size_t i; |
| |
| assert(n_fdset == 0 || fdset); |
| |
| for (i = 0; i < n_fdset; i++) |
| if (fdset[i] == fd) |
| return true; |
| |
| return false; |
| } |
| |
| int close_all_fds(const int except[], size_t n_except) { |
| _cleanup_closedir_ DIR *d = NULL; |
| struct dirent *de; |
| int r = 0; |
| |
| assert(n_except == 0 || except); |
| |
| d = opendir("/proc/self/fd"); |
| if (!d) { |
| struct rlimit rl; |
| int fd, max_fd; |
| |
| |
| |
| |
| assert_se(getrlimit(RLIMIT_NOFILE, &rl) >= 0); |
| |
| if (rl.rlim_max == 0) |
| return -EINVAL; |
| |
| |
| |
| max_fd = (rl.rlim_max == RLIM_INFINITY || rl.rlim_max > INT_MAX) ? INT_MAX : (int) (rl.rlim_max - 1); |
| |
| for (fd = 3; fd >= 0; fd = fd < max_fd ? fd + 1 : -1) { |
| int q; |
| |
| if (fd_in_set(fd, except, n_except)) |
| continue; |
| |
| q = close_nointr(fd); |
| if (q < 0 && q != -EBADF && r >= 0) |
| r = q; |
| } |
| |
| return r; |
| } |
| |
| FOREACH_DIRENT(de, d, return -errno) { |
| int fd = -1, q; |
| |
| if (safe_atoi(de->d_name, &fd) < 0) |
| |
| continue; |
| |
| if (fd < 3) |
| continue; |
| |
| if (fd == dirfd(d)) |
| continue; |
| |
| if (fd_in_set(fd, except, n_except)) |
| continue; |
| |
| q = close_nointr(fd); |
| if (q < 0 && q != -EBADF && r >= 0) |
| r = q; |
| } |
| |
| return r; |
| } |
| |
| int same_fd(int a, int b) { |
| struct stat sta, stb; |
| pid_t pid; |
| int r, fa, fb; |
| |
| assert(a >= 0); |
| assert(b >= 0); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (a == b) |
| return true; |
| |
| |
| pid = getpid_cached(); |
| r = kcmp(pid, pid, KCMP_FILE, a, b); |
| if (r == 0) |
| return true; |
| if (r > 0) |
| return false; |
| if (errno != ENOSYS) |
| return -errno; |
| |
| |
| if (fstat(a, &sta) < 0) |
| return -errno; |
| |
| if (fstat(b, &stb) < 0) |
| return -errno; |
| |
| if ((sta.st_mode & S_IFMT) != (stb.st_mode & S_IFMT)) |
| return false; |
| |
| |
| |
| |
| |
| if (S_ISCHR(sta.st_mode) || S_ISBLK(sta.st_mode)) |
| return false; |
| |
| if (sta.st_dev != stb.st_dev || sta.st_ino != stb.st_ino) |
| return false; |
| |
| |
| |
| |
| |
| fa = fcntl(a, F_GETFL); |
| if (fa < 0) |
| return -errno; |
| |
| fb = fcntl(b, F_GETFL); |
| if (fb < 0) |
| return -errno; |
| |
| return fa == fb; |
| } |
| |
| void cmsg_close_all(struct msghdr *mh) { |
| struct cmsghdr *cmsg; |
| |
| assert(mh); |
| |
| CMSG_FOREACH(cmsg, mh) |
| if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) |
| close_many((int*) CMSG_DATA(cmsg), (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int)); |
| } |
| |
| bool fdname_is_valid(const char *s) { |
| const char *p; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (!s) |
| return false; |
| |
| for (p = s; *p; p++) { |
| if (*p < ' ') |
| return false; |
| if (*p >= 127) |
| return false; |
| if (*p == ':') |
| return false; |
| } |
| |
| return p - s < 256; |
| } |
| |
| int fd_get_path(int fd, char **ret) { |
| _cleanup_close_ int dir = -1; |
| char fdname[DECIMAL_STR_MAX(int)]; |
| int r; |
| |
| dir = open("/proc/self/fd/", O_CLOEXEC | O_DIRECTORY | O_PATH); |
| if (dir < 0) |
| |
| |
| return errno == ENOENT ? -EOPNOTSUPP : -errno; |
| |
| xsprintf(fdname, "%i", fd); |
| |
| r = readlinkat_malloc(dir, fdname, ret); |
| if (r == -ENOENT) |
| |
| return -EBADF; |
| |
| return r; |
| } |
| |
| int move_fd(int from, int to, int cloexec) { |
| int r; |
| |
| |
| |
| |
| |
| if (from < 0) |
| return -EBADF; |
| if (to < 0) |
| return -EBADF; |
| |
| if (from == to) { |
| |
| if (cloexec >= 0) { |
| r = fd_cloexec(to, cloexec); |
| if (r < 0) |
| return r; |
| } |
| |
| return to; |
| } |
| |
| if (cloexec < 0) { |
| int fl; |
| |
| fl = fcntl(from, F_GETFD, 0); |
| if (fl < 0) |
| return -errno; |
| |
| cloexec = !!(fl & FD_CLOEXEC); |
| } |
| |
| r = dup3(from, to, cloexec ? O_CLOEXEC : 0); |
| if (r < 0) |
| return -errno; |
| |
| assert(r == to); |
| |
| safe_close(from); |
| |
| return to; |
| } |
| |
| int acquire_data_fd(const void *data, size_t size, unsigned flags) { |
| |
| _cleanup_close_pair_ int pipefds[2] = { -1, -1 }; |
| char pattern[] = "/dev/shm/data-fd-XXXXXX"; |
| _cleanup_close_ int fd = -1; |
| int isz = 0, r; |
| ssize_t n; |
| off_t f; |
| |
| assert(data || size == 0); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (size == 0 && ((flags & ACQUIRE_NO_DEV_NULL) == 0)) { |
| |
| r = open("/dev/null", O_RDONLY|O_CLOEXEC|O_NOCTTY); |
| if (r < 0) |
| return -errno; |
| |
| return r; |
| } |
| |
| if ((flags & ACQUIRE_NO_MEMFD) == 0) { |
| fd = memfd_new("data-fd"); |
| if (fd < 0) |
| goto try_pipe; |
| |
| n = write(fd, data, size); |
| if (n < 0) |
| return -errno; |
| if ((size_t) n != size) |
| return -EIO; |
| |
| f = lseek(fd, 0, SEEK_SET); |
| if (f != 0) |
| return -errno; |
| |
| r = memfd_set_sealed(fd); |
| if (r < 0) |
| return r; |
| |
| return TAKE_FD(fd); |
| } |
| |
| try_pipe: |
| if ((flags & ACQUIRE_NO_PIPE) == 0) { |
| if (pipe2(pipefds, O_CLOEXEC|O_NONBLOCK) < 0) |
| return -errno; |
| |
| isz = fcntl(pipefds[1], F_GETPIPE_SZ, 0); |
| if (isz < 0) |
| return -errno; |
| |
| if ((size_t) isz < size) { |
| isz = (int) size; |
| if (isz < 0 || (size_t) isz != size) |
| return -E2BIG; |
| |
| |
| (void) fcntl(pipefds[1], F_SETPIPE_SZ, isz); |
| |
| |
| isz = fcntl(pipefds[1], F_GETPIPE_SZ, 0); |
| if (isz < 0) |
| return -errno; |
| |
| if ((size_t) isz < size) |
| goto try_dev_shm; |
| } |
| |
| n = write(pipefds[1], data, size); |
| if (n < 0) |
| return -errno; |
| if ((size_t) n != size) |
| return -EIO; |
| |
| (void) fd_nonblock(pipefds[0], false); |
| |
| return TAKE_FD(pipefds[0]); |
| } |
| |
| try_dev_shm: |
| if ((flags & ACQUIRE_NO_TMPFILE) == 0) { |
| fd = open("/dev/shm", O_RDWR|O_TMPFILE|O_CLOEXEC, 0500); |
| if (fd < 0) |
| goto try_dev_shm_without_o_tmpfile; |
| |
| n = write(fd, data, size); |
| if (n < 0) |
| return -errno; |
| if ((size_t) n != size) |
| return -EIO; |
| |
| |
| return fd_reopen(fd, O_RDONLY|O_CLOEXEC); |
| } |
| |
| try_dev_shm_without_o_tmpfile: |
| if ((flags & ACQUIRE_NO_REGULAR) == 0) { |
| fd = mkostemp_safe(pattern); |
| if (fd < 0) |
| return fd; |
| |
| n = write(fd, data, size); |
| if (n < 0) { |
| r = -errno; |
| goto unlink_and_return; |
| } |
| if ((size_t) n != size) { |
| r = -EIO; |
| goto unlink_and_return; |
| } |
| |
| |
| r = open(pattern, O_RDONLY|O_CLOEXEC); |
| if (r < 0) |
| r = -errno; |
| |
| unlink_and_return: |
| (void) unlink(pattern); |
| return r; |
| } |
| |
| return -EOPNOTSUPP; |
| } |
| |
| |
| #define DATA_FD_MEMORY_LIMIT (64U*1024U) |
| |
| |
| #define DATA_FD_TMP_LIMIT (1024U*1024U) |
| |
| int fd_duplicate_data_fd(int fd) { |
| |
| _cleanup_close_ int copy_fd = -1, tmp_fd = -1; |
| _cleanup_free_ void *remains = NULL; |
| size_t remains_size = 0; |
| const char *td; |
| struct stat st; |
| int r; |
| |
| |
| |
| |
| |
| |
| |
| if (fstat(fd, &st) < 0) |
| return -errno; |
| |
| |
| if (S_ISDIR(st.st_mode)) |
| return -EISDIR; |
| if (S_ISLNK(st.st_mode)) |
| return -ELOOP; |
| if (!S_ISREG(st.st_mode) && !S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode) && !S_ISCHR(st.st_mode)) |
| return -EBADFD; |
| |
| |
| |
| |
| |
| if (!S_ISREG(st.st_mode) || st.st_size < DATA_FD_MEMORY_LIMIT) { |
| |
| |
| copy_fd = memfd_new("data-fd"); |
| if (copy_fd >= 0) { |
| off_t f; |
| |
| r = copy_bytes(fd, copy_fd, DATA_FD_MEMORY_LIMIT, 0); |
| if (r < 0) |
| return r; |
| |
| f = lseek(copy_fd, 0, SEEK_SET); |
| if (f != 0) |
| return -errno; |
| |
| if (r == 0) { |
| |
| r = memfd_set_sealed(copy_fd); |
| if (r < 0) |
| return r; |
| |
| return TAKE_FD(copy_fd); |
| } |
| |
| |
| |
| } else { |
| _cleanup_(close_pairp) int pipefds[2] = { -1, -1 }; |
| int isz; |
| |
| |
| |
| |
| if (pipe2(pipefds, O_CLOEXEC|O_NONBLOCK) < 0) |
| return -errno; |
| |
| isz = fcntl(pipefds[1], F_GETPIPE_SZ, 0); |
| if (isz < 0) |
| return -errno; |
| |
| |
| if ((size_t) isz < DATA_FD_MEMORY_LIMIT) { |
| |
| (void) fcntl(pipefds[1], F_SETPIPE_SZ, DATA_FD_MEMORY_LIMIT); |
| |
| isz = fcntl(pipefds[1], F_GETPIPE_SZ, 0); |
| if (isz < 0) |
| return -errno; |
| } |
| |
| if ((size_t) isz >= DATA_FD_MEMORY_LIMIT) { |
| |
| r = copy_bytes_full(fd, pipefds[1], DATA_FD_MEMORY_LIMIT, 0, &remains, &remains_size); |
| if (r < 0 && r != -EAGAIN) |
| return r; |
| |
| |
| |
| if (r == 0) { |
| |
| (void) fd_nonblock(pipefds[0], false); |
| |
| return TAKE_FD(pipefds[0]); |
| } |
| |
| |
| |
| copy_fd = TAKE_FD(pipefds[0]); |
| } |
| } |
| } |
| |
| |
| if ((!S_ISREG(st.st_mode) || st.st_size < DATA_FD_TMP_LIMIT) && |
| (DATA_FD_MEMORY_LIMIT + remains_size) < DATA_FD_TMP_LIMIT) { |
| off_t f; |
| |
| tmp_fd = open_tmpfile_unlinkable(NULL , O_RDWR|O_CLOEXEC); |
| if (tmp_fd < 0) |
| return tmp_fd; |
| |
| if (copy_fd >= 0) { |
| |
| |
| |
| r = copy_bytes(copy_fd, tmp_fd, UINT64_MAX, 0); |
| if (r < 0) |
| return r; |
| |
| assert(r == 0); |
| } |
| |
| if (remains_size > 0) { |
| |
| |
| |
| r = loop_write(tmp_fd, remains, remains_size, false); |
| if (r < 0) |
| return r; |
| } |
| |
| r = copy_bytes(fd, tmp_fd, DATA_FD_TMP_LIMIT - DATA_FD_MEMORY_LIMIT - remains_size, COPY_REFLINK); |
| if (r < 0) |
| return r; |
| if (r == 0) |
| goto finish; |
| |
| |
| f = lseek(tmp_fd, 0, SEEK_SET); |
| if (f != 0) |
| return -errno; |
| |
| safe_close(copy_fd); |
| copy_fd = TAKE_FD(tmp_fd); |
| |
| remains = mfree(remains); |
| remains_size = 0; |
| } |
| |
| |
| r = var_tmp_dir(&td); |
| if (r < 0) |
| return r; |
| |
| tmp_fd = open_tmpfile_unlinkable(td, O_RDWR|O_CLOEXEC); |
| if (tmp_fd < 0) |
| return tmp_fd; |
| |
| if (copy_fd >= 0) { |
| |
| |
| r = copy_bytes(copy_fd, tmp_fd, UINT64_MAX, COPY_REFLINK); |
| if (r < 0) |
| return r; |
| |
| assert(r == 0); |
| } |
| |
| if (remains_size > 0) { |
| |
| r = loop_write(tmp_fd, remains, remains_size, false); |
| if (r < 0) |
| return r; |
| } |
| |
| |
| r = copy_bytes(fd, tmp_fd, UINT64_MAX, COPY_REFLINK); |
| if (r < 0) |
| return r; |
| |
| assert(r == 0); |
| |
| finish: |
| |
| |
| |
| return fd_reopen(tmp_fd, O_RDONLY|O_CLOEXEC); |
| } |
| |
| int fd_move_above_stdio(int fd) { |
| int flags, copy; |
| PROTECT_ERRNO; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (fd < 0 || fd > 2) |
| return fd; |
| |
| flags = fcntl(fd, F_GETFD, 0); |
| if (flags < 0) |
| return fd; |
| |
| if (flags & FD_CLOEXEC) |
| copy = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
| else |
| copy = fcntl(fd, F_DUPFD, 3); |
| if (copy < 0) |
| return fd; |
| |
| assert(copy > 2); |
| |
| (void) close(fd); |
| return copy; |
| } |
| |
| int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd) { |
| |
| int fd[3] = { |
| original_input_fd, |
| original_output_fd, |
| original_error_fd |
| }; |
| |
| int r, i, |
| null_fd = -1, |
| copy_fd[3] = { -1, -1, -1 }; |
| bool null_readable, null_writable; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| null_readable = original_input_fd < 0; |
| null_writable = original_output_fd < 0 || original_error_fd < 0; |
| |
| |
| if (null_readable || null_writable) { |
| |
| |
| null_fd = open("/dev/null", (null_readable && null_writable ? O_RDWR : |
| null_readable ? O_RDONLY : O_WRONLY) | O_CLOEXEC); |
| if (null_fd < 0) { |
| r = -errno; |
| goto finish; |
| } |
| |
| |
| if (null_fd < 3) { |
| int copy; |
| |
| copy = fcntl(null_fd, F_DUPFD_CLOEXEC, 3); |
| if (copy < 0) { |
| r = -errno; |
| goto finish; |
| } |
| |
| safe_close(null_fd); |
| null_fd = copy; |
| } |
| } |
| |
| |
| for (i = 0; i < 3; i++) { |
| |
| if (fd[i] < 0) |
| fd[i] = null_fd; |
| else if (fd[i] != i && fd[i] < 3) { |
| |
| copy_fd[i] = fcntl(fd[i], F_DUPFD_CLOEXEC, 3); |
| if (copy_fd[i] < 0) { |
| r = -errno; |
| goto finish; |
| } |
| |
| fd[i] = copy_fd[i]; |
| } |
| } |
| |
| |
| |
| |
| for (i = 0; i < 3; i++) { |
| |
| if (fd[i] == i) { |
| |
| |
| r = fd_cloexec(i, false); |
| if (r < 0) |
| goto finish; |
| |
| } else { |
| assert(fd[i] > 2); |
| |
| if (dup2(fd[i], i) < 0) { |
| r = -errno; |
| goto finish; |
| } |
| } |
| } |
| |
| r = 0; |
| |
| finish: |
| |
| |
| safe_close_above_stdio(original_input_fd); |
| if (original_output_fd != original_input_fd) |
| safe_close_above_stdio(original_output_fd); |
| if (original_error_fd != original_input_fd && original_error_fd != original_output_fd) |
| safe_close_above_stdio(original_error_fd); |
| |
| |
| for (i = 0; i < 3; i++) |
| safe_close(copy_fd[i]); |
| |
| |
| safe_close_above_stdio(null_fd); |
| |
| return r; |
| } |
| |
| int fd_reopen(int fd, int flags) { |
| char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; |
| int new_fd; |
| |
| |
| |
| |
| |
| |
| |
| |
| xsprintf(procfs_path, "/proc/self/fd/%i", fd); |
| new_fd = open(procfs_path, flags); |
| if (new_fd < 0) |
| return -errno; |
| |
| return new_fd; |
| } |
| |
| int read_nr_open(void) { |
| _cleanup_free_ char *nr_open = NULL; |
| int r; |
| |
| |
| |
| |
| r = read_one_line_file("/proc/sys/fs/nr_open", &nr_open); |
| if (r < 0) |
| log_debug_errno(r, "Failed to read /proc/sys/fs/nr_open, ignoring: %m"); |
| else { |
| int v; |
| |
| r = safe_atoi(nr_open, &v); |
| if (r < 0) |
| log_debug_errno(r, "Failed to parse /proc/sys/fs/nr_open value '%s', ignoring: %m", nr_open); |
| else |
| return v; |
| } |
| |
| |
| return 1024 * 1024; |
| } |