diff options
author | Simon Herkenhoff <sherkenhoff@de.adit-jv.com> | 2019-02-04 22:16:00 +0900 |
---|---|---|
committer | Christoph Lipka <clipka@users.noreply.github.com> | 2019-02-04 14:16:00 +0100 |
commit | 18321f3551098d6313c0f3f3620bc74b53b84472 (patch) | |
tree | fb9200b6782a1b9f3d368d6f530f9d521bbefe1d /src/tests/dlt-test-fork-handler.c | |
parent | 892325dbc0983a18d5d16a10faefc0e982bf9660 (diff) | |
download | DLT-daemon-18321f3551098d6313c0f3f3620bc74b53b84472.tar.gz |
libdlt: Do not allow DLT usage in forked child (#95)
DLT shall not be used in a forked child until a variant of exec() is
called, because DLT is using non async-signal-safe functions.
The forking process can continue to use libdlt's logging facilities, but
any attempt to use libdlt from the forked child will be denied.
The fork-handler test is updated to reflect this by trying to log from
the forked child which will fail. The fork then calls exec and runs
another application that can continue to use DLT.
Signed-off-by: Simon Herkenhoff <sherkenhoff@jp.adit-jv.com>
Diffstat (limited to 'src/tests/dlt-test-fork-handler.c')
-rw-r--r-- | src/tests/dlt-test-fork-handler.c | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/src/tests/dlt-test-fork-handler.c b/src/tests/dlt-test-fork-handler.c index 4bd3321..118753b 100644 --- a/src/tests/dlt-test-fork-handler.c +++ b/src/tests/dlt-test-fork-handler.c @@ -25,6 +25,8 @@ */ #include <unistd.h> /* for fork() */ +#include <time.h> +#include <errno.h> #include "dlt.h" @@ -34,23 +36,31 @@ int main() { DltContext mainContext; + struct timespec timeout, r; + + timeout.tv_sec = 0; + timeout.tv_nsec = 200000000L; DLT_REGISTER_APP("PRNT", "Parent application"); DLT_REGISTER_CONTEXT(mainContext, "CTXP", "Parent context"); DLT_LOG(mainContext, DLT_LOG_WARN, DLT_STRING("First message before fork")); - usleep(200000); + nanosleep(&timeout, &r); pid_t pid = fork(); - if (pid == 0) { /* child process */ /* this message should not be visible */ - /* DLT_LOG(mainContext, DLT_LOG_WARN, DLT_STRING("Child's first message after fork, pid: "), DLT_INT32(getpid())); */ - /* unfortunately, this message does arrive, I assume because it still has (locally) valid data ... */ + DLT_LOG(mainContext, DLT_LOG_WARN, DLT_STRING("Child's first message after fork, pid: "), DLT_INT32(getpid())); + /* this will not register CHLD application */ DLT_REGISTER_APP("CHLD", "Child application"); + /* this will not register CTXC context */ DLT_REGISTER_CONTEXT(mainContext, "CTXC", "Child context"); + /* this will not log a message */ DLT_LOG(mainContext, DLT_LOG_WARN, DLT_STRING("Child's second message after fork, pid: "), DLT_INT32(getpid())); - usleep(400000); + nanosleep(&timeout, &r); + if (execlp("dlt-example-user", "dlt-example-user", "-n 1", + "you should see this message", NULL)) + return errno; } else if (pid == -1) /* error in fork */ { @@ -58,7 +68,7 @@ int main() } else { /* parent */ DLT_LOG(mainContext, DLT_LOG_WARN, DLT_STRING("Parent's first message after fork, pid: "), DLT_INT32(getpid())); - usleep(500000); + nanosleep(&timeout, &r); } DLT_UNREGISTER_APP() |