2020-11-29 19:37:24 +03:00
|
|
|
#include "sc_signal.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
2020-12-28 06:18:18 +03:00
|
|
|
#include <string.h>
|
2020-11-29 19:37:24 +03:00
|
|
|
|
2022-08-20 18:56:21 +03:00
|
|
|
void test1(void)
|
2020-11-29 19:37:24 +03:00
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
char tmp[128] = "";
|
2020-12-28 06:18:18 +03:00
|
|
|
|
2021-04-07 00:28:50 +03:00
|
|
|
sc_signal_snprintf(tmp, 0, "%s", "test");
|
|
|
|
assert(strcmp(tmp, "") == 0);
|
2020-12-28 06:18:18 +03:00
|
|
|
|
2021-04-07 00:28:50 +03:00
|
|
|
sc_signal_snprintf(tmp, sizeof(tmp), "%s", "test");
|
|
|
|
assert(strcmp(tmp, "test") == 0);
|
|
|
|
sc_signal_snprintf(tmp, sizeof(tmp), "%s", NULL);
|
|
|
|
assert(strcmp(tmp, "(null)") == 0);
|
|
|
|
sc_signal_snprintf(tmp, sizeof(tmp), "%d", -3);
|
|
|
|
assert(strcmp(tmp, "-3") == 0);
|
|
|
|
sc_signal_snprintf(tmp, sizeof(tmp), "%u", 3);
|
|
|
|
assert(strcmp(tmp, "3") == 0);
|
|
|
|
sc_signal_snprintf(tmp, sizeof(tmp), "%ld", -1000000000l);
|
|
|
|
assert(strcmp(tmp, "-1000000000") == 0);
|
|
|
|
sc_signal_snprintf(tmp, sizeof(tmp), "%lld", -100000000000ll);
|
|
|
|
assert(strcmp(tmp, "-100000000000") == 0);
|
|
|
|
sc_signal_snprintf(tmp, sizeof(tmp), "%lu", 1000000000l);
|
|
|
|
assert(strcmp(tmp, "1000000000") == 0);
|
|
|
|
sc_signal_snprintf(tmp, sizeof(tmp), "%llu", 100000000000ll);
|
|
|
|
assert(strcmp(tmp, "100000000000") == 0);
|
2020-12-28 06:18:18 +03:00
|
|
|
|
2021-04-07 00:28:50 +03:00
|
|
|
char *x = (char *) 0xabcdef;
|
|
|
|
sc_signal_snprintf(tmp, sizeof(tmp), "%p", x);
|
|
|
|
assert(strcmp(tmp, "0xabcdef") == 0);
|
2020-12-28 06:18:18 +03:00
|
|
|
|
2021-04-07 00:28:50 +03:00
|
|
|
sc_signal_snprintf(tmp, sizeof(tmp), "%%p", x);
|
|
|
|
assert(strcmp(tmp, "%p") == 0);
|
2020-12-28 06:18:18 +03:00
|
|
|
|
2021-04-07 00:28:50 +03:00
|
|
|
assert(sc_signal_snprintf(tmp, sizeof(tmp), "%c", 3) == -1);
|
|
|
|
assert(sc_signal_snprintf(tmp, sizeof(tmp), "%llx", 3) == -1);
|
|
|
|
assert(sc_signal_snprintf(tmp, sizeof(tmp), "%lx", 3) == -1);
|
2020-12-28 06:18:18 +03:00
|
|
|
|
2021-04-07 00:28:50 +03:00
|
|
|
sc_signal_log(1, tmp, sizeof(tmp), "%s", "test");
|
2020-11-29 19:37:24 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:56:21 +03:00
|
|
|
void test2(void)
|
2020-11-29 19:37:24 +03:00
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
assert(sc_signal_init() == 0);
|
2020-11-29 19:37:24 +03:00
|
|
|
}
|
|
|
|
|
2021-01-31 05:21:59 +03:00
|
|
|
#ifdef SC_HAVE_WRAP
|
2021-04-07 00:28:50 +03:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
2021-01-31 05:21:59 +03:00
|
|
|
|
2021-02-19 05:20:07 +03:00
|
|
|
bool fail_signal;
|
|
|
|
extern void (*__real_signal(int sig, void (*func)(int)))(int);
|
|
|
|
void (*__wrap_signal(int sig, void (*func)(int)))(int)
|
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
if (fail_signal) {
|
|
|
|
return SIG_ERR;
|
|
|
|
}
|
2021-02-19 05:20:07 +03:00
|
|
|
|
2021-04-07 00:28:50 +03:00
|
|
|
return __real_signal(sig, func);
|
2021-02-19 05:20:07 +03:00
|
|
|
}
|
|
|
|
|
2021-01-31 05:21:59 +03:00
|
|
|
void sig_handler(int signum)
|
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
(void) signum;
|
2021-01-31 05:21:59 +03:00
|
|
|
}
|
|
|
|
|
2021-02-01 04:36:00 +03:00
|
|
|
void test3x(int signal)
|
2021-01-31 05:21:59 +03:00
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
assert(true);
|
|
|
|
} else if (pid) {
|
|
|
|
int status = 0;
|
|
|
|
wait(&status);
|
|
|
|
if (WEXITSTATUS(status) == -2) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
assert(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("Running child \n");
|
|
|
|
raise(signal);
|
|
|
|
printf("Child done \n");
|
|
|
|
exit(-2);
|
|
|
|
}
|
2021-02-01 04:36:00 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:56:21 +03:00
|
|
|
void test3(void)
|
2021-02-01 04:36:00 +03:00
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
test3x(SIGSEGV);
|
|
|
|
test3x(SIGABRT);
|
|
|
|
test3x(SIGBUS);
|
|
|
|
test3x(SIGFPE);
|
|
|
|
sc_signal_log_fd = STDOUT_FILENO;
|
|
|
|
test3x(SIGILL);
|
|
|
|
sc_signal_log_fd = -1;
|
|
|
|
test3x(SIGUSR1);
|
|
|
|
test3x(SIGSYS);
|
2021-02-01 04:36:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void test4x(int signal)
|
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
assert(true);
|
|
|
|
} else if (pid) {
|
|
|
|
int status = 0;
|
|
|
|
wait(&status);
|
|
|
|
if (WEXITSTATUS(status) == -2) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
assert(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(sc_signal_init() == 0);
|
|
|
|
sc_signal_log_fd = STDOUT_FILENO;
|
|
|
|
sc_signal_shutdown_fd = STDOUT_FILENO;
|
|
|
|
printf("Running child \n");
|
|
|
|
raise(signal);
|
|
|
|
sleep(1);
|
|
|
|
printf("Child done \n");
|
|
|
|
exit(-2);
|
|
|
|
}
|
2021-01-31 05:21:59 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:56:21 +03:00
|
|
|
void test4(void)
|
2021-01-31 05:21:59 +03:00
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
test4x(SIGINT);
|
|
|
|
test4x(SIGTERM);
|
|
|
|
test4x(SIGUSR2);
|
2021-02-01 04:36:00 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:56:21 +03:00
|
|
|
void test5(void)
|
2021-02-01 04:36:00 +03:00
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
assert(true);
|
|
|
|
} else if (pid) {
|
|
|
|
int status = 0;
|
|
|
|
wait(&status);
|
|
|
|
if (WEXITSTATUS(status) == -2) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
assert(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(sc_signal_init() == 0);
|
|
|
|
sc_signal_shutdown_fd = STDOUT_FILENO;
|
|
|
|
printf("Running child \n");
|
|
|
|
raise(SIGINT);
|
|
|
|
raise(SIGINT);
|
|
|
|
sleep(1);
|
|
|
|
printf("Child done \n");
|
|
|
|
exit(-2);
|
|
|
|
}
|
2021-02-01 04:36:00 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:56:21 +03:00
|
|
|
void test6(void)
|
2021-02-01 04:36:00 +03:00
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
assert(true);
|
|
|
|
} else if (pid) {
|
|
|
|
int status = 0;
|
|
|
|
wait(&status);
|
|
|
|
if (WEXITSTATUS(status) == -2) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
assert(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(sc_signal_init() == 0);
|
|
|
|
sc_signal_shutdown_fd = 1222;
|
|
|
|
printf("Running child \n");
|
|
|
|
raise(SIGINT);
|
|
|
|
sleep(1);
|
|
|
|
printf("Child done \n");
|
|
|
|
exit(-2);
|
|
|
|
}
|
2021-02-01 04:36:00 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:56:21 +03:00
|
|
|
void test7(void)
|
2021-02-01 04:36:00 +03:00
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
assert(true);
|
|
|
|
} else if (pid) {
|
|
|
|
int status = 0;
|
|
|
|
wait(&status);
|
|
|
|
if (WEXITSTATUS(status) == -2) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
assert(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(sc_signal_init() == 0);
|
|
|
|
printf("Running child \n");
|
|
|
|
raise(SIGINT);
|
|
|
|
sleep(1);
|
|
|
|
printf("Child done \n");
|
|
|
|
exit(-2);
|
|
|
|
}
|
2021-02-01 04:36:00 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:56:21 +03:00
|
|
|
void test8(void)
|
2021-02-01 04:36:00 +03:00
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
assert(true);
|
|
|
|
} else if (pid) {
|
|
|
|
int status = 0;
|
|
|
|
wait(&status);
|
|
|
|
if (WEXITSTATUS(status) == -2) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
assert(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(sc_signal_init() == 0);
|
|
|
|
sc_signal_shutdown_fd = -1;
|
|
|
|
printf("Running child \n");
|
|
|
|
raise(SIGINT);
|
|
|
|
sleep(1);
|
|
|
|
printf("Child done \n");
|
|
|
|
exit(-2);
|
|
|
|
}
|
2021-01-31 05:21:59 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:56:21 +03:00
|
|
|
void test9(void)
|
2021-02-19 05:20:07 +03:00
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
fail_signal = true;
|
|
|
|
assert(sc_signal_init() != 0);
|
|
|
|
fail_signal = false;
|
2021-02-19 05:20:07 +03:00
|
|
|
}
|
|
|
|
|
2021-01-31 05:21:59 +03:00
|
|
|
#else
|
2022-08-20 18:56:21 +03:00
|
|
|
void test3(void)
|
2021-01-31 05:21:59 +03:00
|
|
|
{
|
|
|
|
}
|
2022-08-20 18:56:21 +03:00
|
|
|
void test4(void)
|
2021-01-31 05:21:59 +03:00
|
|
|
{
|
|
|
|
}
|
2022-08-20 18:56:21 +03:00
|
|
|
void test5(void)
|
2021-02-01 04:36:00 +03:00
|
|
|
{
|
|
|
|
}
|
2022-08-20 18:56:21 +03:00
|
|
|
void test6(void)
|
2021-02-01 04:36:00 +03:00
|
|
|
{
|
|
|
|
}
|
2022-08-20 18:56:21 +03:00
|
|
|
void test7(void)
|
2021-02-01 04:36:00 +03:00
|
|
|
{
|
|
|
|
}
|
2022-08-20 18:56:21 +03:00
|
|
|
void test8(void)
|
2021-02-01 04:36:00 +03:00
|
|
|
{
|
2021-02-19 05:20:07 +03:00
|
|
|
}
|
2022-08-20 18:56:21 +03:00
|
|
|
void test9(void)
|
2021-02-19 05:20:07 +03:00
|
|
|
{
|
2021-02-01 04:36:00 +03:00
|
|
|
}
|
2021-01-31 05:21:59 +03:00
|
|
|
#endif
|
|
|
|
|
2022-08-20 18:56:21 +03:00
|
|
|
int main(void)
|
2020-11-29 19:37:24 +03:00
|
|
|
{
|
2021-04-07 00:28:50 +03:00
|
|
|
test1();
|
|
|
|
test2();
|
|
|
|
test3();
|
|
|
|
test4();
|
|
|
|
test5();
|
|
|
|
test6();
|
|
|
|
test7();
|
|
|
|
test8();
|
|
|
|
test9();
|
2020-11-29 19:37:24 +03:00
|
|
|
|
2021-04-07 00:28:50 +03:00
|
|
|
return 0;
|
2020-11-29 19:37:24 +03:00
|
|
|
}
|