-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add signal and exit code support (#9)
Resolves #6 Decided on syntax `EXPECT_EXIT(status, { /* code */ }` because it's the most versatile. Also a EXPECT_SIGNAL variant with the same syntax.
- Loading branch information
1 parent
859b8f8
commit 5593bd8
Showing
9 changed files
with
166 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "fork.h" | ||
|
||
caught_internal_process_status create_caught_internal_process_status(int type, int status) | ||
{ | ||
caught_internal_process_status new = { | ||
.type = type, | ||
.status = status, | ||
.status_str = NULL, | ||
}; | ||
if (type == 1 && status >= 1 && status <= 31) | ||
{ | ||
new.status_str = signal_names[status - 1]; | ||
} | ||
else if (type == 0 && status >= 0 && status <= 1) | ||
{ | ||
new.status_str = exit_status_names[status]; | ||
} | ||
return new; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifndef CAUGHT_FORK | ||
#define CAUGHT_FORK | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
|
||
typedef struct caught_internal_process_status | ||
{ | ||
int type; // 0 for exit status, 1 for signal status | ||
int status; // The exit status or signal, depending on above | ||
const char *status_str; // The string signal, if there is one | ||
} caught_internal_process_status; | ||
|
||
static const char *exit_status_names[] = {"EXIT_SUCCESS", "EXIT_FAILURE"}; | ||
|
||
static const char *signal_names[] = { | ||
"SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", | ||
"SIGFPE", "SIGKILL", "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGALRM", | ||
"SIGTERM", "SIGSTKFLT", "SIGCHLD", "SIGCONT", "SIGSTOP", "SIGTSTP", "SIGTTIN", | ||
"SIGTTOU", "SIGURG", "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGWINCH", | ||
"SIGIO", "SIGPWR", "SIGSYS"}; | ||
|
||
caught_internal_process_status create_caught_internal_process_status(int type, int status); | ||
|
||
#define CAUGHT_INTERNAL_FORK(child_execute_block) \ | ||
caught_internal_process_status caught_internal_fork_child_status = {}; \ | ||
pid_t caught_internal_pid = fork(); \ | ||
if (caught_internal_pid == -1) \ | ||
{ \ | ||
perror("Caught: failed to fork\n"); \ | ||
exit(EXIT_FAILURE); \ | ||
} \ | ||
if (caught_internal_pid == 0) \ | ||
{ \ | ||
child_execute_block \ | ||
\ | ||
perror("Caught: fork segment must call exit to prevent fork bombs\n"); \ | ||
exit(EXIT_FAILURE); \ | ||
} \ | ||
else \ | ||
{ \ | ||
int caught_internal_status = 0; \ | ||
waitpid(caught_internal_pid, &caught_internal_status, 0); \ | ||
if (WIFEXITED(caught_internal_status)) \ | ||
{ \ | ||
caught_internal_fork_child_status = \ | ||
create_caught_internal_process_status(0, WEXITSTATUS(caught_internal_status)); \ | ||
} \ | ||
else if (WIFSIGNALED(caught_internal_status)) \ | ||
{ \ | ||
caught_internal_fork_child_status = \ | ||
create_caught_internal_process_status(1, WTERMSIG(caught_internal_status)); \ | ||
} \ | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// NOTE: The location of this include might differ in your code depending on location | ||
// For example, it could be: #include "caught.h" | ||
#include "../src/caught.h" | ||
|
||
TEST("exit - success") | ||
{ | ||
EXPECT_EXIT(EXIT_SUCCESS, { | ||
exit(EXIT_SUCCESS); | ||
}); | ||
|
||
EXPECT_INT(1 + 1, ==, 2); // This still runs | ||
} | ||
|
||
TEST("exit - failure") | ||
{ | ||
EXPECT_EXIT(EXIT_FAILURE, { | ||
exit(EXIT_FAILURE); | ||
}); | ||
|
||
EXPECT_INT(1 + 1, ==, 2); // This still runs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// NOTE: The location of this include might differ in your code depending on location | ||
// For example, it could be: #include "caught.h" | ||
#include "../src/caught.h" | ||
|
||
TEST("signal - SIGABRT") | ||
{ | ||
EXPECT_SIGNAL(SIGABRT, { | ||
raise(SIGABRT); | ||
}); | ||
|
||
EXPECT_INT(1 + 1, ==, 2); // This still runs | ||
} | ||
|
||
TEST("signal - SIGSEGV") | ||
{ | ||
EXPECT_SIGNAL(SIGSEGV, { | ||
int *ptr = NULL; | ||
ptr[1] = 123; // BAD! | ||
}); | ||
|
||
EXPECT_INT(1 + 1, ==, 2); // This still runs | ||
} |