Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions examples/linux/nat20device/nat20device.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ static ssize_t nat20device_write(struct file* filp,
* nat20device_read - Read file operation
*
* Returns the current response buffer to userspace. Once the entire
* response has been read, the buffer is freed and subsequent reads
* return -EAGAIN until a new request is dispatched via write.
* response has been read, the first subsequent read returns 0 (EOF)
* and frees the response buffer. Subsequent reads return -EAGAIN until
* a new request is dispatched via write.
*/
static ssize_t nat20device_read(struct file* filp, char __user* buf, size_t count, loff_t* f_pos) {
struct nat20device_file_private* file_priv = filp->private_data;
Expand All @@ -197,12 +198,17 @@ static ssize_t nat20device_read(struct file* filp, char __user* buf, size_t coun

/* Check if we have a response buffer */
if (!file_priv->response.data) {
/* No response available, return -EAGAIN to indicate try again later. */
ret = -EAGAIN;
goto out;
}
Comment thread
werwurm marked this conversation as resolved.

/* Calculate bytes remaining from current offset */
if (file_priv->response.size <= *f_pos) {
/* Entire response has been consumed, free buffer and return EOF. */
kfree(file_priv->response.data);
file_priv->response.data = NULL;
file_priv->response.size = 0;
ret = 0;
goto out;
}
Expand All @@ -220,16 +226,7 @@ static ssize_t nat20device_read(struct file* filp, char __user* buf, size_t coun
/* Update offset */
*f_pos += bytes_to_read;

/* Response fully consumed — free it so subsequent reads
* return -EAGAIN until the next write/dispatch cycle. */
if (*f_pos >= file_priv->response.size) {
kfree(file_priv->response.data);
file_priv->response.data = NULL;
file_priv->response.size = 0;
}

ret = bytes_to_read;
Comment on lines 226 to 229

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So would a user space process, that doesn't or partially reads the response. Closing the file descriptor will also free the buffer.


out:
mutex_unlock(&file_priv->lock);
return ret;
Expand Down
Loading