Skip to content

Commit eb78fde

Browse files
authored
Return EOF once after the buffer is consumed (#112)
The nat20 device now returns 0 on the first read after the entire response has been consumed to indicate EOF. Subsequent reads still return EAGAIN to indicate that a new request need to be issued before a new request can be read. The EOF state is cleared by a subsequent write even if the response was not or only partially read.
1 parent 61c5a18 commit eb78fde

1 file changed

Lines changed: 8 additions & 11 deletions

File tree

examples/linux/nat20device/nat20device.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ static ssize_t nat20device_write(struct file* filp,
182182
* nat20device_read - Read file operation
183183
*
184184
* Returns the current response buffer to userspace. Once the entire
185-
* response has been read, the buffer is freed and subsequent reads
186-
* return -EAGAIN until a new request is dispatched via write.
185+
* response has been read, the first subsequent read returns 0 (EOF)
186+
* and frees the response buffer. Subsequent reads return -EAGAIN until
187+
* a new request is dispatched via write.
187188
*/
188189
static ssize_t nat20device_read(struct file* filp, char __user* buf, size_t count, loff_t* f_pos) {
189190
struct nat20device_file_private* file_priv = filp->private_data;
@@ -197,12 +198,17 @@ static ssize_t nat20device_read(struct file* filp, char __user* buf, size_t coun
197198

198199
/* Check if we have a response buffer */
199200
if (!file_priv->response.data) {
201+
/* No response available, return -EAGAIN to indicate try again later. */
200202
ret = -EAGAIN;
201203
goto out;
202204
}
203205

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

223-
/* Response fully consumed — free it so subsequent reads
224-
* return -EAGAIN until the next write/dispatch cycle. */
225-
if (*f_pos >= file_priv->response.size) {
226-
kfree(file_priv->response.data);
227-
file_priv->response.data = NULL;
228-
file_priv->response.size = 0;
229-
}
230-
231229
ret = bytes_to_read;
232-
233230
out:
234231
mutex_unlock(&file_priv->lock);
235232
return ret;

0 commit comments

Comments
 (0)