varlink: add varlink_peek_dup_fd() helper

This new call is like varlink_peek_fd() (i.e. gets an fd out of the
connection but leaving it also in there), and combines ith with
F_DUPFD_CLOEXEC to make a copy of it.

We previously already had varlink_dup_fd() which was a duplicating
version for pushing an fd *into* the connection. To reduce confusion,
let's rename that one varlink_push_dup_fd() to make the symmetry to
valrink_push_fd() clear so that we have no:

varlink_peer_push_fd()        → put fd in without dup'ing
varlink_peer_push_dup_fd()    → same with F_DUPFD_CLOEXEC
varlink_peer_peek_fd()        → get fd out without dup'ing
varlink_peer_peek_dup_fd()    → same with F_DUPFD_CLOEXEC
This commit is contained in:
Lennart Poettering
2024-01-23 10:22:27 +01:00
parent 52bd61373b
commit b219dcd45a
2 changed files with 13 additions and 2 deletions

View File

@@ -3093,7 +3093,7 @@ int varlink_push_fd(Varlink *v, int fd) {
return i;
}
int varlink_dup_fd(Varlink *v, int fd) {
int varlink_push_dup_fd(Varlink *v, int fd) {
_cleanup_close_ int dp = -1;
int r;
@@ -3141,6 +3141,16 @@ int varlink_peek_fd(Varlink *v, size_t i) {
return v->input_fds[i];
}
int varlink_peek_dup_fd(Varlink *v, size_t i) {
int fd;
fd = varlink_peek_fd(v, i);
if (fd < 0)
return fd;
return RET_NERRNO(fcntl(fd, F_DUPFD_CLOEXEC, 3));
}
int varlink_take_fd(Varlink *v, size_t i) {
assert_return(v, -EINVAL);

View File

@@ -156,11 +156,12 @@ int varlink_dispatch(Varlink *v, JsonVariant *parameters, const JsonDispatch tab
/* Write outgoing fds into the socket (to be associated with the next enqueued message) */
int varlink_push_fd(Varlink *v, int fd);
int varlink_dup_fd(Varlink *v, int fd);
int varlink_push_dup_fd(Varlink *v, int fd);
int varlink_reset_fds(Varlink *v);
/* Read incoming fds from the socket (associated with the currently handled message) */
int varlink_peek_fd(Varlink *v, size_t i);
int varlink_peek_dup_fd(Varlink *v, size_t i);
int varlink_take_fd(Varlink *v, size_t i);
int varlink_set_allow_fd_passing_input(Varlink *v, bool b);