-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(radarbox): Implement rbfeeder_fixcputemp
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
Showing
6 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.o | ||
*.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |