Use EINTR safe wrappers for open() and shm_open()

This commit is contained in:
Kovid Goyal 2021-01-31 07:02:11 +05:30
parent 231f054bc3
commit d55fde9eea
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
8 changed files with 47 additions and 11 deletions

View File

@ -6,6 +6,7 @@
*/
#include "loop-utils.h"
#include "safe-wrappers.h"
#include "state.h"
#include "threading.h"
#include "screen.h"

View File

@ -6,6 +6,7 @@
*/
#include "data-types.h"
#include "safe-wrappers.h"
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
@ -99,7 +100,7 @@ spawn(PyObject *self UNUSED, PyObject *args) {
if (setsid() == -1) exit_on_err("setsid() in child process failed");
// Establish the controlling terminal (see man 7 credentials)
int tfd = open(name, O_RDWR);
int tfd = safe_open(name, O_RDWR, 0);
if (tfd == -1) exit_on_err("Failed to open controlling terminal");
#ifdef TIOCSCTTY
// On BSD open() does not establish the controlling terminal

View File

@ -12,6 +12,7 @@
#undef _DARWIN_C_SOURCE
#endif
#include "data-types.h"
#include "safe-wrappers.h"
#include "control-codes.h"
#include "wcwidth-std.h"
#include "wcswidth.h"
@ -110,7 +111,7 @@ open_tty(PyObject *self UNUSED, PyObject *args) {
int flags = O_RDWR | O_CLOEXEC | O_NOCTTY;
if (!read_with_timeout) flags |= O_NONBLOCK;
static char ctty[L_ctermid+1];
int fd = open(ctermid(ctty), flags);
int fd = safe_open(ctermid(ctty), flags, 0);
if (fd == -1) { PyErr_Format(PyExc_OSError, "Failed to open controlling terminal: %s (identified with ctermid()) with error: %s", ctty, strerror(errno)); return NULL; }
struct termios *termios_p = calloc(1, sizeof(struct termios));
if (!termios_p) return PyErr_NoMemory();

View File

@ -329,10 +329,4 @@ SPRITE_MAP_HANDLE alloc_sprite_map(unsigned int, unsigned int);
SPRITE_MAP_HANDLE free_sprite_map(SPRITE_MAP_HANDLE);
const char* get_hyperlink_for_id(const HYPERLINK_POOL_HANDLE, hyperlink_id_type id, bool only_url);
static inline void safe_close(int fd, const char* file UNUSED, const int line UNUSED) {
#if 0
printf("Closing fd: %d from file: %s line: %d\n", fd, file, line);
#endif
while(close(fd) != 0 && errno == EINTR);
}
void log_event(const char *format, ...) __attribute__((format(printf, 1, 2)));

View File

@ -12,6 +12,7 @@
#endif
#include "disk-cache.h"
#include "safe-wrappers.h"
#include "uthash.h"
#include "loop-utils.h"
#include "threading.h"
@ -85,7 +86,7 @@ open_cache_file(const char *cache_path) {
int fd = -1;
#ifdef O_TMPFILE
while (fd < 0) {
fd = open(cache_path, O_TMPFILE | O_CLOEXEC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
fd = safe_open(cache_path, O_TMPFILE | O_CLOEXEC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
if (fd > -1 || errno != EINTR) break;
}
#else

View File

@ -9,6 +9,7 @@
#include "state.h"
#include "disk-cache.h"
#include "iqsort.h"
#include "safe-wrappers.h"
#include <sys/types.h>
#include <sys/stat.h>
@ -371,8 +372,8 @@ load_image_data(GraphicsManager *self, Image *img, const GraphicsCommand *g, con
case 's': // POSIX shared memory
if (g->payload_sz > 2048) ABRT(EINVAL, "Filename too long");
snprintf(fname, sizeof(fname)/sizeof(fname[0]), "%.*s", (int)g->payload_sz, payload);
if (transmission_type == 's') fd = shm_open(fname, O_RDONLY, 0);
else fd = open(fname, O_CLOEXEC | O_RDONLY);
if (transmission_type == 's') fd = safe_shm_open(fname, O_RDONLY, 0);
else fd = safe_open(fname, O_CLOEXEC | O_RDONLY, 0);
if (fd == -1) ABRT(EBADF, "Failed to open file for graphics transmission with error: [%d] %s", errno, strerror(errno));
img->data_loaded = mmap_img_file(self, img, fd, g->data_sz, g->data_offset);
safe_close(fd, __FILE__, __LINE__);

View File

@ -6,6 +6,7 @@
*/
#include "loop-utils.h"
#include "safe-wrappers.h"
#include <signal.h>
bool

36
kitty/safe-wrappers.h Normal file
View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2021 Kovid Goyal <kovid at kovidgoyal.net>
*
* Distributed under terms of the GPL3 license.
*/
#pragma once
#include "data-types.h"
#include <fcntl.h>
#include <sys/mman.h>
static inline int safe_open(const char *path, int flags, mode_t mode) {
while (true) {
int fd = open(path, flags, mode);
if (fd == -1 && errno == EINTR) continue;
return fd;
}
}
static inline int safe_shm_open(const char *path, int flags, mode_t mode) {
while (true) {
int fd = shm_open(path, flags, mode);
if (fd == -1 && errno == EINTR) continue;
return fd;
}
}
static inline void safe_close(int fd, const char* file UNUSED, const int line UNUSED) {
#if 0
printf("Closing fd: %d from file: %s line: %d\n", fd, file, line);
#endif
while(close(fd) != 0 && errno == EINTR);
}