Refactor syscall: Use EXIT_FAILURE instead of magic numbers & close files on failure

This commit is contained in:
sBubshait
2024-11-13 18:18:41 +00:00
parent b1c5819469
commit 9549ca28e5

View File

@@ -138,7 +138,6 @@ syscall_exit (int status)
}
/* Executes a given command with the relevant args, by calling process_execute.
Acquires the filesystem lock as process_execute accesses the file system.
Returns PID for the process that is running the CMD_LINE
*/
static pid_t
@@ -146,9 +145,7 @@ syscall_exec (const char *cmd_line)
{
validate_user_string (cmd_line);
pid_t pid = process_execute(cmd_line);
return pid;
return process_execute (cmd_line); /* Returns the PID of the new process */
}
/* Handles the syscall of wait. Effectively a wrapper for process_wait as the
@@ -156,7 +153,7 @@ syscall_exec (const char *cmd_line)
static int
syscall_wait (pid_t pid)
{
return process_wait (pid);
return process_wait (pid); /* Returns the exit status of the waited process */
}
/* Handles the syscall for file creation. First validates the user file
@@ -202,14 +199,17 @@ syscall_open (const char *file)
struct file *ptr = filesys_open (file);
lock_release (&filesys_lock);
if (ptr == NULL)
return -1;
return EXIT_FAILURE;
/* Allocate space for a struct representing a mapping from an FD to a struct
file. */
struct open_file *file_info
= (struct open_file*) malloc (sizeof (struct open_file));
if (file_info == NULL)
return -1;
{
file_close (ptr);
return EXIT_FAILURE;
}
/* Populate the above struct, with a unique FD and the current open file */
file_info->fd = fd_counter++;
@@ -230,7 +230,7 @@ syscall_filesize (int fd)
{
struct open_file *file_info = fd_get_file (fd);
if (file_info == NULL)
return -1;
return EXIT_FAILURE;
lock_acquire (&filesys_lock);
int bytes = file_length (file_info->file);
@@ -249,7 +249,7 @@ syscall_read (int fd, void *buffer, unsigned size)
/* Only console (fd = 0) or other files, not including STDOUT, (fd > 1) are
allowed. */
if (fd < 0 || fd == STDOUT_FILENO)
return -1;
return EXIT_FAILURE;
validate_user_pointer (buffer, size);
@@ -267,7 +267,7 @@ syscall_read (int fd, void *buffer, unsigned size)
/* Reading from a file. */
struct open_file *file_info = fd_get_file (fd);
if (file_info == NULL)
return -1;
return EXIT_FAILURE;
lock_acquire (&filesys_lock);
int bytes_written = file_read (file_info->file, buffer, size);