ci / test (push) Successful in 16s
ci / prune-artifacts (pull_request) Skipped
ci / kernel-build (pull_request) Skipped
ci / bench (push) Successful in 11s
ci / patches-apply (push) Successful in 22s
ci / prune-artifacts (push) Skipped
ci / kernel-build (push) Skipped
ci / quality (push) Successful in 37s
ci / badges (push) Skipped
ci / qemu-tools (pull_request) Successful in 45s
ci / qemu-tools (push) Successful in 49s
ci / test (pull_request) Successful in 17s
ci / mcdc (pull_request) Successful in 19s
ci / bench (pull_request) Successful in 8s
ci / mcdc (push) Successful in 23s
ci / patches-apply (pull_request) Successful in 22s
ci / quality (pull_request) Successful in 34s
ci / badges (pull_request) Skipped
Sysfs paths were built in fixed 80/96-byte stack buffers with unchecked snprintf, so a root longer than about 70 bytes was truncated and both relays silently read as unavailable. Each operation now formats all of its paths up front into PATH_MAX buffers and checks every snprintf return. A root whose longest path does not fit is refused with a stderr line and the call reports unavailable, off or no-op; nothing is ever truncated. The export argument comes from a constant table instead of runtime formatting. Regression tests: a 200-byte root through the fake io (full path reaches the backend), the exact PATH_MAX boundary, one byte over (refused and logged on available, get and set, io untouched), and a real scratch tree under a root over 200 bytes. MC/DC stays at 100% (50/50 conditions). Fixes #10 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01441BzdGcvQmXkiN2qDjfZg
165 lines
4.9 KiB
C
165 lines
4.9 KiB
C
#include "relays.h"
|
|
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
|
|
/* GPIO1_A0 and GPIO1_A1; gpiochip1 base is 32. See relays.h. */
|
|
static const int s_gpio[WARDEN_RELAY_COUNT] = { 32, 33 };
|
|
static const char * const s_name[WARDEN_RELAY_COUNT] = { "Relay 1", "Relay 2" };
|
|
/* The same numbers as the strings written to `export`, so no runtime formatting. */
|
|
static const char * const s_gpio_str[WARDEN_RELAY_COUNT] = { "32", "33" };
|
|
|
|
/* ---- default sysfs backend (Tier-2 plumbing; covered by integration tests) ---- */
|
|
|
|
static const char * gpio_root(void)
|
|
{
|
|
const char * r = getenv("WARDEN_GPIO_ROOT");
|
|
return (r && *r) ? r : "/sys/class/gpio";
|
|
}
|
|
|
|
static bool sysfs_exists(const char * path)
|
|
{
|
|
struct stat st;
|
|
return stat(path, &st) == 0;
|
|
}
|
|
|
|
static void sysfs_write(const char * path, const char * value)
|
|
{
|
|
FILE * f = fopen(path, "w");
|
|
if(!f) return;
|
|
fputs(value, f);
|
|
fclose(f);
|
|
}
|
|
|
|
static bool sysfs_read_line(const char * path, char * buf, size_t n)
|
|
{
|
|
FILE * f = fopen(path, "r");
|
|
if(!f) return false;
|
|
bool ok = fgets(buf, (int)n, f) != NULL;
|
|
fclose(f);
|
|
return ok;
|
|
}
|
|
|
|
static bool sysfs_read_int(const char * path, int * out)
|
|
{
|
|
FILE * f = fopen(path, "r");
|
|
if(!f) return false;
|
|
bool ok = fscanf(f, "%d", out) == 1;
|
|
fclose(f);
|
|
return ok;
|
|
}
|
|
|
|
static const struct relay_io s_sysfs_io = {
|
|
sysfs_exists, sysfs_write, sysfs_read_line, sysfs_read_int
|
|
};
|
|
|
|
static const struct relay_io * s_io = &s_sysfs_io;
|
|
|
|
void warden_relay__set_io(const struct relay_io * io) { s_io = io ? io : &s_sysfs_io; }
|
|
|
|
/* ---- decision logic (Tier-1; measured to 100% MC/DC via the fake io) ---- */
|
|
|
|
const char * warden_relay_name(uint32_t idx)
|
|
{
|
|
return idx < WARDEN_RELAY_COUNT ? s_name[idx] : "-";
|
|
}
|
|
|
|
/**
|
|
* Every sysfs path one relay operation can touch. Each holds PATH_MAX bytes, so
|
|
* any root the kernel would accept fits.
|
|
*/
|
|
struct relay_paths {
|
|
char gpio[PATH_MAX]; /* <root>/gpioN */
|
|
char export_[PATH_MAX]; /* <root>/export */
|
|
char direction[PATH_MAX]; /* <root>/gpioN/direction */
|
|
char value[PATH_MAX]; /* <root>/gpioN/value */
|
|
};
|
|
|
|
/**
|
|
* Format the paths for relay @p idx up front. A `$WARDEN_GPIO_ROOT` too long for
|
|
* any of them is refused with a log line, never truncated: a cut-short path
|
|
* would silently report both relays unavailable, or name a different node.
|
|
*/
|
|
static bool relay_paths(uint32_t idx, struct relay_paths * rp)
|
|
{
|
|
const char * root = gpio_root();
|
|
int gpio = s_gpio[idx];
|
|
const int n[] = {
|
|
snprintf(rp->gpio, sizeof(rp->gpio), "%s/gpio%d", root, gpio),
|
|
snprintf(rp->export_, sizeof(rp->export_), "%s/export", root),
|
|
snprintf(rp->direction, sizeof(rp->direction), "%s/gpio%d/direction", root, gpio),
|
|
snprintf(rp->value, sizeof(rp->value), "%s/gpio%d/value", root, gpio),
|
|
};
|
|
for(size_t i = 0; i < sizeof(n) / sizeof(n[0]); i++) {
|
|
/* The unsigned cast also maps an encoding error (n < 0) to "does not fit". */
|
|
if((unsigned)n[i] >= PATH_MAX) {
|
|
fprintf(stderr, "relays: WARDEN_GPIO_ROOT is too long (%zu bytes): sysfs paths "
|
|
"under it exceed PATH_MAX (%d), refusing it\n", strlen(root), PATH_MAX);
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool exported(const struct relay_paths * rp)
|
|
{
|
|
return s_io->exists(rp->gpio);
|
|
}
|
|
|
|
/**
|
|
* Export and set the direction, without disturbing the level. "out" on an
|
|
* unexported pin latches the kernel's default low, which would drop a closed
|
|
* relay the first time this page is opened; reading the current level first and
|
|
* writing it straight back makes the export transparent.
|
|
*/
|
|
static bool ensure_ready(uint32_t idx, const struct relay_paths * rp)
|
|
{
|
|
if(!exported(rp)) {
|
|
s_io->write(rp->export_, s_gpio_str[idx]);
|
|
if(!exported(rp)) return false;
|
|
}
|
|
|
|
char dir[16] = "";
|
|
if(!s_io->read_line(rp->direction, dir, sizeof(dir))) return false;
|
|
|
|
if(strncmp(dir, "out", 3) != 0) {
|
|
/* Preserve the level across the switch to output. */
|
|
int cur;
|
|
if(!s_io->read_int(rp->value, &cur)) cur = 0;
|
|
s_io->write(rp->direction, cur ? "high" : "low");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool warden_relay_available(uint32_t idx)
|
|
{
|
|
if(idx >= WARDEN_RELAY_COUNT) return false;
|
|
struct relay_paths rp;
|
|
if(!relay_paths(idx, &rp)) return false;
|
|
return ensure_ready(idx, &rp);
|
|
}
|
|
|
|
bool warden_relay_get(uint32_t idx)
|
|
{
|
|
if(idx >= WARDEN_RELAY_COUNT) return false;
|
|
struct relay_paths rp;
|
|
if(!relay_paths(idx, &rp)) return false;
|
|
if(!exported(&rp)) return false;
|
|
|
|
int v;
|
|
if(!s_io->read_int(rp.value, &v)) v = 0;
|
|
return v != 0;
|
|
}
|
|
|
|
void warden_relay_set(uint32_t idx, bool on)
|
|
{
|
|
if(idx >= WARDEN_RELAY_COUNT) return;
|
|
struct relay_paths rp;
|
|
if(!relay_paths(idx, &rp)) return;
|
|
if(!ensure_ready(idx, &rp)) return;
|
|
s_io->write(rp.value, on ? "1" : "0");
|
|
}
|