Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions userspace/tests/t_abort.c
Original file line number Diff line number Diff line change
@@ -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 <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strerror.h>
#include <string.h>
#include <sys/wait.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>

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();
Expand All @@ -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[])
Expand All @@ -41,15 +45,15 @@ 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);
}

// Trigger the SIGABRT signal.
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;
}
14 changes: 8 additions & 6 deletions userspace/tests/t_alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
/// @copyright (c) 2014-2024 This file is distributed under the MIT License.
/// See LICENSE.md for details.

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strerror.h>
#include <string.h>
#include <sys/wait.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>

/// @brief Signal handler for SIGALRM.
/// @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);
Expand All @@ -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);
Expand All @@ -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[])
Expand All @@ -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);
}

Expand Down
18 changes: 10 additions & 8 deletions userspace/tests/t_big_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
/// @copyright (c) 2014-2024 This file is distributed under the MIT License.
/// See LICENSE.md for details.

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <strerror.h>
#include <string.h>
#include <sys/stat.h>
#include <syslog.h>
#include <unistd.h>

#define FILENAME "/home/user/test.txt"
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -45,15 +47,15 @@ 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;
}

// 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;
}
Expand All @@ -63,15 +65,15 @@ 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;
}

// 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;
Expand All @@ -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;
}

Expand Down
14 changes: 7 additions & 7 deletions userspace/tests/t_chdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
/// @copyright (c) 2014-2024 This file is distributed under the MIT License.
/// See LICENSE.md for details.

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <strerror.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

int main(int argc, char *argv[])
Expand All @@ -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;
}
24 changes: 13 additions & 11 deletions userspace/tests/t_creat.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
/// @copyright (c) 2014-2024 This file is distributed under the MIT License.
/// See LICENSE.md for details.

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <strerror.h>
#include <string.h>
#include <sys/stat.h>
#include <syslog.h>
#include <unistd.h>

int main(int argc, char *argv[])
Expand All @@ -23,32 +25,32 @@ 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);
}

// 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);
}
Expand All @@ -58,28 +60,28 @@ 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);
}

// 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);
}

Expand Down
Loading