]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fd-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include <linux/btrfs.h>
6 #include <linux/magic.h>
8 #include <sys/resource.h>
12 #include "alloc-util.h"
13 #include "dirent-util.h"
19 #include "missing_fcntl.h"
20 #include "missing_syscall.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "process-util.h"
24 #include "socket-util.h"
25 #include "sort-util.h"
26 #include "stat-util.h"
27 #include "stdio-util.h"
28 #include "tmpfile-util.h"
31 /* The maximum number of iterations in the loop to close descriptors in the fallback case
32 * when /proc/self/fd/ is inaccessible. */
33 #define MAX_FD_LOOP_LIMIT (1024*1024)
35 int close_nointr(int fd
) {
42 * Just ignore EINTR; a retry loop is the wrong thing to do on
45 * http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
46 * https://bugzilla.gnome.org/show_bug.cgi?id=682819
47 * http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
48 * https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
56 int safe_close(int fd
) {
59 * Like close_nointr() but cannot fail. Guarantees errno is
60 * unchanged. Is a NOP with negative fds passed, and returns
61 * -1, so that it can be used in this syntax:
63 * fd = safe_close(fd);
69 /* The kernel might return pretty much any error code
70 * via close(), but the fd will be closed anyway. The
71 * only condition we want to check for here is whether
72 * the fd was invalid at all... */
74 assert_se(close_nointr(fd
) != -EBADF
);
80 void safe_close_pair(int p
[static 2]) {
84 /* Special case pairs which use the same fd in both
86 p
[0] = p
[1] = safe_close(p
[0]);
90 p
[0] = safe_close(p
[0]);
91 p
[1] = safe_close(p
[1]);
94 void close_many(const int fds
[], size_t n_fd
) {
95 assert(fds
|| n_fd
<= 0);
97 for (size_t i
= 0; i
< n_fd
; i
++)
101 int fclose_nointr(FILE *f
) {
104 /* Same as close_nointr(), but for fclose() */
106 errno
= 0; /* Extra safety: if the FILE* object is not encapsulating an fd, it might not set errno
107 * correctly. Let's hence initialize it to zero first, so that we aren't confused by any
108 * prior errno here */
115 return errno_or_else(EIO
);
118 FILE* safe_fclose(FILE *f
) {
120 /* Same as safe_close(), but for fclose() */
125 assert_se(fclose_nointr(f
) != -EBADF
);
131 DIR* safe_closedir(DIR *d
) {
136 assert_se(closedir(d
) >= 0 || errno
!= EBADF
);
142 int fd_nonblock(int fd
, bool nonblock
) {
147 flags
= fcntl(fd
, F_GETFL
, 0);
151 nflags
= UPDATE_FLAG(flags
, O_NONBLOCK
, nonblock
);
155 return RET_NERRNO(fcntl(fd
, F_SETFL
, nflags
));
158 int fd_cloexec(int fd
, bool cloexec
) {
163 flags
= fcntl(fd
, F_GETFD
, 0);
167 nflags
= UPDATE_FLAG(flags
, FD_CLOEXEC
, cloexec
);
171 return RET_NERRNO(fcntl(fd
, F_SETFD
, nflags
));
174 _pure_
static bool fd_in_set(int fd
, const int fdset
[], size_t n_fdset
) {
175 assert(n_fdset
== 0 || fdset
);
177 for (size_t i
= 0; i
< n_fdset
; i
++)
184 int get_max_fd(void) {
188 /* Return the highest possible fd, based RLIMIT_NOFILE, but enforcing FD_SETSIZE-1 as lower boundary
189 * and INT_MAX as upper boundary. */
191 if (getrlimit(RLIMIT_NOFILE
, &rl
) < 0)
194 m
= MAX(rl
.rlim_cur
, rl
.rlim_max
);
195 if (m
< FD_SETSIZE
) /* Let's always cover at least 1024 fds */
198 if (m
== RLIM_INFINITY
|| m
> INT_MAX
) /* Saturate on overflow. After all fds are "int", hence can
199 * never be above INT_MAX */
202 return (int) (m
- 1);
205 static int close_all_fds_frugal(const int except
[], size_t n_except
) {
208 assert(n_except
== 0 || except
);
210 /* This is the inner fallback core of close_all_fds(). This never calls malloc() or opendir() or so
211 * and hence is safe to be called in signal handler context. Most users should call close_all_fds(),
212 * but when we assume we are called from signal handler context, then use this simpler call
215 max_fd
= get_max_fd();
219 /* Refuse to do the loop over more too many elements. It's better to fail immediately than to
220 * spin the CPU for a long time. */
221 if (max_fd
> MAX_FD_LOOP_LIMIT
)
222 return log_debug_errno(SYNTHETIC_ERRNO(EPERM
),
223 "Refusing to loop over %d potential fds.",
226 for (int fd
= 3; fd
>= 0; fd
= fd
< max_fd
? fd
+ 1 : -1) {
229 if (fd_in_set(fd
, except
, n_except
))
232 q
= close_nointr(fd
);
233 if (q
< 0 && q
!= -EBADF
&& r
>= 0)
240 static bool have_close_range
= true; /* Assume we live in the future */
242 static int close_all_fds_special_case(const int except
[], size_t n_except
) {
243 assert(n_except
== 0 || except
);
245 /* Handles a few common special cases separately, since they are common and can be optimized really
246 * nicely, since we won't need sorting for them. Returns > 0 if the special casing worked, 0
249 if (!have_close_range
)
255 /* Close everything. Yay! */
257 if (close_range(3, -1, 0) >= 0)
260 if (ERRNO_IS_NOT_SUPPORTED(errno
) || ERRNO_IS_PRIVILEGE(errno
)) {
261 have_close_range
= false;
268 /* Close all but exactly one, then we don't need no sorting. This is a pretty common
269 * case, hence let's handle it specially. */
271 if ((except
[0] <= 3 || close_range(3, except
[0]-1, 0) >= 0) &&
272 (except
[0] >= INT_MAX
|| close_range(MAX(3, except
[0]+1), -1, 0) >= 0))
275 if (ERRNO_IS_NOT_SUPPORTED(errno
) || ERRNO_IS_PRIVILEGE(errno
)) {
276 have_close_range
= false;
287 int close_all_fds_without_malloc(const int except
[], size_t n_except
) {
290 assert(n_except
== 0 || except
);
292 r
= close_all_fds_special_case(except
, n_except
);
295 if (r
> 0) /* special case worked! */
298 return close_all_fds_frugal(except
, n_except
);
301 int close_all_fds(const int except
[], size_t n_except
) {
302 _cleanup_closedir_
DIR *d
= NULL
;
305 assert(n_except
== 0 || except
);
307 r
= close_all_fds_special_case(except
, n_except
);
310 if (r
> 0) /* special case worked! */
313 if (have_close_range
) {
314 _cleanup_free_
int *sorted_malloc
= NULL
;
318 /* In the best case we have close_range() to close all fds between a start and an end fd,
319 * which we can use on the "inverted" exception array, i.e. all intervals between all
320 * adjacent pairs from the sorted exception array. This changes loop complexity from O(n)
321 * where n is number of open fds to O(m⋅log(m)) where m is the number of fds to keep
322 * open. Given that we assume n ≫ m that's preferable to us. */
324 assert(n_except
< SIZE_MAX
);
325 n_sorted
= n_except
+ 1;
327 if (n_sorted
> 64) /* Use heap for large numbers of fds, stack otherwise */
328 sorted
= sorted_malloc
= new(int, n_sorted
);
330 sorted
= newa(int, n_sorted
);
333 memcpy(sorted
, except
, n_except
* sizeof(int));
335 /* Let's add fd 2 to the list of fds, to simplify the loop below, as this
336 * allows us to cover the head of the array the same way as the body */
337 sorted
[n_sorted
-1] = 2;
339 typesafe_qsort(sorted
, n_sorted
, cmp_int
);
341 for (size_t i
= 0; i
< n_sorted
-1; i
++) {
344 start
= MAX(sorted
[i
], 2); /* The first three fds shall always remain open */
345 end
= MAX(sorted
[i
+1], 2);
347 assert(end
>= start
);
349 if (end
- start
<= 1)
352 /* Close everything between the start and end fds (both of which shall stay open) */
353 if (close_range(start
+ 1, end
- 1, 0) < 0) {
354 if (!ERRNO_IS_NOT_SUPPORTED(errno
) && !ERRNO_IS_PRIVILEGE(errno
))
357 have_close_range
= false;
362 if (have_close_range
) {
363 /* The loop succeeded. Let's now close everything beyond the end */
365 if (sorted
[n_sorted
-1] >= INT_MAX
) /* Dont let the addition below overflow */
368 if (close_range(sorted
[n_sorted
-1] + 1, -1, 0) >= 0)
371 if (!ERRNO_IS_NOT_SUPPORTED(errno
) && !ERRNO_IS_PRIVILEGE(errno
))
374 have_close_range
= false;
378 /* Fallback on OOM or if close_range() is not supported */
381 d
= opendir("/proc/self/fd");
383 return close_all_fds_frugal(except
, n_except
); /* ultimate fallback if /proc/ is not available */
385 FOREACH_DIRENT(de
, d
, return -errno
) {
388 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
391 if (safe_atoi(de
->d_name
, &fd
) < 0)
392 /* Let's better ignore this, just in case */
401 if (fd_in_set(fd
, except
, n_except
))
404 q
= close_nointr(fd
);
405 if (q
< 0 && q
!= -EBADF
&& r
>= 0) /* Valgrind has its own FD and doesn't want to have it closed */
412 int same_fd(int a
, int b
) {
413 struct stat sta
, stb
;
420 /* Compares two file descriptors. Note that semantics are
421 * quite different depending on whether we have kcmp() or we
422 * don't. If we have kcmp() this will only return true for
423 * dup()ed file descriptors, but not otherwise. If we don't
424 * have kcmp() this will also return true for two fds of the same
425 * file, created by separate open() calls. Since we use this
426 * call mostly for filtering out duplicates in the fd store
427 * this difference hopefully doesn't matter too much. */
432 /* Try to use kcmp() if we have it. */
433 pid
= getpid_cached();
434 r
= kcmp(pid
, pid
, KCMP_FILE
, a
, b
);
439 if (!IN_SET(errno
, ENOSYS
, EACCES
, EPERM
))
442 /* We don't have kcmp(), use fstat() instead. */
443 if (fstat(a
, &sta
) < 0)
446 if (fstat(b
, &stb
) < 0)
449 if ((sta
.st_mode
& S_IFMT
) != (stb
.st_mode
& S_IFMT
))
452 /* We consider all device fds different, since two device fds
453 * might refer to quite different device contexts even though
454 * they share the same inode and backing dev_t. */
456 if (S_ISCHR(sta
.st_mode
) || S_ISBLK(sta
.st_mode
))
459 if (sta
.st_dev
!= stb
.st_dev
|| sta
.st_ino
!= stb
.st_ino
)
462 /* The fds refer to the same inode on disk, let's also check
463 * if they have the same fd flags. This is useful to
464 * distinguish the read and write side of a pipe created with
466 fa
= fcntl(a
, F_GETFL
);
470 fb
= fcntl(b
, F_GETFL
);
477 void cmsg_close_all(struct msghdr
*mh
) {
478 struct cmsghdr
*cmsg
;
482 CMSG_FOREACH(cmsg
, mh
)
483 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
)
484 close_many((int*) CMSG_DATA(cmsg
), (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int));
487 bool fdname_is_valid(const char *s
) {
490 /* Validates a name for $LISTEN_FDNAMES. We basically allow
491 * everything ASCII that's not a control character. Also, as
492 * special exception the ":" character is not allowed, as we
493 * use that as field separator in $LISTEN_FDNAMES.
495 * Note that the empty string is explicitly allowed
496 * here. However, we limit the length of the names to 255
502 for (p
= s
; *p
; p
++) {
511 return p
- s
<= FDNAME_MAX
;
514 int fd_get_path(int fd
, char **ret
) {
517 r
= readlink_malloc(FORMAT_PROC_FD_PATH(fd
), ret
);
519 /* ENOENT can mean two things: that the fd does not exist or that /proc is not mounted. Let's make
520 * things debuggable and distinguish the two. */
522 if (proc_mounted() == 0)
523 return -ENOSYS
; /* /proc is not available or not set up properly, we're most likely in some chroot
525 return -EBADF
; /* The directory exists, hence it's the fd that doesn't. */
531 int move_fd(int from
, int to
, int cloexec
) {
534 /* Move fd 'from' to 'to', make sure FD_CLOEXEC remains equal if requested, and release the old fd. If
535 * 'cloexec' is passed as -1, the original FD_CLOEXEC is inherited for the new fd. If it is 0, it is turned
536 * off, if it is > 0 it is turned on. */
546 r
= fd_cloexec(to
, cloexec
);
557 fl
= fcntl(from
, F_GETFD
, 0);
561 cloexec
= !!(fl
& FD_CLOEXEC
);
564 r
= dup3(from
, to
, cloexec
? O_CLOEXEC
: 0);
575 int fd_move_above_stdio(int fd
) {
579 /* Moves the specified file descriptor if possible out of the range [0…2], i.e. the range of
580 * stdin/stdout/stderr. If it can't be moved outside of this range the original file descriptor is
581 * returned. This call is supposed to be used for long-lasting file descriptors we allocate in our code that
582 * might get loaded into foreign code, and where we want ensure our fds are unlikely used accidentally as
583 * stdin/stdout/stderr of unrelated code.
585 * Note that this doesn't fix any real bugs, it just makes it less likely that our code will be affected by
586 * buggy code from others that mindlessly invokes 'fprintf(stderr, …' or similar in places where stderr has
587 * been closed before.
589 * This function is written in a "best-effort" and "least-impact" style. This means whenever we encounter an
590 * error we simply return the original file descriptor, and we do not touch errno. */
592 if (fd
< 0 || fd
> 2)
595 flags
= fcntl(fd
, F_GETFD
, 0);
599 if (flags
& FD_CLOEXEC
)
600 copy
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
602 copy
= fcntl(fd
, F_DUPFD
, 3);
612 int rearrange_stdio(int original_input_fd
, int original_output_fd
, int original_error_fd
) {
614 int fd
[3] = { /* Put together an array of fds we work on */
621 null_fd
= -1, /* if we open /dev/null, we store the fd to it here */
622 copy_fd
[3] = { -1, -1, -1 }; /* This contains all fds we duplicate here temporarily, and hence need to close at the end */
623 bool null_readable
, null_writable
;
625 /* Sets up stdin, stdout, stderr with the three file descriptors passed in. If any of the descriptors is
626 * specified as -1 it will be connected with /dev/null instead. If any of the file descriptors is passed as
627 * itself (e.g. stdin as STDIN_FILENO) it is left unmodified, but the O_CLOEXEC bit is turned off should it be
630 * Note that if any of the passed file descriptors are > 2 they will be closed — both on success and on
631 * failure! Thus, callers should assume that when this function returns the input fds are invalidated.
633 * Note that when this function fails stdin/stdout/stderr might remain half set up!
635 * O_CLOEXEC is turned off for all three file descriptors (which is how it should be for
636 * stdin/stdout/stderr). */
638 null_readable
= original_input_fd
< 0;
639 null_writable
= original_output_fd
< 0 || original_error_fd
< 0;
641 /* First step, open /dev/null once, if we need it */
642 if (null_readable
|| null_writable
) {
644 /* Let's open this with O_CLOEXEC first, and convert it to non-O_CLOEXEC when we move the fd to the final position. */
645 null_fd
= open("/dev/null", (null_readable
&& null_writable
? O_RDWR
:
646 null_readable
? O_RDONLY
: O_WRONLY
) | O_CLOEXEC
);
652 /* If this fd is in the 0…2 range, let's move it out of it */
656 copy
= fcntl(null_fd
, F_DUPFD_CLOEXEC
, 3); /* Duplicate this with O_CLOEXEC set */
662 CLOSE_AND_REPLACE(null_fd
, copy
);
666 /* Let's assemble fd[] with the fds to install in place of stdin/stdout/stderr */
667 for (i
= 0; i
< 3; i
++) {
670 fd
[i
] = null_fd
; /* A negative parameter means: connect this one to /dev/null */
671 else if (fd
[i
] != i
&& fd
[i
] < 3) {
672 /* This fd is in the 0…2 territory, but not at its intended place, move it out of there, so that we can work there. */
673 copy_fd
[i
] = fcntl(fd
[i
], F_DUPFD_CLOEXEC
, 3); /* Duplicate this with O_CLOEXEC set */
674 if (copy_fd
[i
] < 0) {
683 /* At this point we now have the fds to use in fd[], and they are all above the stdio range, so that we
684 * have freedom to move them around. If the fds already were at the right places then the specific fds are
685 * -1. Let's now move them to the right places. This is the point of no return. */
686 for (i
= 0; i
< 3; i
++) {
690 /* fd is already in place, but let's make sure O_CLOEXEC is off */
691 r
= fd_cloexec(i
, false);
698 if (dup2(fd
[i
], i
) < 0) { /* Turns off O_CLOEXEC on the new fd. */
708 /* Close the original fds, but only if they were outside of the stdio range. Also, properly check for the same
709 * fd passed in multiple times. */
710 safe_close_above_stdio(original_input_fd
);
711 if (original_output_fd
!= original_input_fd
)
712 safe_close_above_stdio(original_output_fd
);
713 if (original_error_fd
!= original_input_fd
&& original_error_fd
!= original_output_fd
)
714 safe_close_above_stdio(original_error_fd
);
716 /* Close the copies we moved > 2 */
717 for (i
= 0; i
< 3; i
++)
718 safe_close(copy_fd
[i
]);
720 /* Close our null fd, if it's > 2 */
721 safe_close_above_stdio(null_fd
);
726 int fd_reopen(int fd
, int flags
) {
729 /* Reopens the specified fd with new flags. This is useful for convert an O_PATH fd into a regular one, or to
730 * turn O_RDWR fds into O_RDONLY fds.
732 * This doesn't work on sockets (since they cannot be open()ed, ever).
734 * This implicitly resets the file read index to 0. */
736 if (FLAGS_SET(flags
, O_DIRECTORY
)) {
737 /* If we shall reopen the fd as directory we can just go via "." and thus bypass the whole
738 * magic /proc/ directory, and make ourselves independent of that being mounted. */
739 new_fd
= openat(fd
, ".", flags
);
746 new_fd
= open(FORMAT_PROC_FD_PATH(fd
), flags
);
753 return -ENOSYS
; /* if we have no /proc/, the concept is not implementable */
755 return r
> 0 ? -EBADF
: -ENOENT
; /* If /proc/ is definitely around then this means the fd is
756 * not valid, otherwise let's propagate the original
763 int read_nr_open(void) {
764 _cleanup_free_
char *nr_open
= NULL
;
767 /* Returns the kernel's current fd limit, either by reading it of /proc/sys if that works, or using the
768 * hard-coded default compiled-in value of current kernels (1M) if not. This call will never fail. */
770 r
= read_one_line_file("/proc/sys/fs/nr_open", &nr_open
);
772 log_debug_errno(r
, "Failed to read /proc/sys/fs/nr_open, ignoring: %m");
776 r
= safe_atoi(nr_open
, &v
);
778 log_debug_errno(r
, "Failed to parse /proc/sys/fs/nr_open value '%s', ignoring: %m", nr_open
);
783 /* If we fail, fall back to the hard-coded kernel limit of 1024 * 1024. */
787 /* This is here because it's fd-related and is called from sd-journal code. Other btrfs-related utilities are
788 * in src/shared, but libsystemd must not link to libsystemd-shared, see docs/ARCHITECTURE.md. */
789 int btrfs_defrag_fd(int fd
) {
794 r
= fd_verify_regular(fd
);
798 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_DEFRAG
, NULL
));