If the process does an exec() (or fork, or..) all descriptors are kept open by default, unless O_CLOEXEC is set when opening them. This is usefull for stdin/out/err so that new process is connected to them, but it is very rare for it to be usefull for any other descriptor. In general this leads to descriptors being kept open unnecessarily, which either will block other process from getting them (for example if the child survives the parent but it does something else). Or for a "leak" which unnecessarily uses descriptors and memory in the child process. Let's ensure we do not leak it for this component as we do not need it. Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
54 lines
1010 B
C
54 lines
1010 B
C
/*
|
|
* Copyright 2023 Google LLC
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <linux/input.h>
|
|
#include <nsi_tracing.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "linux_evdev_bottom.h"
|
|
|
|
int linux_evdev_read(int fd, uint16_t *type, uint16_t *code, int32_t *value)
|
|
{
|
|
struct input_event ev;
|
|
int ret;
|
|
|
|
ret = read(fd, &ev, sizeof(ev));
|
|
if (ret < 0) {
|
|
if (errno == EAGAIN || errno == EINTR) {
|
|
return NATIVE_LINUX_EVDEV_NO_DATA;
|
|
}
|
|
nsi_print_warning("Read error: %s", strerror(errno));
|
|
return -EIO;
|
|
} else if (ret < sizeof(ev)) {
|
|
nsi_print_warning("Unexpected read size: %d, expecting %d",
|
|
ret, sizeof(ev));
|
|
return -EIO;
|
|
}
|
|
|
|
*type = ev.type;
|
|
*code = ev.code;
|
|
*value = ev.value;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int linux_evdev_open(const char *path)
|
|
{
|
|
int fd;
|
|
|
|
fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
|
if (fd < 0) {
|
|
nsi_print_error_and_exit(
|
|
"Failed to open the evdev device %s: %s\n",
|
|
path, strerror(errno));
|
|
}
|
|
|
|
return fd;
|
|
}
|