mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-16 13:08:25 +00:00
Cleanup previous PR
Fix problems found while reviewing the EINTR handling changes to safe-wrappers.h merged from PR #10615. - safe_connect: the PR waited for POLLOUT and read SO_ERROR after an EINTR from connect(). That only works for sockets where an interrupted connect continues in the background, such as TCP. AF_UNIX stream sockets on Linux are left unconnected but writable, so safe_connect returned success on an unconnected socket. This broke the single instance launcher when the server's listen backlog was full. Now connect() is retried after EINTR, EISCONN on a retry is treated as success, and poll()+SO_ERROR is used only on EALREADY. While waiting, SO_SNDTIMEO is honored (with a monotonic deadline across EINTR from poll) instead of polling with an infinite timeout. The EAGAIN retry the PR dropped is restored. - safe_mkstemp: rather than copying the template into a PATH_MAX buffer, which rejected templates longer than 1024 bytes on macOS/BSD, restore only the trailing X characters when mkostemp() is interrupted. Use mkostemp(O_CLOEXEC) on all platforms. macOS has had it since 10.12 and disk-cache.c already relied on it. This removes the duplicated platform #if blocks and the racy mkstemp+fcntl(FD_CLOEXEC) fallback. - disk-cache: open_cache_file_without_tmpfile had its own mkostemp EINTR loop with the same template corruption bug. Use safe_mkstemp instead. - Add safe_write_all() and use it in place of the hand-written write loops in dnd.c and the single instance launcher. Also fix finish_remote_data() in dnd.c, which passed -1 instead of errno to abrt() when writing the uri list failed.
This commit is contained in:
parent
448aeefe5d
commit
5421df1cd3
4 changed files with 76 additions and 71 deletions
|
|
@ -160,10 +160,7 @@ open_cache_file_without_tmpfile(const char *cache_path) {
|
|||
return -1;
|
||||
}
|
||||
snprintf(buf, sz - 1, template, cache_path);
|
||||
while (fd < 0) {
|
||||
fd = mkostemp(buf, O_CLOEXEC);
|
||||
if (fd > -1 || errno != EINTR) break;
|
||||
}
|
||||
fd = safe_mkstemp(buf);
|
||||
if (fd > -1) unlink(buf);
|
||||
return fd;
|
||||
}
|
||||
|
|
|
|||
18
kitty/dnd.c
18
kitty/dnd.c
|
|
@ -2182,18 +2182,6 @@ drag_process_item_data(Window *w, size_t idx, int has_more, const uint8_t *paylo
|
|||
}
|
||||
}
|
||||
|
||||
static int
|
||||
write_all(int fd, const void *buf, size_t sz) {
|
||||
size_t pos = 0;
|
||||
const char *p = buf;
|
||||
while (pos < sz) {
|
||||
ssize_t ret = safe_write(fd, p + pos, sz - pos);
|
||||
if (ret < 0) return ret;
|
||||
pos += ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
finish_remote_data(Window *w, size_t item_idx) {
|
||||
if (!ds.items[item_idx].fd_plus_one) {
|
||||
|
|
@ -2207,12 +2195,12 @@ finish_remote_data(Window *w, size_t item_idx) {
|
|||
if (lseek(fd, 0, SEEK_SET) == -1) abrt(errno, "error updating uri list after all remote data received");
|
||||
size_t new_size = 0;
|
||||
for (size_t i = 0; i < ds.items[item_idx].num_uris; i++) {
|
||||
int ret = write_all(fd, ds.items[item_idx].uri_list[i], strlen(ds.items[item_idx].uri_list[i]));
|
||||
int ret = safe_write_all(fd, ds.items[item_idx].uri_list[i], strlen(ds.items[item_idx].uri_list[i])) ? errno : 0;
|
||||
new_size += strlen(ds.items[item_idx].uri_list[i]);
|
||||
free((char *)ds.items[item_idx].uri_list[i]);
|
||||
ds.items[item_idx].uri_list[i] = NULL;
|
||||
if (ret) abrt(ret, "error updating uri list after all remote data received");
|
||||
if ((ret = write_all(fd, "\r\n", 2))) abrt(ret, "error updating uri list after all remote data received");
|
||||
if (safe_write_all(fd, "\r\n", 2)) abrt(errno, "error updating uri list after all remote data received");
|
||||
new_size += 2;
|
||||
}
|
||||
free(ds.items[item_idx].uri_list);
|
||||
|
|
@ -2351,7 +2339,7 @@ add_payload(Window *w, DragRemoteItem *ri, bool has_more, const uint8_t *payload
|
|||
size_t outlen = sizeof(buf);
|
||||
if (!base64_decode_stream(&ri->base64_state, payload, payload_sz, buf, &outlen)) abrt(EINVAL, "could not base64 decode drag source item data");
|
||||
ds.total_remote_data_size += outlen;
|
||||
if (outlen && write_all(ri->fd_plus_one - 1, buf, outlen) < 0) abrt(errno, "could not write drag source item data to file");
|
||||
if (outlen && safe_write_all(ri->fd_plus_one - 1, buf, outlen) < 0) abrt(errno, "could not write drag source item data to file");
|
||||
} break;
|
||||
default: {
|
||||
if (ri->data_sz + payload_sz > ri->data_capacity) {
|
||||
|
|
|
|||
|
|
@ -381,17 +381,7 @@ talk_to_instance(int s, struct sockaddr_un *server_addr, int argc, char *argv[],
|
|||
// Check the identity of the listening process before sending it our
|
||||
// environment, cwd and session data.
|
||||
if (!peer_is_same_user(s)) do_exit(1);
|
||||
size_t pos = 0;
|
||||
while (pos < output.used) {
|
||||
errno = 0;
|
||||
ssize_t nbytes = write(s, output.data + pos, output.used - pos);
|
||||
if (nbytes <= 0) {
|
||||
if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) continue;
|
||||
break;
|
||||
}
|
||||
pos += nbytes;
|
||||
}
|
||||
if (pos < output.used) fail_on_errno("Failed to write message to single instance socket");
|
||||
if (safe_write_all(s, output.data, output.used) != 0) fail_on_errno("Failed to write message to single instance socket");
|
||||
shutdown(s, SHUT_RDWR);
|
||||
safe_close(s, __FILE__, __LINE__);
|
||||
cleanup_entries.si.close_fd1 = false;
|
||||
|
|
|
|||
|
|
@ -15,7 +15,9 @@
|
|||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static inline int
|
||||
|
|
@ -28,27 +30,57 @@ safe_lockf(int fd, int function, off_t size) {
|
|||
}
|
||||
|
||||
static inline int
|
||||
safe_connect(int socket_fd, struct sockaddr *addr, socklen_t addrlen) {
|
||||
/* If connect() is interrupted by EINTR on a stream socket, the connection
|
||||
* continues asynchronously in the background. Retrying connect() directly
|
||||
* fails with EISCONN or EALREADY. Instead, wait for writability with poll()
|
||||
* and query SO_ERROR to obtain the final connection outcome. */
|
||||
int ret = connect(socket_fd, addr, addrlen);
|
||||
if (ret < 0 && errno == EINTR) {
|
||||
struct pollfd pfd = {.fd = socket_fd, .events = POLLOUT};
|
||||
while (poll(&pfd, 1, -1) < 0) {
|
||||
if (errno != EINTR) return -1;
|
||||
}
|
||||
int err = 0;
|
||||
socklen_t errlen = sizeof(err);
|
||||
if (getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0) return -1;
|
||||
if (err != 0) {
|
||||
errno = err;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
wait_for_pending_connect(int socket_fd) {
|
||||
// Wait for a connect() that is continuing in the background to complete,
|
||||
// honoring any send timeout set on the socket, as a blocking connect() would.
|
||||
struct timeval tv = {0};
|
||||
socklen_t sz = sizeof(tv);
|
||||
int64_t deadline = 0;
|
||||
if (getsockopt(socket_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, &sz) == 0 && (tv.tv_sec > 0 || tv.tv_usec > 0)) {
|
||||
struct timespec now;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) return -1;
|
||||
deadline = (int64_t)now.tv_sec * 1000000000ll + now.tv_nsec + (int64_t)tv.tv_sec * 1000000000ll + (int64_t)tv.tv_usec * 1000ll;
|
||||
}
|
||||
struct pollfd pfd = {.fd = socket_fd, .events = POLLOUT};
|
||||
while (true) {
|
||||
int timeout_ms = -1;
|
||||
if (deadline) {
|
||||
struct timespec now;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) return -1;
|
||||
int64_t remaining = deadline - ((int64_t)now.tv_sec * 1000000000ll + now.tv_nsec);
|
||||
if (remaining <= 0) { errno = ETIMEDOUT; return -1; }
|
||||
remaining = (remaining + 999999ll) / 1000000ll;
|
||||
timeout_ms = remaining > INT_MAX ? INT_MAX : (int)remaining;
|
||||
}
|
||||
int ret = poll(&pfd, 1, timeout_ms);
|
||||
if (ret > 0) break;
|
||||
if (ret < 0 && errno != EINTR) return -1;
|
||||
}
|
||||
int err = 0;
|
||||
socklen_t errlen = sizeof(err);
|
||||
if (getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0) return -1;
|
||||
if (err != 0) { errno = err; return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
safe_connect(int socket_fd, struct sockaddr *addr, socklen_t addrlen) {
|
||||
/* What happens after connect() is interrupted by EINTR depends on the
|
||||
* socket type. AF_UNIX sockets on Linux are left unconnected and connect()
|
||||
* must simply be retried. For TCP the connection continues in the
|
||||
* background, so a retry fails with EALREADY while it is in progress or
|
||||
* EISCONN once it has succeeded. */
|
||||
bool interrupted = false;
|
||||
while (true) {
|
||||
if (connect(socket_fd, addr, addrlen) == 0) return 0;
|
||||
switch (errno) {
|
||||
case EINTR: interrupted = true; continue;
|
||||
case EAGAIN: continue;
|
||||
case EISCONN: return interrupted ? 0 : -1;
|
||||
case EALREADY: return interrupted ? wait_for_pending_connect(socket_fd) : -1;
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline int
|
||||
|
|
@ -71,32 +103,17 @@ safe_accept(int socket_fd, struct sockaddr *addr, socklen_t *addrlen) {
|
|||
|
||||
static inline int
|
||||
safe_mkstemp(char *template) {
|
||||
/* mkstemp and mkostemp modify the template buffer in place. Preserve a copy
|
||||
* of the original template so it can be restored if interrupted by EINTR. */
|
||||
size_t len = strlen(template);
|
||||
char saved[PATH_MAX];
|
||||
if (len >= sizeof(saved)) {
|
||||
errno = ENAMETOOLONG;
|
||||
return -1;
|
||||
}
|
||||
memcpy(saved, template, len + 1);
|
||||
|
||||
/* mkostemp() replaces the trailing X characters of the template in place.
|
||||
* Restore them if it is interrupted by EINTR, otherwise the retry fails
|
||||
* with EINVAL. */
|
||||
size_t len = strlen(template), num_x = 0;
|
||||
while (num_x < len && template[len - 1 - num_x] == 'X') num_x++;
|
||||
while (true) {
|
||||
#if defined(__linux__) || defined(_GNU_SOURCE) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
||||
int fd = mkostemp(template, O_CLOEXEC);
|
||||
#else
|
||||
int fd = mkstemp(template);
|
||||
#endif
|
||||
if (fd == -1 && errno == EINTR) {
|
||||
memcpy(template, saved, len + 1);
|
||||
memset(template + len - num_x, 'X', num_x);
|
||||
continue;
|
||||
}
|
||||
#if !defined(__linux__) && !defined(_GNU_SOURCE) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
|
||||
if (fd > -1) {
|
||||
int flags = fcntl(fd, F_GETFD);
|
||||
if (flags > -1) fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
|
||||
}
|
||||
#endif
|
||||
return fd;
|
||||
}
|
||||
}
|
||||
|
|
@ -163,6 +180,19 @@ safe_write(int fd, const void *buf, size_t nbyte) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
// Write all of buf, returns 0 on success and -1 on failure with errno set
|
||||
static inline int
|
||||
safe_write_all(int fd, const void *buf, size_t nbyte) {
|
||||
const char *p = buf;
|
||||
while (nbyte > 0) {
|
||||
ssize_t ret = safe_write(fd, p, nbyte);
|
||||
if (ret < 0) return -1;
|
||||
if (ret == 0) { errno = EIO; return -1; }
|
||||
p += ret; nbyte -= (size_t)ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
safe_dup(int a) {
|
||||
int ret;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue