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)
return r;
r = fd;
fd = -1;
return r;
return TAKE_FD(fd);
}
try_pipe:
@@ -524,10 +521,7 @@ try_pipe:
(void) fd_nonblock(pipefds[0], false);
r = pipefds[0];
pipefds[0] = -1;
return r;
return TAKE_FD(pipefds[0]);
}
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) {
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);
fd = fd_parent;
fd_parent = -1;
fd = TAKE_FD(fd_parent);
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. */
safe_close(fd);
fd = child;
child = -1;
fd = TAKE_FD(child);
}
if (!done) {
@@ -864,16 +862,11 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
*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;

View File

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

View File

@@ -496,10 +496,7 @@ int acquire_terminal(
fd = safe_close(fd);
}
r = fd;
fd = -1;
return r;
return TAKE_FD(fd);
}
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 */
*ret_uid = candidate;
r = lock_fd;
lock_fd = -1;
return r;
return TAKE_FD(lock_fd);
next:
;

View File

@@ -844,8 +844,7 @@ static int manager_setup_notify(Manager *m) {
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);
}

View File

@@ -1238,10 +1238,7 @@ static int fifo_address_create(
goto fail;
}
r = fd;
fd = -1;
return r;
return TAKE_FD(fd);
fail:
mac_selinux_create_file_clear();
@@ -1251,7 +1248,6 @@ fail:
static int special_address_create(const char *path, bool writable) {
_cleanup_close_ int fd = -1;
struct stat st;
int r;
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))
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);
@@ -1290,10 +1282,7 @@ static int usbffs_address_create(const char *path) {
if (!S_ISREG(st.st_mode))
return -EEXIST;
r = fd;
fd = -1;
return r;
return TAKE_FD(fd);
}
static int mq_address_create(
@@ -1306,7 +1295,6 @@ static int mq_address_create(
struct stat st;
mode_t old_mask;
struct mq_attr _attr, *attr = NULL;
int r;
assert(path);
@@ -1338,10 +1326,7 @@ static int mq_address_create(
st.st_gid != getgid())
return -EEXIST;
r = fd;
fd = -1;
return r;
return TAKE_FD(fd);
}
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 */
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)

View File

@@ -117,13 +117,9 @@ int import_fork_tar_x(const char *path, pid_t *ret) {
_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) {
@@ -165,11 +161,7 @@ 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]);
}

View File

@@ -210,8 +210,7 @@ static int raw_import_maybe_convert_qcow2(RawImport *i) {
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;
}

View File

@@ -453,8 +453,7 @@ static int transfer_start(Transfer *t) {
}
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);

View File

@@ -272,8 +272,7 @@ static int raw_pull_maybe_convert_qcow2(RawPull *i) {
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;
}

View File

@@ -441,9 +441,7 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve
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, ...) {

View File

@@ -103,10 +103,7 @@ int arp_network_bind_raw_socket(int ifindex, be32_t address, const struct ether_
if (r < 0)
return -errno;
r = s;
s = -1;
return r;
return TAKE_FD(s);
}
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)
return -errno;
r = s;
s = -1;
return r;
return TAKE_FD(s);
}
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)
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,

View File

@@ -67,9 +67,7 @@ int dhcp6_network_bind_udp_socket(int index, struct in6_addr *local_address) {
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,

View File

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

View File

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

Some files were not shown because too many files have changed in this diff Show More