Skip to content

Commit

Permalink
feat(radarbox): Implement rbfeeder_fixcputemp
Browse files Browse the repository at this point in the history
Use LD_PRELOAD hook to workaround radarbox cpu temperature segfault issue.
Allow container to run on userspace podman where sysfs volume is impossible.

Signed-off-by: Jiaxun Yang <[email protected]>
  • Loading branch information
FlyGoat committed Apr 14, 2024
1 parent 035d9ac commit 5f4c28d
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
16 changes: 15 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,21 @@ RUN set -x && \
# mlat-client: simple test
/usr/local/share/radarbox-mlat-client/venv/bin/python3 -c 'import mlat.client'

FROM debian:bullseye as rbfeeder_fixcputemp
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
ADD rbfeeder_fixcputemp ./
RUN set -x && \
apt-get update && \
apt-get install -y --no-install-suggests --no-install-recommends \
build-essential
ARG TARGETARCH
RUN if [ $TARGETARCH != "arm" ]; then \
apt-get install -y crossbuild-essential-armhf \
; fi
RUN make CC=arm-linux-gnueabihf-gcc

# THTTPD
FROM alpine:3.13.2 AS thttpd
FROM alpine:3.19.1 AS thttpd

ENV THTTPD_VERSION=2.29

Expand Down Expand Up @@ -244,6 +257,7 @@ COPY --from=confd /opt/confd/bin/confd /copy_root/opt/confd/bin/
COPY --from=radarbox /usr/bin/rbfeeder /copy_root/usr/bin/rbfeeder_armhf
COPY --from=radarbox /usr/bin/dump1090-rb /copy_root/usr/bin/dump1090-rbs
COPY --from=radarbox /usr/local/share/radarbox-mlat-client /copy_root/usr/local/share/radarbox-mlat-client
COPY --from=rbfeeder_fixcputemp ./librbfeeder_fixcputemp.so /copy_root/usr/lib/arm-linux-gnueabihf/librbfeeder_fixcputemp.so
ADD build /copy_root/build

FROM debian:bullseye-slim as serve
Expand Down
3 changes: 2 additions & 1 deletion build/rbfeeder_wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

# attempt to run natively
if /usr/bin/rbfeeder_armhf --no-start --version >/dev/null 2>&1; then
export LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/librbfeeder_fixcputemp.so
/usr/bin/rbfeeder_armhf "$@"
elif qemu-arm-static /usr/bin/rbfeeder_armhf --no-start --version >/dev/null 2>&1; then
qemu-arm-static /usr/bin/rbfeeder_armhf "$@"
qemu-arm-static -E LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/librbfeeder_fixcputemp.so /usr/bin/rbfeeder_armhf "$@"
else
for i in {1..5}; do
echo "FATAL: Could not run rbfeeder natively or via qemu!" | mawk -W interactive '{printf "%c[34m[radarbox-feeder]%c[0m %s\n", 27, 27, $0}'
Expand Down
2 changes: 2 additions & 0 deletions rbfeeder_fixcputemp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
*.so
11 changes: 11 additions & 0 deletions rbfeeder_fixcputemp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CFLAGS := $(CFLAGS) -fPIC -Wall

TARGET = librbfeeder_fixcputemp

all: $(TARGET).so

$(TARGET).so: $(TARGET).o
$(CC) $^ -shared -o $@

clean:
rm *.so *.o
24 changes: 24 additions & 0 deletions rbfeeder_fixcputemp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Fixup CPU Teampeature for RBFeeder

This is a simple hook library for RBFeeder to fixup the CPU temperature measurement function on non-Raspberry Pi devices.

## Implementation
When running on a Raspberry Pi, the CPU temperature is read from the `/sys/class/thermal/thermal_zone0/temp` file. However this file does not exist on some other devices, and RBFeeder will crash because the file is not found.

This library hooked `fopen` function to intercept the file open request for `/sys/class/thermal/thermal_zone0/temp` and return a fake file handle with a fixed temperature value if we are unable to open that file.

## Usage
1. Build the library by running `make` with cross compiler or on the target device.
```
$ make CC=arm-linux-gnueabihf-gcc
```

2. LD_PRELOAD the library when running RBFeeder.
```
$ LD_PRELOAD=./libfixcputemp.so ./rbfeeder
```

or if you are using qemu
```
$ qemu-arm -E LD_PRELOAD=./librbfeeder_fixcputemp.so ./rbfeeder
```
56 changes: 56 additions & 0 deletions rbfeeder_fixcputemp/librbfeeder_fixcputemp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

#undef fopen

ssize_t cputemp_read(void *cookie, char *buf, size_t size)
{
const char temp[] = "0";
size_t len = sizeof(temp);
if (size < len)
{
return -1;
}
memcpy(buf, temp, len);
return len;
}

cookie_io_functions_t cputemp_funcs = {
.read = cputemp_read,
};

FILE *fopen(const char *path, const char *mode)
{
FILE *orig_file = NULL;
static FILE *(*real_fopen)(const char *, const char *) = NULL;
if (real_fopen == NULL)
{
real_fopen = dlsym(RTLD_NEXT, "fopen");
if (real_fopen == NULL)
{
fprintf(stderr, "fixcputemp: Error in `dlsym`: %s\n", dlerror());
return NULL;
}
}

orig_file = real_fopen(path, mode);
if (orig_file)
{
return orig_file;
}

if (strcmp(path, "/sys/class/thermal/thermal_zone0/temp") == 0)
{
if (strcmp(mode, "r") != 0)
{
fprintf(stderr, "fixcputemp: fopen() called with mode other than 'r'\n");
return NULL;
}
return fopencookie(NULL, "r", cputemp_funcs);
}
return NULL;
}

0 comments on commit 5f4c28d

Please sign in to comment.