mirror of
https://github.com/systemd/systemd
synced 2024-07-22 02:34:54 +00:00
fd-util: modernization
This commit is contained in:
parent
9ef362bf3d
commit
1276e63370
|
@ -92,22 +92,22 @@ void safe_close_pair(int p[static 2]) {
|
|||
p[1] = safe_close(p[1]);
|
||||
}
|
||||
|
||||
void close_many(const int fds[], size_t n_fd) {
|
||||
assert(fds || n_fd <= 0);
|
||||
void close_many(const int fds[], size_t n_fds) {
|
||||
assert(fds || n_fds == 0);
|
||||
|
||||
for (size_t i = 0; i < n_fd; i++)
|
||||
safe_close(fds[i]);
|
||||
FOREACH_ARRAY(fd, fds, n_fds)
|
||||
safe_close(*fd);
|
||||
}
|
||||
|
||||
void close_many_unset(int fds[], size_t n_fd) {
|
||||
assert(fds || n_fd <= 0);
|
||||
void close_many_unset(int fds[], size_t n_fds) {
|
||||
assert(fds || n_fds == 0);
|
||||
|
||||
for (size_t i = 0; i < n_fd; i++)
|
||||
fds[i] = safe_close(fds[i]);
|
||||
FOREACH_ARRAY(fd, fds, n_fds)
|
||||
*fd = safe_close(*fd);
|
||||
}
|
||||
|
||||
void close_many_and_free(int *fds, size_t n_fds) {
|
||||
assert(fds || n_fds <= 0);
|
||||
assert(fds || n_fds == 0);
|
||||
|
||||
close_many(fds, n_fds);
|
||||
free(fds);
|
||||
|
@ -189,15 +189,15 @@ int fd_cloexec(int fd, bool cloexec) {
|
|||
int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec) {
|
||||
int ret = 0, r;
|
||||
|
||||
assert(n_fds == 0 || fds);
|
||||
assert(fds || n_fds == 0);
|
||||
|
||||
for (size_t i = 0; i < n_fds; i++) {
|
||||
if (fds[i] < 0) /* Skip gracefully over already invalidated fds */
|
||||
FOREACH_ARRAY(fd, fds, n_fds) {
|
||||
if (*fd < 0) /* Skip gracefully over already invalidated fds */
|
||||
continue;
|
||||
|
||||
r = fd_cloexec(fds[i], cloexec);
|
||||
if (r < 0 && ret >= 0) /* Continue going, but return first error */
|
||||
ret = r;
|
||||
r = fd_cloexec(*fd, cloexec);
|
||||
if (r < 0) /* Continue going, but return first error */
|
||||
RET_GATHER(ret, r);
|
||||
else
|
||||
ret = 1; /* report if we did anything */
|
||||
}
|
||||
|
@ -205,14 +205,15 @@ int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) {
|
||||
assert(n_fdset == 0 || fdset);
|
||||
static bool fd_in_set(int fd, const int fds[], size_t n_fds) {
|
||||
assert(fd >= 0);
|
||||
assert(fds || n_fds == 0);
|
||||
|
||||
for (size_t i = 0; i < n_fdset; i++) {
|
||||
if (fdset[i] < 0)
|
||||
FOREACH_ARRAY(i, fds, n_fds) {
|
||||
if (*i < 0)
|
||||
continue;
|
||||
|
||||
if (fdset[i] == fd)
|
||||
if (*i == fd)
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -243,7 +244,7 @@ int get_max_fd(void) {
|
|||
static int close_all_fds_frugal(const int except[], size_t n_except) {
|
||||
int max_fd, r = 0;
|
||||
|
||||
assert(n_except == 0 || except);
|
||||
assert(except || n_except == 0);
|
||||
|
||||
/* This is the inner fallback core of close_all_fds(). This never calls malloc() or opendir() or so
|
||||
* and hence is safe to be called in signal handler context. Most users should call close_all_fds(),
|
||||
|
@ -258,8 +259,7 @@ static int close_all_fds_frugal(const int except[], size_t n_except) {
|
|||
* spin the CPU for a long time. */
|
||||
if (max_fd > MAX_FD_LOOP_LIMIT)
|
||||
return log_debug_errno(SYNTHETIC_ERRNO(EPERM),
|
||||
"Refusing to loop over %d potential fds.",
|
||||
max_fd);
|
||||
"Refusing to loop over %d potential fds.", max_fd);
|
||||
|
||||
for (int fd = 3; fd >= 0; fd = fd < max_fd ? fd + 1 : -EBADF) {
|
||||
int q;
|
||||
|
@ -268,8 +268,8 @@ static int close_all_fds_frugal(const int except[], size_t n_except) {
|
|||
continue;
|
||||
|
||||
q = close_nointr(fd);
|
||||
if (q < 0 && q != -EBADF && r >= 0)
|
||||
r = q;
|
||||
if (q != -EBADF)
|
||||
RET_GATHER(r, q);
|
||||
}
|
||||
|
||||
return r;
|
||||
|
@ -598,7 +598,7 @@ int move_fd(int from, int to, int cloexec) {
|
|||
if (fl < 0)
|
||||
return -errno;
|
||||
|
||||
cloexec = !!(fl & FD_CLOEXEC);
|
||||
cloexec = FLAGS_SET(fl, FD_CLOEXEC);
|
||||
}
|
||||
|
||||
r = dup3(from, to, cloexec ? O_CLOEXEC : 0);
|
||||
|
|
|
@ -32,8 +32,8 @@ static inline int safe_close_above_stdio(int fd) {
|
|||
return safe_close(fd);
|
||||
}
|
||||
|
||||
void close_many(const int fds[], size_t n_fd);
|
||||
void close_many_unset(int fds[], size_t n_fd);
|
||||
void close_many(const int fds[], size_t n_fds);
|
||||
void close_many_unset(int fds[], size_t n_fds);
|
||||
void close_many_and_free(int *fds, size_t n_fds);
|
||||
|
||||
int fclose_nointr(FILE *f);
|
||||
|
|
Loading…
Reference in a new issue