Skip to content

Commit 9c0cd2d

Browse files
author
Roman Rashchupkin
committed
Add libcare-stresstest logging to files
-change some printfs to logging
1 parent 26b0c6e commit 9c0cd2d

File tree

4 files changed

+101
-21
lines changed

4 files changed

+101
-21
lines changed

src/kpatch_log.c

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

77
#include "kpatch_log.h"
8+
#ifdef STRESS_TEST
9+
#include "kpatch_user.h"
10+
#endif
811

912
int log_level = LOG_INFO;
1013
int log_indent;
14+
static FILE *log_file;
1115

1216
static void __valog(int level, const char *prefix, const char *fmt, va_list va)
1317
{
1418
FILE *f = level <= LOG_WARN ? stderr : stdout;
1519
if (prefix)
1620
fprintf(f, "%s", prefix);
1721

22+
if (log_file) {
23+
va_list vaf;
24+
va_copy(vaf, va);
25+
if (prefix)
26+
fprintf(log_file, "%s", prefix);
27+
vfprintf(log_file, fmt, vaf);
28+
fflush(log_file);
29+
}
1830
vfprintf(f, fmt, va);
1931
}
2032

@@ -100,3 +112,20 @@ void _kpfatalerror(const char *file, int line, const char *fmt, ...)
100112

101113
exit(EXIT_FAILURE);
102114
}
115+
116+
int log_file_init(char *fname)
117+
{
118+
if (!fname)
119+
return -1;
120+
log_file = fopen(fname, "a");
121+
if (!log_file)
122+
return -1;
123+
return 0;
124+
}
125+
126+
void log_file_free()
127+
{
128+
if (log_file)
129+
fclose(log_file);
130+
log_file = NULL;
131+
}

src/kpatch_log.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ void _kplogerror(const char *filename, int line, const char *fmt, ...)
3333
#define LOG_DEBUG 3
3434
#define LOG_TRACE 5
3535

36+
int log_file_init(char *fname);
37+
void log_file_free();
38+
3639
#endif

src/kpatch_process.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -746,9 +746,9 @@ process_print_cmdline(kpatch_process_t *proc)
746746

747747
for (i = 0; i < rv; i++) {
748748
if (buf[i] != '\n' && isprint(buf[i]))
749-
putchar(buf[i]);
749+
kpinfo("%c", buf[i]);
750750
else
751-
printf("\\x%02x", (unsigned char)buf[i]);
751+
kpinfo("\\x%02x", (unsigned char)buf[i]);
752752
}
753753
}
754754

@@ -860,9 +860,9 @@ kpatch_process_kickstart_execve_wrapper(kpatch_process_t *proc)
860860
if (ret < 0)
861861
return -1;
862862

863-
printf("kpatch_ctl real cmdline=\"");
863+
kpinfo("kpatch_ctl real cmdline=\"");
864864
process_print_cmdline(proc);
865-
printf("\"\n");
865+
kpinfo("\"\n");
866866

