Skip to content

Commit

Permalink
Read all events and verify dev node after adding watcher fd (#85)
Browse files Browse the repository at this point in the history
* Currently, the dev node is checked before adding the watcher FD. If
the dev node is created between this check and the addition of the
watcher FD, it will be missed by the poll. Therefore, check the dev
node’s existence after adding the watcher FD. Additionally, read all
events once the poll completes to ensure no events are missed.

* define the local pointer variable properly.

---------

Signed-off-by: Abhishek <quic_abhishes@quicinc.com>
  • Loading branch information
quic-abhishes authored Dec 19, 2024
1 parent cdcdd50 commit 1f93b9b
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/adsp_default_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,18 @@ static int fastrpc_wait_for_secure_device(int domain)
VERIFY_EPRINTF("Error: inotify_add_watch failed, invalid fd errno = %s\n", strerror(errno));
return AEE_EINVALIDFD;
}

if (fastrpc_dev_exists(dev_name))
goto bail;

memset(pfd, 0 , sizeof(pfd));
pfd[0].fd = inotify_fd;
pfd[0].events = POLLIN;

while (1) {
int ret = 0;
char buffer[EVENT_BUF_LEN];
struct inotify_event *event;

ret = poll(pfd, 1, POLL_TIMEOUT);
if(ret < 0){
Expand All @@ -160,21 +165,27 @@ static int fastrpc_wait_for_secure_device(int domain)
err = AEE_EPOLL;
break;
}
/* read on inotify fd never reads partial events. */
ssize_t len = read(inotify_fd, buffer, sizeof(buffer));
if (len < 0) {
VERIFY_EPRINTF("Error: %s: read failed, errno = %s\n", __func__, strerror(errno));
err = AEE_EEVENTREAD;
break;
}
// Check if the event corresponds to the creation of the device node.
struct inotify_event* event = (struct inotify_event*)buffer;
if (event->wd == watch_fd && (event->mask & IN_CREATE) &&
(std_strcmp(dev_name, event->name) == 0)) {
// Device node created, process proceed to open and use it.
VERIFY_IPRINTF("Device node created!\n");
break; // Exit the loop after device creation is detected.
/* Loop over all events in the buffer. */
for (char *ptr = buffer; ptr < buffer + len;
ptr += sizeof(struct inotify_event) + event->len) {
event = (struct inotify_event *) ptr;
/* Check if the event corresponds to the creation of the device node. */
if (event->wd == watch_fd && (event->mask & IN_CREATE) &&
(std_strcmp(dev_name, event->name) == 0)) {
/* Device node created, process proceed to open and use it. */
VERIFY_IPRINTF("Device node %s created!\n", event->name);
goto bail; /* Exit the loop after device creation is detected. */
}
}
}
bail:
inotify_rm_watch(inotify_fd, watch_fd);
close(inotify_fd);

Expand Down

0 comments on commit 1f93b9b

Please sign in to comment.