Skip to content

Commit fb0f8af

Browse files
author
Roman Rashchupkin
committed
Notify libcare-stresstest about child events
1 parent 9c0cd2d commit fb0f8af

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

src/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ kpatch_make: kpatch_make.o
3636
LIBUNWIND_LIBS := $(shell pkg-config --libs libunwind libunwind-ptrace)
3737

3838

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

43-
libcare-stresstest: kpatch_user-stresstest.o kpatch_storage.o kpatch_patch.c kpatch_elf.o kpatch_ptrace.o kpatch_coro.o
44-
libcare-stresstest: kpatch_process.o kpatch_common.o rbtree.o kpatch_log.o
43+
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
44+
libcare-stresstest: kpatch_process.o kpatch_common.o rbtree.o
4545
libcare-stresstest: LDLIBS += -lelf -lrt $(LIBUNWIND_LIBS)
4646

4747
libcare-client: libcare-client.o

src/kpatch_log.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
#include <errno.h>
66

77
#include "kpatch_log.h"
8+
89
#ifdef STRESS_TEST
910
#include "kpatch_user.h"
11+
extern int parent_pid;
1012
#endif
1113

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

69+
#ifdef STRESS_TEST
70+
if (parent_pid >= 0)
71+
stress_test_notify_parent();
72+
#endif
6773
exit(1);
6874
}
6975

src/kpatch_user.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ cmd_update(int argc, char *argv[])
567567

568568
#ifdef STRESS_TEST
569569

570+
int parent_pid = -1;
570571
static int process_pid = -1;
571572
static char test_log_base[PATH_MAX-sizeof("-4194304")];
572573
static char test_log_name[PATH_MAX];
@@ -647,16 +648,53 @@ static int stress_test_log_init(int is_base_process)
647648
return 0;
648649
}
649650

651+
void stress_test_notify_parent()
652+
{
653+
if (parent_pid < 0)
654+
return;
655+
union sigval code;
656+
code.sival_int = process_pid;
657+
sigqueue(parent_pid, SIGCHLD, code);
658+
}
659+
660+
void stress_test_signal_handler(int sig, siginfo_t *info, void *ucontext)
661+
{
662+
switch (sig) {
663+
case SIGCHLD: {
664+
if (info->si_code == SI_QUEUE) {
665+
kpinfo("Child process pid %d fatal error.\n", info->si_pid);
666+
return;
667+
} else {
668+
int pid, status;
669+
while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
670+
kpinfo("Child process pid %d exited.\n", pid);
671+
return;
672+
}
673+
break;
674+
}
675+
}
676+
}
677+
678+
void stress_test_install_sigaction()
679+
{
680+
struct sigaction sigact;
681+
sigemptyset(&sigact.sa_mask);
682+
sigact.sa_flags = SA_SIGINFO | SA_RESTART;
683+
sigact.sa_sigaction = &stress_test_signal_handler;
684+
sigaction(SIGCHLD, &sigact, NULL);
685+
}
686+
650687
static int cmd_stress_test(int fd, int argc, char *argv[])
651688
{
652689
if (sscanf(argv[1], "%d", &process_pid) != 1) {
653690
kplogerror("Can't parse pid from %s\n", argv[1]);
654691
return -1;
655692
}
656-
kpinfo("Spawning child to patch pid %d\n", process_pid);
657693

694+
parent_pid = getpid();
658695
int child = fork();
659696
if (child == 0) {
697+
signal(SIGCHLD, SIG_IGN);
660698
log_file_free();
661699
if (stress_test_log_init(0))
662700
kpfatal("Can't initialize log.\n");
@@ -666,6 +704,7 @@ static int cmd_stress_test(int fd, int argc, char *argv[])
666704
log_file_free();
667705
exit(rv);
668706
}
707+
kpinfo("Spawned child %d to patch pid %d\n", child, process_pid);
669708
close(fd);
670709
return 0;
671710
}
@@ -1020,7 +1059,8 @@ int main(int argc, char *argv[])
10201059
#ifdef STRESS_TEST
10211060
if (argc < 3)
10221061
return usage("not enough arguments.");
1023-
signal(SIGCHLD, SIG_IGN);
1062+
1063+
stress_test_install_sigaction();
10241064
return cmd_server(argc, argv);
10251065
#else
10261066
if (argc < 1)

src/kpatch_user.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77

88
int cmd_patch_user(int argc, char *argv[]);
99
int cmd_unpatch_user(int argc, char *argv[]);
10+
void stress_test_notify_parent();
1011

1112
#endif

0 commit comments

Comments
 (0)