macro: introduce new TAKE_FD() macro

This is similar to TAKE_PTR() but operates on file descriptors, and thus
assigns -1 to the fd parameter after returning it.

Removes 60 lines from our codebase. Pretty good too I think.
This commit is contained in:
Lennart Poettering 2018-03-22 17:04:29 +01:00
parent 2f4cefe6ce
commit c10d6bdb89
25 changed files with 64 additions and 130 deletions

14
coccinelle/take-fd.cocci Normal file
View File

@ -0,0 +1,14 @@
@@
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);

View File

@ -484,10 +484,7 @@ int acquire_data_fd(const void *data, size_t size, unsigned flags) {
if (r < 0) if (r < 0)
return r; return r;
r = fd; return TAKE_FD(fd);
fd = -1;
return r;
} }
try_pipe: try_pipe:
@ -524,10 +521,7 @@ try_pipe:
(void) fd_nonblock(pipefds[0], false); (void) fd_nonblock(pipefds[0], false);
r = pipefds[0]; return TAKE_FD(pipefds[0]);
pipefds[0] = -1;
return r;
} }
try_dev_shm: try_dev_shm:

View File

@ -105,3 +105,11 @@ int rearrange_stdio(int original_input_fd, int original_output_fd, int original_
static inline int make_null_stdio(void) { static inline int make_null_stdio(void) {
return rearrange_stdio(-1, -1, -1); 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_; \
})

View File

@ -730,8 +730,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
} }
safe_close(fd); safe_close(fd);
fd = fd_parent; fd = TAKE_FD(fd_parent);
fd_parent = -1;
continue; continue;
} }
@ -849,8 +848,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
/* And iterate again, but go one directory further down. */ /* And iterate again, but go one directory further down. */
safe_close(fd); safe_close(fd);
fd = child; fd = TAKE_FD(child);
child = -1;
} }
if (!done) { if (!done) {
@ -864,16 +862,11 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
*ret = TAKE_PTR(done); *ret = TAKE_PTR(done);
if (flags & CHASE_OPEN) { 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 /* 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. */ * opening /proc/self/fd/xyz. */
assert(fd >= 0); assert(fd >= 0);
q = fd; return TAKE_FD(fd);
fd = -1;
return q;
} }
return exists; return exists;

View File

@ -168,8 +168,5 @@ int memfd_new_and_map(const char *name, size_t sz, void **p) {
if (r < 0) if (r < 0)
return r; return r;
r = fd; return TAKE_FD(fd);
fd = -1;
return r;
} }

View File

