Skip to content

Commit

Permalink
Add option to send READY=1 notification to systemd after tee-supplica…
Browse files Browse the repository at this point in the history
…nt 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
  • Loading branch information
embetrix committed May 30, 2024
1 parent 3eac340 commit c7649f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions flags.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
30 changes: 30 additions & 0 deletions tee-supplicant/src/tee_supplicant.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <tee_supp_fs.h>
#include <tee_supplicant.h>
#include <unistd.h>
#include <dlfcn.h>

#include "optee_msg_supplicant.h"

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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' },
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c7649f8

Please sign in to comment.