awarbler / yashd_project

0 stars 0 forks source link

Job control #49

Closed awarbler closed 5 days ago

awarbler commented 5 days ago

Signal Handlers:

Removed sigtstp_handler, sigint_handler, and sigchld_handler functions. The sig_chld function was simplified, removing the handling for WIFSTOPPED and WIFCONTINUED. It now only checks for WIFEXITED and WIFSIGNALED. setup_signal_handlers() function was removed along with individual handlers for SIGINT, SIGTSTP, and SIGCHLD.

  1. Logging Changes:

The logRequest function remains similar, but minor improvements were made for error handling and output format. The log_command function now uses a simpler allocation and thread creation process, ensuring memory safety and better thread management.

  1. Job Control Updates:

Removed the complex signal handlers (sigint_handler, sigtstp_handler) for job control. Simplified the handling of job control commands like fg and bg. Now, the focus is on managing ch_pid and updating job statuses directly in these functions. Updated fg_command() and bg_command() to streamline their logic. Both functions now: Directly search for the job without relying on signal handlers. Ensure proper transfer of terminal control using tcsetpgrp() when bringing a job to the foreground. Handle stopped and running states explicitly.

  1. Process Execution and Redirection:

The function apply_redirections now takes input_file and output_file as arguments directly, simplifying redirection handling. Removed complex input/output parsing from within serveClient() and centralized it in apply_redirections(). The main execution of commands in serveClient() was simplified: Forking and process execution are handled in a more streamlined way. Removed extra checks and redundant code for setting process group IDs (setpgid).

  1. Pipe Handling:

The handle_pipe() function was simplified: Removed redundant error handling and complex redirection logic. Now directly executes left and right commands with proper file descriptor redirections. Closed pipe file descriptors promptly after usage to avoid leaks. validatePipes() function remains largely unchanged, focusing on parsing left and right commands.

  1. Improved Error Handling:

Added more consistent error messages throughout the file. Standardized the format of error messages sent to the client for invalid commands, pipe errors, and job control issues.

  1. Simplified Client Command Handling:

Removed duplicate checks for CMD and CTL commands. Consolidated handling of control characters (CTL) in a single if-block within serveClient(). Removed handle_control() function; directly processed control commands like Ctrl+C, Ctrl+Z, and Ctrl+D in serveClient().

  1. Daemon Initialization:

The daemon_init function remains mostly unchanged but improved error messages for better debugging.

  1. Removed Unused Code:

Removed commented-out and legacy code that was no longer in use, such as: Old signal handlers. Extra checks for redirections within cmd_args. Legacy logging code snippets.

  1. Consistency and Code Clean-Up:

Improved naming conventions and standardized error message formats. Ensured consistent usage of strncpy for string copying to prevent buffer overflows. Removed unnecessary debug statements and consolidated logging output for clarity. Conclusion The new version of yashd.c is significantly more streamlined, focusing on core functionality and simplifying complex logic. The removal of specific signal handlers (e.g., sigtstp_handler, sigint_handler) indicates a shift away from manual signal management towards simpler process handling within the main functions like fg_command and bg_command. The code should now be more maintainable, with fewer edge cases and potential bugs, while still adhering to the project requirements.