From c7649f82c6ace3666240bf5a192f730a4f8271f6 Mon Sep 17 00:00:00 2001 From: Ayoub Zaki Date: Thu, 30 May 2024 13:36:59 +0200 Subject: [PATCH] Add option to send READY=1 notification to systemd after tee-supplicant is setup This option is very useful when tee-supplicant is started from systemd and can used with Type=notify to signal readiness Note: this cannot be used with daemonize option as it the process is forking --- flags.mk | 2 ++ tee-supplicant/src/tee_supplicant.c | 30 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/flags.mk b/flags.mk index be4088c1..31d36785 100644 --- a/flags.mk +++ b/flags.mk @@ -9,6 +9,8 @@ PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config C_COMPILER=$(shell readlink -f $$(which $(CC))) +override LDFLAGS += -ldl + override CFLAGS += -Wall -Wbad-function-cast -Wcast-align \ -Werror-implicit-function-declaration -Wextra \ -Wfloat-equal -Wformat-nonliteral -Wformat-security \ diff --git a/tee-supplicant/src/tee_supplicant.c b/tee-supplicant/src/tee_supplicant.c index 98dec3ce..94587c94 100644 --- a/tee-supplicant/src/tee_supplicant.c +++ b/tee-supplicant/src/tee_supplicant.c @@ -54,6 +54,7 @@ #include #include #include +#include #include "optee_msg_supplicant.h" @@ -493,6 +494,8 @@ static int usage(int status) fprintf(stderr, "\t-h, --help: this help\n"); fprintf(stderr, "\t-d, --daemonize: run as a daemon (fork and return " "after child has opened the TEE device or on error)\n"); + fprintf(stderr, "\t-n, --sdnotify: signal READY=1 to systemd " + "after tee-supplicant is setup)\n"); fprintf(stderr, "\t-f, --fs-parent-path: secure fs parent path [%s]\n", supplicant_params.fs_parent_path); fprintf(stderr, "\t-l, --ta-path: TA load path\n"); @@ -819,6 +822,7 @@ int main(int argc, char *argv[]) struct thread_arg arg = { .fd = -1 }; int pipefd[2] = { 0, }; bool daemonize = false; + bool sdnotify = false; char *dev = NULL; int e = 0; int long_index = 0; @@ -835,6 +839,7 @@ int main(int argc, char *argv[]) /* long name | has argument | flag | short value */ { "help", no_argument, 0, 'h' }, { "daemonize", no_argument, 0, 'd' }, + { "sdnotify", no_argument, 0, 'n' }, { "fs-parent-path", required_argument, 0, 'f' }, { "ta-path", required_argument, 0, 'l' }, { "ta-dir", required_argument, 0, 't' }, @@ -852,6 +857,9 @@ int main(int argc, char *argv[]) case 'd': daemonize = true; break; + case 'n': + sdnotify = true; + break; case 'f': supplicant_params.fs_parent_path = optarg; break; @@ -923,6 +931,28 @@ int main(int argc, char *argv[]) } } + if (sdnotify) { + /* we are set here notify systemd */ + int(*__sd_notify__)(); + void *systemd = dlopen("libsystemd.so", RTLD_LAZY); + if (systemd) { + *(int**)(&__sd_notify__) = dlsym(systemd, "sd_notify"); + if (__sd_notify__) { + int ret = __sd_notify__(0, "READY=1"); + if (ret <= 0) { + fprintf(stderr, "sd_notify failed: %d\n", ret); + } + } + else { + fprintf(stderr, "Couldn't find sd_notify symbol: %s\n", dlerror()); + } + dlclose(systemd); + } + else { + fprintf(stderr, "Couldn't open libsystemd.so: %s\n", dlerror()); + } + } + if (daemonize) { /* Release parent */ if (write(pipefd[1], "", 1) != 1) {