Skip to content

Commit 8833c9a

Browse files
committed
Improve error checking in pipe.
1 parent 6240081 commit 8833c9a

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

mentos/src/fs/pipe.c

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,15 @@ static ssize_t pipe_buffer_write(pipe_buffer_t *pipe_buffer, const char *src, si
483483
/// @return 1 if the process is woken up, 0 if it remains in the wait queue.
484484
int pipe_read_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync)
485485
{
486+
// Retrieve private data associated with this wait queue entry.
486487
pipe_inode_info_t *pipe_info = (pipe_inode_info_t *)wait->private;
488+
489+
// Check the private data.
490+
if (pipe_info == NULL) {
491+
pr_err("The private data is not a pipe_inode_info_t.\n");
492+
return -1;
493+
}
494+
487495
// Validate that data is available in the pipe for reading.
488496
if ((pipe_info_has_data(pipe_info) > 0) || (pipe_info->writers == 0)) {
489497
// Check if the task is in an appropriate sleep state to be woken up.
@@ -492,13 +500,13 @@ int pipe_read_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync)
492500
wait->task->state = mode;
493501

494502
// Signal that the task has been woken up.
495-
pr_debug("pipe_read_wake_function: Data available or no more writers, waking up reader %d.\n", wait->task->pid);
503+
pr_debug("Data available or no more writers, waking up reader %d.\n", wait->task->pid);
496504
return 1;
497505
} else {
498-
pr_debug("pipe_read_wake_function: Reader %d not in the correct state for wake-up.\n", wait->task->pid);
506+
pr_debug("Reader %d not in the correct state for wake-up.\n", wait->task->pid);
499507
}
500508
} else {
501-
pr_debug("pipe_read_wake_function: No data available, reader %d should continue waiting.\n", wait->task->pid);
509+
pr_debug("No data available, reader %d should continue waiting.\n", wait->task->pid);
502510
}
503511

504512
// No wake-up action taken, continue waiting.
@@ -512,21 +520,30 @@ int pipe_read_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync)
512520
/// @return 1 if the process is woken up, 0 if it remains in the wait queue.
513521
int pipe_write_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync)
514522
{
523+
// Retrieve private data associated with this wait queue entry.
524+
pipe_inode_info_t *pipe_info = (pipe_inode_info_t *)wait->private;
525+
526+
// Check the private data.
527+
if (pipe_info == NULL) {
528+
pr_err("The private data is not a pipe_inode_info_t.\n");
529+
return -1;
530+
}
531+
515532
// Check if there is available space in the pipe for writing.
516-
if (pipe_info_has_space((pipe_inode_info_t *)wait->private) > 0) {
533+
if (pipe_info_has_space(pipe_info) > 0) {
517534
// Only tasks in the state TASK_UNINTERRUPTIBLE or TASK_STOPPED can be woken up.
518535
if ((wait->task->state == TASK_UNINTERRUPTIBLE) || (wait->task->state == TASK_STOPPED)) {
519536
// Set the wake-up mode for the task.
520537
wait->task->state = mode;
521538

522539
// Signal that the task has been woken up.
523-
pr_debug("pipe_write_wake_function: Space available, waking up writer %d.\n", wait->task->pid);
540+
pr_debug("Space available, waking up writer %d.\n", wait->task->pid);
524541
return 1;
525542
} else {
526-
pr_debug("pipe_write_wake_function: Writer %d not in the correct state for wake-up.\n", wait->task->pid);
543+
pr_debug("Writer %d not in the correct state for wake-up.\n", wait->task->pid);
527544
}
528545
} else {
529-
pr_debug("pipe_write_wake_function: No space available, writer %d should continue waiting.\n", wait->task->pid);
546+
pr_debug("No space available, writer %d should continue waiting.\n", wait->task->pid);
530547
}
531548

532549
// No wake-up action taken, continue waiting.
@@ -935,9 +952,6 @@ static ssize_t pipe_read(vfs_file_t *file, char *buffer, off_t offset, size_t nb
935952
// Update the total bytes read and the read index.
936953
bytes_read = bytes_read + bytes_to_read;
937954
pipe_info->read_index = pipe_info->read_index + bytes_to_read;
938-
939-
// Wake up tasks that might be waiting to write to the pipe.
940-
pipe_wake_up_tasks(&pipe_info->write_wait, "pipe_read");
941955
}
942956
} else {
943957
// If in blocking mode, put the process to sleep until data is available.
@@ -952,6 +966,11 @@ static ssize_t pipe_read(vfs_file_t *file, char *buffer, off_t offset, size_t nb
952966
// Release the mutex after reading.
953967
mutex_unlock(&pipe_info->mutex);
954968

969+
// Wake up tasks that might be waiting to write to the pipe.
970+
if (bytes_read > 0) {
971+
pipe_wake_up_tasks(&pipe_info->write_wait, "pipe_read");
972+
}
973+
955974
return bytes_read;
956975
}
957976

@@ -1019,9 +1038,6 @@ static ssize_t pipe_write(vfs_file_t *file, const void *buffer, off_t offset, si
10191038
// Update the total bytes written and the write index.
10201039
bytes_written = bytes_written + bytes_to_write;
10211040
pipe_info->write_index = pipe_info->write_index + bytes_to_write;
1022-
1023-
// Wake up tasks waiting to read from the pipe.
1024-
pipe_wake_up_tasks(&pipe_info->read_wait, "pipe_write");
10251041
}
10261042
} else {
10271043
// Blocking behavior: Put the process to sleep until space is available.
@@ -1036,6 +1052,11 @@ static ssize_t pipe_write(vfs_file_t *file, const void *buffer, off_t offset, si
10361052
// Release the mutex after the write operation is complete.
10371053
mutex_unlock(&pipe_info->mutex);
10381054

1055+
// Wake up tasks waiting to read from the pipe.
1056+
if (bytes_written > 0) {
1057+
pipe_wake_up_tasks(&pipe_info->read_wait, "pipe_write");
1058+
}
1059+
10391060
return bytes_written;
10401061
}
10411062

0 commit comments

Comments
 (0)