From 192433f97aa83f1a4547db70e07d57c852b7ace6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Stelmach?= Date: Mon, 15 Feb 2021 15:41:27 +0100 Subject: logger: testing tool for STDIO logger MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I029a7def836b317a4e059c7a56301e169910a830 Signed-off-by: Ɓukasz Stelmach --- tools/testing/selftests/logger/.gitignore | 1 + tools/testing/selftests/logger/Makefile | 24 +++++ tools/testing/selftests/logger/logger.c | 155 ++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 tools/testing/selftests/logger/.gitignore create mode 100644 tools/testing/selftests/logger/Makefile create mode 100644 tools/testing/selftests/logger/logger.c diff --git a/tools/testing/selftests/logger/.gitignore b/tools/testing/selftests/logger/.gitignore new file mode 100644 index 000000000000..b1e5ec6fe6b1 --- /dev/null +++ b/tools/testing/selftests/logger/.gitignore @@ -0,0 +1 @@ +logger-test diff --git a/tools/testing/selftests/logger/Makefile b/tools/testing/selftests/logger/Makefile new file mode 100644 index 000000000000..02a9019108d8 --- /dev/null +++ b/tools/testing/selftests/logger/Makefile @@ -0,0 +1,24 @@ +CFLAGS += -I../../../../usr/include/ +CFLAGS += -std=gnu99 +CFLAGS += -D_GNU_SOURCE +LDFLAGS = -pthread + +.PHONY: all clean + +include ../lib.mk + +TEST_CUSTOM_PROGS := $(OUTPUT)/logger-test +all: $(TEST_CUSTOM_PROGS) + +OBJS = \ + logger.o + +OBJS := $(patsubst %,$(OUTPUT)/%,$(OBJS)) + +$(TEST_CUSTOM_PROGS): $(OBJS) + $(CC) -o $(TEST_CUSTOM_PROGS) $(OBJS) $(LDFLAGS) + +$(OBJS): $(OUTPUT)/%.o: %.c + $(CC) -c $^ -o $@ $(CFLAGS) + +EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(OBJS) diff --git a/tools/testing/selftests/logger/logger.c b/tools/testing/selftests/logger/logger.c new file mode 100644 index 000000000000..0446e796fa35 --- /dev/null +++ b/tools/testing/selftests/logger/logger.c @@ -0,0 +1,155 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LOGGER_SET_VERSION 44550 +#define LOGGER_SET_TAG 44551 +#define LOGGER_SET_PRIO 44552 + +#define handle_error_en(en, msg) \ + do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) + +#define BIT(nr) ((1UL) << (nr)) + +void *tstart(void *arg) +{ + int *fd = arg; + write(*fd, "child thread msg #1\nchild thread msg #2", 39); + return 0; +} + +int main(int ac, char *av[]) { + + char *device = "/dev/log_main"; + char *msg = "The Foo"; + char *tag = "stdio"; + int c, fd, s; + pid_t child; + pthread_t tid; + struct iovec vec[3]; + unsigned char prio = 4; + unsigned long test_mask = ~0UL; + + while (1) { + static struct option long_options[] = { + {"priority", required_argument, 0, 'p'}, + {"tag", required_argument, 0, 't'}, + {"test-mask", required_argument, 0, 'm'}, + {"device", required_argument, 0, 'd'}, + {0, 0, 0, 0} + }; + + c = getopt_long(ac, av, "p:t:m:d:", long_options, NULL); + if (c == -1) + break; + + switch (c) { + case 'p': + prio = (unsigned char) strtol(optarg, NULL, 10); + break; + case 't': + tag = strdup(optarg); + break; + case 'm': + test_mask = (unsigned char) strtol(optarg, NULL, 16); + break; + case 'd': + device = strdup(optarg); + break; + default: + exit(1); + } + } + + setlinebuf(stdout); + fd = open(device, O_WRONLY); + + if (ac >= 2) { + } + + if (ac == 3) { + } + + if (test_mask & BIT(0)) { + vec[0].iov_base = &prio; + vec[0].iov_len = 1; + vec[1].iov_base = tag; + vec[1].iov_len = strlen(tag) + 1; + vec[2].iov_base = msg; + vec[2].iov_len = strlen(msg) + 1; + + writev(fd, vec, 3); + if (test_mask & BIT(1)) { + msg = "line #1\nline #2"; + vec[2].iov_base = msg; + vec[2].iov_len = strlen(msg) + 1; + + writev(fd, vec, 3); + } + } + + ioctl(fd, LOGGER_SET_PRIO, prio); + ioctl(fd, LOGGER_SET_TAG, "\006\000\000\000stdio"); + + if (test_mask & BIT(2)) { + + write(fd, "The Foo From STDIO\n", 19); + + write(fd, "LINE #1\nLINE #2", 15); + write(fd, " CONTINUED\nONCE", 15); + write(fd, " AGAIN\n", 7); + } + + if (test_mask & BIT(3)) { + msg = malloc(8000); + if (!msg) + return 1; + + for (int i = 0; i < 8000; i++) + msg[i] = ' ' + (i % 96); + msg[7999] = '\n'; + write(fd, msg, 8000); + } + + if (test_mask & BIT(4)) { + child = fork(); + if (child < 0) { + return -1; + } else if (child == 0) { + sleep(1); + printf("child: %d\n", getpid()); + write(fd, "child 1\n", 8); + sleep(1); + write(fd, "child 2\n", 8); + close(fd); + return 0; + } + write(fd, "PARENT\n", 7); + printf("PARENT: %d\n", getpid()); + wait(&s); + } + + if (test_mask & BIT(5)) { + s = pthread_create(&tid, NULL, &tstart, &fd); + if (s != 0) + handle_error_en(s, "pthread_create"); + sleep(1); + write(fd, "PARENT THREAD\n", 14); + + s = pthread_join(tid, NULL); + if (s != 0) + handle_error_en(s, "pthread_join"); + } + + return 0; +} -- cgit v1.2.3