diff --git a/src/liboeffis.c b/src/liboeffis.c index d516650..b1855d8 100644 --- a/src/liboeffis.c +++ b/src/liboeffis.c @@ -327,7 +327,6 @@ xdp_session_path(char *sender_name, char *token) static int connect_to_eis_returned(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { - int eisfd; struct oeffis *oeffis = userdata; int rc = sd_bus_message_get_errno(m); @@ -336,27 +335,37 @@ connect_to_eis_returned(sd_bus_message *m, void *userdata, sd_bus_error *ret_err return rc; } - rc = sd_bus_message_read(m, "h", &eisfd); + int sd_eisfd; + rc = sd_bus_message_read(m, "h", &sd_eisfd); if (rc < 0) { oeffis_disconnect(oeffis, "Unable to get fd from portal: %s", strerror(-rc)); return -rc; } /* the fd is owned by the message */ - rc = xerrno(xdup(eisfd)); - if (rc < 0) { - oeffis_disconnect(oeffis, "Failed to dup fd: %s", strerror(-rc)); - return -rc; + _cleanup_close_ int eisfd = xerrno(xdup(sd_eisfd)); + if (eisfd < 0) { + oeffis_disconnect(oeffis, "Failed to dup fd: %s", strerror(-eisfd)); + return -eisfd; } else { - eisfd = rc; - int flags = fcntl(eisfd, F_GETFL, 0); - fcntl(eisfd, F_SETFL, flags | O_NONBLOCK); + int flags = xerrno(fcntl(eisfd, F_GETFL, 0)); + if (flags >= 0) + rc = xerrno(fcntl(eisfd, F_SETFL, flags | O_NONBLOCK)); + else + rc = flags; + + if (rc < 0) { + oeffis_disconnect(oeffis, + "Failed to set the fd to non-blocking: %s", + strerror(-rc)); + return -rc; + } } log_debug("Got fd %d from portal", eisfd); - rc = oeffis_set_eis_fd(oeffis, eisfd); + rc = oeffis_set_eis_fd(oeffis, steal_fd(&eisfd)); if (rc < 0) { oeffis_disconnect(oeffis, "Failed to set the fd: %s", strerror(-rc)); return -rc;