867867
return 0;
868868
}
@@ -1137,11 +1137,11 @@ kpatch_process_init(kpatch_process_t *proc,
11371137
void
11381138
kpatch_process_print_short(kpatch_process_t *proc)
11391139
{
1140-
printf("kpatch_ctl targeting pid %d\n", proc->pid);
1140+
kpinfo("kpatch_ctl targeting pid %d\n", proc->pid);
11411141
if (proc->send_fd == -1) {
1142-
printf("kpatch_ctl cmdline=\"");
1142+
kpinfo("kpatch_ctl cmdline=\"");
11431143
process_print_cmdline(proc);
1144-
printf("\"\n");
1144+
kpinfo("\"\n");
11451145
}
11461146
}
11471147

src/kpatch_user.c

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sys/socket.h>
1212
#include <sys/un.h>
1313
#include <sys/wait.h>
14+
#include <sys/types.h>
1415

1516
#include <gelf.h>
1617
#include <libunwind.h>
@@ -566,7 +567,11 @@ cmd_update(int argc, char *argv[])
566567

567568
#ifdef STRESS_TEST
568569

569-
struct test_data {
570+
static int process_pid = -1;
571+
static char test_log_base[PATH_MAX-sizeof("-4194304")];
572+
static char test_log_name[PATH_MAX];
573+
574+
static struct test_data {
570575
int option_period;
571576
int stat_cycle_num;
572577
} test_info = { .option_period = 0, .stat_cycle_num = 0 };
@@ -581,26 +586,20 @@ server_wait(int pid, int period)
581586
for (i=0; i<period; i++) {
582587
nanosleep(&req, &rem);
583588
if (kill(pid, 0) != 0) {
584-
fprintf(stderr, "Process %d terminated.\n", pid);
589+
kpinfo("Process %d terminated.\n", pid);
585590
return -1;
586591
}
587592
}
588593
return 0;
589594
}
590595

591596
static int
592-
server_stress_test(int fd, int argc, char *argv[])
597+
server_stress_test(int fd, int pid)
593598
{
594-
int pid;
595599
int delay;
596600
test_info.stat_cycle_num = 0;
597601
srand(time(NULL));
598602

599-
if (sscanf(argv[1], "%d", &pid) != 1) {
600-
kperr("Can't parse pid from %s\n", argv[1]);
601-
return -1;
602-
}
603-
604603
while (1) {
605604
while (patch_user(storage_dir, pid, 0, fd) < 0)
606605
if (server_wait(pid, 1) < 0)
@@ -627,11 +626,44 @@ server_stress_test(int fd, int argc, char *argv[])
627626
return 0;
628627
}
629628

629+
static int stress_test_log_init(int is_base_process)
630+
{
631+
if (!strlen(test_log_base))
632+
return 0;
633+
if (is_base_process) {
634+
if (snprintf(test_log_name, PATH_MAX, "%s-base", test_log_base) >= PATH_MAX) {
635+
kplogerror("Can't initialize log \'%s\'-<PID>\n", test_log_base);
636+
return -1;
637+
}
638+
} else
639+
if (snprintf(test_log_name, PATH_MAX, "%s-%d", test_log_base, getpid()) >= PATH_MAX) {
640+
kplogerror("Can't initialize log \'%s\'-<PID>\n", test_log_base);
641+
return -1;
642+
}
643+
if (log_file_init(test_log_name)) {
644+
kplogerror("Can't open log file \'%s\'\n", test_log_name);
645+
return -1;
646+
}
647+
return 0;
648+
}
649+
630650
static int cmd_stress_test(int fd, int argc, char *argv[])
631651
{
652+
if (sscanf(argv[1], "%d", &process_pid) != 1) {
653+
kplogerror("Can't parse pid from %s\n", argv[1]);
654+
return -1;
655+
}
656+
kpinfo("Spawning child to patch pid %d\n", process_pid);
657+
632658
int child = fork();
633659
if (child == 0) {
634-
int rv = server_stress_test(fd, argc, argv);
660+
log_file_free();
661+
if (stress_test_log_init(0))
662+
kpfatal("Can't initialize log.\n");
663+
664+
int rv = server_stress_test(fd, process_pid);
665+
666+
log_file_free();
635667
exit(rv);
636668
}
637669
close(fd);
@@ -640,7 +672,11 @@ static int cmd_stress_test(int fd, int argc, char *argv[])
640672

641673
static int usage_stresstest()
642674
{
643-
fprintf(stderr, "usage: libcare-stresstest PERIOD(ms, 0 - only patch) <UNIX socket> [STORAGE ROOT]\n");
675+
fprintf(stderr, "usage: libcare-stresstest [args] PERIOD(ms, 0 - only patch) <UNIX socket> [STORAGE ROOT]\n");
676+
fprintf(stderr, "\nOptions:\n");
677+
fprintf(stderr, " -v - verbose mode\n");
678+
fprintf(stderr, " -l <BASE> - log file name <BASE>-PID\n");
679+
fprintf(stderr, " -h - this message\n");
644680
return -1;
645681
}
646682

@@ -828,9 +864,11 @@ cmd_server(int argc, char *argv[])
828864
}
829865

830866
#ifdef STRESS_TEST
831-
if (sscanf(argv[0], "%d", &test_info.option_period) != 1) {
867+
if (stress_test_log_init(1))
868+
kpfatal("Can't initialize log.\n");
869+
if (sscanf(argv[0], "%d", &test_info.option_period) != 1)
832870
kplogerror("Can't parse period from %s\n", argv[0]);
833-
}
871+
kpinfo("Starting libcare-stresstest: period=%d\n", test_info.option_period);
834872
#endif
835873

836874
sfd = server_bind_socket(argv[1]);
@@ -954,13 +992,23 @@ int main(int argc, char *argv[])
954992
{
955993
int opt;
956994

957-
while ((opt = getopt(argc, argv, "+vh")) != EOF) {
995+
while ((opt = getopt(argc, argv, "+vl:h")) != EOF) {
958996
switch (opt) {
959997
case 'v':
960998
log_level += 1;
961999
break;
9621000
case 'h':
9631001
return usage(NULL);
1002+
case 'l':
1003+
#ifdef STRESS_TEST
1004+
strncpy(test_log_base, optarg, sizeof(test_log_base));
1005+
if (test_log_base[sizeof(test_log_base)-1]) {
1006+
usage_stresstest();
1007+
kpfatal("Can't initialize log\n");
1008+
break;
1009+
}
1010+
break;
1011+
#endif
9641012
default:
9651013
return usage("unknown option");
9661014
}

0 commit comments

Comments
 (0)