@ -496,10 +496,7 @@ int acquire_terminal(
fd = safe_close(fd); fd = safe_close(fd);
} }
r = fd; return TAKE_FD(fd);
fd = -1;
return r;
} }
int release_terminal(void) { int release_terminal(void) {

View File

@ -319,10 +319,7 @@ static int pick_uid(char **suggested_paths, const char *name, uid_t *ret_uid) {
(void) make_uid_symlinks(candidate, name, true); /* also add direct lookup symlinks */ (void) make_uid_symlinks(candidate, name, true); /* also add direct lookup symlinks */
*ret_uid = candidate; *ret_uid = candidate;
r = lock_fd; return TAKE_FD(lock_fd);
lock_fd = -1;
return r;
next: next:
; ;

View File

@ -844,8 +844,7 @@ static int manager_setup_notify(Manager *m) {
if (r < 0) if (r < 0)
return log_error_errno(errno, "SO_PASSCRED failed: %m"); return log_error_errno(errno, "SO_PASSCRED failed: %m");
m->notify_fd = fd; m->notify_fd = TAKE_FD(fd);
fd = -1;
log_debug("Using notification socket %s", m->notify_socket); log_debug("Using notification socket %s", m->notify_socket);
} }

View File

@ -1238,10 +1238,7 @@ static int fifo_address_create(
goto fail; goto fail;
} }
r = fd; return TAKE_FD(fd);
fd = -1;
return r;
fail: fail:
mac_selinux_create_file_clear(); mac_selinux_create_file_clear();
@ -1251,7 +1248,6 @@ fail:
static int special_address_create(const char *path, bool writable) { static int special_address_create(const char *path, bool writable) {
_cleanup_close_ int fd = -1; _cleanup_close_ int fd = -1;
struct stat st; struct stat st;
int r;
assert(path); assert(path);
@ -1266,16 +1262,12 @@ static int special_address_create(const char *path, bool writable) {
if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode))
return -EEXIST; return -EEXIST;
r = fd; return TAKE_FD(fd);
fd = -1;
return r;
} }
static int usbffs_address_create(const char *path) { static int usbffs_address_create(const char *path) {
_cleanup_close_ int fd = -1; _cleanup_close_ int fd = -1;
struct stat st; struct stat st;
int r;
assert(path); assert(path);
@ -1290,10 +1282,7 @@ static int usbffs_address_create(const char *path) {
if (!S_ISREG(st.st_mode)) if (!S_ISREG(st.st_mode))
return -EEXIST; return -EEXIST;
r = fd; return TAKE_FD(fd);
fd = -1;
return r;
} }
static int mq_address_create( static int mq_address_create(
@ -1306,7 +1295,6 @@ static int mq_address_create(
struct stat st; struct stat st;
mode_t old_mask; mode_t old_mask;
struct mq_attr _attr, *attr = NULL; struct mq_attr _attr, *attr = NULL;
int r;
assert(path); assert(path);
@ -1338,10 +1326,7 @@ static int mq_address_create(
st.st_gid != getgid()) st.st_gid != getgid())
return -EEXIST; return -EEXIST;
r = fd; return TAKE_FD(fd);
fd = -1;
return r;
} }
static int socket_symlink(Socket *s) { static int socket_symlink(Socket *s) {

View File

@ -326,13 +326,10 @@ int raw_export_start(RawExport *e, const char *path, int fd, ImportCompressType
/* Try to take a reflink snapshot of the file, if we can t make the export atomic */ /* Try to take a reflink snapshot of the file, if we can t make the export atomic */
tfd = reflink_snapshot(sfd, path); tfd = reflink_snapshot(sfd, path);
if (tfd >= 0) { if (tfd >= 0)
e->input_fd = tfd; e->input_fd = TAKE_FD(tfd);
tfd = -1; else
} else { e->input_fd = TAKE_FD(sfd);
e->input_fd = sfd;
sfd = -1;
}
r = import_compress_init(&e->compress, compress); r = import_compress_init(&e->compress, compress);
if (r < 0) if (r < 0)

View File

@ -117,13 +117,9 @@ int import_fork_tar_x(const char *path, pid_t *ret) {
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
pipefd[0] = safe_close(pipefd[0]);
r = pipefd[1];
pipefd[1] = -1;
*ret = pid; *ret = pid;
return r; return TAKE_FD(pipefd[1]);
} }
int import_fork_tar_c(const char *path, pid_t *ret) { int import_fork_tar_c(const char *path, pid_t *ret) {
@ -165,11 +161,7 @@ int import_fork_tar_c(const char *path, pid_t *ret) {
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
pipefd[1] = safe_close(pipefd[1]);
r = pipefd[0];
pipefd[0] = -1;
*ret = pid; *ret = pid;
return r; return TAKE_FD(pipefd[0]);
} }

View File

@ -210,8 +210,7 @@ static int raw_import_maybe_convert_qcow2(RawImport *i) {
free_and_replace(i->temp_path, t); free_and_replace(i->temp_path, t);
safe_close(i->output_fd); safe_close(i->output_fd);
i->output_fd = converted_fd; i->output_fd = TAKE_FD(converted_fd);
converted_fd = -1;
return 1; return 1;
} }

View File

@ -453,8 +453,7 @@ static int transfer_start(Transfer *t) {
} }
pipefd[1] = safe_close(pipefd[1]); pipefd[1] = safe_close(pipefd[1]);
t->log_fd = pipefd[0]; t->log_fd = TAKE_FD(pipefd[0]);
pipefd[0] = -1;
t->stdin_fd = safe_close(t->stdin_fd); t->stdin_fd = safe_close(t->stdin_fd);

View File

@ -272,8 +272,7 @@ static int raw_pull_maybe_convert_qcow2(RawPull *i) {
free_and_replace(i->temp_path, t); free_and_replace(i->temp_path, t);
safe_close(i->raw_job->disk_fd); safe_close(i->raw_job->disk_fd);
i->raw_job->disk_fd = converted_fd; i->raw_job->disk_fd = TAKE_FD(converted_fd);
converted_fd = -1;
return 1; return 1;
} }

View File

@ -441,9 +441,7 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve
if (r < 0) if (r < 0)
return r; return r;
r = fd; return TAKE_FD(fd);
fd = -1;
return r;
} }
_public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) { _public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {

View File

@ -103,10 +103,7 @@ int arp_network_bind_raw_socket(int ifindex, be32_t address, const struct ether_
if (r < 0) if (r < 0)
return -errno; return -errno;
r = s; return TAKE_FD(s);
s = -1;
return r;
} }
static int arp_send_packet(int fd, int ifindex, static int arp_send_packet(int fd, int ifindex,

View File

@ -123,10 +123,7 @@ static int _bind_raw_socket(int ifindex, union sockaddr_union *link,
if (r < 0) if (r < 0)
return -errno; return -errno;
r = s; return TAKE_FD(s);
s = -1;
return r;
} }
int dhcp_network_bind_raw_socket(int ifindex, union sockaddr_union *link, int dhcp_network_bind_raw_socket(int ifindex, union sockaddr_union *link,
@ -211,10 +208,7 @@ int dhcp_network_bind_udp_socket(int ifindex, be32_t address, uint16_t port) {
if (r < 0) if (r < 0)
return -errno; return -errno;
r = s; return TAKE_FD(s);
s = -1;
return r;
} }
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link, int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,

View File

@ -67,9 +67,7 @@ int dhcp6_network_bind_udp_socket(int index, struct in6_addr *local_address) {
if (r < 0) if (r < 0)
return -errno; return -errno;
r = s; return TAKE_FD(s);
s = -1;
return r;
} }
int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address, int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address,

View File

@ -98,9 +98,7 @@ static int icmp6_bind_router_message(const struct icmp6_filter *filter,
if (r < 0) if (r < 0)
return -errno; return -errno;
r = s; return TAKE_FD(s);
s = -1;
return r;
} }
int icmp6_bind_router_solicitation(int index) { int icmp6_bind_router_solicitation(int index) {

View File

@ -92,8 +92,5 @@ int lldp_network_bind_raw_socket(int ifindex) {
if (r < 0) if (r < 0)
return -errno; return -errno;
r = fd; return TAKE_FD(fd);
fd = -1;
return r;
} }

View File

@ -1145,8 +1145,7 @@ void session_restore_vt(Session *s) {
* little dance to avoid having the terminal be available * little dance to avoid having the terminal be available
* for reuse before we've cleaned it up. * for reuse before we've cleaned it up.
*/ */
old_fd = s->vtfd; old_fd = TAKE_FD(s->vtfd);
s->vtfd = -1;
vt = session_open_vt(s); vt = session_open_vt(s);
safe_close(old_fd); safe_close(old_fd);

View File

@ -325,7 +325,7 @@ static int dns_scope_socket(
union sockaddr_union sa = {}; union sockaddr_union sa = {};
socklen_t salen; socklen_t salen;
static const int one = 1; static const int one = 1;
int ret, r, ifindex; int r, ifindex;
assert(s); assert(s);
@ -409,10 +409,7 @@ static int dns_scope_socket(
if (r < 0 && errno != EINPROGRESS) if (r < 0 && errno != EINPROGRESS)
return -errno; return -errno;
ret = fd; return TAKE_FD(fd);
fd = -1;
return ret;
} }
int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port) { int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port) {

View File

@ -447,10 +447,8 @@ static int manager_dns_stub_udp_fd(Manager *m) {
return r; return r;
(void) sd_event_source_set_description(m->dns_stub_udp_event_source, "dns-stub-udp"); (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) { static int on_dns_stub_stream_packet(DnsStream *s) {
@ -542,10 +540,8 @@ static int manager_dns_stub_tcp_fd(Manager *m) {
return r; return r;
(void) sd_event_source_set_description(m->dns_stub_tcp_event_source, "dns-stub-tcp"); (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) { int manager_dns_stub_start(Manager *m) {

View File

@ -89,11 +89,8 @@ static int setup_machine_raw(uint64_t size, sd_bus_error *error) {
* /var/lib/machines. */ * /var/lib/machines. */
fd = open("/var/lib/machines.raw", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); fd = open("/var/lib/machines.raw", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (fd >= 0) { if (fd >= 0)
r = fd; return TAKE_FD(fd);
fd = -1;
return r;
}
if (errno != ENOENT) if (errno != ENOENT)
return sd_bus_error_set_errnof(error, errno, "Failed to open /var/lib/machines.raw: %m"); return sd_bus_error_set_errnof(error, errno, "Failed to open /var/lib/machines.raw: %m");
@ -162,10 +159,7 @@ static int setup_machine_raw(uint64_t size, sd_bus_error *error) {
goto fail; goto fail;
} }
r = fd; return TAKE_FD(fd);
fd = -1;
return r;
fail: fail:
unlink_noerrno(tmp); unlink_noerrno(tmp);

View File

@ -325,8 +325,8 @@ static void setup_remaining_vcs(int src_fd, unsigned src_idx, bool utf8) {
static int find_source_vc(char **ret_path, unsigned *ret_idx) { static int find_source_vc(char **ret_path, unsigned *ret_idx) {
_cleanup_free_ char *path = NULL; _cleanup_free_ char *path = NULL;
int r, err = 0;
unsigned i; unsigned i;
int ret_fd, r, err = 0;
path = new(char, sizeof("/dev/tty63")); path = new(char, sizeof("/dev/tty63"));
if (!path) if (!path)
@ -359,18 +359,16 @@ static int find_source_vc(char **ret_path, unsigned *ret_idx) {
/* all checks passed, return this one as a source console */ /* all checks passed, return this one as a source console */
*ret_idx = i; *ret_idx = i;
*ret_path = TAKE_PTR(path); *ret_path = TAKE_PTR(path);
ret_fd = fd; return TAKE_FD(fd);
fd = -1;
return ret_fd;
} }
return log_error_errno(err, "No usable source console found: %m"); return log_error_errno(err, "No usable source console found: %m");
} }
static int verify_source_vc(char **ret_path, const char *src_vc) { static int verify_source_vc(char **ret_path, const char *src_vc) {
char *path;
_cleanup_close_ int fd = -1; _cleanup_close_ int fd = -1;
int ret_fd, r; char *path;
int r;
fd = open_terminal(src_vc, O_RDWR|O_CLOEXEC|O_NOCTTY); fd = open_terminal(src_vc, O_RDWR|O_CLOEXEC|O_NOCTTY);
if (fd < 0) if (fd < 0)
@ -393,9 +391,7 @@ static int verify_source_vc(char **ret_path, const char *src_vc) {
return log_oom(); return log_oom();
*ret_path = path; *ret_path = path;
ret_fd = fd; return TAKE_FD(fd);
fd = -1;
return ret_fd;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {