Skip to content

Commit

Permalink
Notify libcare-stresstest about child events
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Rashchupkin committed Feb 27, 2018
1 parent 363eacb commit a5dca61
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ kpatch_make: kpatch_make.o
LIBUNWIND_LIBS := $(shell pkg-config --libs libunwind libunwind-ptrace)


libcare-ctl: kpatch_user.o kpatch_storage.o kpatch_patch.c kpatch_elf.o kpatch_ptrace.o kpatch_coro.o
libcare-ctl: kpatch_user.o kpatch_storage.o kpatch_patch.o kpatch_elf.o kpatch_ptrace.o kpatch_coro.o
libcare-ctl: kpatch_process.o kpatch_common.o rbtree.o kpatch_log.o
libcare-ctl: LDLIBS += -lelf -lrt $(LIBUNWIND_LIBS)

libcare-stresstest: kpatch_user-stresstest.o kpatch_storage.o kpatch_patch.c kpatch_elf.o kpatch_ptrace.o kpatch_coro.o
libcare-stresstest: kpatch_process.o kpatch_common.o rbtree.o kpatch_log.o
libcare-stresstest: kpatch_user-stresstest.o kpatch_log-stresstest.o kpatch_storage.o kpatch_patch.o kpatch_elf.o kpatch_ptrace.o kpatch_coro.o
libcare-stresstest: kpatch_process.o kpatch_common.o rbtree.o
libcare-stresstest: LDLIBS += -lelf -lrt $(LIBUNWIND_LIBS)

libcare-client: libcare-client.o
Expand Down
6 changes: 6 additions & 0 deletions src/kpatch_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#include <errno.h>

#include "kpatch_log.h"

#ifdef STRESS_TEST
#include "kpatch_user.h"
extern int parent_pid;
#endif

int log_level = LOG_INFO;
Expand Down Expand Up @@ -64,6 +66,10 @@ void kpfatal(const char *fmt, ...)
__valog(LOG_ERR, "FATAL! ", fmt, va);
va_end(va);

#ifdef STRESS_TEST
if (parent_pid >= 0)
stress_test_notify_parent();
#endif
exit(1);
}

Expand Down
44 changes: 42 additions & 2 deletions src/kpatch_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ cmd_update(int argc, char *argv[])

#ifdef STRESS_TEST

int parent_pid = -1;
static int process_pid = -1;
static char test_log_base[PATH_MAX-sizeof("-4194304")];
static char test_log_name[PATH_MAX];
Expand Down Expand Up @@ -647,16 +648,53 @@ static int stress_test_log_init(int is_base_process)
return 0;
}

void stress_test_notify_parent()
{
if (parent_pid < 0)
return;
union sigval code;
code.sival_int = process_pid;
sigqueue(parent_pid, SIGCHLD, code);
}

void stress_test_signal_handler(int sig, siginfo_t *info, void *ucontext)
{
switch (sig) {
case SIGCHLD: {
if (info->si_code == SI_QUEUE) {
kpinfo("Child process pid %d fatal error.\n", info->si_pid);
return;
} else {
int pid, status;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
kpinfo("Process pid %d exited.\n", pid);
return;
}
break;
}
}
}

void stress_test_install_sigaction()
{
struct sigaction sigact;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = SA_SIGINFO | SA_RESTART;
sigact.sa_sigaction = &stress_test_signal_handler;
sigaction(SIGCHLD, &sigact, NULL);
}

static int cmd_stress_test(int fd, int argc, char *argv[])
{
if (sscanf(argv[1], "%d", &process_pid) != 1) {
kplogerror("Can't parse pid from %s\n", argv[1]);
return -1;
}
kpinfo("Spawning child to patch pid %d\n", process_pid);

parent_pid = getpid();
int child = fork();
if (child == 0) {
signal(SIGCHLD, SIG_DFL);
log_file_free();
if (stress_test_log_init(0))
kpfatal("Can't initialize log.\n");
Expand All @@ -666,6 +704,7 @@ static int cmd_stress_test(int fd, int argc, char *argv[])
log_file_free();
exit(rv);
}
kpinfo("Spawned child %d to patch pid %d\n", child, process_pid);
close(fd);
return 0;
}
Expand Down Expand Up @@ -1020,7 +1059,8 @@ int main(int argc, char *argv[])
#ifdef STRESS_TEST
if (argc < 3)
return usage("not enough arguments.");
signal(SIGCHLD, SIG_IGN);

stress_test_install_sigaction();
return cmd_server(argc, argv);
#else
if (argc < 1)
Expand Down
1 change: 1 addition & 0 deletions src/kpatch_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@

int cmd_patch_user(int argc, char *argv[]);
int cmd_unpatch_user(int argc, char *argv[]);
void stress_test_notify_parent();

#endif

0 comments on commit a5dca61

Please sign in to comment.