]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fd-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 int close_nointr(int fd
) {
32 * Just ignore EINTR; a retry loop is the wrong thing to do on
35 * http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
36 * https://bugzilla.gnome.org/show_bug.cgi?id=682819
37 * http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
38 * https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
46 int safe_close(int fd
) {
49 * Like close_nointr() but cannot fail. Guarantees errno is
50 * unchanged. Is a NOP with negative fds passed, and returns
51 * -1, so that it can be used in this syntax:
53 * fd = safe_close(fd);
59 /* The kernel might return pretty much any error code
60 * via close(), but the fd will be closed anyway. The
61 * only condition we want to check for here is whether
62 * the fd was invalid at all... */
64 assert_se(close_nointr(fd
) != -EBADF
);
70 void safe_close_pair(int p
[]) {
74 /* Special case pairs which use the same fd in both
76 p
[0] = p
[1] = safe_close(p
[0]);
80 p
[0] = safe_close(p
[0]);
81 p
[1] = safe_close(p
[1]);
84 void close_many(const int fds
[], unsigned n_fd
) {
87 assert(fds
|| n_fd
<= 0);
89 for (i
= 0; i
< n_fd
; i
++)
93 int fclose_nointr(FILE *f
) {
96 /* Same as close_nointr(), but for fclose() */
107 FILE* safe_fclose(FILE *f
) {
109 /* Same as safe_close(), but for fclose() */
114 assert_se(fclose_nointr(f
) != EBADF
);
120 DIR* safe_closedir(DIR *d
) {
125 assert_se(closedir(d
) >= 0 || errno
!= EBADF
);
131 int fd_nonblock(int fd
, bool nonblock
) {
136 flags
= fcntl(fd
, F_GETFL
, 0);
141 nflags
= flags
| O_NONBLOCK
;
143 nflags
= flags
& ~O_NONBLOCK
;
148 if (fcntl(fd
, F_SETFL
, nflags
) < 0)
154 int fd_cloexec(int fd
, bool cloexec
) {
159 flags
= fcntl(fd
, F_GETFD
, 0);
164 nflags
= flags
| FD_CLOEXEC
;
166 nflags
= flags
& ~FD_CLOEXEC
;
171 if (fcntl(fd
, F_SETFD
, nflags
) < 0)
177 _pure_
static bool fd_in_set(int fd
, const int fdset
[], unsigned n_fdset
) {
180 assert(n_fdset
== 0 || fdset
);
182 for (i
= 0; i
< n_fdset
; i
++)
189 int close_all_fds(const int except
[], unsigned n_except
) {
190 _cleanup_closedir_
DIR *d
= NULL
;
194 assert(n_except
== 0 || except
);
196 d
= opendir("/proc/self/fd");
201 /* When /proc isn't available (for example in chroots)
202 * the fallback is brute forcing through the fd
205 assert_se(getrlimit(RLIMIT_NOFILE
, &rl
) >= 0);
206 for (fd
= 3; fd
< (int) rl
.rlim_max
; fd
++) {
208 if (fd_in_set(fd
, except
, n_except
))
211 if (close_nointr(fd
) < 0)
212 if (errno
!= EBADF
&& r
== 0)
219 while ((de
= readdir(d
))) {
222 if (hidden_file(de
->d_name
))
225 if (safe_atoi(de
->d_name
, &fd
) < 0)
226 /* Let's better ignore this, just in case */
235 if (fd_in_set(fd
, except
, n_except
))
238 if (close_nointr(fd
) < 0) {
239 /* Valgrind has its own FD and doesn't want to have it closed */
240 if (errno
!= EBADF
&& r
== 0)
248 int same_fd(int a
, int b
) {
249 struct stat sta
, stb
;
256 /* Compares two file descriptors. Note that semantics are
257 * quite different depending on whether we have kcmp() or we
258 * don't. If we have kcmp() this will only return true for
259 * dup()ed file descriptors, but not otherwise. If we don't
260 * have kcmp() this will also return true for two fds of the same
261 * file, created by separate open() calls. Since we use this
262 * call mostly for filtering out duplicates in the fd store
263 * this difference hopefully doesn't matter too much. */
268 /* Try to use kcmp() if we have it. */
270 r
= kcmp(pid
, pid
, KCMP_FILE
, a
, b
);
278 /* We don't have kcmp(), use fstat() instead. */
279 if (fstat(a
, &sta
) < 0)
282 if (fstat(b
, &stb
) < 0)
285 if ((sta
.st_mode
& S_IFMT
) != (stb
.st_mode
& S_IFMT
))
288 /* We consider all device fds different, since two device fds
289 * might refer to quite different device contexts even though
290 * they share the same inode and backing dev_t. */
292 if (S_ISCHR(sta
.st_mode
) || S_ISBLK(sta
.st_mode
))
295 if (sta
.st_dev
!= stb
.st_dev
|| sta
.st_ino
!= stb
.st_ino
)
298 /* The fds refer to the same inode on disk, let's also check
299 * if they have the same fd flags. This is useful to
300 * distinguish the read and write side of a pipe created with
302 fa
= fcntl(a
, F_GETFL
);
306 fb
= fcntl(b
, F_GETFL
);
313 void cmsg_close_all(struct msghdr
*mh
) {
314 struct cmsghdr
*cmsg
;
318 CMSG_FOREACH(cmsg
, mh
)
319 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
)
320 close_many((int*) CMSG_DATA(cmsg
), (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int));