--- /dev/null
+@@
+local idexpression p;
+expression q;
+@@
+- p = q;
+- q = -1;
+- return p;
++ return TAKE_FD(q);
+@@
+expression p, q;
+@@
+- p = q;
+- q = -1;
++ p = TAKE_FD(q);
if (r < 0)
return r;
- r = fd;
- fd = -1;
-
- return r;
+ return TAKE_FD(fd);
}
try_pipe:
(void) fd_nonblock(pipefds[0], false);
- r = pipefds[0];
- pipefds[0] = -1;
-
- return r;
+ return TAKE_FD(pipefds[0]);
}
try_dev_shm:
static inline int make_null_stdio(void) {
return rearrange_stdio(-1, -1, -1);
}
+
+/* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
+#define TAKE_FD(fd) \
+ ({ \
+ int _fd_ = (fd); \
+ (fd) = -1; \
+ _fd_; \
+ })
}
safe_close(fd);
- fd = fd_parent;
- fd_parent = -1;
+ fd = TAKE_FD(fd_parent);
continue;
}
/* And iterate again, but go one directory further down. */
safe_close(fd);
- fd = child;
- child = -1;
+ fd = TAKE_FD(child);
}
if (!done) {
*ret = TAKE_PTR(done);
if (flags & CHASE_OPEN) {
- int q;
-
/* Return the O_PATH fd we currently are looking to the caller. It can translate it to a proper fd by
* opening /proc/self/fd/xyz. */
assert(fd >= 0);
- q = fd;
- fd = -1;
-
- return q;
+ return TAKE_FD(fd);
}
return exists;
if (r < 0)
return r;
- r = fd;
- fd = -1;
-
- return r;
+ return TAKE_FD(fd);
}
fd = safe_close(fd);
}
- r = fd;
- fd = -1;
-
- return r;
+ return TAKE_FD(fd);
}
int release_terminal(void) {
(void) make_uid_symlinks(candidate, name, true); /* also add direct lookup symlinks */
*ret_uid = candidate;
- r = lock_fd;
- lock_fd = -1;
-
- return r;
+ return TAKE_FD(lock_fd);
next:
;
if (r < 0)
return log_error_errno(errno, "SO_PASSCRED failed: %m");
- m->notify_fd = fd;
- fd = -1;
+ m->notify_fd = TAKE_FD(fd);
log_debug("Using notification socket %s", m->notify_socket);
}
goto fail;
}
- r = fd;
- fd = -1;
-
- return r;
+ return TAKE_FD(fd);
fail:
mac_selinux_create_file_clear();
static int special_address_create(const char *path, bool writable) {
_cleanup_close_ int fd = -1;
struct stat st;
- int r;
assert(path);
if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode))
return -EEXIST;
- r = fd;
- fd = -1;
-
- return r;
+ return TAKE_FD(fd);
}
static int usbffs_address_create(const char *path) {
_cleanup_close_ int fd = -1;
struct stat st;
- int r;
assert(path);
if (!S_ISREG(st.st_mode))
return -EEXIST;
- r = fd;
- fd = -1;
-
- return r;
+ return TAKE_FD(fd);
}
static int mq_address_create(
struct stat st;
mode_t old_mask;
struct mq_attr _attr, *attr = NULL;
- int r;
assert(path);
st.st_gid != getgid())
return -EEXIST;
- r = fd;
- fd = -1;
-
- return r;
+ return TAKE_FD(fd);
}
static int socket_symlink(Socket *s) {
/* Try to take a reflink snapshot of the file, if we can t make the export atomic */
tfd = reflink_snapshot(sfd, path);
- if (tfd >= 0) {
- e->input_fd = tfd;
- tfd = -1;
- } else {
- e->input_fd = sfd;
- sfd = -1;
- }
+ if (tfd >= 0)
+ e->input_fd = TAKE_FD(tfd);
+ else
+ e->input_fd = TAKE_FD(sfd);
r = import_compress_init(&e->compress, compress);
if (r < 0)
_exit(EXIT_FAILURE);
}
- pipefd[0] = safe_close(pipefd[0]);
- r = pipefd[1];
- pipefd[1] = -1;
-
*ret = pid;
- return r;
+ return TAKE_FD(pipefd[1]);
}
int import_fork_tar_c(const char *path, pid_t *ret) {
_exit(EXIT_FAILURE);
}
- pipefd[1] = safe_close(pipefd[1]);
- r = pipefd[0];
- pipefd[0] = -1;
-
*ret = pid;
- return r;
+ return TAKE_FD(pipefd[0]);
}
free_and_replace(i->temp_path, t);
safe_close(i->output_fd);
- i->output_fd = converted_fd;
- converted_fd = -1;
+ i->output_fd = TAKE_FD(converted_fd);
return 1;
}
}
pipefd[1] = safe_close(pipefd[1]);
- t->log_fd = pipefd[0];
- pipefd[0] = -1;
+ t->log_fd = TAKE_FD(pipefd[0]);
t->stdin_fd = safe_close(t->stdin_fd);
free_and_replace(i->temp_path, t);
safe_close(i->raw_job->disk_fd);
- i->raw_job->disk_fd = converted_fd;
- converted_fd = -1;
+ i->raw_job->disk_fd = TAKE_FD(converted_fd);
return 1;
}
if (r < 0)
return r;
- r = fd;
- fd = -1;
- return r;
+ return TAKE_FD(fd);
}
_public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
if (r < 0)
return -errno;
- r = s;
- s = -1;
-
- return r;
+ return TAKE_FD(s);
}
static int arp_send_packet(int fd, int ifindex,
if (r < 0)
return -errno;
- r = s;
- s = -1;
-
- return r;
+ return TAKE_FD(s);
}
int dhcp_network_bind_raw_socket(int ifindex, union sockaddr_union *link,
if (r < 0)
return -errno;
- r = s;
- s = -1;
-
- return r;
+ return TAKE_FD(s);
}
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
if (r < 0)
return -errno;
- r = s;
- s = -1;
- return r;
+ return TAKE_FD(s);
}
int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address,
if (r < 0)
return -errno;
- r = s;
- s = -1;
- return r;
+ return TAKE_FD(s);
}
int icmp6_bind_router_solicitation(int index) {
if (r < 0)
return -errno;
- r = fd;
- fd = -1;
-
- return r;
+ return TAKE_FD(fd);
}
* little dance to avoid having the terminal be available
* for reuse before we've cleaned it up.
*/
- old_fd = s->vtfd;
- s->vtfd = -1;
+ old_fd = TAKE_FD(s->vtfd);
vt = session_open_vt(s);
safe_close(old_fd);
union sockaddr_union sa = {};
socklen_t salen;
static const int one = 1;
- int ret, r, ifindex;
+ int r, ifindex;
assert(s);
if (r < 0 && errno != EINPROGRESS)
return -errno;
- ret = fd;
- fd = -1;
-
- return ret;
+ return TAKE_FD(fd);
}
int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port) {
return r;
(void) sd_event_source_set_description(m->dns_stub_udp_event_source, "dns-stub-udp");
- m->dns_stub_udp_fd = fd;
- fd = -1;
- return m->dns_stub_udp_fd;
+ return m->dns_stub_udp_fd = TAKE_FD(fd);
}
static int on_dns_stub_stream_packet(DnsStream *s) {
return r;
(void) sd_event_source_set_description(m->dns_stub_tcp_event_source, "dns-stub-tcp");
- m->dns_stub_tcp_fd = fd;
- fd = -1;
- return m->dns_stub_tcp_fd;
+ return m->dns_stub_tcp_fd = TAKE_FD(fd);
}
int manager_dns_stub_start(Manager *m) {
* /var/lib/machines. */
fd = open("/var/lib/machines.raw", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
- if (fd >= 0) {
- r = fd;
- fd = -1;
- return r;
- }
+ if (fd >= 0)
+ return TAKE_FD(fd);
if (errno != ENOENT)
return sd_bus_error_set_errnof(error, errno, "Failed to open /var/lib/machines.raw: %m");
goto fail;
}
- r = fd;
- fd = -1;
-
- return r;
+ return TAKE_FD(fd);
fail:
unlink_noerrno(tmp);
static int find_source_vc(char **ret_path, unsigned *ret_idx) {
_cleanup_free_ char *path = NULL;
+ int r, err = 0;
unsigned i;
- int ret_fd, r, err = 0;
path = new(char, sizeof("/dev/tty63"));
if (!path)
/* all checks passed, return this one as a source console */
*ret_idx = i;
*ret_path = TAKE_PTR(path);
- ret_fd = fd;
- fd = -1;
- return ret_fd;
+ return TAKE_FD(fd);
}
return log_error_errno(err, "No usable source console found: %m");
}
static int verify_source_vc(char **ret_path, const char *src_vc) {
- char *path;
_cleanup_close_ int fd = -1;
- int ret_fd, r;
+ char *path;
+ int r;
fd = open_terminal(src_vc, O_RDWR|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
return log_oom();
*ret_path = path;
- ret_fd = fd;
- fd = -1;
- return ret_fd;
+ return TAKE_FD(fd);
}
int main(int argc, char **argv) {