Refactor syscall: Use EXIT_FAILURE instead of magic numbers & close files on failure
This commit is contained in:
@@ -138,7 +138,6 @@ syscall_exit (int status)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Executes a given command with the relevant args, by calling process_execute.
|
/* 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
|
Returns PID for the process that is running the CMD_LINE
|
||||||
*/
|
*/
|
||||||
static pid_t
|
static pid_t
|
||||||
@@ -146,9 +145,7 @@ syscall_exec (const char *cmd_line)
|
|||||||
{
|
{
|
||||||
validate_user_string (cmd_line);
|
validate_user_string (cmd_line);
|
||||||
|
|
||||||
pid_t pid = process_execute(cmd_line);
|
return process_execute (cmd_line); /* Returns the PID of the new process */
|
||||||
|
|
||||||
return pid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Handles the syscall of wait. Effectively a wrapper for process_wait as the
|
/* 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
|
static int
|
||||||
syscall_wait (pid_t pid)
|
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
|
/* 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);
|
struct file *ptr = filesys_open (file);
|
||||||
lock_release (&filesys_lock);
|
lock_release (&filesys_lock);
|
||||||
if (ptr == NULL)
|
if (ptr == NULL)
|
||||||
return -1;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
/* Allocate space for a struct representing a mapping from an FD to a struct
|
/* Allocate space for a struct representing a mapping from an FD to a struct
|
||||||
file. */
|
file. */
|
||||||
struct open_file *file_info
|
struct open_file *file_info
|
||||||
= (struct open_file*) malloc (sizeof (struct open_file));
|
= (struct open_file*) malloc (sizeof (struct open_file));
|
||||||
if (file_info == NULL)
|
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 */
|
/* Populate the above struct, with a unique FD and the current open file */
|
||||||
file_info->fd = fd_counter++;
|
file_info->fd = fd_counter++;
|
||||||
@@ -230,7 +230,7 @@ syscall_filesize (int fd)
|
|||||||
{
|
{
|
||||||
struct open_file *file_info = fd_get_file (fd);
|
struct open_file *file_info = fd_get_file (fd);
|
||||||
if (file_info == NULL)
|
if (file_info == NULL)
|
||||||
return -1;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
lock_acquire (&filesys_lock);
|
lock_acquire (&filesys_lock);
|
||||||
int bytes = file_length (file_info->file);
|
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
|
/* Only console (fd = 0) or other files, not including STDOUT, (fd > 1) are
|
||||||
allowed. */
|
allowed. */
|
||||||
if (fd < 0 || fd == STDOUT_FILENO)
|
if (fd < 0 || fd == STDOUT_FILENO)
|
||||||
return -1;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
validate_user_pointer (buffer, size);
|
validate_user_pointer (buffer, size);
|
||||||
|
|
||||||
@@ -267,7 +267,7 @@ syscall_read (int fd, void *buffer, unsigned size)
|
|||||||
/* Reading from a file. */
|
/* Reading from a file. */
|
||||||
struct open_file *file_info = fd_get_file (fd);
|
struct open_file *file_info = fd_get_file (fd);
|
||||||
if (file_info == NULL)
|
if (file_info == NULL)
|
||||||
return -1;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
lock_acquire (&filesys_lock);
|
lock_acquire (&filesys_lock);
|
||||||
int bytes_written = file_read (file_info->file, buffer, size);
|
int bytes_written = file_read (file_info->file, buffer, size);
|
||||||
|
|||||||
Reference in New Issue
Block a user