From dfb32ce3d9707520a9587af378810fa597f3eee7 Mon Sep 17 00:00:00 2001 From: "Enrico Fraccaroli (Galfurian)" Date: Mon, 9 Mar 2026 11:05:56 +0100 Subject: [PATCH] feature(tests): convert output to syslog logging --- userspace/tests/t_abort.c | 16 +- userspace/tests/t_alarm.c | 14 +- userspace/tests/t_big_write.c | 18 +- userspace/tests/t_chdir.c | 14 +- userspace/tests/t_creat.c | 24 +-- userspace/tests/t_dup.c | 50 +++--- userspace/tests/t_environ.c | 16 +- userspace/tests/t_exec.c | 11 +- userspace/tests/t_ext2_audit_mount_cache.c | 54 +++--- userspace/tests/t_ext2_audit_overflow.c | 132 +++++++------- userspace/tests/t_ext2_audit_read_failure.c | 161 +++++++++--------- userspace/tests/t_ext2_audit_write_boundary.c | 136 +++++++-------- userspace/tests/t_fflush.c | 32 ++-- userspace/tests/t_fhs.c | 66 +++---- userspace/tests/t_fork.c | 17 +- userspace/tests/t_gid.c | 18 +- userspace/tests/t_groups.c | 9 +- userspace/tests/t_grp.c | 4 +- userspace/tests/t_hashmap.c | 25 +-- userspace/tests/t_itimer.c | 10 +- userspace/tests/t_kill.c | 30 ++-- userspace/tests/t_list.c | 33 ++-- userspace/tests/t_mem.c | 6 +- userspace/tests/t_mkdir.c | 16 +- userspace/tests/t_msgget.c | 24 +-- userspace/tests/t_ndtree.c | 27 +-- userspace/tests/t_periodic1.c | 14 +- userspace/tests/t_periodic2.c | 10 +- userspace/tests/t_periodic3.c | 10 +- userspace/tests/t_pipe_blocking.c | 17 +- userspace/tests/t_pipe_non_blocking.c | 21 +-- userspace/tests/t_pwd.c | 8 +- userspace/tests/t_scanf.c | 12 +- userspace/tests/t_schedfb.c | 13 +- userspace/tests/t_semflg.c | 35 ++-- userspace/tests/t_semget.c | 46 ++--- userspace/tests/t_semop.c | 16 +- userspace/tests/t_shm.c | 29 ++-- userspace/tests/t_shmget.c | 23 +-- userspace/tests/t_sigaction.c | 28 +-- userspace/tests/t_sigfpe.c | 24 +-- userspace/tests/t_siginfo.c | 20 ++- userspace/tests/t_sigmask.c | 32 ++-- userspace/tests/t_sigusr.c | 18 +- userspace/tests/t_sleep.c | 4 +- userspace/tests/t_spwd.c | 15 +- userspace/tests/t_stopcont.c | 29 ++-- userspace/tests/t_syslog.c | 22 ++- userspace/tests/t_time.c | 8 +- userspace/tests/t_write_read.c | 26 +-- 50 files changed, 763 insertions(+), 680 deletions(-) diff --git a/userspace/tests/t_abort.c b/userspace/tests/t_abort.c index e03fa3a6..08449a05 100644 --- a/userspace/tests/t_abort.c +++ b/userspace/tests/t_abort.c @@ -1,25 +1,29 @@ /// @file t_abort.c /// @brief Demonstrates handling of the SIGABRT signal. +/// @details This program sets up a signal handler for the SIGABRT signal and +/// triggers the signal using the `abort` function. /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include #include void sig_handler(int sig) { - printf("handler(%d) : Starting handler.\n", sig); + syslog(LOG_INFO, "[t_abort] handler(%d) : Starting handler.\n", sig); if (sig == SIGABRT) { static int counter = 0; counter += 1; - printf("handler(%d) : Correct signal. ABRT (%d/3)\n", sig, counter); + syslog(LOG_INFO, "[t_abort] handler(%d) : Correct signal. ABRT (%d/3)\n", sig, counter); if (counter < 3) { // Re-trigger the abort signal up to 3 times. abort(); @@ -28,9 +32,9 @@ void sig_handler(int sig) exit(EXIT_SUCCESS); } } else { - printf("handler(%d) : Wrong signal.\n", sig); + syslog(LOG_INFO, "[t_abort] handler(%d) : Wrong signal.\n", sig); } - printf("handler(%d) : Ending handler.\n", sig); + syslog(LOG_INFO, "[t_abort] handler(%d) : Ending handler.\n", sig); } int main(int argc, char *argv[]) @@ -41,7 +45,7 @@ int main(int argc, char *argv[]) // Set up the signal handler for SIGABRT. if (sigaction(SIGABRT, &action, NULL) == -1) { - perror("signal setup failed"); + syslog(LOG_ERR, "[t_abort] signal setup failed: %s", strerror(errno)); exit(EXIT_FAILURE); } @@ -49,7 +53,7 @@ int main(int argc, char *argv[]) abort(); // This point should never be reached. - perror("abort() failed to terminate the process"); + syslog(LOG_ERR, "[t_abort] abort() failed to terminate the process: %s", strerror(errno)); return EXIT_FAILURE; } diff --git a/userspace/tests/t_alarm.c b/userspace/tests/t_alarm.c index 0752c68b..689c2c96 100644 --- a/userspace/tests/t_alarm.c +++ b/userspace/tests/t_alarm.c @@ -3,12 +3,14 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include #include @@ -16,7 +18,7 @@ /// @param sig The signal number. void alarm_handler(int sig) { - printf("handler(%d) : Starting handler.\n", sig); + syslog(LOG_INFO, "[t_alarm] handler(%d) : Starting handler.\n", sig); if (sig == SIGALRM) { // Set an alarm to go off after 1 seconds. alarm(1); @@ -25,7 +27,7 @@ void alarm_handler(int sig) unsigned int rest = alarm(1); // Expected value: 1 (since the previous alarm was just set to 1 seconds). - printf("handler(%d) : alarm(1) result: %d.\n", sig, rest); + syslog(LOG_INFO, "[t_alarm] handler(%d) : alarm(1) result: %d.\n", sig, rest); // Cancel the alarm and get the remaining time of the previous alarm. rest = alarm(0); @@ -35,13 +37,13 @@ void alarm_handler(int sig) // you see the value 4 instead of 1. The exact value can vary slightly // depending on the system’s execution speed and the time taken to // execute the intermediate code. - printf("handler(%d) : alarm(0) result: %d.\n", sig, rest); + syslog(LOG_INFO, "[t_alarm] handler(%d) : alarm(0) result: %d.\n", sig, rest); exit(EXIT_SUCCESS); } else { - printf("handler(%d) : Wrong signal.\n", sig); + syslog(LOG_INFO, "[t_alarm] handler(%d) : Wrong signal.\n", sig); } - printf("handler(%d) : Ending handler.\n", sig); + syslog(LOG_INFO, "[t_alarm] handler(%d) : Ending handler.\n", sig); } int main(int argc, char *argv[]) @@ -52,7 +54,7 @@ int main(int argc, char *argv[]) // Set up the signal handler for SIGALRM. if (sigaction(SIGALRM, &action, NULL) < 0) { - perror("signal setup failed"); + syslog(LOG_ERR, "[t_alarm] signal setup failed: %s", strerror(errno)); exit(EXIT_FAILURE); } diff --git a/userspace/tests/t_big_write.c b/userspace/tests/t_big_write.c index 91498195..2438569a 100644 --- a/userspace/tests/t_big_write.c +++ b/userspace/tests/t_big_write.c @@ -5,12 +5,14 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include #define FILENAME "/home/user/test.txt" @@ -26,7 +28,7 @@ int main(int argc, char *argv[]) // Open the file with specified flags and mode. int fd = open(FILENAME, O_WRONLY | O_CREAT | O_TRUNC, mode); if (fd < 0) { - fprintf(stderr, "Failed to open file %s: %s\n", FILENAME, strerror(errno)); + syslog(LOG_ERR, "[t_big_write] Failed to open file %s: %s\n", FILENAME, strerror(errno)); return EXIT_FAILURE; } @@ -35,7 +37,7 @@ int main(int argc, char *argv[]) for (unsigned i = 'A'; i < 'z'; ++i) { memset(write_buffer, i, sizeof(write_buffer)); if (write(fd, write_buffer, sizeof(write_buffer)) < 0) { - fprintf(stderr, "Writing to file %s failed: %s\n", FILENAME, strerror(errno)); + syslog(LOG_ERR, "[t_big_write] Writing to file %s failed: %s\n", FILENAME, strerror(errno)); close(fd); unlink(FILENAME); return EXIT_FAILURE; @@ -45,7 +47,7 @@ int main(int argc, char *argv[]) // Close the file descriptor. if (close(fd) < 0) { - fprintf(stderr, "Failed to close file %s: %s\n", FILENAME, strerror(errno)); + syslog(LOG_ERR, "[t_big_write] Failed to close file %s: %s\n", FILENAME, strerror(errno)); unlink(FILENAME); return EXIT_FAILURE; } @@ -53,7 +55,7 @@ int main(int argc, char *argv[]) // Open the file with specified flags and mode. fd = open(FILENAME, O_RDONLY, mode); if (fd < 0) { - fprintf(stderr, "Failed to open file %s: %s\n", FILENAME, strerror(errno)); + syslog(LOG_ERR, "[t_big_write] Failed to open file %s: %s\n", FILENAME, strerror(errno)); unlink(FILENAME); return EXIT_FAILURE; } @@ -63,7 +65,7 @@ int main(int argc, char *argv[]) for (unsigned i = 'A'; i < 'z'; ++i) { memset(write_buffer, i, sizeof(write_buffer)); if (read(fd, read_buffer, sizeof(read_buffer)) < 0) { - fprintf(stderr, "Reading from file %s failed: %s\n", FILENAME, strerror(errno)); + syslog(LOG_ERR, "[t_big_write] Reading from file %s failed: %s\n", FILENAME, strerror(errno)); close(fd); unlink(FILENAME); return EXIT_FAILURE; @@ -71,7 +73,7 @@ int main(int argc, char *argv[]) // Verify read data matches what was written. if (memcmp(write_buffer, read_buffer, sizeof(write_buffer)) != 0) { - fprintf(stderr, "Data mismatch in file %s at iteration %u, char %c\n", FILENAME, times, i); + syslog(LOG_ERR, "[t_big_write] Data mismatch in file %s at iteration %u, char %c\n", FILENAME, times, i); close(fd); unlink(FILENAME); return EXIT_FAILURE; @@ -81,14 +83,14 @@ int main(int argc, char *argv[]) // Close the file descriptor. if (close(fd) < 0) { - fprintf(stderr, "Failed to close file %s: %s\n", FILENAME, strerror(errno)); + syslog(LOG_ERR, "[t_big_write] Failed to close file %s: %s\n", FILENAME, strerror(errno)); unlink(FILENAME); return EXIT_FAILURE; } // Delete the test file. if (unlink(FILENAME) < 0) { - fprintf(stderr, "Failed to delete file %s: %s\n", FILENAME, strerror(errno)); + syslog(LOG_ERR, "[t_big_write] Failed to delete file %s: %s\n", FILENAME, strerror(errno)); return EXIT_FAILURE; } diff --git a/userspace/tests/t_chdir.c b/userspace/tests/t_chdir.c index b4bddb11..04b84bf0 100644 --- a/userspace/tests/t_chdir.c +++ b/userspace/tests/t_chdir.c @@ -3,9 +3,12 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include +#include #include +#include #include int main(int argc, char *argv[]) @@ -19,19 +22,16 @@ int main(int argc, char *argv[]) if (getcwd(cwd, sizeof(cwd)) != NULL) { // Compare cwd and the expected directory. if (strcmp(cwd, directory) == 0) { - printf("Successfully changed to the directory.\n"); + syslog(LOG_INFO, "[t_chdir] Successfully changed to the directory.\n"); return EXIT_SUCCESS; } - printf( - "Directory change failed or directory differs: expected %s " - "but got %s\n", - directory, cwd); + syslog(LOG_INFO, "[t_chdir] Directory change failed or directory differs: expected %s but got %s\n", directory, cwd); } else { - perror("getcwd failed"); + syslog(LOG_ERR, "[t_chdir] getcwd failed: %s", strerror(errno)); } } else { - perror("chdir failed"); + syslog(LOG_ERR, "[t_chdir] chdir failed: %s", strerror(errno)); } return EXIT_FAILURE; } diff --git a/userspace/tests/t_creat.c b/userspace/tests/t_creat.c index 9ce9c0ae..9a87894e 100644 --- a/userspace/tests/t_creat.c +++ b/userspace/tests/t_creat.c @@ -5,12 +5,14 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include int main(int argc, char *argv[]) @@ -23,21 +25,21 @@ int main(int argc, char *argv[]) int fd = creat(filename, 0660); if (fd < 0) { // Error handling for file creation failure. - fprintf(STDERR_FILENO, "creat: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_creat] creat: %s: %s\n", filename, strerror(errno)); exit(EXIT_FAILURE); } // Write the string to the file. if (write(fd, content, content_size) != content_size) { // Error handling for write failure. - fprintf(STDERR_FILENO, "write: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_creat] write: %s: %s\n", filename, strerror(errno)); // Close the file descriptor. if (close(fd) < 0) { - fprintf(STDERR_FILENO, "close: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_creat] close: %s: %s\n", filename, strerror(errno)); } // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_creat] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } @@ -45,10 +47,10 @@ int main(int argc, char *argv[]) // Close the file descriptor. if (close(fd) < 0) { // Error handling for close failure. - fprintf(STDERR_FILENO, "close: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_creat] close: %s: %s\n", filename, strerror(errno)); // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_creat] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } @@ -58,20 +60,20 @@ int main(int argc, char *argv[]) // Get the status of the file filename. if (stat(filename, &st) < 0) { // Error handling for stat failure. - fprintf(STDERR_FILENO, "stat: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_creat] stat: %s: %s\n", filename, strerror(errno)); // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_creat] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } // Check if the file size is correct. if (st.st_size != content_size) { - fprintf(STDERR_FILENO, "Wrong file size. (expected: %ld, is: %ld)\n", content_size, st.st_size); + syslog(LOG_ERR, "[t_creat] Wrong file size. (expected: %ld, is: %ld)\n", content_size, st.st_size); // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_creat] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } @@ -79,7 +81,7 @@ int main(int argc, char *argv[]) // Remove the file. if (unlink(filename) < 0) { // Error handling for unlink failure. - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_creat] unlink: %s: %s\n", filename, strerror(errno)); exit(EXIT_FAILURE); } diff --git a/userspace/tests/t_dup.c b/userspace/tests/t_dup.c index aaafdad0..56412b72 100644 --- a/userspace/tests/t_dup.c +++ b/userspace/tests/t_dup.c @@ -6,12 +6,14 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include int main(int argc, char *argv[]) @@ -25,34 +27,34 @@ int main(int argc, char *argv[]) // Open the file with specified flags and mode. fd1 = open(filename, flags, mode); if (fd1 < 0) { - fprintf(STDERR_FILENO, "Failed to open file %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] Failed to open file %s: %s\n", filename, strerror(errno)); exit(EXIT_FAILURE); } // Duplicate the file descriptor. fd2 = dup(fd1); if (fd2 < 0) { - fprintf(STDERR_FILENO, "Failed to dup fd %d: %s\n", fd1, strerror(errno)); + syslog(LOG_ERR, "[t_dup] Failed to dup fd %d: %s\n", fd1, strerror(errno)); // Close the file descriptor. if (close(fd1) < 0) { - fprintf(STDERR_FILENO, "close fd1: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] close fd1: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } // Write "foo" to the first file descriptor. if (write(fd1, "foo", 3) != 3) { - fprintf(STDERR_FILENO, "Writing to fd %d failed: %s\n", fd1, strerror(errno)); + syslog(LOG_ERR, "[t_dup] Writing to fd %d failed: %s\n", fd1, strerror(errno)); // Close the file descriptor. if (close(fd1) < 0) { - fprintf(STDERR_FILENO, "close fd1: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] close fd1: %s: %s\n", filename, strerror(errno)); } if (close(fd2) < 0) { - fprintf(STDERR_FILENO, "close fd2: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] close fd2: %s: %s\n", filename, strerror(errno)); } // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } @@ -60,10 +62,10 @@ int main(int argc, char *argv[]) // Close the file descriptor. if (close(fd1) < 0) { // Error handling for close failure. - fprintf(STDERR_FILENO, "close fd1: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] close fd1: %s: %s\n", filename, strerror(errno)); // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } @@ -71,14 +73,14 @@ int main(int argc, char *argv[]) // Write "bar" to the duplicated file descriptor. if (write(fd2, "bar", 3) != 3) { // Error handling for write failure. - fprintf(STDERR_FILENO, "Writing to fd %d failed: %s\n", fd2, strerror(errno)); + syslog(LOG_ERR, "[t_dup] Writing to fd %d failed: %s\n", fd2, strerror(errno)); // Close the file descriptor. if (close(fd2) < 0) { - fprintf(STDERR_FILENO, "close fd2: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] close fd2: %s: %s\n", filename, strerror(errno)); } // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } @@ -86,10 +88,10 @@ int main(int argc, char *argv[]) // Close the file descriptor. if (close(fd2) < 0) { // Error handling for close failure. - fprintf(STDERR_FILENO, "close fd2: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] close fd2: %s: %s\n", filename, strerror(errno)); // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } @@ -98,10 +100,10 @@ int main(int argc, char *argv[]) fd1 = open(filename, O_RDONLY, mode); if (fd1 < 0) { // Error handling for file open failure. - fprintf(STDERR_FILENO, "Failed to open file %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] Failed to open file %s: %s\n", filename, strerror(errno)); // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } @@ -112,14 +114,14 @@ int main(int argc, char *argv[]) // Read the content of the file. if (read(fd1, buf, 6) < 0) { // Error handling for read failure. - fprintf(STDERR_FILENO, "Reading from fd %d failed: %s\n", fd1, strerror(errno)); + syslog(LOG_ERR, "[t_dup] Reading from fd %d failed: %s\n", fd1, strerror(errno)); // Close the file descriptor. if (close(fd1) < 0) { - fprintf(STDERR_FILENO, "close fd1: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] close fd1: %s: %s\n", filename, strerror(errno)); } // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } @@ -127,20 +129,20 @@ int main(int argc, char *argv[]) // Close the file descriptor. if (close(fd1) < 0) { // Error handling for close failure. - fprintf(STDERR_FILENO, "close fd1: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] close fd1: %s: %s\n", filename, strerror(errno)); // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } // Check if the file content is as expected. if (strcmp(buf, "foobar") != 0) { - fprintf(STDERR_FILENO, "Unexpected file content: %s\n", buf); + syslog(LOG_ERR, "[t_dup] Unexpected file content: %s\n", buf); // Remove the file. if (unlink(filename) < 0) { - fprintf(STDERR_FILENO, "unlink: %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] unlink: %s: %s\n", filename, strerror(errno)); } exit(EXIT_FAILURE); } @@ -148,7 +150,7 @@ int main(int argc, char *argv[]) // Remove the file. if (unlink(filename) < 0) { // Error handling for unlink failure. - fprintf(STDERR_FILENO, "Failed to delete file %s: %s\n", filename, strerror(errno)); + syslog(LOG_ERR, "[t_dup] Failed to delete file %s: %s\n", filename, strerror(errno)); exit(EXIT_FAILURE); } diff --git a/userspace/tests/t_environ.c b/userspace/tests/t_environ.c index a45f0970..10091c14 100644 --- a/userspace/tests/t_environ.c +++ b/userspace/tests/t_environ.c @@ -6,10 +6,12 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include +#include #include int main(int argc, char *argv[]) @@ -20,44 +22,44 @@ int main(int argc, char *argv[]) // Set the environment variable if (setenv(env_var, initial_value, 1) != 0) { - perror("setenv failed"); + syslog(LOG_ERR, "[t_environ] setenv failed: %s", strerror(errno)); return EXIT_FAILURE; } // Retrieve the environment variable const char *value = getenv(env_var); if (!value) { - fprintf(stderr, "getenv failed: Environment variable %s not found.\n", env_var); + syslog(LOG_ERR, "[t_environ] getenv failed: Environment variable %s not found.\n", env_var); return EXIT_FAILURE; } // Verify the retrieved value matches the set value if (strcmp(value, initial_value) != 0) { - fprintf(stderr, "Mismatch: Expected '%s', but got '%s'.\n", initial_value, value); + syslog(LOG_ERR, "[t_environ] Mismatch: Expected '%s', but got '%s'.\n", initial_value, value); return EXIT_FAILURE; } // Update the environment variable if (setenv(env_var, updated_value, 1) != 0) { - perror("setenv failed (update)"); + syslog(LOG_ERR, "[t_environ] setenv failed (update): %s", strerror(errno)); return EXIT_FAILURE; } // Retrieve the updated environment variable value = getenv(env_var); if (!value) { - fprintf(stderr, "getenv failed: Environment variable %s not found after update.\n", env_var); + syslog(LOG_ERR, "[t_environ] getenv failed: Environment variable %s not found after update.\n", env_var); return EXIT_FAILURE; } // Verify the retrieved value matches the updated value if (strcmp(value, updated_value) != 0) { - fprintf(stderr, "Mismatch after update: Expected '%s', but got '%s'.\n", updated_value, value); + syslog(LOG_ERR, "[t_environ] Mismatch after update: Expected '%s', but got '%s'.\n", updated_value, value); return EXIT_FAILURE; } // Print success message - printf("Environment variable %s tested successfully.\n", env_var); + syslog(LOG_INFO, "[t_environ] Environment variable %s tested successfully.\n", env_var); return EXIT_SUCCESS; } diff --git a/userspace/tests/t_exec.c b/userspace/tests/t_exec.c index d8166d25..2ecc1bf7 100644 --- a/userspace/tests/t_exec.c +++ b/userspace/tests/t_exec.c @@ -1,12 +1,15 @@ /// @file t_exec.c -/// @brief +/// @brief Test program for the exec system call. /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include +#include #include #include +#include #include int main(int argc, char *argv[]) @@ -19,7 +22,7 @@ int main(int argc, char *argv[]) if (pid < 0) { // Error in forking - perror("fork"); + syslog(LOG_ERR, "[t_exec] fork: %s", strerror(errno)); return EXIT_FAILURE; } @@ -31,12 +34,12 @@ int main(int argc, char *argv[]) execl("/bin/echo", "echo", "Exec test successful", NULL); // If exec fails, print an error and exit - perror("execl"); + syslog(LOG_ERR, "[t_exec] execl: %s", strerror(errno)); exit(EXIT_FAILURE); } else { // Parent process: Wait for the child process to complete if (waitpid(pid, &status, 0) == -1) { - perror("waitpid"); + syslog(LOG_ERR, "[t_exec] waitpid: %s", strerror(errno)); return EXIT_FAILURE; } diff --git a/userspace/tests/t_ext2_audit_mount_cache.c b/userspace/tests/t_ext2_audit_mount_cache.c index 707db52e..2aa6d3d5 100644 --- a/userspace/tests/t_ext2_audit_mount_cache.c +++ b/userspace/tests/t_ext2_audit_mount_cache.c @@ -30,14 +30,14 @@ /// @return 0 on success, 1 on failure int test_mount_operational(void) { - syslog(LOG_INFO, "[TEST] Filesystem operational after mount...\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] [TEST] Filesystem operational after mount...\n"); // If the filesystem mounted successfully and is operational, // the cache must have been created properly int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to open file - filesystem may not be mounted: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_mount_cache] Failed to open file - filesystem may not be mounted: %s\n", strerror(errno)); return 1; } @@ -46,11 +46,11 @@ int test_mount_operational(void) close(fd); if (written != (ssize_t)strlen(TEST_DATA)) { - syslog(LOG_ERR, "Write failed or incomplete\n"); + syslog(LOG_ERR, "[t_ext2_audit_mount_cache] Write failed or incomplete\n"); return 1; } - syslog(LOG_INFO, " ✓ File write successful (cache operational)\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] ✓ File write successful (cache operational)\n"); return 0; } @@ -58,7 +58,7 @@ int test_mount_operational(void) /// @return 0 on success, 1 on failure int test_cache_under_load(void) { - syslog(LOG_INFO, "[TEST] Cache under load...\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] [TEST] Cache under load...\n"); // Create multiple files in quick succession // This stresses the cache system @@ -69,7 +69,7 @@ int test_cache_under_load(void) int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to create file %d", i); + syslog(LOG_ERR, "[t_ext2_audit_mount_cache] Failed to create file %d", i); return 1; } @@ -82,12 +82,12 @@ int test_cache_under_load(void) close(fd); if (written != to_write) { - syslog(LOG_ERR, "Write to file %d incomplete: %ld of %ld\n", i, written, to_write); + syslog(LOG_ERR, "[t_ext2_audit_mount_cache] Write to file %d incomplete: %ld of %ld\n", i, written, to_write); return 1; } } - syslog(LOG_INFO, " ✓ Multiple operations successful\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] ✓ Multiple operations successful\n"); return 0; } @@ -95,12 +95,12 @@ int test_cache_under_load(void) /// @return 0 on success, 1 on failure int test_cache_on_reads(void) { - syslog(LOG_INFO, "[TEST] Cache used on reads...\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] [TEST] Cache used on reads...\n"); // Create a file with multi-block data int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to create file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_mount_cache] Failed to create file: %s\n", strerror(errno)); return 1; } @@ -117,17 +117,17 @@ int test_cache_on_reads(void) close(fd); if (bytes != sizeof(data)) { - syslog(LOG_ERR, "Read failed\n"); + syslog(LOG_ERR, "[t_ext2_audit_mount_cache] Read failed\n"); return 1; } // Verify data (would fail if cache corrupted) if (memcmp(buffer, data, sizeof(data)) != 0) { - syslog(LOG_ERR, "Data corruption detected\n"); + syslog(LOG_ERR, "[t_ext2_audit_mount_cache] Data corruption detected\n"); return 1; } - syslog(LOG_INFO, " ✓ Cache functional on reads\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] ✓ Cache functional on reads\n"); return 0; } @@ -135,13 +135,13 @@ int test_cache_on_reads(void) /// @return 0 on success, 1 on failure int test_cache_lifecycle(void) { - syslog(LOG_INFO, "[TEST] Cache lifecycle...\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] [TEST] Cache lifecycle...\n"); // Cycle through write-read multiple times for (int cycle = 0; cycle < 5; cycle++) { int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to open for write in cycle %d\n", cycle); + syslog(LOG_ERR, "[t_ext2_audit_mount_cache] Failed to open for write in cycle %d\n", cycle); return 1; } @@ -158,29 +158,29 @@ int test_cache_lifecycle(void) close(fd); if (bytes != sizeof(write_data)) { - syslog(LOG_ERR, "Read failed in cycle %d\n", cycle); + syslog(LOG_ERR, "[t_ext2_audit_mount_cache] Read failed in cycle %d\n", cycle); return 1; } if (memcmp(write_data, read_data, sizeof(write_data)) != 0) { - syslog(LOG_ERR, "Data mismatch in cycle %d\n", cycle); + syslog(LOG_ERR, "[t_ext2_audit_mount_cache] Data mismatch in cycle %d\n", cycle); return 1; } } - syslog(LOG_INFO, " ✓ Cache lifecycle stable\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] ✓ Cache lifecycle stable\n"); return 0; } int main(void) { openlog("t_ext2_mount_cache", LOG_CONS | LOG_PID, LOG_USER); - syslog(LOG_INFO, "\n=== EXT2 Mount Cache Test Suite ===\n"); - syslog(LOG_INFO, "Testing: Issue #4 - Missing NULL check after kmem_cache_create\n"); - syslog(LOG_INFO, "Location: ext2.c:3772 in ext2_mount()\n"); - syslog(LOG_INFO, "Bug: kmem_cache_create() result not checked\n"); - syslog(LOG_INFO, "Note: This test verifies filesystem is fully operational\n"); - syslog(LOG_INFO, " (which proves cache was initialized)\n\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] \n=== EXT2 Mount Cache Test Suite ===\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] Testing: Issue #4 - Missing NULL check after kmem_cache_create\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] Location: ext2.c:3772 in ext2_mount()\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] Bug: kmem_cache_create() result not checked\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] Note: This test verifies filesystem is fully operational\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] (which proves cache was initialized)\n\n"); int failures = 0; @@ -189,13 +189,13 @@ int main(void) failures += test_cache_on_reads(); failures += test_cache_lifecycle(); - syslog(LOG_INFO, "=== Results ===\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] === Results ===\n"); if (failures == 0) { - syslog(LOG_INFO, "✅ ALL TESTS PASSED\n"); + syslog(LOG_INFO, "[t_ext2_audit_mount_cache] ✅ ALL TESTS PASSED\n"); closelog(); return 0; } else { - syslog(LOG_ERR, "❌ %d TEST(S) FAILED\n", failures); + syslog(LOG_ERR, "[t_ext2_audit_mount_cache] ❌ %d TEST(S) FAILED\n", failures); closelog(); return 1; } diff --git a/userspace/tests/t_ext2_audit_overflow.c b/userspace/tests/t_ext2_audit_overflow.c index dc526f4e..a7fc36cb 100644 --- a/userspace/tests/t_ext2_audit_overflow.c +++ b/userspace/tests/t_ext2_audit_overflow.c @@ -8,16 +8,16 @@ /// @copyright (c) 2024 - Audit Fix Test /// @see EXT2_AUDIT_REPORT.md - Issue #2 -#include +#include +#include +#include #include #include +#include #include -#include -#include #include -#include -#include -#include +#include +#include #define TEST_FILE "/tmp/test_overflow.txt" @@ -25,42 +25,42 @@ /// @return 0 on success, 1 on failure int test_large_offset_handling(void) { - syslog(LOG_INFO, "[TEST] Large offset handling...\n"); - + syslog(LOG_INFO, "[t_ext2_audit_overflow] [TEST] Large offset handling...\n"); + int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to create test file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_overflow] Failed to create test file: %s\n", strerror(errno)); return 1; } - + // Try to write at a very large offset // This should either succeed with proper file extension, // or fail gracefully with EOVERFLOW or similar - off_t large_offset = 1024 * 1024 * 100; // 100MB offset - + off_t large_offset = 1024 * 1024 * 100; // 100MB offset + if (lseek(fd, large_offset, SEEK_SET) < 0) { - syslog(LOG_INFO, " - lseek to large offset failed (expected on some systems)\n"); + syslog(LOG_INFO, "[t_ext2_audit_overflow] - lseek to large offset failed (expected on some systems)\n"); close(fd); - return 0; // Not a test failure, just system limitation + return 0; // Not a test failure, just system limitation } - + char test_data[] = "test"; - ssize_t written = write(fd, test_data, strlen(test_data)); - + ssize_t written = write(fd, test_data, strlen(test_data)); + if (written < 0) { - syslog(LOG_INFO, " - Write at large offset failed (may be expected)\n"); + syslog(LOG_INFO, "[t_ext2_audit_overflow] - Write at large offset failed (may be expected)\n"); close(fd); - return 0; // Not a failure, system may not support it + return 0; // Not a failure, system may not support it } - + if (written != (ssize_t)strlen(test_data)) { - syslog(LOG_ERR, "Write partial data at large offset\n"); + syslog(LOG_ERR, "[t_ext2_audit_overflow] Write partial data at large offset\n"); close(fd); return 1; } - + close(fd); - syslog(LOG_INFO, " ✓ Large offset handled safely\n"); + syslog(LOG_INFO, "[t_ext2_audit_overflow] ✓ Large offset handled safely\n"); return 0; } @@ -68,109 +68,109 @@ int test_large_offset_handling(void) /// @return 0 on success, 1 on failure int test_near_uint32_boundary(void) { - syslog(LOG_INFO, "[TEST] Near uint32_t boundary conditions...\n"); - + syslog(LOG_INFO, "[t_ext2_audit_overflow] [TEST] Near uint32_t boundary conditions...\n"); + // This is a structural test - it simulates what the ext2_write_inode_data // function does without actually creating huge files - + // The vulnerable code is: // uint32_t end_offset = (inode->size >= offset + nbyte) ? (offset + nbyte) : (inode->size); - + // This can overflow if: - uint32_t offset = 0xFFFFFFF0; // Very large offset - uint32_t nbyte = 0x20; // Small write, but offset + nbyte overflows - + uint32_t offset = 0xFFFFFFF0; // Very large offset + uint32_t nbyte = 0x20; // Small write, but offset + nbyte overflows + // Simulating the vulnerable check: // if ((offset + nbyte) > inode->size) - + // In C, this addition would overflow - uint32_t sum = offset + nbyte; // Overflows! - - printf(" Offset: 0x%08X (%u)\n", offset, offset); - printf(" Nbyte: 0x%08X (%u)\n", nbyte, nbyte); - printf(" Sum: 0x%08X (%u) - OVERFLOW OCCURRED\n", sum, sum); - + uint32_t sum = offset + nbyte; // Overflows! + + syslog(LOG_INFO, "[t_ext2_audit_overflow] Offset: 0x%08X (%u)\n", offset, offset); + syslog(LOG_INFO, "[t_ext2_audit_overflow] Nbyte: 0x%08X (%u)\n", nbyte, nbyte); + syslog(LOG_INFO, "[t_ext2_audit_overflow] Sum: 0x%08X (%u) - OVERFLOW OCCURRED\n", sum, sum); + // A properly fixed version should catch this if (offset > UINT32_MAX - nbyte) { - syslog(LOG_INFO, " ✓ Overflow would be detected by proper bounds check\n"); + syslog(LOG_INFO, "[t_ext2_audit_overflow] ✓ Overflow would be detected by proper bounds check\n"); } else { - syslog(LOG_INFO, " ✗ Overflow not detected - vulnerable!\n"); + syslog(LOG_INFO, "[t_ext2_audit_overflow] ✗ Overflow not detected - vulnerable!\n"); } - - return 0; // This is a demonstration test + + return 0; // This is a demonstration test } /// @brief Test mixed boundary conditions /// @return 0 on success, 1 on failure int test_mixed_boundary_conditions(void) { - syslog(LOG_INFO, "[TEST] Mixed boundary conditions...\n"); - + syslog(LOG_INFO, "[t_ext2_audit_overflow] [TEST] Mixed boundary conditions...\n"); + // Create a real file and test realistic but large writes int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to create test file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_overflow] Failed to create test file: %s\n", strerror(errno)); return 1; } - + // Write a known pattern char pattern[] = "BOUNDARY_TEST"; - + // First write: normal if (write(fd, pattern, strlen(pattern)) != (ssize_t)strlen(pattern)) { - syslog(LOG_ERR, "Initial write failed\n"); + syslog(LOG_ERR, "[t_ext2_audit_overflow] Initial write failed\n"); close(fd); return 1; } - + // Second write: ensure offset tracking is correct if (write(fd, pattern, strlen(pattern)) != (ssize_t)strlen(pattern)) { - syslog(LOG_ERR, "Second write failed\n"); + syslog(LOG_ERR, "[t_ext2_audit_overflow] Second write failed\n"); close(fd); return 1; } - + // Verify file has both patterns struct stat st; if (fstat(fd, &st) < 0) { - syslog(LOG_ERR, "Failed to fstat file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_overflow] Failed to fstat file: %s\n", strerror(errno)); close(fd); return 1; } - + close(fd); - + size_t expected_size_t = strlen(pattern) * 2; if (st.st_size != expected_size_t) { - syslog(LOG_ERR, "File size mismatch: expected %zu, got %ld\n", expected_size_t, st.st_size); + syslog(LOG_ERR, "[t_ext2_audit_overflow] File size mismatch: expected %zu, got %ld\n", expected_size_t, st.st_size); return 1; } - - syslog(LOG_INFO, " ✓ Boundary conditions handled correctly\n"); + + syslog(LOG_INFO, "[t_ext2_audit_overflow] ✓ Boundary conditions handled correctly\n"); return 0; } int main(void) { openlog("t_ext2_overflow", LOG_CONS | LOG_PID, LOG_USER); - syslog(LOG_INFO, "\n=== EXT2 Overflow Test Suite ===\n"); - syslog(LOG_INFO, "Testing: Issue #2 - Integer overflow in write operations\n"); - syslog(LOG_INFO, "Location: ext2.c:1876 in ext2_write_inode_data()\n"); - syslog(LOG_INFO, "Bug: No check for offset + nbyte overflow\n\n"); - + syslog(LOG_INFO, "[t_ext2_audit_overflow] \n=== EXT2 Overflow Test Suite ===\n"); + syslog(LOG_INFO, "[t_ext2_audit_overflow] Testing: Issue #2 - Integer overflow in write operations\n"); + syslog(LOG_INFO, "[t_ext2_audit_overflow] Location: ext2.c:1876 in ext2_write_inode_data()\n"); + syslog(LOG_INFO, "[t_ext2_audit_overflow] Bug: No check for offset + nbyte overflow\n\n"); + int failures = 0; - + failures += test_large_offset_handling(); failures += test_near_uint32_boundary(); failures += test_mixed_boundary_conditions(); - - syslog(LOG_INFO, "=== Results ===\n"); + + syslog(LOG_INFO, "[t_ext2_audit_overflow] === Results ===\n"); if (failures == 0) { - syslog(LOG_INFO, "✅ ALL TESTS PASSED\n"); + syslog(LOG_INFO, "[t_ext2_audit_overflow] ✅ ALL TESTS PASSED\n"); closelog(); return 0; } else { - syslog(LOG_ERR, "❌ %d TEST(S) FAILED\n", failures); + syslog(LOG_ERR, "[t_ext2_audit_overflow] ❌ %d TEST(S) FAILED\n", failures); closelog(); return 1; } diff --git a/userspace/tests/t_ext2_audit_read_failure.c b/userspace/tests/t_ext2_audit_read_failure.c index beffb281..747926d0 100644 --- a/userspace/tests/t_ext2_audit_read_failure.c +++ b/userspace/tests/t_ext2_audit_read_failure.c @@ -8,75 +8,75 @@ /// @copyright (c) 2024 - Audit Fix Test /// @see EXT2_AUDIT_REPORT.md - Issue #3 -#include +#include +#include #include #include +#include #include -#include -#include #include -#include -#include +#include +#include -#define TEST_FILE "/tmp/test_read_basic.txt" +#define TEST_FILE "/tmp/test_read_basic.txt" #define TEST_DATA_SIZE 8192 /// @brief Test basic read from newly written file /// @return 0 on success, 1 on failure int test_read_after_write(void) { - syslog(LOG_INFO, "[TEST] Read after write...\n"); - + syslog(LOG_INFO, "[t_ext2_audit_read_failure] [TEST] Read after write...\n"); + // Create test file with known content int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to create test file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Failed to create test file: %s\n", strerror(errno)); return 1; } - + // Write distinguishable pattern char *write_data = malloc(TEST_DATA_SIZE); for (int i = 0; i < TEST_DATA_SIZE; i++) { write_data[i] = (char)(i & 0xFF); } - + ssize_t written = write(fd, write_data, TEST_DATA_SIZE); close(fd); - + if (written != TEST_DATA_SIZE) { - syslog(LOG_ERR, "Failed to write all data\n"); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Failed to write all data\n"); free(write_data); return 1; } - + // Now read it back fd = open(TEST_FILE, O_RDONLY, 0); if (fd < 0) { - syslog(LOG_ERR, "Failed to open for reading: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Failed to open for reading: %s\n", strerror(errno)); free(write_data); return 1; } - - char *read_data = malloc(TEST_DATA_SIZE); + + char *read_data = malloc(TEST_DATA_SIZE); ssize_t read_bytes = read(fd, read_data, TEST_DATA_SIZE); close(fd); - + if (read_bytes != TEST_DATA_SIZE) { - syslog(LOG_ERR, "Read failed or incomplete: %ld bytes\n", read_bytes); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Read failed or incomplete: %ld bytes\n", read_bytes); free(write_data); free(read_data); return 1; } - + // Verify data integrity if (memcmp(write_data, read_data, TEST_DATA_SIZE) != 0) { - syslog(LOG_ERR, "Data mismatch after read\n"); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Data mismatch after read\n"); free(write_data); free(read_data); return 1; } - - syslog(LOG_INFO, " ✓ Read data matches written data\n"); + + syslog(LOG_INFO, "[t_ext2_audit_read_failure] ✓ Read data matches written data\n"); free(write_data); free(read_data); return 0; @@ -86,60 +86,60 @@ int test_read_after_write(void) /// @return 0 on success, 1 on failure int test_read_across_blocks(void) { - syslog(LOG_INFO, "[TEST] Read across block boundaries...\n"); - + syslog(LOG_INFO, "[t_ext2_audit_read_failure] [TEST] Read across block boundaries...\n"); + int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to create test file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Failed to create test file: %s\n", strerror(errno)); return 1; } - + // Write 3 blocks worth of data const int block_size = 4096; - char *write_data = malloc(block_size * 3); - + char *write_data = malloc(block_size * 3); + // Fill with repeating pattern for (int i = 0; i < block_size * 3; i++) { - write_data[i] = (char)((i / block_size) + 'A'); // Different byte per block + write_data[i] = (char)((i / block_size) + 'A'); // Different byte per block } - + ssize_t written = write(fd, write_data, block_size * 3); close(fd); - + if (written != block_size * 3) { - syslog(LOG_ERR, "Failed to write\n"); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Failed to write\n"); free(write_data); return 1; } - + // Read back and verify each block - fd = open(TEST_FILE, O_RDONLY, 0); - char *read_data = malloc(block_size * 3); + fd = open(TEST_FILE, O_RDONLY, 0); + char *read_data = malloc(block_size * 3); ssize_t read_bytes = read(fd, read_data, block_size * 3); close(fd); - + if (read_bytes != block_size * 3) { - syslog(LOG_ERR, "Failed to read all blocks\n"); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Failed to read all blocks\n"); free(write_data); free(read_data); return 1; } - + // Verify block by block for (int block = 0; block < 3; block++) { char expected = 'A' + block; for (int i = 0; i < block_size; i++) { int offset = block * block_size + i; if (read_data[offset] != expected) { - syslog(LOG_ERR, "Block %d byte %d mismatch\n", block, i); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Block %d byte %d mismatch\n", block, i); free(write_data); free(read_data); return 1; } } } - - syslog(LOG_INFO, " ✓ All blocks read correctly\n"); + + syslog(LOG_INFO, "[t_ext2_audit_read_failure] ✓ All blocks read correctly\n"); free(write_data); free(read_data); return 0; @@ -149,56 +149,57 @@ int test_read_across_blocks(void) /// @return 0 on success, 1 on failure int test_partial_reads(void) { - syslog(LOG_INFO, "[TEST] Partial reads...\n"); - + syslog(LOG_INFO, "[t_ext2_audit_read_failure] [TEST] Partial reads...\n"); + int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to create test file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Failed to create test file: %s\n", strerror(errno)); return 1; } - + // Write known data char *data = malloc(TEST_DATA_SIZE); for (int i = 0; i < TEST_DATA_SIZE; i++) { data[i] = (char)(i & 0xFF); } - + write(fd, data, TEST_DATA_SIZE); close(fd); - + // Read in small chunks - fd = open(TEST_FILE, O_RDONLY, 0); + fd = open(TEST_FILE, O_RDONLY, 0); const int chunk_size = 1000; char chunk[chunk_size]; int total_read = 0; - + while (1) { ssize_t bytes = read(fd, chunk, chunk_size); - if (bytes <= 0) break; - + if (bytes <= 0) + break; + // Verify this chunk for (int i = 0; i < bytes; i++) { char expected = (char)((total_read + i) & 0xFF); if (chunk[i] != expected) { - syslog(LOG_ERR, "Chunk read mismatch at offset %d\n", total_read + i); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Chunk read mismatch at offset %d\n", total_read + i); close(fd); free(data); return 1; } } - + total_read += bytes; } - + close(fd); - + if (total_read != TEST_DATA_SIZE) { - syslog(LOG_ERR, "Did not read all data: got %d of %d\n", total_read, TEST_DATA_SIZE); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Did not read all data: got %d of %d\n", total_read, TEST_DATA_SIZE); free(data); return 1; } - - syslog(LOG_INFO, " ✓ All partial reads consistent and correct\n"); + + syslog(LOG_INFO, "[t_ext2_audit_read_failure] ✓ All partial reads consistent and correct\n"); free(data); return 0; } @@ -207,64 +208,64 @@ int test_partial_reads(void) /// @return 0 on success, 1 on failure int test_read_eof_behavior(void) { - syslog(LOG_INFO, "[TEST] Read at EOF behavior...\n"); - + syslog(LOG_INFO, "[t_ext2_audit_read_failure] [TEST] Read at EOF behavior...\n"); + int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to create test file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Failed to create test file: %s\n", strerror(errno)); return 1; } - + // Write small amount write(fd, "small", 5); close(fd); - + // Try to read beyond EOF fd = open(TEST_FILE, O_RDONLY, 0); char buffer[1024]; - + ssize_t bytes = read(fd, buffer, 1024); if (bytes != 5) { - syslog(LOG_ERR, "Read at small file returned %ld, expected 5\n", bytes); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Read at small file returned %ld, expected 5\n", bytes); close(fd); return 1; } - + // Try second read (should return 0 for EOF) bytes = read(fd, buffer, 1024); if (bytes != 0) { - syslog(LOG_ERR, "Read at EOF returned %ld, expected 0\n", bytes); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] Read at EOF returned %ld, expected 0\n", bytes); close(fd); return 1; } - + close(fd); - syslog(LOG_INFO, " ✓ EOF behavior correct\n"); + syslog(LOG_INFO, "[t_ext2_audit_read_failure] ✓ EOF behavior correct\n"); return 0; } int main(void) { openlog("t_ext2_read_failure", LOG_CONS | LOG_PID, LOG_USER); - syslog(LOG_INFO, "\n=== EXT2 Read Failure Test Suite ===\n"); - syslog(LOG_INFO, "Testing: Issue #3 - Silent read failures\n"); - syslog(LOG_INFO, "Location: ext2.c:1809-1815 in ext2_read_inode_data()\n"); - syslog(LOG_INFO, "Bug: Error on block read is ignored, stale cache returned\n\n"); - + syslog(LOG_INFO, "[t_ext2_audit_read_failure] \n=== EXT2 Read Failure Test Suite ===\n"); + syslog(LOG_INFO, "[t_ext2_audit_read_failure] Testing: Issue #3 - Silent read failures\n"); + syslog(LOG_INFO, "[t_ext2_audit_read_failure] Location: ext2.c:1809-1815 in ext2_read_inode_data()\n"); + syslog(LOG_INFO, "[t_ext2_audit_read_failure] Bug: Error on block read is ignored, stale cache returned\n\n"); + int failures = 0; - + failures += test_read_after_write(); failures += test_read_across_blocks(); failures += test_partial_reads(); failures += test_read_eof_behavior(); - - syslog(LOG_INFO, "=== Results ===\n"); + + syslog(LOG_INFO, "[t_ext2_audit_read_failure] === Results ===\n"); if (failures == 0) { - syslog(LOG_INFO, "✅ ALL TESTS PASSED\n"); + syslog(LOG_INFO, "[t_ext2_audit_read_failure] ✅ ALL TESTS PASSED\n"); closelog(); return 0; } else { - syslog(LOG_ERR, "❌ %d TEST(S) FAILED\n", failures); + syslog(LOG_ERR, "[t_ext2_audit_read_failure] ❌ %d TEST(S) FAILED\n", failures); closelog(); return 1; } diff --git a/userspace/tests/t_ext2_audit_write_boundary.c b/userspace/tests/t_ext2_audit_write_boundary.c index 38ffbf3d..ee948b47 100644 --- a/userspace/tests/t_ext2_audit_write_boundary.c +++ b/userspace/tests/t_ext2_audit_write_boundary.c @@ -8,98 +8,98 @@ /// @copyright (c) 2024 - Audit Fix Test /// @see EXT2_AUDIT_REPORT.md - Issue #1 -#include +#include +#include #include #include +#include #include -#include -#include #include -#include -#include +#include +#include -#define TEST_FILE "/tmp/test_write_boundary.txt" -#define TEST_DATA_SIZE 8192 // 8KB - spans 2x 4KB blocks -#define BLOCK_SIZE 4096 +#define TEST_FILE "/tmp/test_write_boundary.txt" +#define TEST_DATA_SIZE 8192 // 8KB - spans 2x 4KB blocks +#define BLOCK_SIZE 4096 /// @brief Test writing data that spans multiple blocks with unaligned offset /// @return 0 on success, 1 on failure int test_unaligned_write_spanning_blocks(void) { - syslog(LOG_INFO, "[TEST] Unaligned write spanning multiple blocks\n"); - + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] [TEST] Unaligned write spanning multiple blocks\n"); + // Create and open file for writing int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to create test file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Failed to create test file: %s\n", strerror(errno)); return 1; } - + // Allocate test data - fill with pattern char *write_data = malloc(TEST_DATA_SIZE); if (!write_data) { - syslog(LOG_ERR, "Failed to allocate write buffer: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Failed to allocate write buffer: %s\n", strerror(errno)); close(fd); return 1; } - + // Fill with distinguishable pattern for (int i = 0; i < TEST_DATA_SIZE; i++) { write_data[i] = (char)(i & 0xFF); } - + // Write unaligned: offset 2048, then 8192 bytes // This will: // - Start in middle of block 0 (offset 2048, write 2048 bytes) // - Span into block 1 (write 4096 bytes) // - End at start of block 2 (write 2048 bytes) // The bug would overflow when setting right=block_size on the last block - + ssize_t written = write(fd, write_data, TEST_DATA_SIZE); if (written != TEST_DATA_SIZE) { - syslog(LOG_ERR, "Failed to write all data: wrote %ld of %d\n", written, TEST_DATA_SIZE); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Failed to write all data: wrote %ld of %d\n", written, TEST_DATA_SIZE); close(fd); free(write_data); return 1; } - + close(fd); - + // Read back and verify data integrity fd = open(TEST_FILE, O_RDONLY, 0); if (fd < 0) { - syslog(LOG_ERR, "Failed to open test file for reading: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Failed to open test file for reading: %s\n", strerror(errno)); free(write_data); return 1; } - + char *read_data = malloc(TEST_DATA_SIZE); if (!read_data) { - syslog(LOG_ERR, "Failed to allocate read buffer: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Failed to allocate read buffer: %s\n", strerror(errno)); close(fd); free(write_data); return 1; } - + ssize_t read_bytes = read(fd, read_data, TEST_DATA_SIZE); close(fd); - + if (read_bytes != TEST_DATA_SIZE) { - syslog(LOG_ERR, "Failed to read all data: read %ld of %d\n", read_bytes, TEST_DATA_SIZE); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Failed to read all data: read %ld of %d\n", read_bytes, TEST_DATA_SIZE); free(write_data); free(read_data); return 1; } - + // Verify data matches if (memcmp(write_data, read_data, TEST_DATA_SIZE) != 0) { - syslog(LOG_ERR, "Data mismatch: written data differs from read data\n"); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Data mismatch: written data differs from read data\n"); free(write_data); free(read_data); return 1; } - - syslog(LOG_INFO, " ✓ Data written and read back correctly\n"); + + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] ✓ Data written and read back correctly\n"); free(write_data); free(read_data); return 0; @@ -109,42 +109,42 @@ int test_unaligned_write_spanning_blocks(void) /// @return 0 on success, 1 on failure int test_exact_block_boundary_write(void) { - syslog(LOG_INFO, "[TEST] Write at exact block boundary\n"); - + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] [TEST] Write at exact block boundary\n"); + int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to create test file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Failed to create test file: %s\n", strerror(errno)); return 1; } - + // Write exactly 2 blocks worth char *data = malloc(BLOCK_SIZE * 2); memset(data, 0xAA, BLOCK_SIZE * 2); - + ssize_t written = write(fd, data, BLOCK_SIZE * 2); close(fd); - + if (written != BLOCK_SIZE * 2) { - syslog(LOG_ERR, "Failed to write: wrote %ld of %d\n", written, BLOCK_SIZE * 2); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Failed to write: wrote %ld of %d\n", written, BLOCK_SIZE * 2); free(data); return 1; } - + // Verify file size struct stat st; if (stat(TEST_FILE, &st) < 0) { - syslog(LOG_ERR, "Failed to stat file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Failed to stat file: %s\n", strerror(errno)); free(data); return 1; } - + if (st.st_size != BLOCK_SIZE * 2) { - syslog(LOG_ERR, "File size mismatch: expected %d, got %ld\n", BLOCK_SIZE * 2, st.st_size); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] File size mismatch: expected %d, got %ld\n", BLOCK_SIZE * 2, st.st_size); free(data); return 1; } - - syslog(LOG_INFO, " ✓ Boundary write successful, file size correct\n"); + + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] ✓ Boundary write successful, file size correct\n"); free(data); return 0; } @@ -153,51 +153,51 @@ int test_exact_block_boundary_write(void) /// @return 0 on success, 1 on failure int test_multiple_partial_writes(void) { - syslog(LOG_INFO, "[TEST] Multiple partial writes\n"); - + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] [TEST] Multiple partial writes\n"); + int fd = open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd < 0) { - syslog(LOG_ERR, "Failed to create test file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Failed to create test file: %s\n", strerror(errno)); return 1; } - + // Write in chunks that don't align with block boundaries const int num_writes = 5; - const int chunk_size = 1500; // Odd size to cause boundary issues - + const int chunk_size = 1500; // Odd size to cause boundary issues + char *data = malloc(chunk_size); for (int i = 0; i < chunk_size; i++) { data[i] = (char)i; } - + for (int i = 0; i < num_writes; i++) { ssize_t written = write(fd, data, chunk_size); if (written != chunk_size) { - syslog(LOG_ERR, "Write %d failed: wrote %ld of %d\n", i, written, chunk_size); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Write %d failed: wrote %ld of %d\n", i, written, chunk_size); close(fd); free(data); return 1; } } - + close(fd); - + // Verify final file size struct stat st; if (stat(TEST_FILE, &st) < 0) { - syslog(LOG_ERR, "Failed to stat file: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] Failed to stat file: %s\n", strerror(errno)); free(data); return 1; } - + int expected_size = num_writes * chunk_size; if (st.st_size != expected_size) { - syslog(LOG_ERR, "File size mismatch: expected %d, got %ld\n", expected_size, st.st_size); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] File size mismatch: expected %d, got %ld\n", expected_size, st.st_size); free(data); return 1; } - - syslog(LOG_INFO, " ✓ Multiple writes successful, file size correct\n"); + + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] ✓ Multiple writes successful, file size correct\n"); free(data); return 0; } @@ -205,25 +205,25 @@ int test_multiple_partial_writes(void) int main(void) { openlog("t_ext2_audit_write_boundary", LOG_CONS | LOG_PID, LOG_USER); - - syslog(LOG_INFO, "=== EXT2 Write Boundary Test Suite ===\n"); - syslog(LOG_INFO, "Testing: Issue #1 - Buffer overflow on write boundary\n"); - syslog(LOG_INFO, "Location: ext2.c:1901 in ext2_write_inode_data()\n"); - syslog(LOG_INFO, "Bug: right = fs->block_size (should be block_size - 1)\n"); - + + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] === EXT2 Write Boundary Test Suite ===\n"); + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] Testing: Issue #1 - Buffer overflow on write boundary\n"); + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] Location: ext2.c:1901 in ext2_write_inode_data()\n"); + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] Bug: right = fs->block_size (should be block_size - 1)\n"); + int failures = 0; - + failures += test_unaligned_write_spanning_blocks(); failures += test_exact_block_boundary_write(); failures += test_multiple_partial_writes(); - - syslog(LOG_INFO, "=== Results ===\n"); + + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] === Results ===\n"); if (failures == 0) { - syslog(LOG_INFO, "✅ ALL TESTS PASSED\n"); + syslog(LOG_INFO, "[t_ext2_audit_write_boundary] ✅ ALL TESTS PASSED\n"); closelog(); return 0; } else { - syslog(LOG_ERR, "❌ %d TEST(S) FAILED\n", failures); + syslog(LOG_ERR, "[t_ext2_audit_write_boundary] ❌ %d TEST(S) FAILED\n", failures); closelog(); return 1; } diff --git a/userspace/tests/t_fflush.c b/userspace/tests/t_fflush.c index b13ab599..bd3ffd0e 100644 --- a/userspace/tests/t_fflush.c +++ b/userspace/tests/t_fflush.c @@ -3,36 +3,38 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include +#include #include +#include #include -#include int main(int argc, char *argv[]) { // Test 1: fflush with stdout - printf("Testing fflush with stdout..."); + syslog(LOG_INFO, "[t_fflush] Testing fflush with stdout..."); if (fflush(STDOUT_FILENO) == 0) { - printf(" SUCCESS\n"); + syslog(LOG_INFO, "[t_fflush] SUCCESS\n"); } else { - printf(" FAILED\n"); + syslog(LOG_INFO, "[t_fflush] FAILED\n"); return 1; } // Test 2: fflush with stderr - printf("Testing fflush with stderr..."); + syslog(LOG_INFO, "[t_fflush] Testing fflush with stderr..."); if (fflush(STDERR_FILENO) == 0) { - printf(" SUCCESS\n"); + syslog(LOG_INFO, "[t_fflush] SUCCESS\n"); } else { - printf(" FAILED\n"); + syslog(LOG_INFO, "[t_fflush] FAILED\n"); return 1; } // Test 3: fflush with negative value (flush all streams) - printf("Testing fflush with -1 (all streams)..."); + syslog(LOG_INFO, "[t_fflush] Testing fflush with -1 (all streams)..."); if (fflush(-1) == 0) { - printf(" SUCCESS\n"); + syslog(LOG_INFO, "[t_fflush] SUCCESS\n"); } else { - printf(" FAILED\n"); + syslog(LOG_INFO, "[t_fflush] FAILED\n"); return 1; } @@ -45,20 +47,20 @@ int main(int argc, char *argv[]) } if (fd >= 0) { write(fd, "test data", 9); - printf("Testing fflush with file descriptor..."); + syslog(LOG_INFO, "[t_fflush] Testing fflush with file descriptor..."); if (fflush(fd) == 0) { - printf(" SUCCESS\n"); + syslog(LOG_INFO, "[t_fflush] SUCCESS\n"); } else { - printf(" FAILED\n"); + syslog(LOG_INFO, "[t_fflush] FAILED\n"); close(fd); return 1; } close(fd); } else { // If we can't create files in either location, that's okay for this test - printf("Skipping file descriptor test (no writable directory found).\n"); + syslog(LOG_INFO, "[t_fflush] Skipping file descriptor test (no writable directory found).\n"); } - printf("All fflush tests passed!\n"); + syslog(LOG_INFO, "[t_fflush] All fflush tests passed!\n"); return 0; } diff --git a/userspace/tests/t_fhs.c b/userspace/tests/t_fhs.c index 8eb086ed..1c577ce2 100644 --- a/userspace/tests/t_fhs.c +++ b/userspace/tests/t_fhs.c @@ -3,8 +3,10 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include +#include #include /// @brief Test structure for FHS directory verification. @@ -16,33 +18,33 @@ typedef struct { /// @brief List of FHS directories to verify. static const fhs_test_t fhs_tests[] = { - {"/tmp", 01777, "Temporary files directory"}, - {"/home", 0755, "User home directories"}, - {"/root", 0700, "Root home directory"}, - {"/var", 0755, "Variable data"}, - {"/var/tmp", 01777, "Temporary variable data"}, - {"/var/log", 0755, "Log files"}, - {"/usr", 0755, "User programs and data"}, - {"/usr/bin", 0755, "User executable programs"}, - {"/usr/lib", 0755, "User libraries"}, - {"/usr/share", 0755, "User data"}, - {"/bin", 0755, "Essential executable programs"}, - {"/lib", 0755, "Essential system libraries"}, - {"/sbin", 0755, "System administration programs"}, - {"/etc", 0755, "System configuration"}, - {"/dev", 0755, "Device files"}, - {"/mnt", 0755, "Temporary mount points"}, - {"/media", 0755, "Removable media mount points"}, - {NULL, 0, NULL} // Null terminator + {"/tmp", 01777, "Temporary files directory" }, + {"/home", 0755, "User home directories" }, + {"/root", 0700, "Root home directory" }, + {"/var", 0755, "Variable data" }, + {"/var/tmp", 01777, "Temporary variable data" }, + {"/var/log", 0755, "Log files" }, + {"/usr", 0755, "User programs and data" }, + {"/usr/bin", 0755, "User executable programs" }, + {"/usr/lib", 0755, "User libraries" }, + {"/usr/share", 0755, "User data" }, + {"/bin", 0755, "Essential executable programs" }, + {"/lib", 0755, "Essential system libraries" }, + {"/sbin", 0755, "System administration programs"}, + {"/etc", 0755, "System configuration" }, + {"/dev", 0755, "Device files" }, + {"/mnt", 0755, "Temporary mount points" }, + {"/media", 0755, "Removable media mount points" }, + {NULL, 0, NULL } // Null terminator }; int main(int argc, char *argv[]) { - int total_tests = 0; + int total_tests = 0; int passed_tests = 0; int failed_tests = 0; - printf("=== Filesystem Hierarchy Standard (FHS) Verification ===\n\n"); + syslog(LOG_INFO, "[t_fhs] === Filesystem Hierarchy Standard (FHS) Verification ===\n\n"); // Iterate through all FHS directories to test for (size_t i = 0; fhs_tests[i].path != NULL; i++) { @@ -56,38 +58,36 @@ int main(int argc, char *argv[]) // Directory exists, check if it's actually a directory if (S_ISDIR(stat_buf.st_mode)) { // Get the permission bits (lower 12 bits) - mode_t actual_mode = stat_buf.st_mode & 07777; + mode_t actual_mode = stat_buf.st_mode & 07777; mode_t expected_mode = test->expected_mode; if (actual_mode == expected_mode) { - printf("[PASS] %s (%s) - Mode: 0%o\n", - test->path, test->description, actual_mode); + syslog(LOG_INFO, "[t_fhs] [PASS] %s (%s) - Mode: 0%o\n", test->path, test->description, actual_mode); passed_tests++; } else { - printf("[WARN] %s (%s) - Expected mode 0%o, got 0%o\n", - test->path, test->description, expected_mode, actual_mode); + syslog(LOG_INFO, "[t_fhs] [WARN] %s (%s) - Expected mode 0%o, got 0%o\n", test->path, test->description, expected_mode, actual_mode); passed_tests++; // Still count as pass since directory exists } } else { - printf("[FAIL] %s - Exists but is not a directory\n", test->path); + syslog(LOG_INFO, "[t_fhs] [FAIL] %s - Exists but is not a directory\n", test->path); failed_tests++; } } else { - printf("[FAIL] %s - Does not exist\n", test->path); + syslog(LOG_INFO, "[t_fhs] [FAIL] %s - Does not exist\n", test->path); failed_tests++; } } - printf("\n=== Test Summary ===\n"); - printf("Total: %d\n", total_tests); - printf("Passed: %d\n", passed_tests); - printf("Failed: %d\n", failed_tests); + syslog(LOG_INFO, "[t_fhs] \n=== Test Summary ===\n"); + syslog(LOG_INFO, "[t_fhs] Total: %d\n", total_tests); + syslog(LOG_INFO, "[t_fhs] Passed: %d\n", passed_tests); + syslog(LOG_INFO, "[t_fhs] Failed: %d\n", failed_tests); if (failed_tests == 0) { - printf("\n✓ All FHS directories verified successfully!\n"); + syslog(LOG_INFO, "[t_fhs] \n✓ All FHS directories verified successfully!\n"); return 0; } else { - printf("\n✗ %d FHS directories are missing or misconfigured.\n", failed_tests); + syslog(LOG_INFO, "[t_fhs] \n✗ %d FHS directories are missing or misconfigured.\n", failed_tests); return 1; } } diff --git a/userspace/tests/t_fork.c b/userspace/tests/t_fork.c index 9e3318f1..1b08fad9 100644 --- a/userspace/tests/t_fork.c +++ b/userspace/tests/t_fork.c @@ -7,10 +7,13 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include +#include #include #include +#include #include int main(int argc, char *argv[]) @@ -22,34 +25,34 @@ int main(int argc, char *argv[]) if (pid < 0) { // Error in forking - perror("fork"); + syslog(LOG_ERR, "[t_fork] fork: %s", strerror(errno)); return EXIT_FAILURE; } if (pid == 0) { // Child process - printf("Child process: PID = %d, Parent PID = %d\n", getpid(), getppid()); + syslog(LOG_INFO, "[t_fork] Child process: PID = %d, Parent PID = %d\n", getpid(), getppid()); // Simulate some work in the child sleep(1); - printf("Child process exiting successfully.\n"); + syslog(LOG_INFO, "[t_fork] Child process exiting successfully.\n"); exit(EXIT_SUCCESS); } else { // Parent process - printf("Parent process: PID = %d, Child PID = %d\n", getpid(), pid); + syslog(LOG_INFO, "[t_fork] Parent process: PID = %d, Child PID = %d\n", getpid(), pid); // Wait for the child process to complete int status; if (waitpid(pid, &status, 0) == -1) { - perror("waitpid"); + syslog(LOG_ERR, "[t_fork] waitpid: %s", strerror(errno)); return EXIT_FAILURE; } // Check if the child exited normally if (WIFEXITED(status)) { - printf("Parent process: Child exited with status %d.\n", WEXITSTATUS(status)); + syslog(LOG_INFO, "[t_fork] Parent process: Child exited with status %d.\n", WEXITSTATUS(status)); return EXIT_SUCCESS; } - printf("Parent process: Child did not exit normally.\n"); + syslog(LOG_INFO, "[t_fork] Parent process: Child did not exit normally.\n"); return EXIT_FAILURE; } } diff --git a/userspace/tests/t_gid.c b/userspace/tests/t_gid.c index 6e5b319c..47c6c75f 100644 --- a/userspace/tests/t_gid.c +++ b/userspace/tests/t_gid.c @@ -6,11 +6,13 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include +#include #include /// @brief List all groups and their members. @@ -18,11 +20,11 @@ static void list_groups(void) { struct group *iter; while ((iter = getgrent()) != NULL) { - printf("Group name: \"%12s\", passwd: \"%12s\"\n", iter->gr_name, iter->gr_passwd); + syslog(LOG_INFO, "[t_gid] Group name: %12s , passwd: %12s\n", iter->gr_name, iter->gr_passwd); puts("Names: [ "); size_t count = 0; while (iter->gr_mem[count] != NULL) { - printf("%s ", iter->gr_mem[count]); + syslog(LOG_INFO, "[t_gid] %s ", iter->gr_mem[count]); count += 1; } puts("]\n\n"); @@ -31,14 +33,14 @@ static void list_groups(void) int main(int argc, char **argv) { - printf("List of all groups:\n"); + syslog(LOG_INFO, "[t_gid] List of all groups:\n"); // List all groups. list_groups(); // Reset the group list to the beginning. setgrent(); - printf("List all groups again:\n"); + syslog(LOG_INFO, "[t_gid] List all groups again:\n"); // List all groups again. list_groups(); @@ -48,22 +50,22 @@ int main(int argc, char **argv) // Get the group information for the group with GID 0. struct group *root_group = getgrgid(0); if (root_group == NULL) { - fprintf(STDERR_FILENO, "Error in getgrgid function: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_gid] Error in getgrgid function: %s\n", strerror(errno)); return EXIT_FAILURE; } if (strcmp(root_group->gr_name, "root") != 0) { - fprintf(STDERR_FILENO, "Error: Expected group name 'root', got '%s'\n", root_group->gr_name); + syslog(LOG_ERR, "[t_gid] Error: Expected group name 'root', got '%s'\n", root_group->gr_name); return EXIT_FAILURE; } // Get the group information for the group named "root". root_group = getgrnam("root"); if (root_group == NULL) { - fprintf(STDERR_FILENO, "Error in getgrnam function: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_gid] Error in getgrnam function: %s\n", strerror(errno)); return EXIT_FAILURE; } if (root_group->gr_gid != 0) { - fprintf(STDERR_FILENO, "Error: Expected GID 0, got %d\n", root_group->gr_gid); + syslog(LOG_ERR, "[t_gid] Error: Expected GID 0, got %d\n", root_group->gr_gid); return EXIT_FAILURE; } diff --git a/userspace/tests/t_groups.c b/userspace/tests/t_groups.c index 3968ab24..31e9123a 100644 --- a/userspace/tests/t_groups.c +++ b/userspace/tests/t_groups.c @@ -8,9 +8,11 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include +#include #include #include @@ -22,7 +24,7 @@ int main(int argc, char **argv) pid_t sid = getsid(0); // Print the IDs of the current process. - printf("pid: %d, gid: %d, sid: %d\n\n", pid, gid, sid); + syslog(LOG_INFO, "[t_groups] pid: %d, gid: %d, sid: %d\n\n", pid, gid, sid); // Fork 5 child processes. for (int i = 0; i < 5; ++i) { @@ -38,10 +40,7 @@ int main(int argc, char **argv) nanosleep(&req, NULL); // Print the IDs of the child process and its parent. - printf( - "%d) pid_child: %d, gid_child: %d, ppid_child: %d, sid_child: " - "%d\n", - i, pid_child, gid_child, ppid_child, sid_child); + syslog(LOG_INFO, "[t_groups] %d) pid_child: %d, gid_child: %d, ppid_child: %d, sid_child: %d\n", i, pid_child, gid_child, ppid_child, sid_child); exit(EXIT_SUCCESS); } } diff --git a/userspace/tests/t_grp.c b/userspace/tests/t_grp.c index 0435b543..51d8a79e 100644 --- a/userspace/tests/t_grp.c +++ b/userspace/tests/t_grp.c @@ -8,11 +8,13 @@ /// See LICENSE.md for details. #include +#include #include #include #include #include #include +#include #include /// @brief Test the getgrnam function. @@ -22,7 +24,7 @@ static void __test_getgrnam(void) { // Test that getgrnam returns NULL for a non-existent group name. if (getgrnam("r") != NULL) { - errx(EXIT_FAILURE, "Group entry for non-existent group \"r\" found"); + errx(EXIT_FAILURE, "Group entry for non-existent group r found"); } // Test that getgrnam returns a valid entry for the "root" group. diff --git a/userspace/tests/t_hashmap.c b/userspace/tests/t_hashmap.c index fe4e7c5b..2cc12daf 100644 --- a/userspace/tests/t_hashmap.c +++ b/userspace/tests/t_hashmap.c @@ -4,13 +4,14 @@ /// See LICENSE.md for details. #include +#include +#include #include #include #include +#include #include -#include "hashmap.h" - // Custom allocation function for hashmap entries hashmap_entry_t *alloc_entry(void) { @@ -37,35 +38,35 @@ int main(void) hashmap_insert(&map, "grape", "A small purple or green fruit"); if (strcmp(hashmap_get(&map, "apple"), "A sweet red fruit") != 0) { - fprintf(stderr, "Error: Failed to retrieve 'apple'\n"); + syslog(LOG_ERR, "[t_hashmap] Error: Failed to retrieve 'apple'\n"); return 1; } if (strcmp(hashmap_get(&map, "banana"), "A long yellow fruit") != 0) { - fprintf(stderr, "Error: Failed to retrieve 'banana'\n"); + syslog(LOG_ERR, "[t_hashmap] Error: Failed to retrieve 'banana'\n"); return 1; } if (strcmp(hashmap_get(&map, "grape"), "A small purple or green fruit") != 0) { - fprintf(stderr, "Error: Failed to retrieve 'grape'\n"); + syslog(LOG_ERR, "[t_hashmap] Error: Failed to retrieve 'grape'\n"); return 1; } // Test retrieving a non-existent key if (hashmap_get(&map, "orange") != NULL) { - fprintf(stderr, "Error: Retrieved value for non-existent key 'orange'\n"); + syslog(LOG_ERR, "[t_hashmap] Error: Retrieved value for non-existent key 'orange'\n"); return 1; } // Test updating an existing key hashmap_insert(&map, "apple", "A popular fruit often red or green"); if (strcmp(hashmap_get(&map, "apple"), "A popular fruit often red or green") != 0) { - fprintf(stderr, "Error: Failed to update value for 'apple'\n"); + syslog(LOG_ERR, "[t_hashmap] Error: Failed to update value for 'apple'\n"); return 1; } // Test removing a key hashmap_remove(&map, "banana"); if (hashmap_get(&map, "banana") != NULL) { - fprintf(stderr, "Error: Key 'banana' was not removed\n"); + syslog(LOG_ERR, "[t_hashmap] Error: Key 'banana' was not removed\n"); return 1; } @@ -75,22 +76,22 @@ int main(void) // Test reinserting and retrieval after some removals hashmap_insert(&map, "banana", "A reinserted long yellow fruit"); if (strcmp(hashmap_get(&map, "banana"), "A reinserted long yellow fruit") != 0) { - fprintf(stderr, "Error: Failed to retrieve reinserted 'banana'\n"); + syslog(LOG_ERR, "[t_hashmap] Error: Failed to retrieve reinserted 'banana'\n"); return 1; } // Test the removal of all items and final cleanup hashmap_destroy(&map); if (hashmap_get(&map, "apple") != NULL) { - fprintf(stderr, "Error: Key 'apple' still exists after destroy\n"); + syslog(LOG_ERR, "[t_hashmap] Error: Key 'apple' still exists after destroy\n"); return 1; } if (hashmap_get(&map, "grape") != NULL) { - fprintf(stderr, "Error: Key 'grape' still exists after destroy\n"); + syslog(LOG_ERR, "[t_hashmap] Error: Key 'grape' still exists after destroy\n"); return 1; } if (hashmap_get(&map, "banana") != NULL) { - fprintf(stderr, "Error: Key 'banana' still exists after destroy\n"); + syslog(LOG_ERR, "[t_hashmap] Error: Key 'banana' still exists after destroy\n"); return 1; } diff --git a/userspace/tests/t_itimer.c b/userspace/tests/t_itimer.c index f0a0fe70..c1b7410b 100644 --- a/userspace/tests/t_itimer.c +++ b/userspace/tests/t_itimer.c @@ -7,12 +7,14 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include #include @@ -46,7 +48,7 @@ int main(void) // Start the timer if (setitimer(ITIMER_REAL, &timer, NULL) == -1) { - perror("setitimer"); + syslog(LOG_ERR, "[t_itimer] setitimer: %s", strerror(errno)); return EXIT_FAILURE; } @@ -62,7 +64,7 @@ int main(void) current_time = time(NULL); // Print the elapsed time since the start - printf("Timer event %d fired at %u seconds since start\n", timer_count, current_time - start_time); + syslog(LOG_INFO, "[t_itimer] Timer event %d fired at %u seconds since start\n", timer_count, current_time - start_time); } } @@ -72,11 +74,11 @@ int main(void) timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = 0; if (setitimer(ITIMER_REAL, &timer, NULL) == -1) { - perror("setitimer (stop)"); + syslog(LOG_ERR, "[t_itimer] setitimer (stop): %s", strerror(errno)); return EXIT_FAILURE; } - printf("Test completed: Timer fired %d times\n", timer_count); + syslog(LOG_INFO, "[t_itimer] Test completed: Timer fired %d times\n", timer_count); return EXIT_SUCCESS; } diff --git a/userspace/tests/t_kill.c b/userspace/tests/t_kill.c index ea4a3f6b..52502fa5 100644 --- a/userspace/tests/t_kill.c +++ b/userspace/tests/t_kill.c @@ -8,12 +8,14 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include #include @@ -21,13 +23,13 @@ /// @param sig The signal number. void child_sigusr1_handler(int sig) { - printf("handler(sig: %d) : Starting handler (pid: %d).\n", sig, getpid()); - printf("handler(sig: %d) : Ending handler (pid: %d).\n", sig, getpid()); + syslog(LOG_INFO, "[t_kill] handler(sig: %d) : Starting handler (pid: %d).\n", sig, getpid()); + syslog(LOG_INFO, "[t_kill] handler(sig: %d) : Ending handler (pid: %d).\n", sig, getpid()); } int main(int argc, char *argv[]) { - printf("main : Creating child!\n"); + syslog(LOG_INFO, "[t_kill] main : Creating child!\n"); // Fork the process to create a child pid_t cpid = fork(); @@ -35,18 +37,18 @@ int main(int argc, char *argv[]) if (cpid == 0) { // Child process cpid = getpid(); // Get the child PID - printf("I'm the child (pid: %d)!\n", cpid); + syslog(LOG_INFO, "[t_kill] I'm the child (pid: %d)!\n", cpid); // Set up a signal handler for SIGUSR1 in the child struct sigaction action; memset(&action, 0, sizeof(action)); // Clear the action structure action.sa_handler = child_sigusr1_handler; // Set handler function - printf("Setting up signal handler for SIGUSR1 in child (pid: %d)...\n", cpid); + syslog(LOG_INFO, "[t_kill] Setting up signal handler for SIGUSR1 in child (pid: %d)...\n", cpid); // Check if setting up the signal handler fails. if (sigaction(SIGUSR1, &action, NULL) == -1) { - fprintf(STDERR_FILENO, "Failed to set signal handler for SIGUSR1: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_kill] Failed to set signal handler for SIGUSR1: %s\n", strerror(errno)); return EXIT_FAILURE; // Return failure if handler setup fails } @@ -55,14 +57,14 @@ int main(int argc, char *argv[]) // Child process loop - waiting for signals while (1) { - printf("I'm the child (pid: %d): I'm waiting...\n", cpid); + syslog(LOG_INFO, "[t_kill] I'm the child (pid: %d): I'm waiting...\n", cpid); // Sleep for 500 ms. nanosleep(&req, NULL); } } else if (cpid > 0) { // Parent process - printf("I'm the parent (pid: %d)!\n", getpid()); + syslog(LOG_INFO, "[t_kill] I'm the parent (pid: %d)!\n", getpid()); // Request to sleep for 1.5 seconds. struct timespec req = {1, 500000000}; @@ -70,11 +72,11 @@ int main(int argc, char *argv[]) // Sleep for 1.5 seconds. nanosleep(&req, NULL); - printf("Sending SIGUSR1 to child (pid: %d)!\n", cpid); + syslog(LOG_INFO, "[t_kill] Sending SIGUSR1 to child (pid: %d)!\n", cpid); // Send SIGUSR1 to the child process if (kill(cpid, SIGUSR1) == -1) { - fprintf(STDERR_FILENO, "Failed to send SIGUSR1 to child: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_kill] Failed to send SIGUSR1 to child: %s\n", strerror(errno)); return EXIT_FAILURE; // Return failure if signal sending fails } @@ -83,20 +85,20 @@ int main(int argc, char *argv[]) // Send SIGTERM to the child process to terminate it if (kill(cpid, SIGTERM) == -1) { - fprintf(STDERR_FILENO, "Failed to send SIGTERM to child: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_kill] Failed to send SIGTERM to child: %s\n", strerror(errno)); return EXIT_FAILURE; // Return failure if termination fails } // Wait for the child process to terminate if (wait(NULL) == -1) { - fprintf(STDERR_FILENO, "Failed to wait for child process: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_kill] Failed to wait for child process: %s\n", strerror(errno)); return EXIT_FAILURE; // Return failure if wait fails } - printf("main : Child has terminated. End of parent process.\n"); + syslog(LOG_INFO, "[t_kill] main : Child has terminated. End of parent process.\n"); } else { // Fork failed - fprintf(STDERR_FILENO, "Failed to fork: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_kill] Failed to fork: %s\n", strerror(errno)); return EXIT_FAILURE; // Return failure if fork fails } diff --git a/userspace/tests/t_list.c b/userspace/tests/t_list.c index 6615452a..fc1da854 100644 --- a/userspace/tests/t_list.c +++ b/userspace/tests/t_list.c @@ -4,11 +4,12 @@ /// See LICENSE.md for details. #include +#include +#include #include #include #include - -#include "list.h" // Include the list.h header file here +#include // Custom allocation and deallocation functions for list nodes static inline listnode_t *node_alloc(void) @@ -38,49 +39,49 @@ int main(void) list_insert_back(&list1, "banana"); list_insert_front(&list1, "cherry"); if (list_size(&list1) != 3) { - printf("Error: list_insert_front or list_insert_back failed\n"); + syslog(LOG_INFO, "[t_list] Error: list_insert_front or list_insert_back failed\n"); return 1; } // Test peeking at the front and back if (strcmp(list_peek_front(&list1), "cherry") != 0) { - printf("Error: list_peek_front failed\n"); + syslog(LOG_INFO, "[t_list] Error: list_peek_front failed\n"); return 1; } if (strcmp(list_peek_back(&list1), "banana") != 0) { - printf("Error: list_peek_back failed\n"); + syslog(LOG_INFO, "[t_list] Error: list_peek_back failed\n"); return 1; } // Test list size and empty check if (list_size(&list1) != 3) { - printf("Error: list_size failed\n"); + syslog(LOG_INFO, "[t_list] Error: list_size failed\n"); return 1; } if (list_empty(&list1)) { - printf("Error: list_empty failed\n"); + syslog(LOG_INFO, "[t_list] Error: list_empty failed\n"); return 1; } // Test remove from front char *removed = list_remove_front(&list1); if (strcmp(removed, "cherry") != 0) { - printf("Error: list_remove_front failed\n"); + syslog(LOG_INFO, "[t_list] Error: list_remove_front failed\n"); return 1; } if (list_size(&list1) != 2) { - printf("Error: list size after list_remove_front is incorrect\n"); + syslog(LOG_INFO, "[t_list] Error: list size after list_remove_front is incorrect\n"); return 1; } // Test remove from back removed = list_remove_back(&list1); if (strcmp(removed, "banana") != 0) { - printf("Error: list_remove_back failed\n"); + syslog(LOG_INFO, "[t_list] Error: list_remove_back failed\n"); return 1; } if (list_size(&list1) != 1) { - printf("Error: list size after list_remove_back is incorrect\n"); + syslog(LOG_INFO, "[t_list] Error: list size after list_remove_back is incorrect\n"); return 1; } @@ -88,7 +89,7 @@ int main(void) list_insert_back(&list1, "banana"); listnode_t *found_node = list_find(&list1, "banana"); if (!(found_node && strcmp(found_node->value, "banana") == 0)) { - printf("Error: list_find failed\n"); + syslog(LOG_INFO, "[t_list] Error: list_find failed\n"); return 1; } @@ -99,11 +100,11 @@ int main(void) list_insert_back(&list2, "kiwi"); list_merge(&list1, &list2); if (!list_empty(&list2)) { - printf("Error: list_merge failed; source list is not empty\n"); + syslog(LOG_ERR, "[t_list] Error: list_merge failed; source list is not empty\n"); return 1; } if (list_size(&list1) != 6) { - printf("Error: list size after list_merge is incorrect\n"); + syslog(LOG_INFO, "[t_list] Error: list size after list_merge is incorrect\n"); return 1; } @@ -111,11 +112,11 @@ int main(void) list_destroy(&list1); list_destroy(&list2); if (!list_empty(&list1) || list_size(&list1) != 0) { - printf("Error: list_destroy failed for list1\n"); + syslog(LOG_INFO, "[t_list] Error: list_destroy failed for list1\n"); return 1; } if (!list_empty(&list2) || list_size(&list2) != 0) { - printf("Error: list_destroy failed for list2\n"); + syslog(LOG_INFO, "[t_list] Error: list_destroy failed for list2\n"); return 1; } diff --git a/userspace/tests/t_mem.c b/userspace/tests/t_mem.c index c8bdc2ee..4730a73e 100644 --- a/userspace/tests/t_mem.c +++ b/userspace/tests/t_mem.c @@ -6,9 +6,11 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include +#include #include #include @@ -21,7 +23,7 @@ int main(int argc, char *argv[]) // Allocate memory for an array of row pointers (rows x cols matrix). int **M = (int **)malloc(rows * sizeof(int *)); if (M == NULL) { - fprintf(stderr, "Failed to allocate memory for row pointers.\n"); + syslog(LOG_ERR, "[t_mem] Failed to allocate memory for row pointers.\n"); return EXIT_FAILURE; } @@ -29,7 +31,7 @@ int main(int argc, char *argv[]) for (int i = 0; i < rows; ++i) { M[i] = (int *)malloc(cols * sizeof(int)); if (M[i] == NULL) { - fprintf(stderr, "Failed to allocate memory for row %d.\n", i); + syslog(LOG_ERR, "[t_mem] Failed to allocate memory for row %d.\n", i); // Free any previously allocated memory to prevent memory leaks. for (int j = 0; j < i; ++j) { diff --git a/userspace/tests/t_mkdir.c b/userspace/tests/t_mkdir.c index 9d4bde9a..ccde07ba 100644 --- a/userspace/tests/t_mkdir.c +++ b/userspace/tests/t_mkdir.c @@ -6,12 +6,14 @@ /// See LICENSE.md for details. #include +#include #include #include #include #include #include #include +#include #include /// @brief Constructs a full file path by combining a parent directory and a subdirectory name. @@ -50,11 +52,11 @@ int create_dir(const char *parent_directory, const char *directory_name, mode_t { char path[PATH_MAX]; if (__build_path(parent_directory, directory_name, path, sizeof(path))) { - fprintf(stderr, "Error: Path construction failed.\n"); + syslog(LOG_ERR, "[t_mkdir] Error: Path construction failed.\n"); return EXIT_FAILURE; } if (mkdir(path, mode) < 0) { - fprintf(stderr, "Failed to create directory %s: %s\n", path, strerror(errno)); + syslog(LOG_ERR, "[t_mkdir] Failed to create directory %s: %s\n", path, strerror(errno)); return EXIT_FAILURE; } return EXIT_SUCCESS; @@ -68,11 +70,11 @@ int remove_dir(const char *parent_directory, const char *directory_name) { char path[PATH_MAX]; if (__build_path(parent_directory, directory_name, path, sizeof(path))) { - fprintf(stderr, "Error: Path construction failed.\n"); + syslog(LOG_ERR, "[t_mkdir] Error: Path construction failed.\n"); return EXIT_FAILURE; } if (rmdir(path) < 0) { - fprintf(stderr, "Failed to remove directory %s: %s\n", path, strerror(errno)); + syslog(LOG_ERR, "[t_mkdir] Failed to remove directory %s: %s\n", path, strerror(errno)); return EXIT_FAILURE; } return EXIT_SUCCESS; @@ -86,16 +88,16 @@ int check_dir(const char *parent_directory, const char *directory_name) { char path[PATH_MAX]; if (__build_path(parent_directory, directory_name, path, sizeof(path))) { - fprintf(stderr, "Error: Path construction failed.\n"); + syslog(LOG_ERR, "[t_mkdir] Error: Path construction failed.\n"); return EXIT_FAILURE; } struct stat buffer; if (stat(path, &buffer) < 0) { - fprintf(stderr, "Failed to check directory `%s`: %s\n", path, strerror(errno)); + syslog(LOG_ERR, "[t_mkdir] Failed to check directory `%s`: %s\n", path, strerror(errno)); return EXIT_FAILURE; } if (!S_ISDIR(buffer.st_mode)) { - fprintf(stderr, "Path `%s` is not a directory.\n", path); + syslog(LOG_ERR, "[t_mkdir] Path `%s` is not a directory.\n", path); return EXIT_FAILURE; } return EXIT_SUCCESS; diff --git a/userspace/tests/t_msgget.c b/userspace/tests/t_msgget.c index 59f3e630..1ee40237 100644 --- a/userspace/tests/t_msgget.c +++ b/userspace/tests/t_msgget.c @@ -13,10 +13,12 @@ #include #include #include +#include #include #include #include #include +#include #include #define MESSAGE_LEN 100 @@ -40,9 +42,9 @@ static inline void __send_message(int msqid, long mtype, message_t *message, con strncpy(message->mesg_text, msg, MESSAGE_LEN); // Send the message. if (msgsnd(msqid, message, sizeof(message->mesg_text), 0) < 0) { - perror("Failed to send the message"); + syslog(LOG_ERR, "[t_msgget] Failed to send the message: %s", strerror(errno)); } else { - printf("[%2d] Message sent (%2ld) `%s`\n", getpid(), message->mesg_type, message->mesg_text); + syslog(LOG_INFO, "[t_msgget] [%2d] Message sent (%2ld) `%s`\n", getpid(), message->mesg_type, message->mesg_text); } } @@ -56,11 +58,9 @@ static inline void __receive_message(int msqid, long mtype, message_t *message) memset(message->mesg_text, 0, sizeof(char) * MESSAGE_LEN); // Receive the message. if (msgrcv(msqid, message, sizeof(message->mesg_text), mtype, 0) < 0) { - perror("Failed to receive the message"); + syslog(LOG_ERR, "[t_msgget] Failed to receive the message: %s", strerror(errno)); } else { - printf( - "[%2d] Message received (%2ld) `%s` (Query: %2ld)\n", getpid(), message->mesg_type, message->mesg_text, - mtype); + syslog(LOG_INFO, "[t_msgget] [%2d] Message received (%2ld) `%s` (Query: %2ld)\n", getpid(), message->mesg_type, message->mesg_text, mtype); } } @@ -78,19 +78,19 @@ int main(int argc, char *argv[]) // Generating a key using ftok key = ftok("/", 5); if (key < 0) { - perror("Failed to generate key using ftok"); + syslog(LOG_ERR, "[t_msgget] Failed to generate key using ftok: %s", strerror(errno)); return EXIT_FAILURE; } - printf("Generated key using ftok (key = %d)\n", key); + syslog(LOG_INFO, "[t_msgget] Generated key using ftok (key = %d)\n", key); // ======================================================================== // Create the first message queue. msqid = msgget(key, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (msqid < 0) { - perror("Failed to create message queue"); + syslog(LOG_ERR, "[t_msgget] Failed to create message queue: %s", strerror(errno)); return EXIT_FAILURE; } - printf("Created message queue (id : %d)\n", msqid); + syslog(LOG_INFO, "[t_msgget] Created message queue (id : %d)\n", msqid); // ======================================================================== // Send the message. @@ -127,9 +127,9 @@ int main(int argc, char *argv[]) // Delete the message queue. ret = msgctl(msqid, IPC_RMID, NULL); if (ret < 0) { - perror("Failed to remove message queue."); + syslog(LOG_ERR, "[t_msgget] Failed to remove message queue.: %s", strerror(errno)); return EXIT_FAILURE; } - printf("Correctly removed message queue.\n"); + syslog(LOG_INFO, "[t_msgget] Correctly removed message queue.\n"); return EXIT_SUCCESS; } diff --git a/userspace/tests/t_ndtree.c b/userspace/tests/t_ndtree.c index 2257fb33..bdce78b0 100644 --- a/userspace/tests/t_ndtree.c +++ b/userspace/tests/t_ndtree.c @@ -3,10 +3,11 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. -#include "ndtree.h" - +#include +#include #include #include +#include #include // Custom allocator for tree nodes @@ -26,7 +27,7 @@ void custom_free_node(ndtree_node_t *node) { free(node); } int compare_node(void *lhs, void *rhs) { return (*(int *)lhs) - (*(int *)rhs); } // Function to print node values (assuming integer values for simplicity) -void print_node(ndtree_node_t *node) { printf("Node value: %d\n", *(int *)(node->value)); } +void print_node(ndtree_node_t *node) { syslog(LOG_INFO, "[t_ndtree] Node value: %d\n", *(int *)(node->value)); } int main(void) { @@ -38,7 +39,7 @@ int main(void) int root_value = 1; ndtree_node_t *root = ndtree_create_root(&tree, &root_value); if (!root) { - fprintf(stderr, "Error: Failed to create root node\n"); + syslog(LOG_ERR, "[t_ndtree] Error: Failed to create root node\n"); return 1; } @@ -50,7 +51,7 @@ int main(void) ndtree_node_t *child2 = ndtree_create_child_of_node(&tree, root, &child2_value); ndtree_node_t *child3 = ndtree_create_child_of_node(&tree, root, &child3_value); if (!child1 || !child2 || !child3) { - fprintf(stderr, "Error: Failed to create one or more child nodes for root\n"); + syslog(LOG_ERR, "[t_ndtree] Error: Failed to create one or more child nodes for root\n"); return 1; } @@ -59,7 +60,7 @@ int main(void) int child1_2_value = 6; if (!ndtree_create_child_of_node(&tree, child1, &child1_1_value) || !ndtree_create_child_of_node(&tree, child1, &child1_2_value)) { - fprintf(stderr, "Error: Failed to create one or more child nodes for child1\n"); + syslog(LOG_ERR, "[t_ndtree] Error: Failed to create one or more child nodes for child1\n"); return 1; } @@ -68,7 +69,7 @@ int main(void) int child2_2_value = 8; if (!ndtree_create_child_of_node(&tree, child2, &child2_1_value) || !ndtree_create_child_of_node(&tree, child2, &child2_2_value)) { - fprintf(stderr, "Error: Failed to create one or more child nodes for child2\n"); + syslog(LOG_ERR, "[t_ndtree] Error: Failed to create one or more child nodes for child2\n"); return 1; } @@ -77,25 +78,25 @@ int main(void) int child3_2_value = 10; if (!ndtree_create_child_of_node(&tree, child3, &child3_1_value) || !ndtree_create_child_of_node(&tree, child3, &child3_2_value)) { - fprintf(stderr, "Error: Failed to create one or more child nodes for child3\n"); + syslog(LOG_ERR, "[t_ndtree] Error: Failed to create one or more child nodes for child3\n"); return 1; } // Verify the number of children at each level if (ndtree_node_count_children(root) != 3) { - fprintf(stderr, "Error: Expected root to have 3 children\n"); + syslog(LOG_ERR, "[t_ndtree] Error: Expected root to have 3 children\n"); return 1; } if (ndtree_node_count_children(child1) != 2) { - fprintf(stderr, "Error: Expected child1 to have 2 children\n"); + syslog(LOG_ERR, "[t_ndtree] Error: Expected child1 to have 2 children\n"); return 1; } if (ndtree_node_count_children(child2) != 2) { - fprintf(stderr, "Error: Expected child2 to have 2 children\n"); + syslog(LOG_ERR, "[t_ndtree] Error: Expected child2 to have 2 children\n"); return 1; } if (ndtree_node_count_children(child3) != 2) { - fprintf(stderr, "Error: Expected child3 to have 2 children\n"); + syslog(LOG_ERR, "[t_ndtree] Error: Expected child3 to have 2 children\n"); return 1; } @@ -105,7 +106,7 @@ int main(void) // Test removing a node with children and verify ndtree_tree_remove_node(&tree, child2, NULL); if (ndtree_node_count_children(root) != 2) { - fprintf(stderr, "Error: Expected root to have 2 children after removing child2\n"); + syslog(LOG_ERR, "[t_ndtree] Error: Expected root to have 2 children after removing child2\n"); return 1; } diff --git a/userspace/tests/t_periodic1.c b/userspace/tests/t_periodic1.c index 79aed3dc..d757e433 100644 --- a/userspace/tests/t_periodic1.c +++ b/userspace/tests/t_periodic1.c @@ -7,10 +7,12 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include +#include #include int main(int argc, char *argv[]) @@ -20,7 +22,7 @@ int main(int argc, char *argv[]) // Get current scheduling parameters. if (sched_getparam(cpid, ¶m) == -1) { - fprintf(STDERR_FILENO, "Failed to get scheduling parameters: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_periodic1] Failed to get scheduling parameters: %s\n", strerror(errno)); return EXIT_FAILURE; } @@ -32,7 +34,7 @@ int main(int argc, char *argv[]) // Set modified scheduling parameters. if (sched_setparam(cpid, ¶m) == -1) { - fprintf(STDERR_FILENO, "Failed to set scheduling parameters: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_periodic1] Failed to set scheduling parameters: %s\n", strerror(errno)); return EXIT_FAILURE; } @@ -42,7 +44,7 @@ int main(int argc, char *argv[]) if (fork() == 0) { char *_argv[] = {"/bin/tests/t_periodic2", NULL}; execv(_argv[0], _argv); - fprintf(STDERR_FILENO, "Failed to execute %s: %s\n", _argv[0], strerror(errno)); + syslog(LOG_ERR, "[t_periodic1] Failed to execute %s: %s\n", _argv[0], strerror(errno)); return EXIT_FAILURE; } @@ -50,7 +52,7 @@ int main(int argc, char *argv[]) if (fork() == 0) { char *_argv[] = {"/bin/tests/t_periodic3", NULL}; execv(_argv[0], _argv); - fprintf(STDERR_FILENO, "Failed to execute %s: %s\n", _argv[0], strerror(errno)); + syslog(LOG_ERR, "[t_periodic1] Failed to execute %s: %s\n", _argv[0], strerror(errno)); return EXIT_FAILURE; } @@ -59,11 +61,11 @@ int main(int argc, char *argv[]) if (++counter == 10) { counter = 0; } - printf("[periodic1] counter %d\n", counter); + syslog(LOG_INFO, "[t_periodic1] [periodic1] counter %d\n", counter); // Wait for the next period. if (waitperiod() == -1) { - fprintf(STDERR_FILENO, "[%s] Error in waitperiod: %s\n", argv[0], strerror(errno)); + syslog(LOG_ERR, "[t_periodic1] [%s] Error in waitperiod: %s\n", argv[0], strerror(errno)); break; } } diff --git a/userspace/tests/t_periodic2.c b/userspace/tests/t_periodic2.c index 42065025..d59e9e20 100644 --- a/userspace/tests/t_periodic2.c +++ b/userspace/tests/t_periodic2.c @@ -7,10 +7,12 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include +#include #include int main(int argc, char *argv[]) @@ -20,7 +22,7 @@ int main(int argc, char *argv[]) // Get current scheduling parameters. if (sched_getparam(cpid, ¶m) == -1) { - fprintf(STDERR_FILENO, "Failed to get scheduling parameters: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_periodic2] Failed to get scheduling parameters: %s\n", strerror(errno)); return EXIT_FAILURE; } @@ -32,7 +34,7 @@ int main(int argc, char *argv[]) // Set modified scheduling parameters. if (sched_setparam(cpid, ¶m) == -1) { - fprintf(STDERR_FILENO, "Failed to set scheduling parameters: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_periodic2] Failed to set scheduling parameters: %s\n", strerror(errno)); return EXIT_FAILURE; } @@ -43,11 +45,11 @@ int main(int argc, char *argv[]) if (++counter == 10) { break; } - printf("[periodic2] counter: %d\n", counter); + syslog(LOG_INFO, "[t_periodic2] [periodic2] counter: %d\n", counter); // Wait for the next period. if (waitperiod() == -1) { - fprintf(STDERR_FILENO, "[%s] Error in waitperiod: %s\n", argv[0], strerror(errno)); + syslog(LOG_ERR, "[t_periodic2] [%s] Error in waitperiod: %s\n", argv[0], strerror(errno)); break; } } diff --git a/userspace/tests/t_periodic3.c b/userspace/tests/t_periodic3.c index 320bb713..7a0e523c 100644 --- a/userspace/tests/t_periodic3.c +++ b/userspace/tests/t_periodic3.c @@ -9,11 +9,13 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include +#include #include int main(int argc, char *argv[]) @@ -24,7 +26,7 @@ int main(int argc, char *argv[]) // Get the current scheduling parameters for this process if (sched_getparam(cpid, ¶m) == -1) { // If fetching parameters fails, print an error message and exit - fprintf(stderr, "[%s] Error in sched_getparam: %s\n", argv[0], strerror(errno)); + syslog(LOG_ERR, "[t_periodic3] [%s] Error in sched_getparam: %s\n", argv[0], strerror(errno)); return EXIT_FAILURE; } @@ -36,7 +38,7 @@ int main(int argc, char *argv[]) // Set the new scheduling parameters for this process if (sched_setparam(cpid, ¶m) == -1) { // If setting parameters fails, print an error message and exit - fprintf(stderr, "[%s] Error in sched_setparam: %s\n", argv[0], strerror(errno)); + syslog(LOG_ERR, "[t_periodic3] [%s] Error in sched_setparam: %s\n", argv[0], strerror(errno)); return EXIT_FAILURE; } @@ -50,12 +52,12 @@ int main(int argc, char *argv[]) } // Print the current counter value - printf("[periodic3] counter: %d\n", counter); + syslog(LOG_INFO, "[t_periodic3] [periodic3] counter: %d\n", counter); // Wait for the next period and check for errors if (waitperiod() == -1) { // If waitperiod fails, print an error message and break out of the loop - fprintf(stderr, "[%s] Error in waitperiod: %s\n", argv[0], strerror(errno)); + syslog(LOG_ERR, "[t_periodic3] [%s] Error in waitperiod: %s\n", argv[0], strerror(errno)); break; } } diff --git a/userspace/tests/t_pipe_blocking.c b/userspace/tests/t_pipe_blocking.c index 5fbd3482..66f1dbfc 100644 --- a/userspace/tests/t_pipe_blocking.c +++ b/userspace/tests/t_pipe_blocking.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -21,7 +22,7 @@ int main(void) // Create a pipe. if (pipe(fds) == -1) { - fprintf(stderr, "Failed to create pipe\n"); + syslog(LOG_ERR, "[t_pipe_blocking] Failed to create pipe\n"); return 1; } @@ -29,7 +30,7 @@ int main(void) pid_t pid = fork(); if (pid == -1) { - fprintf(stderr, "Failed to fork process\n"); + syslog(LOG_ERR, "[t_pipe_blocking] Failed to fork process\n"); close(fds[0]); close(fds[1]); return 1; @@ -38,13 +39,13 @@ int main(void) // Child process: reads from the pipe. close(fds[1]); // Close unused write end - printf("Child waiting to read from pipe...\n"); + syslog(LOG_INFO, "[t_pipe_blocking] Child waiting to read from pipe...\n"); do { bytes_read = read(fds[0], read_msg, sizeof(read_msg)); if (bytes_read > 0) { - printf("Child read message: '%s' (%ld bytes)\n", read_msg, bytes_read); + syslog(LOG_INFO, "[t_pipe_blocking] Child read message: '%s' (%ld bytes)\n", read_msg, bytes_read); } else if ((bytes_read == -1) && (errno != EAGAIN)) { - fprintf(stderr, "Error occurred during read in child process\n"); + syslog(LOG_ERR, "[t_pipe_blocking] Error occurred during read in child process\n"); error_code = 1; break; } @@ -60,13 +61,13 @@ int main(void) timespec_t req = {0, 200000000}; nanosleep(&req, NULL); - printf("Parent writing to pipe...\n"); + syslog(LOG_INFO, "[t_pipe_blocking] Parent writing to pipe...\n"); bytes_written = write(fds[1], write_msg, sizeof(write_msg)); if (bytes_written > 0) { - printf("Parent wrote message: '%s' (%ld bytes)\n", write_msg, bytes_written); + syslog(LOG_INFO, "[t_pipe_blocking] Parent wrote message: '%s' (%ld bytes)\n", write_msg, bytes_written); } else if (bytes_written == -1) { - fprintf(stderr, "Error occurred during write in parent process\n"); + syslog(LOG_ERR, "[t_pipe_blocking] Error occurred during write in parent process\n"); error_code = 1; } diff --git a/userspace/tests/t_pipe_non_blocking.c b/userspace/tests/t_pipe_non_blocking.c index 9d3b68ab..e7e61138 100644 --- a/userspace/tests/t_pipe_non_blocking.c +++ b/userspace/tests/t_pipe_non_blocking.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -22,13 +23,13 @@ int main(void) // Create a pipe. if (pipe(fds) == -1) { - fprintf(stderr, "Failed to create pipe\n"); + syslog(LOG_ERR, "[t_pipe_non_blocking] Failed to create pipe\n"); return 1; } // Set both ends of the pipe to non-blocking mode. if (fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 || fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1) { - fprintf(stderr, "Failed to set pipe to non-blocking mode\n"); + syslog(LOG_ERR, "[t_pipe_non_blocking] Failed to set pipe to non-blocking mode\n"); close(fds[0]); close(fds[1]); return 1; @@ -38,7 +39,7 @@ int main(void) pid_t pid = fork(); if (pid == -1) { - fprintf(stderr, "Failed to fork process\n"); + syslog(LOG_ERR, "[t_pipe_non_blocking] Failed to fork process\n"); close(fds[0]); close(fds[1]); return 1; @@ -50,18 +51,18 @@ int main(void) // Request to sleep for 100 ms. struct timespec req = {0, 100000000}; - printf("Child waiting to read from pipe...\n"); + syslog(LOG_INFO, "[t_pipe_non_blocking] Child waiting to read from pipe...\n"); do { bytes_read = read(fds[0], read_msg, sizeof(read_msg)); if (bytes_read > 0) { - printf("Child read message: '%s' (%ld bytes)\n", read_msg, bytes_read); + syslog(LOG_INFO, "[t_pipe_non_blocking] Child read message: '%s' (%ld bytes)\n", read_msg, bytes_read); } else if (bytes_read == -1) { if (errno != EAGAIN) { - fprintf(stderr, "Error occurred during read in child process\n"); + syslog(LOG_ERR, "[t_pipe_non_blocking] Error occurred during read in child process\n"); error_code = 1; break; } - printf("Child has nothing to read...\n"); + syslog(LOG_INFO, "[t_pipe_non_blocking] Child has nothing to read...\n"); nanosleep(&req, NULL); } } while (bytes_read != 0); @@ -78,13 +79,13 @@ int main(void) // Sleep for 500 ms. nanosleep(&req, NULL); - printf("Parent writing to pipe...\n"); + syslog(LOG_INFO, "[t_pipe_non_blocking] Parent writing to pipe...\n"); bytes_written = write(fds[1], write_msg, sizeof(write_msg)); if (bytes_written > 0) { - printf("Parent wrote message: '%s' (%ld bytes)\n", write_msg, bytes_written); + syslog(LOG_INFO, "[t_pipe_non_blocking] Parent wrote message: '%s' (%ld bytes)\n", write_msg, bytes_written); } else if (bytes_written == -1) { - fprintf(stderr, "Error occurred during write in parent process\n"); + syslog(LOG_ERR, "[t_pipe_non_blocking] Error occurred during write in parent process\n"); error_code = 1; } diff --git a/userspace/tests/t_pwd.c b/userspace/tests/t_pwd.c index 27f9f6a4..2fd57e90 100644 --- a/userspace/tests/t_pwd.c +++ b/userspace/tests/t_pwd.c @@ -7,11 +7,13 @@ /// See LICENSE.md for details. #include +#include #include #include #include #include #include +#include #include /// @brief Test `getpwnam` for valid and invalid user names. @@ -22,7 +24,7 @@ static void __test_getpwnam(void) // Check for a non-existent user if (getpwnam("r") != NULL) { // If "r" is found, which is unexpected, exit with an error - errx(EXIT_FAILURE, "Password entry for non-existent user \"r\" found"); + errx(EXIT_FAILURE, "Password entry for non-existent user r found"); } // Check for the root user, which should always exist @@ -31,7 +33,7 @@ static void __test_getpwnam(void) errx(EXIT_FAILURE, "Password entry for root user not found"); } else { // If the root entry is found, print confirmation for debugging purposes - printf("Password entry for root user found.\n"); + syslog(LOG_INFO, "[t_pwd] Password entry for root user found.\n"); } } @@ -52,7 +54,7 @@ static void __test_getpwuid(void) errx(EXIT_FAILURE, "Password entry for UID 0 (root) not found"); } else { // If the UID 0 entry is found, print confirmation for debugging purposes - printf("Password entry for UID 0 (root) found.\n"); + syslog(LOG_INFO, "[t_pwd] Password entry for UID 0 (root) found.\n"); } } diff --git a/userspace/tests/t_scanf.c b/userspace/tests/t_scanf.c index 29e13c7e..2bc8c429 100644 --- a/userspace/tests/t_scanf.c +++ b/userspace/tests/t_scanf.c @@ -3,25 +3,27 @@ /// @copyright (c) 2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include +#include int main(void) { int number; char name[100]; - printf("Enter a number: "); + syslog(LOG_INFO, "[t_scanf] Enter a number: "); if (scanf("%d", &number) != 1) { - printf("Failed to read number.\n"); + syslog(LOG_INFO, "[t_scanf] Failed to read number.\n"); return 1; } - printf("Enter your name: "); + syslog(LOG_INFO, "[t_scanf] Enter your name: "); if (scanf("%99s", name) != 1) { // %99s to prevent buffer overflow - printf("Failed to read name.\n"); + syslog(LOG_INFO, "[t_scanf] Failed to read name.\n"); return 1; } - printf("Hello, %s! You entered %d.\n", name, number); + syslog(LOG_INFO, "[t_scanf] Hello, %s! You entered %d.\n", name, number); return 0; } diff --git a/userspace/tests/t_schedfb.c b/userspace/tests/t_schedfb.c index 4a6275e6..e7de4b2c 100644 --- a/userspace/tests/t_schedfb.c +++ b/userspace/tests/t_schedfb.c @@ -8,18 +8,19 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include +#include #include int main(int argc, char *argv[]) { pid_t cpid; - printf("First test: The child processes will sleep, so they will not be " - "scheduled immediately.\n"); + syslog(LOG_INFO, "[t_schedfb] First test: The child processes will sleep, so they will not be scheduled immediately.\n"); // Fork 10 child processes to run the t_alarm test. for (int i = 0; i < 10; ++i) { @@ -27,7 +28,7 @@ int main(int argc, char *argv[]) cpid = fork(); if (cpid == -1) { // Fork failed, print the error and exit. - fprintf(stderr, "Failed to fork process: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_schedfb] Failed to fork process: %s\n", strerror(errno)); return EXIT_FAILURE; } @@ -37,7 +38,7 @@ int main(int argc, char *argv[]) // execl replaces the current process image with the one specified ("/bin/tests/t_alarm"). if (execl("/bin/tests/t_alarm", "t_alarm", NULL) == -1) { // If execl fails, print the error and exit the child process. - fprintf(stderr, "Failed to exec t_alarm: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_schedfb] Failed to exec t_alarm: %s\n", strerror(errno)); return EXIT_FAILURE; } // Child process exits after executing the command. @@ -52,11 +53,11 @@ int main(int argc, char *argv[]) // If wait returns -1 and errno is not ECHILD, print an error. if (errno != ECHILD) { - fprintf(stderr, "Error occurred while waiting for child processes: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_schedfb] Error occurred while waiting for child processes: %s\n", strerror(errno)); return EXIT_FAILURE; } - printf("All child processes have completed.\n"); + syslog(LOG_INFO, "[t_schedfb] All child processes have completed.\n"); return EXIT_SUCCESS; } diff --git a/userspace/tests/t_semflg.c b/userspace/tests/t_semflg.c index 5d8ce9cb..a7ba7a07 100644 --- a/userspace/tests/t_semflg.c +++ b/userspace/tests/t_semflg.c @@ -17,6 +17,7 @@ #include #include #include +#include #include int main(int argc, char *argv[]) @@ -30,29 +31,29 @@ int main(int argc, char *argv[]) // Create a semaphore set with one semaphore. semid = semget(IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (semid < 0) { - perror("Failed to create semaphore set"); + syslog(LOG_ERR, "[t_semflg] Failed to create semaphore set: %s", strerror(errno)); return EXIT_FAILURE; } - printf("[parent] Created semaphore set (semid: %ld)\n", semid); + syslog(LOG_INFO, "[t_semflg] [parent] Created semaphore set (semid: %ld)\n", semid); // ======================================================================== // Set the value of the semaphore to 1. arg.val = 1; ret = semctl(semid, 0, SETVAL, &arg); if (ret < 0) { - perror("Failed to set value of semaphore"); + syslog(LOG_ERR, "[t_semflg] Failed to set value of semaphore: %s", strerror(errno)); return EXIT_FAILURE; } - printf("[parent] Set semaphore value to 1 (semid: %ld)\n", semid); + syslog(LOG_INFO, "[t_semflg] [parent] Set semaphore value to 1 (semid: %ld)\n", semid); // ======================================================================== // Get and verify the semaphore value. ret = semctl(semid, 0, GETVAL, NULL); if (ret < 0) { - perror("Failed to get value of semaphore"); + syslog(LOG_ERR, "[t_semflg] Failed to get value of semaphore: %s", strerror(errno)); return EXIT_FAILURE; } - printf("[parent] Semaphore value is %ld (expected: 1)\n", ret); + syslog(LOG_INFO, "[t_semflg] [parent] Semaphore value is %ld (expected: 1)\n", ret); // ======================================================================== // Fork a child process to manipulate the semaphore. @@ -69,19 +70,19 @@ int main(int argc, char *argv[]) // Perform the increment operation on the semaphore. if (semop(semid, &op_child, 1) < 0) { - perror("[child] Failed to perform semaphore operation"); + syslog(LOG_ERR, "[t_semflg] [child] Failed to perform semaphore operation: %s", strerror(errno)); return EXIT_FAILURE; } - printf("[child] Successfully incremented semaphore (semid: %ld)\n", semid); + syslog(LOG_INFO, "[t_semflg] [child] Successfully incremented semaphore (semid: %ld)\n", semid); // Check the updated value of the semaphore. ret = semctl(semid, 0, GETVAL, NULL); if (ret < 0) { - perror("[child] Failed to get value of semaphore"); + syslog(LOG_ERR, "[t_semflg] [child] Failed to get value of semaphore: %s", strerror(errno)); return EXIT_FAILURE; } - printf("[child] Semaphore value is %ld (expected: 2)\n", ret); - printf("[child] Exiting now.\n"); + syslog(LOG_INFO, "[t_semflg] [child] Semaphore value is %ld (expected: 2)\n", ret); + syslog(LOG_INFO, "[t_semflg] [child] Exiting now.\n"); return EXIT_SUCCESS; } @@ -93,28 +94,28 @@ int main(int argc, char *argv[]) // ======================================================================== // Perform the decrement operation on the semaphore. if (semop(semid, op, 1) < 0) { - perror("[parent] Failed to perform semaphore operation"); + syslog(LOG_ERR, "[t_semflg] [parent] Failed to perform semaphore operation: %s", strerror(errno)); return EXIT_FAILURE; } - printf("[parent] Successfully performed semaphore operations (semid: %ld)\n", semid); + syslog(LOG_INFO, "[t_semflg] [parent] Successfully performed semaphore operations (semid: %ld)\n", semid); // ======================================================================== // Get and verify the final value of the semaphore. ret = semctl(semid, 0, GETVAL, NULL); if (ret < 0) { - perror("[parent] Failed to get value of semaphore"); + syslog(LOG_ERR, "[t_semflg] [parent] Failed to get value of semaphore: %s", strerror(errno)); return EXIT_FAILURE; } - printf("[parent] Semaphore value is %ld (expected: 0)\n", ret); + syslog(LOG_INFO, "[t_semflg] [parent] Semaphore value is %ld (expected: 0)\n", ret); // ======================================================================== // Remove the semaphore set. ret = semctl(semid, 0, IPC_RMID, 0); if (ret < 0) { - perror("[parent] Failed to remove semaphore set"); + syslog(LOG_ERR, "[t_semflg] [parent] Failed to remove semaphore set: %s", strerror(errno)); return EXIT_FAILURE; } - printf("[parent] Successfully removed semaphore set (semid: %ld)\n", semid); + syslog(LOG_INFO, "[t_semflg] [parent] Successfully removed semaphore set (semid: %ld)\n", semid); return EXIT_SUCCESS; } diff --git a/userspace/tests/t_semget.c b/userspace/tests/t_semget.c index aa0ff869..b426cb1d 100644 --- a/userspace/tests/t_semget.c +++ b/userspace/tests/t_semget.c @@ -31,38 +31,38 @@ int main(int argc, char *argv[]) // Generate a unique key using ftok. key = ftok("/", 5); if (key < 0) { - syslog(LOG_ERR, "[t_semget] Failed to generate key using ftok: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget] Failed to generate key using ftok: %s\n", strerror(errno)); return 1; } - syslog(LOG_INFO, "[t_semget] Generated key using ftok (key = %d)\n", key); + syslog(LOG_INFO, "[t_semget] [t_semget] Generated key using ftok (key = %d)\n", key); // ======================================================================== // Create a semaphore set with one semaphore. semid = semget(key, 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (semid < 0) { - syslog(LOG_ERR, "[t_semget] Failed to create semaphore set: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget] Failed to create semaphore set: %s\n", strerror(errno)); return 1; } - syslog(LOG_INFO, "[t_semget][father] Created semaphore set (id : %ld)\n", semid); + syslog(LOG_INFO, "[t_semget] [t_semget][father] Created semaphore set (id : %ld)\n", semid); // ======================================================================== // Set the value of the semaphore to 1. arg.val = 1; ret = semctl(semid, 0, SETVAL, &arg); if (ret < 0) { - syslog(LOG_ERR, "[t_semget] Failed to set semaphore value: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget] Failed to set semaphore value: %s\n", strerror(errno)); return 1; } - syslog(LOG_INFO, "[t_semget][father] Set semaphore value to 1 (id : %ld)\n", semid); + syslog(LOG_INFO, "[t_semget] [t_semget][father] Set semaphore value to 1 (id : %ld)\n", semid); // ======================================================================== // Verify that the semaphore value is set correctly. ret = semctl(semid, 0, GETVAL, NULL); if (ret < 0) { - syslog(LOG_ERR, "[t_semget] Failed to get semaphore value: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget] Failed to get semaphore value: %s\n", strerror(errno)); return 1; } - syslog(LOG_INFO, "[t_semget][father] Semaphore value is %ld (expected: 1)\n", ret); + syslog(LOG_INFO, "[t_semget] [t_semget][father] Semaphore value is %ld (expected: 1)\n", ret); // ======================================================================== // Fork a child process. @@ -78,42 +78,42 @@ int main(int argc, char *argv[]) // Increment the semaphore, unblocking the parent. if (semop(semid, &op_child, 1) < 0) { - syslog(LOG_ERR, "[t_semget][child] Failed to perform child semaphore operation: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget][child] Failed to perform child semaphore operation: %s\n", strerror(errno)); return 1; } - syslog(LOG_INFO, "[t_semget][child] Performed first semaphore operation (id: %ld)\n", semid); + syslog(LOG_INFO, "[t_semget] [t_semget][child] Performed first semaphore operation (id: %ld)\n", semid); // Verify the updated semaphore value. ret = semctl(semid, 0, GETVAL, NULL); if (ret < 0) { - syslog(LOG_ERR, "[t_semget][child] Failed to get semaphore value in child: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget][child] Failed to get semaphore value in child: %s\n", strerror(errno)); return 1; } - syslog(LOG_INFO, "[t_semget][child] Semaphore value after first increment is %ld (concurrent parent may consume immediately)\n", ret); + syslog(LOG_INFO, "[t_semget] [t_semget][child] Semaphore value after first increment is %ld (concurrent parent may consume immediately)\n", ret); // Sleep and perform another increment operation. nanosleep(&req, NULL); if (semop(semid, &op_child, 1) < 0) { - syslog(LOG_ERR, "[t_semget][child] Failed to perform second child semaphore operation: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget][child] Failed to perform second child semaphore operation: %s\n", strerror(errno)); return 1; } - syslog(LOG_INFO, "[t_semget][child] Performed second semaphore operation (id: %ld)\n", semid); + syslog(LOG_INFO, "[t_semget] [t_semget][child] Performed second semaphore operation (id: %ld)\n", semid); // Check final semaphore value. ret = semctl(semid, 0, GETVAL, NULL); if (ret < 0) { - syslog(LOG_ERR, "[t_semget][child] Failed to get final semaphore value in child: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget][child] Failed to get final semaphore value in child: %s\n", strerror(errno)); return 1; } - syslog(LOG_INFO, "[t_semget][child] Final semaphore value is %ld\n", ret); + syslog(LOG_INFO, "[t_semget] [t_semget][child] Final semaphore value is %ld\n", ret); // Delete the semaphore set. ret = semctl(semid, 0, IPC_RMID, 0); if (ret < 0) { - syslog(LOG_ERR, "[t_semget][child] Failed to remove semaphore set in child: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget][child] Failed to remove semaphore set in child: %s\n", strerror(errno)); return 1; } - syslog(LOG_INFO, "[t_semget][child] Removed semaphore set (id: %ld)\n", semid); + syslog(LOG_INFO, "[t_semget] [t_semget][child] Removed semaphore set (id: %ld)\n", semid); // Exit the child process. return 0; @@ -136,22 +136,22 @@ int main(int argc, char *argv[]) // ======================================================================== // Perform the blocking semaphore operations. if (semop(semid, op_father, 3) < 0) { - syslog(LOG_ERR, "[t_semget][father] Failed to perform parent semaphore operations: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget][father] Failed to perform parent semaphore operations: %s\n", strerror(errno)); return 1; } - syslog(LOG_INFO, "[t_semget][father] Performed semaphore operations (id: %ld)\n", semid); + syslog(LOG_INFO, "[t_semget] [t_semget][father] Performed semaphore operations (id: %ld)\n", semid); // Verify that the semaphore value is updated correctly. ret = semctl(semid, 0, GETVAL, NULL); if (ret < 0) { - syslog(LOG_ERR, "[t_semget][father] Failed to get semaphore value in parent: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget][father] Failed to get semaphore value in parent: %s\n", strerror(errno)); return 1; } - syslog(LOG_INFO, "[t_semget][father] Semaphore value is %ld (expected: 0)\n", ret); + syslog(LOG_INFO, "[t_semget] [t_semget][father] Semaphore value is %ld (expected: 0)\n", ret); // Wait for the child process to terminate. if (wait(NULL) == -1) { - syslog(LOG_ERR, "[t_semget] Failed to wait for child process: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_semget] [t_semget] Failed to wait for child process: %s\n", strerror(errno)); return EXIT_FAILURE; // Return failure if wait fails. } diff --git a/userspace/tests/t_semop.c b/userspace/tests/t_semop.c index 2bfcd9ed..1e2a9cda 100644 --- a/userspace/tests/t_semop.c +++ b/userspace/tests/t_semop.c @@ -8,10 +8,12 @@ #include #include #include +#include #include #include #include #include +#include #include int main(int argc, char *argv[]) @@ -55,7 +57,7 @@ int main(int argc, char *argv[]) // Create a semaphore set with 4 semaphores. int semid = semget(17, 4, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (semid == -1) { - perror("Failed to create semaphore set"); + syslog(LOG_ERR, "[t_semop] Failed to create semaphore set: %s", strerror(errno)); return 1; } @@ -66,7 +68,7 @@ int main(int argc, char *argv[]) arg.array = values; if (semctl(semid, 0, SETALL, &arg) == -1) { - perror("Failed to set semaphore values"); + syslog(LOG_ERR, "[t_semop] Failed to set semaphore values: %s", strerror(errno)); return 1; } @@ -76,14 +78,14 @@ int main(int argc, char *argv[]) // Process 1: Wait for semaphore 0 to decrement, then print "cheers!" if (!fork()) { semop(semid, &sops[0], 1); - printf("cheers!\n"); + syslog(LOG_INFO, "[t_semop] cheers!\n"); exit(0); } // Process 2: Wait for semaphore 1, print "course, ", then increment semaphore 0 if (!fork()) { semop(semid, &sops[1], 1); - printf("course, "); + syslog(LOG_INFO, "[t_semop] course, "); semop(semid, &sops[3], 1); // Increment semaphore 0 to signal Process 1. exit(0); } @@ -91,14 +93,14 @@ int main(int argc, char *argv[]) // Process 3: Wait for semaphore 2, print "systems ", then increment semaphore 1 if (!fork()) { semop(semid, &sops[2], 1); - printf("systems "); + syslog(LOG_INFO, "[t_semop] systems "); semop(semid, &sops[4], 1); // Increment semaphore 1 to signal Process 2. exit(0); } // Process 4: Print "From the operating ", then increment semaphore 2 to start Process 3 if (!fork()) { - printf("From the operating "); + syslog(LOG_INFO, "[t_semop] From the operating "); semop(semid, &sops[5], 1); // Increment semaphore 2 to signal Process 3. exit(0); } @@ -112,7 +114,7 @@ int main(int argc, char *argv[]) // ======================================================================== // Remove the semaphore set after all processes are done. if (semctl(semid, 0, IPC_RMID, 0) == -1) { - perror("Failed to remove semaphore set"); + syslog(LOG_ERR, "[t_semop] Failed to remove semaphore set: %s", strerror(errno)); return 1; } diff --git a/userspace/tests/t_shm.c b/userspace/tests/t_shm.c index a3da9392..b48b254f 100644 --- a/userspace/tests/t_shm.c +++ b/userspace/tests/t_shm.c @@ -4,6 +4,7 @@ /// @copyright (c) 2014-2024 /// This file is distributed under the MIT License. See LICENSE.md for details. +#include #include #include #include @@ -25,7 +26,7 @@ int shm_write(void) // Generate a System V IPC key using the predefined file path and id. key = ftok(path, id); if (key == -1) { - syslog(LOG_ERR, "Failed to generate IPC key using ftok."); + syslog(LOG_ERR, "[t_shm] Failed to generate IPC key using ftok."); return EXIT_FAILURE; } @@ -33,14 +34,14 @@ int shm_write(void) // and permissions 0666. shmid = shmget(key, 1024, IPC_CREAT | 0666); if (shmid == -1) { - syslog(LOG_ERR, "Failed to create shared memory segment using shmget."); + syslog(LOG_ERR, "[t_shm] Failed to create shared memory segment using shmget."); return EXIT_FAILURE; } // Attach the shared memory segment to the process's address space. str = (char *)shmat(shmid, NULL, 0); if (str == (char *)-1) { - syslog(LOG_ERR, "Failed to attach shared memory segment using shmat."); + syslog(LOG_ERR, "[t_shm] Failed to attach shared memory segment using shmat."); return EXIT_FAILURE; } @@ -50,7 +51,7 @@ int shm_write(void) // Detach the shared memory segment from the process's address space. if (shmdt(str) < 0) { - syslog(LOG_ERR, "Failed to detach shared memory segment using shmdt."); + syslog(LOG_ERR, "[t_shm] Failed to detach shared memory segment using shmdt."); return EXIT_FAILURE; } @@ -70,7 +71,7 @@ int shm_read(void) // Generate the IPC key using ftok with the predefined file and id. key = ftok(path, id); if (key == -1) { - syslog(LOG_ERR, "Failed to generate IPC key using ftok."); + syslog(LOG_ERR, "[t_shm] Failed to generate IPC key using ftok."); return EXIT_FAILURE; } @@ -78,34 +79,34 @@ int shm_read(void) // Size is set to 1024 bytes, and 0666 allows read/write permissions. shmid = shmget(key, 1024, 0666); if (shmid == -1) { - syslog(LOG_ERR, "Failed to create or access shared memory using shmget."); + syslog(LOG_ERR, "[t_shm] Failed to create or access shared memory using shmget."); return EXIT_FAILURE; } // Attach the process to the shared memory segment in read-only mode (SHM_RDONLY). str = (char *)shmat(shmid, NULL, SHM_RDONLY); if (str == (char *)-1) { - syslog(LOG_ERR, "Failed to attach to shared memory segment using shmat."); + syslog(LOG_ERR, "[t_shm] Failed to attach to shared memory segment using shmat."); return EXIT_FAILURE; } // Check if both hashes match. if (strncmp(str, message, strlen(message)) != 0) { - syslog(LOG_ERR, "Data does not match."); - syslog(LOG_ERR, "Expected : `%s`", message); - syslog(LOG_ERR, "Found : `%s`", str); + syslog(LOG_ERR, "[t_shm] Data does not match."); + syslog(LOG_ERR, "[t_shm] Expected : `%s`", message); + syslog(LOG_ERR, "[t_shm] Found : `%s`", str); return EXIT_FAILURE; } // Detach the process from the shared memory segment after use. if (shmdt(str) < 0) { - syslog(LOG_ERR, "Failed to detach shared memory segment using shmdt."); + syslog(LOG_ERR, "[t_shm] Failed to detach shared memory segment using shmdt."); return EXIT_FAILURE; } // Mark the shared memory segment for removal (IPC_RMID). if (shmctl(shmid, IPC_RMID, NULL) == -1) { - syslog(LOG_ERR, "Failed to mark shared memory segment for removal using shmctl."); + syslog(LOG_ERR, "[t_shm] Failed to mark shared memory segment for removal using shmctl."); return EXIT_FAILURE; } @@ -117,12 +118,12 @@ int main(int argc, char *argv[]) // Open a connection to the syslog. openlog("t_shm", LOG_PID | LOG_CONS, LOG_USER); if (shm_write()) { - syslog(LOG_ERR, "Function shm_write failed."); + syslog(LOG_ERR, "[t_shm] Function shm_write failed."); closelog(); return EXIT_FAILURE; } if (shm_read()) { - syslog(LOG_ERR, "Function shm_read failed."); + syslog(LOG_ERR, "[t_shm] Function shm_read failed."); closelog(); return EXIT_FAILURE; } diff --git a/userspace/tests/t_shmget.c b/userspace/tests/t_shmget.c index 1de44a5c..cb612126 100644 --- a/userspace/tests/t_shmget.c +++ b/userspace/tests/t_shmget.c @@ -4,12 +4,15 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include +#include #include #include #include #include +#include #include /// Define the size of shared memory to hold two integers. @@ -24,10 +27,10 @@ int main(void) // Create shared memory segment with IPC_PRIVATE key and specified memory size. shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600); if (shmid == -1) { - perror("shmget"); + syslog(LOG_ERR, "[t_shmget] shmget: %s", strerror(errno)); return EXIT_FAILURE; } - printf("shmid = %d;\n", shmid); + syslog(LOG_INFO, "[t_shmget] shmid = %d;\n", shmid); // Create a child. cpid = fork(); @@ -35,10 +38,10 @@ int main(void) // Child attaches the shared memory. array = (int *)shmat(shmid, NULL, 0); if (array == NULL) { - perror("shmat"); + syslog(LOG_ERR, "[t_shmget] shmat: %s", strerror(errno)); return EXIT_FAILURE; } - printf("C: %p\n", array); + syslog(LOG_INFO, "[t_shmget] C: %p\n", array); array[0] = 1; return 0; } @@ -46,7 +49,7 @@ int main(void) // Father attaches the shared memory. array = (int *)shmat(shmid, NULL, 0); if (array == NULL) { - perror("shmat"); + syslog(LOG_ERR, "[t_shmget] shmat: %s", strerror(errno)); return EXIT_FAILURE; } @@ -54,21 +57,21 @@ int main(void) while (wait(NULL) != -1) { } - printf("F: %p\n", array); + syslog(LOG_INFO, "[t_shmget] F: %p\n", array); array[1] = 2; - printf("array[%d] : %d\n", 0, array[0]); - printf("array[%d] : %d\n", 1, array[1]); + syslog(LOG_INFO, "[t_shmget] array[%d] : %d\n", 0, array[0]); + syslog(LOG_INFO, "[t_shmget] array[%d] : %d\n", 1, array[1]); // Detatch the shared memory. if (shmdt(array) < 0) { - perror("shmdt"); + syslog(LOG_ERR, "[t_shmget] shmdt: %s", strerror(errno)); return EXIT_FAILURE; } // Remove the shared memory. if (shmctl(shmid, IPC_RMID, NULL) == -1) { - perror("shmctl"); + syslog(LOG_ERR, "[t_shmget] shmctl: %s", strerror(errno)); return EXIT_FAILURE; } return EXIT_SUCCESS; diff --git a/userspace/tests/t_sigaction.c b/userspace/tests/t_sigaction.c index 256bc77b..5a0363b3 100644 --- a/userspace/tests/t_sigaction.c +++ b/userspace/tests/t_sigaction.c @@ -6,12 +6,14 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include /// Pointer to store dynamically allocated values. @@ -23,24 +25,24 @@ static int *values = NULL; /// @param sig Signal number (in this case, SIGUSR1). void sigusr1_handler(int sig) { - printf("handler(sig: %d) : Starting handler.\n", sig); - printf("handler(sig: %d) : values pointer (before allocation): %p\n", sig, (void *)values); + syslog(LOG_INFO, "[t_sigaction] handler(sig: %d) : Starting handler.\n", sig); + syslog(LOG_INFO, "[t_sigaction] handler(sig: %d) : values pointer (before allocation): %p\n", sig, (void *)values); // Allocate memory for an array of 4 integers. values = malloc(sizeof(int) * 4); if (!values) { - perror("Failed to allocate memory in signal handler"); + syslog(LOG_ERR, "[t_sigaction] Failed to allocate memory in signal handler: %s", strerror(errno)); return; } // Populate the array with values and print them. for (int i = 0; i < 4; ++i) { values[i] = i; - printf("values[%d] : `%d`\n", i, values[i]); + syslog(LOG_INFO, "[t_sigaction] values[%d] : `%d`\n", i, values[i]); } - printf("handler(sig: %d) : values pointer (after allocation): %p\n", sig, (void *)values); - printf("handler(sig: %d) : Ending handler.\n", sig); + syslog(LOG_INFO, "[t_sigaction] handler(sig: %d) : values pointer (after allocation): %p\n", sig, (void *)values); + syslog(LOG_INFO, "[t_sigaction] handler(sig: %d) : Ending handler.\n", sig); } int main(int argc, char *argv[]) @@ -51,29 +53,29 @@ int main(int argc, char *argv[]) // Set the SIGUSR1 handler using sigaction. if (sigaction(SIGUSR1, &action, NULL) == -1) { - fprintf(stderr, "Failed to set signal handler for SIGUSR1: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_sigaction] Failed to set signal handler for SIGUSR1: %s\n", strerror(errno)); return 1; } // Display initial state before signal is sent. - printf("main : Calling handler (signal %d).\n", SIGUSR1); - printf("main : values pointer (before signal): %p\n", (void *)values); + syslog(LOG_INFO, "[t_sigaction] main : Calling handler (signal %d).\n", SIGUSR1); + syslog(LOG_INFO, "[t_sigaction] main : values pointer (before signal): %p\n", (void *)values); // Send SIGUSR1 to the current process. int ret = kill(getpid(), SIGUSR1); if (ret == -1) { - perror("Failed to send SIGUSR1"); + syslog(LOG_ERR, "[t_sigaction] Failed to send SIGUSR1: %s", strerror(errno)); return 1; } // Display state after signal handler execution. - printf("main : Returning from handler (signal %d): %d.\n", SIGUSR1, ret); - printf("main : values pointer (after signal): %p\n", (void *)values); + syslog(LOG_INFO, "[t_sigaction] main : Returning from handler (signal %d): %d.\n", SIGUSR1, ret); + syslog(LOG_INFO, "[t_sigaction] main : values pointer (after signal): %p\n", (void *)values); // Print the array populated in the signal handler. if (values != NULL) { for (int i = 0; i < 4; ++i) { - printf("values[%d] : `%d`\n", i, values[i]); + syslog(LOG_INFO, "[t_sigaction] values[%d] : `%d`\n", i, values[i]); } } diff --git a/userspace/tests/t_sigfpe.c b/userspace/tests/t_sigfpe.c index 28045f7f..3ee4525a 100644 --- a/userspace/tests/t_sigfpe.c +++ b/userspace/tests/t_sigfpe.c @@ -12,31 +12,33 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include #include /// Signal handler function that catches and handles SIGFPE. void sig_handler(int sig) { - printf("handler(%d) : Starting handler.\n", sig); + syslog(LOG_INFO, "[t_sigfpe] handler(%d) : Starting handler.\n", sig); if (sig == SIGFPE) { - printf("handler(%d) : Correct signal. FPE\n", sig); - printf("handler(%d) : Exiting\n", sig); + syslog(LOG_INFO, "[t_sigfpe] handler(%d) : Correct signal. FPE\n", sig); + syslog(LOG_INFO, "[t_sigfpe] handler(%d) : Exiting\n", sig); exit(0); } else if (sig == SIGILL) { - printf("handler(%d) : Incorrect signal. ILLEGAL INSTRUCTION\n", sig); - printf("handler(%d) : Exiting\n", sig); + syslog(LOG_INFO, "[t_sigfpe] handler(%d) : Incorrect signal. ILLEGAL INSTRUCTION\n", sig); + syslog(LOG_INFO, "[t_sigfpe] handler(%d) : Exiting\n", sig); exit(0); } else { - printf("handler(%d) : Wrong signal.\n", sig); + syslog(LOG_INFO, "[t_sigfpe] handler(%d) : Wrong signal.\n", sig); } - printf("handler(%d) : Ending handler.\n", sig); + syslog(LOG_INFO, "[t_sigfpe] handler(%d) : Ending handler.\n", sig); } int main(int argc, char *argv[]) @@ -47,7 +49,7 @@ int main(int argc, char *argv[]) // Set the SIGFPE handler using sigaction. if (sigaction(SIGFPE, &action, NULL) == -1) { - printf("Failed to set signal handler (%s).\n", strerror(errno)); + syslog(LOG_INFO, "[t_sigfpe] Failed to set signal handler (%s).\n", strerror(errno)); return 1; } @@ -58,11 +60,11 @@ int main(int argc, char *argv[]) // TODO: Fix the kernel to raise SIGFPE instead of SIGILL for division by zero, and remove this handler. // if (sigaction(SIGILL, &action, NULL) == -1) { - printf("Failed to set signal handler (%s).\n", strerror(errno)); + syslog(LOG_INFO, "[t_sigfpe] Failed to set signal handler (%s).\n", strerror(errno)); return 1; } - printf("Diving by zero (unrecoverable)...\n"); + syslog(LOG_INFO, "[t_sigfpe] Diving by zero (unrecoverable)...\n"); // Should trigger ALU error, fighting the compiler... int d = 1; @@ -71,7 +73,7 @@ int main(int argc, char *argv[]) e -= 1; d /= e; e -= 1; - printf("d: %d, e: %d\n", d, e); + syslog(LOG_INFO, "[t_sigfpe] d: %d, e: %d\n", d, e); return EXIT_SUCCESS; } diff --git a/userspace/tests/t_siginfo.c b/userspace/tests/t_siginfo.c index 3f4db972..168015f0 100644 --- a/userspace/tests/t_siginfo.c +++ b/userspace/tests/t_siginfo.c @@ -4,12 +4,14 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include #include @@ -20,22 +22,22 @@ /// information about the signal. void sig_handler_info(int sig, siginfo_t *siginfo) { - printf("handler(%d, %p) : Starting handler.\n", sig, siginfo); + syslog(LOG_INFO, "[t_siginfo] handler(%d, %p) : Starting handler.\n", sig, siginfo); // Check if the received signal is SIGFPE. if (sig == SIGFPE) { - printf("handler(%d, %p) : Correct signal.\n", sig, siginfo); + syslog(LOG_INFO, "[t_siginfo] handler(%d, %p) : Correct signal.\n", sig, siginfo); // Print additional information from the siginfo structure. - printf("handler(%d, %p) : Code : %d\n", sig, siginfo, siginfo->si_code); - printf("handler(%d, %p) : Exiting\n", sig, siginfo); + syslog(LOG_INFO, "[t_siginfo] handler(%d, %p) : Code : %d\n", sig, siginfo, siginfo->si_code); + syslog(LOG_INFO, "[t_siginfo] handler(%d, %p) : Exiting\n", sig, siginfo); // Exit the process after handling the signal. exit(EXIT_SUCCESS); } // Handle unexpected signals. - printf("handler(%d, %p) : Wrong signal.\n", sig, siginfo); + syslog(LOG_INFO, "[t_siginfo] handler(%d, %p) : Wrong signal.\n", sig, siginfo); exit(EXIT_FAILURE); } @@ -54,11 +56,11 @@ int main(int argc, char *argv[]) // Attempt to set the signal handler for SIGFPE. if (sigaction(SIGFPE, &action, NULL) == -1) { // Print error message if sigaction fails. - printf("Failed to set signal handler (%s).\n", strerror(errno)); + syslog(LOG_INFO, "[t_siginfo] Failed to set signal handler (%s).\n", strerror(errno)); return 1; } - printf("Diving by zero (unrecoverable)...\n"); + syslog(LOG_INFO, "[t_siginfo] Diving by zero (unrecoverable)...\n"); // Perform a division that will eventually cause a divide-by-zero error to // trigger SIGFPE. @@ -70,7 +72,7 @@ int main(int argc, char *argv[]) while (1) { // Check to prevent division by zero for safety in other environments. if (e == 0) { - fprintf(stderr, "Attempt to divide by zero.\n"); + syslog(LOG_ERR, "[t_siginfo] Attempt to divide by zero.\n"); break; } d /= e; @@ -78,7 +80,7 @@ int main(int argc, char *argv[]) } // This line will not be reached if SIGFPE is triggered. - printf("d: %d, e: %d\n", d, e); + syslog(LOG_INFO, "[t_siginfo] d: %d, e: %d\n", d, e); return EXIT_SUCCESS; } diff --git a/userspace/tests/t_sigmask.c b/userspace/tests/t_sigmask.c index b9b0b47a..36c4eb97 100644 --- a/userspace/tests/t_sigmask.c +++ b/userspace/tests/t_sigmask.c @@ -4,21 +4,23 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include /// @brief Handler for SIGUSR1 signal. /// @param sig Signal number (should be SIGUSR1). void sigusr1_handler(int sig) { - printf("handler(sig: %d) : Starting handler.\n", sig); + syslog(LOG_INFO, "[t_sigmask] handler(sig: %d) : Starting handler.\n", sig); // Perform any necessary actions in the handler here. - printf("handler(sig: %d) : Ending handler.\n", sig); + syslog(LOG_INFO, "[t_sigmask] handler(sig: %d) : Ending handler.\n", sig); } int main(int argc, char *argv[]) @@ -33,60 +35,60 @@ int main(int argc, char *argv[]) // Set the signal handler for SIGUSR1. if (sigaction(SIGUSR1, &action, NULL) == -1) { // Print error message if sigaction fails. - fprintf(stderr, "Failed to set signal handler (%d, %s).\n", SIGUSR1, strerror(errno)); + syslog(LOG_ERR, "[t_sigmask] Failed to set signal handler (%d, %s).\n", SIGUSR1, strerror(errno)); return EXIT_FAILURE; } - printf("main : Blocking signal (%d).\n", SIGUSR1); + syslog(LOG_INFO, "[t_sigmask] main : Blocking signal (%d).\n", SIGUSR1); // Define a signal set and initialize it to empty. sigset_t mask; if (sigemptyset(&mask) == -1) { // Check for error in sigemptyset. - perror("sigemptyset"); + syslog(LOG_ERR, "[t_sigmask] sigemptyset: %s", strerror(errno)); return EXIT_FAILURE; } // Add SIGUSR1 to the signal mask. if (sigaddset(&mask, SIGUSR1) == -1) { // Check for error in sigaddset. - perror("sigaddset"); + syslog(LOG_ERR, "[t_sigmask] sigaddset: %s", strerror(errno)); return EXIT_FAILURE; } // Block SIGUSR1 signal. if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) { // Check for error in sigprocmask. - perror("sigprocmask (blocking)"); + syslog(LOG_ERR, "[t_sigmask] sigprocmask (blocking): %s", strerror(errno)); return EXIT_FAILURE; } - printf("main : Calling handler (%d).\n", SIGUSR1); + syslog(LOG_INFO, "[t_sigmask] main : Calling handler (%d).\n", SIGUSR1); // Send SIGUSR1 to the current process. ret = kill(getpid(), SIGUSR1); if (ret == -1) { // Check for error in kill. - perror("kill"); + syslog(LOG_ERR, "[t_sigmask] kill: %s", strerror(errno)); return EXIT_FAILURE; } - printf("main : Returning from handler (%d): %d.\n", SIGUSR1, ret); + syslog(LOG_INFO, "[t_sigmask] main : Returning from handler (%d): %d.\n", SIGUSR1, ret); - printf("main : Unblocking signal (%d).\n", SIGUSR1); + syslog(LOG_INFO, "[t_sigmask] main : Unblocking signal (%d).\n", SIGUSR1); // Unblock SIGUSR1 signal. if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1) { // Check for error in sigprocmask (unblocking). - perror("sigprocmask (unblocking)"); + syslog(LOG_ERR, "[t_sigmask] sigprocmask (unblocking): %s", strerror(errno)); return EXIT_FAILURE; } - printf("main : Calling handler (%d).\n", SIGUSR1); + syslog(LOG_INFO, "[t_sigmask] main : Calling handler (%d).\n", SIGUSR1); // Send SIGUSR1 to the current process again after unblocking. ret = kill(getpid(), SIGUSR1); if (ret == -1) { // Check for error in kill. - perror("kill"); + syslog(LOG_ERR, "[t_sigmask] kill: %s", strerror(errno)); return EXIT_FAILURE; } - printf("main : Returning from handler (%d): %d.\n", SIGUSR1, ret); + syslog(LOG_INFO, "[t_sigmask] main : Returning from handler (%d): %d.\n", SIGUSR1, ret); return EXIT_SUCCESS; } diff --git a/userspace/tests/t_sigusr.c b/userspace/tests/t_sigusr.c index 473de124..44c9f072 100644 --- a/userspace/tests/t_sigusr.c +++ b/userspace/tests/t_sigusr.c @@ -5,12 +5,14 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include #include @@ -18,13 +20,13 @@ /// @param sig Signal number (should be SIGUSR1 or SIGUSR2). void sig_handler(int sig) { - printf("handler(%d) : Starting handler.\n", sig); + syslog(LOG_INFO, "[t_sigusr] handler(%d) : Starting handler.\n", sig); // Static variable to count the number of SIGUSR1 signals received. static int counter = 0; if (sig == SIGUSR1 || sig == SIGUSR2) { - printf("handler(%d) : Correct signal. SIGUSER\n", sig); + syslog(LOG_INFO, "[t_sigusr] handler(%d) : Correct signal. SIGUSER\n", sig); // Increment the counter for received signals. counter += 1; @@ -34,11 +36,11 @@ void sig_handler(int sig) } } else { // Handle unexpected signals. - printf("handler(%d) : Wrong signal.\n", sig); + syslog(LOG_INFO, "[t_sigusr] handler(%d) : Wrong signal.\n", sig); exit(EXIT_FAILURE); } - printf("handler(%d) : Ending handler.\n", sig); + syslog(LOG_INFO, "[t_sigusr] handler(%d) : Ending handler.\n", sig); } int main(int argc, char *argv[]) @@ -51,19 +53,19 @@ int main(int argc, char *argv[]) // Set the signal handler for SIGUSR1. if (sigaction(SIGUSR1, &action, NULL) == -1) { - printf("Failed to set signal handler for SIGUSR1 (%s).\n", strerror(errno)); + syslog(LOG_INFO, "[t_sigusr] Failed to set signal handler for SIGUSR1 (%s).\n", strerror(errno)); return EXIT_FAILURE; } // Set the signal handler for SIGUSR2. if (sigaction(SIGUSR2, &action, NULL) == -1) { - printf("Failed to set signal handler for SIGUSR2 (%s).\n", strerror(errno)); + syslog(LOG_INFO, "[t_sigusr] Failed to set signal handler for SIGUSR2 (%s).\n", strerror(errno)); return EXIT_FAILURE; } // Send SIGUSR1 signal to the current process. if (kill(getpid(), SIGUSR1) == -1) { - perror("kill SIGUSR1"); + syslog(LOG_ERR, "[t_sigusr] kill SIGUSR1: %s", strerror(errno)); return EXIT_FAILURE; } @@ -73,7 +75,7 @@ int main(int argc, char *argv[]) // Send SIGUSR2 signal to the current process. if (kill(getpid(), SIGUSR2) == -1) { - perror("kill SIGUSR2"); + syslog(LOG_ERR, "[t_sigusr] kill SIGUSR2: %s", strerror(errno)); return EXIT_FAILURE; } diff --git a/userspace/tests/t_sleep.c b/userspace/tests/t_sleep.c index 6580ccf1..d2cf91a3 100644 --- a/userspace/tests/t_sleep.c +++ b/userspace/tests/t_sleep.c @@ -4,12 +4,14 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include #include #include #include +#include #include #include @@ -19,7 +21,7 @@ int main(int argc, char *argv[]) struct timespec req = {0, 500000000}; // 500 ms = 500,000,000 nanoseconds if (nanosleep(&req, NULL) != 0) { - fprintf(stderr, "nanosleep error: %s\n", strerror(errno)); + syslog(LOG_ERR, "[t_sleep] nanosleep error: %s\n", strerror(errno)); return EXIT_FAILURE; } diff --git a/userspace/tests/t_spwd.c b/userspace/tests/t_spwd.c index ac14ee39..de07a27a 100644 --- a/userspace/tests/t_spwd.c +++ b/userspace/tests/t_spwd.c @@ -4,16 +4,17 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include +#include #include #include #include #include #include +#include #include #include -#include - /// @brief Generates a SHA-256 hash for a predefined input string. /// @return int EXIT_SUCCESS on success, EXIT_FAILURE on error. int test_generate(void) @@ -49,10 +50,10 @@ int test_generate(void) // Check if both hashes match. if (strncmp(output, expected, strlen(expected)) != 0) { - fprintf(stderr, "Hashes do not match:\n"); - fprintf(stderr, "Input : `%s`\n", input); - fprintf(stderr, "Output : `%s`\n", output); - fprintf(stderr, "Expected : `%s`\n", expected); + syslog(LOG_ERR, "[t_spwd] Hashes do not match:\n"); + syslog(LOG_ERR, "[t_spwd] Input : `%s`\n", input); + syslog(LOG_ERR, "[t_spwd] Output : `%s`\n", output); + syslog(LOG_ERR, "[t_spwd] Expected : `%s`\n", expected); return EXIT_FAILURE; } @@ -68,7 +69,7 @@ int test_getspnam(void) // Check if the user exists in the shadow password database. if (!spbuf) { - fprintf(stderr, "Failed to find user '%s' in the shadow password database.\n", username); + syslog(LOG_ERR, "[t_spwd] Failed to find user '%s' in the shadow password database.\n", username); return EXIT_FAILURE; } diff --git a/userspace/tests/t_stopcont.c b/userspace/tests/t_stopcont.c index 2a3e00d6..56c5a284 100644 --- a/userspace/tests/t_stopcont.c +++ b/userspace/tests/t_stopcont.c @@ -1,12 +1,15 @@ /// @file t_stopcont.c -/// @brief +/// @brief Test program for the stop and continue signals (SIGSTOP and SIGCONT). /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include +#include #include +#include #include #include @@ -15,7 +18,7 @@ void handle_signal(int sig) { if (sig == SIGCONT) { - printf("Received SIGCONT, continuing execution...\n"); + syslog(LOG_INFO, "[t_stopcont] Received SIGCONT, continuing execution...\n"); } } @@ -25,24 +28,24 @@ int main(int argc, char *argv[]) if (pid < 0) { // Error handling for fork failure. - perror("fork failed"); + syslog(LOG_ERR, "[t_stopcont] fork failed: %s", strerror(errno)); exit(EXIT_FAILURE); } else if (pid == 0) { // Child process. // Error handling for signal setup failure. if (signal(SIGCONT, handle_signal) == SIG_ERR) { - perror("signal setup failed"); + syslog(LOG_ERR, "[t_stopcont] signal setup failed: %s", strerror(errno)); exit(EXIT_FAILURE); } - printf("Child process (PID: %d) started.\n", getpid()); + syslog(LOG_INFO, "[t_stopcont] Child process (PID: %d) started.\n", getpid()); // Sleep for 100 ms. timespec_t req = {0, 100000000}; while (1) { - printf("Child process running...\n"); + syslog(LOG_INFO, "[t_stopcont] Child process running...\n"); nanosleep(&req, NULL); } @@ -54,30 +57,30 @@ int main(int argc, char *argv[]) // Let the child process run for a bit. nanosleep(&req, NULL); if (kill(pid, SIGSTOP) == -1) { - perror("failed to send SIGSTOP"); + syslog(LOG_ERR, "[t_stopcont] failed to send SIGSTOP: %s", strerror(errno)); exit(EXIT_FAILURE); } - printf("Parent sending SIGSTOP to child (PID: %d).\n", pid); + syslog(LOG_INFO, "[t_stopcont] Parent sending SIGSTOP to child (PID: %d).\n", pid); // Wait for a bit before continuing the child process. nanosleep(&req, NULL); if (kill(pid, SIGCONT) == -1) { - perror("failed to send SIGCONT"); + syslog(LOG_ERR, "[t_stopcont] failed to send SIGCONT: %s", strerror(errno)); exit(EXIT_FAILURE); } - printf("Parent sending SIGCONT to child (PID: %d).\n", pid); + syslog(LOG_INFO, "[t_stopcont] Parent sending SIGCONT to child (PID: %d).\n", pid); // Wait for a bit before terminating the child process. nanosleep(&req, NULL); if (kill(pid, SIGTERM) == -1) { - perror("failed to send SIGTERM"); + syslog(LOG_ERR, "[t_stopcont] failed to send SIGTERM: %s", strerror(errno)); exit(EXIT_FAILURE); } - printf("Parent sending SIGTERM to child (PID: %d).\n", pid); + syslog(LOG_INFO, "[t_stopcont] Parent sending SIGTERM to child (PID: %d).\n", pid); // Wait for the child process to finish. if (wait(NULL) == -1) { - perror("wait failed"); + syslog(LOG_ERR, "[t_stopcont] wait failed: %s", strerror(errno)); exit(EXIT_FAILURE); } } diff --git a/userspace/tests/t_syslog.c b/userspace/tests/t_syslog.c index 8d9e7e82..c6391e5b 100644 --- a/userspace/tests/t_syslog.c +++ b/userspace/tests/t_syslog.c @@ -1,3 +1,9 @@ +/// @file t_syslog.c +/// @brief Test program for syslog functionality. +/// @copyright (c) 2014-2024 This file is distributed under the MIT License. +/// See LICENSE.md for details. + +#include #include #include @@ -10,14 +16,14 @@ int main(void) setlogmask(LOG_UPTO(LOG_WARNING)); // Log messages at different levels to test filtering - syslog(LOG_DEBUG, "This is a debug message and should not appear.\n"); - syslog(LOG_INFO, "This is an info message and should not appear.\n"); - syslog(LOG_NOTICE, "This is a notice message and should not appear.\n"); - syslog(LOG_WARNING, "This is a warning message and should appear.\n"); - syslog(LOG_ERR, "This is an error message and should appear.\n"); - syslog(LOG_CRIT, "This is a critical message and should appear.\n"); - syslog(LOG_ALERT, "This is an alert message and should appear.\n"); - syslog(LOG_EMERG, "This is an emergency message and should appear.\n"); + syslog(LOG_DEBUG, "[t_syslog] This is a debug message and should not appear.\n"); + syslog(LOG_INFO, "[t_syslog] This is an info message and should not appear.\n"); + syslog(LOG_NOTICE, "[t_syslog] This is a notice message and should not appear.\n"); + syslog(LOG_WARNING, "[t_syslog] This is a warning message and should appear.\n"); + syslog(LOG_ERR, "[t_syslog] This is an error message and should appear.\n"); + syslog(LOG_CRIT, "[t_syslog] This is a critical message and should appear.\n"); + syslog(LOG_ALERT, "[t_syslog] This is an alert message and should appear.\n"); + syslog(LOG_EMERG, "[t_syslog] This is an emergency message and should appear.\n"); // Close the syslog connection closelog(); diff --git a/userspace/tests/t_time.c b/userspace/tests/t_time.c index 1d43941d..0cb6bd18 100644 --- a/userspace/tests/t_time.c +++ b/userspace/tests/t_time.c @@ -3,8 +3,10 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include +#include #include int main(void) @@ -14,18 +16,18 @@ int main(void) // Check if the time function failed if (current_time == (time_t)-1) { - perror("Error: time() failed"); + syslog(LOG_ERR, "[t_time] Error: time() failed: %s", strerror(errno)); return EXIT_FAILURE; } // Convert to local time and print the result char *time_str = ctime(¤t_time); if (time_str == NULL) { - perror("Error: ctime() failed"); + syslog(LOG_ERR, "[t_time] Error: ctime() failed: %s", strerror(errno)); return EXIT_FAILURE; } - printf("Current time is: `%s`\n", time_str); + syslog(LOG_INFO, "[t_time] Current time is: `%s`\n", time_str); return EXIT_SUCCESS; } diff --git a/userspace/tests/t_write_read.c b/userspace/tests/t_write_read.c index 0d36a0c5..79322560 100644 --- a/userspace/tests/t_write_read.c +++ b/userspace/tests/t_write_read.c @@ -3,6 +3,7 @@ /// @copyright (c) 2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include #include #include #include @@ -10,6 +11,7 @@ #include #include #include +#include #include /// @brief Creates a file with the specified name and mode. @@ -20,7 +22,7 @@ int create_file(const char *filename, mode_t mode) { int fd = creat(filename, mode); if (fd < 0) { - printf("Failed to create file %s: %s\n", filename, strerror(errno)); + syslog(LOG_INFO, "[t_write_read] Failed to create file %s: %s\n", filename, strerror(errno)); return EXIT_FAILURE; } close(fd); @@ -41,20 +43,20 @@ int check_content(const char *filename, const char *content, int length) // Open the file for reading. int fd = open(filename, O_RDONLY, 0); if (fd < 0) { - printf("Failed to open file %s: %s\n", filename, strerror(errno)); + syslog(LOG_INFO, "[t_write_read] Failed to open file %s: %s\n", filename, strerror(errno)); return EXIT_FAILURE; } // Read the content from the file. if (read(fd, &buffer, max(min(length, 256), 0)) < 0) { // Ensure not to exceed buffer size. - printf("Reading from file %s failed: %s\n", filename, strerror(errno)); + syslog(LOG_INFO, "[t_write_read] Reading from file %s failed: %s\n", filename, strerror(errno)); close(fd); return EXIT_FAILURE; } // Compare the read content with the expected content. if (strcmp(buffer, content) != 0) { - printf("Unexpected file content `%s`, expecting `%s`.\n", buffer, content); + syslog(LOG_INFO, "[t_write_read] Unexpected file content `%s`, expecting `%s`.\n", buffer, content); close(fd); return EXIT_FAILURE; } @@ -84,13 +86,13 @@ int write_content(const char *filename, const char *content, int length, int tru // Open the file with the specified flags. int fd = open(filename, flags, 0); if (fd < 0) { - printf("Failed to open file %s: %s\n", filename, strerror(errno)); + syslog(LOG_INFO, "[t_write_read] Failed to open file %s: %s\n", filename, strerror(errno)); return EXIT_FAILURE; } // Write the content to the file. if (write(fd, content, length) < 0) { - printf("Writing on file %s failed: %s\n", filename, strerror(errno)); + syslog(LOG_INFO, "[t_write_read] Writing on file %s failed: %s\n", filename, strerror(errno)); close(fd); return EXIT_FAILURE; } @@ -115,18 +117,18 @@ int test_write_read(const char *filename) // Open the file for writing. int fd = open(filename, O_WRONLY, 0); if (fd < 0) { - printf("Failed to open file %s: %s\n", filename, strerror(errno)); + syslog(LOG_INFO, "[t_write_read] Failed to open file %s: %s\n", filename, strerror(errno)); return EXIT_FAILURE; } // Write content to the file. if (write(fd, "foo", 3) != 3) { - printf("First write to %s failed: %s\n", filename, strerror(errno)); + syslog(LOG_INFO, "[t_write_read] First write to %s failed: %s\n", filename, strerror(errno)); close(fd); return EXIT_FAILURE; } if (write(fd, "bar", 3) != 3) { - printf("Second write to %s failed: %s\n", filename, strerror(errno)); + syslog(LOG_INFO, "[t_write_read] Second write to %s failed: %s\n", filename, strerror(errno)); close(fd); return EXIT_FAILURE; } @@ -219,7 +221,7 @@ int main(int argc, char *argv[]) char *filename = "/home/user/t_write_read.txt"; // Test write and read operations. - printf("Running `test_write_read`...\n"); + syslog(LOG_INFO, "[t_write_read] Running `test_write_read`...\n"); if (test_write_read(filename)) { // Clean up if there was an error. unlink(filename); @@ -229,7 +231,7 @@ int main(int argc, char *argv[]) unlink(filename); // Test truncating and overwriting content. - printf("Running `test_truncate`...\n"); + syslog(LOG_INFO, "[t_write_read] Running `test_truncate`...\n"); if (test_truncate(filename)) { // Clean up if there was an error. unlink(filename); @@ -239,7 +241,7 @@ int main(int argc, char *argv[]) unlink(filename); // Test appending content. - printf("Running `test_append`...\n"); + syslog(LOG_INFO, "[t_write_read] Running `test_append`...\n"); if (test_append(filename)) { // Clean up if there was an error. unlink(filename);