Skip to content

Commit

Permalink
Refactor source and Makefile a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
c4rlo committed Nov 23, 2024
1 parent 35ee140 commit 974b2b1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 56 deletions.
32 changes: 16 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
prog := ath10k-fixer
prefix := /usr/local
install_runner := sudo

CXXFLAGS := $(CXXFLAGS) -MMD -MP -Wall -Wextra -Werror -Wtype-limits -Wpedantic -pedantic-errors \
-std=c++23 -D_GNU_SOURCE -march=native -pipe -Isrc
CXXFLAGS_release := -DNDEBUG -O3 -flto
CXXFLAGS_debug := -ggdb3 -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG
CXXFLAGS_base := -MMD -MP -Wall -Wextra -Werror -Wtype-limits -Wpedantic -pedantic-errors \
-std=c++23 -D_GNU_SOURCE -march=native -pipe -Isrc
CXXFLAGS_release := -O3 -flto -DNDEBUG
CXXFLAGS_debug := -Og -ggdb3 -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG

LDFLAGS := $(LDFLAGS) -pipe
LDFLAGS_base := -pipe
LDFLAGS_release := -flto -s
LDFLAGS_debug := -fsanitize=address -fsanitize=undefined
LDLIBS := -lstdc++

MAKEFLAGS := -j $(shell nproc)
.DEFAULT_GOAL := debug

prog := ath10k-fixer
modes := debug release
cppfiles := $(wildcard src/*.cpp)
objects := $(notdir $(cppfiles:.cpp=.o))
build_dirs := $(addprefix build_,$(modes))

$(modes): %: build_%/$(prog)
$(build_dirs):
mkdir -p $@

define template
objects_$(1) := $$(addprefix build_$(1)/,$$(objects))
$$(objects_$(1)): CXXFLAGS += $$(CXXFLAGS_$(1))
$$(objects_$(1)): | build_$(1)
build_$(1)/%.o: src/%.cpp
objects_$(1) := $$(addprefix build_$(1)/,$$(notdir $$(cppfiles:.cpp=.o)))
$$(objects_$(1)): override CXXFLAGS := $$(CXXFLAGS_base) $$(CXXFLAGS_$(1)) $$(CXXFLAGS)
build_$(1)/%.o: src/%.cpp Makefile
$$(COMPILE.cc) -o $$@ $$<
build_$(1)/%.o: | build_$(1)
-include $$(objects_$(1):.o=.d)
build_$(1)/$$(prog): LDFLAGS += $$(LDFLAGS_$(1))
build_$(1)/$$(prog): $$(objects_$(1))
$$(LINK.o) $$^ $$(LDLIBS) -o $$@
$(1): build_$(1)/$$(prog)
build_$(1)/$$(prog): override LDFLAGS := $$(LDFLAGS_base) $$(LDFLAGS_$(1)) $$(LDFLAGS)
build_$(1)/$$(prog): $$(objects_$(1)) Makefile
$$(LINK.o) $$(filter-out Makefile,$$^) $$(LDLIBS) -o $$@
build_$(1)/$$(prog): | build_$(1)
endef

$(foreach mode,$(modes),$(eval $(call template,$(mode))))
Expand All @@ -57,5 +57,5 @@ uninstall:
$(DESTDIR)$(prefix)/bin/$(prog) \
$(DESTDIR)$(prefix)/lib/systemd/system/$(prog).service

.PHONY: all clean compile_commands.json install uninstall $(modes)
.PHONY: all clean install uninstall $(modes)
.DELETE_ON_ERROR:
77 changes: 37 additions & 40 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
#include <cerrno>
#include <concepts>
#include <cstddef>
#include <iostream>
#include <print>
#include <regex>
#include <string_view>
#include <system_error>

#include <fcntl.h>
#include <poll.h>
#include <signal.h>
Expand All @@ -7,13 +16,6 @@
#include <sys/wait.h>
#include <unistd.h>

#include <cerrno>
#include <concepts>
#include <iostream>
#include <regex>
#include <string_view>
#include <system_error>


const std::regex WIFI_CRASHED_RE(
R"(;ath10k_pci.*(?:could not init core|failed to pop paddr list))",
Expand All @@ -26,9 +28,8 @@ bool reportError(std::string_view action, int code)
return false;
}

std::cerr << "Failed to " << action << ": "
<< std::error_code(code, std::system_category()).message()
<< '\n';
std::println(std::cerr, "Failed to {}: {}", action,
std::error_code(code, std::system_category()).message());

return true;
}
Expand All @@ -46,12 +47,12 @@ struct SpawnAttrGuard {

void doRunCommand(const char* const argv[])
{
posix_spawnattr_t attr;
posix_spawnattr_t attr{};
if (reportError("init spawn attr", posix_spawnattr_init(&attr))) {
return;
}
SpawnAttrGuard attrGuard{&attr};
sigset_t noSigs;
sigset_t noSigs{};
sigemptyset(&noSigs);
reportError("set spawn attr sigmask",
posix_spawnattr_setsigmask(&attr, &noSigs));
Expand All @@ -65,23 +66,21 @@ void doRunCommand(const char* const argv[])
}

int wstatus{};
rc = waitpid(pid, &wstatus, 0);
if (rc == -1) {
if (waitpid(pid, &wstatus, 0) == -1) {
reportError("wait for sub-process", errno);
}
else if (WIFSIGNALED(wstatus)) {
const int sig = WTERMSIG(wstatus);
std::cerr << "sub-process died of SIG" << sigabbrev_np(sig) << " ("
<< sig << "): " << sigdescr_np(sig) << '\n';
std::println(std::cerr, "sub-process died of SIG{} ({}): {}",
sigabbrev_np(sig), sig, sigdescr_np(sig));
}
else if (WIFEXITED(wstatus)) {
const int rc = WEXITSTATUS(wstatus);
if (rc != 0) {
std::cerr << "sub-process error: rc=" << rc << '\n';
if (const int rc = WEXITSTATUS(wstatus); rc != 0) {
std::println(std::cerr, "sub-process error: rc={}", rc);
}
}
else {
std::cerr << "sub-process died abnormally: " << wstatus << '\n';
std::println(std::cerr, "sub-process died abnormally: {}", wstatus);
}
}

Expand All @@ -94,7 +93,7 @@ void runCommand(const std::convertible_to<const char*> auto&... args)
void processEntry(std::string_view msg)
{
if (std::regex_search(msg.begin(), msg.end(), WIFI_CRASHED_RE)) {
std::cout << "Reloading ath10k_pci kernel module" << std::endl;
std::println("Reloading ath10k_pci kernel module");
runCommand("modprobe", "-r", "ath10k_pci");
runCommand("modprobe", "ath10k_pci");
}
Expand All @@ -120,20 +119,18 @@ int main()
}
FdGuard kmsgFdGuard{kmsgFd};

const off_t offset = lseek(kmsgFd, 0, SEEK_END);
if (offset == -1) {
if (lseek(kmsgFd, 0, SEEK_END) == -1) {
reportError("seek to end of /dev/kmsg", errno);
return 1;
}

std::cout << "Monitoring kernel log for ath10k_pci trouble..." << std::endl;
std::println("Monitoring kernel log for ath10k_pci trouble...");

sigset_t sigs;
sigset_t sigs{};
sigemptyset(&sigs);
sigaddset(&sigs, SIGINT);
sigaddset(&sigs, SIGTERM);
int rc = sigprocmask(SIG_SETMASK, &sigs, nullptr);
if (rc == -1) {
if (sigprocmask(SIG_SETMASK, &sigs, nullptr) == -1) {
reportError("set signal mask", errno);
return 1;
}
Expand All @@ -148,37 +145,37 @@ int main()
while (true) {
pollfd pollFds[] = { {.fd = kmsgFd, .events = POLLIN, .revents = 0 },
{.fd = sigFd, .events = POLLIN, .revents = 0 } };
rc = poll(pollFds, sizeof(pollFds)/sizeof(pollFds[0]), -1);
if (rc == -1) {
if (poll(pollFds, std::size(pollFds), -1) == -1) {
reportError("poll", errno);
return 1;
}

if (pollFds[1].revents & POLLIN) {
signalfd_siginfo sigInfo;
signalfd_siginfo sigInfo{};
const ssize_t sz = read(sigFd, &sigInfo, sizeof sigInfo);
if (sz == sizeof sigInfo) {
std::cout << "Caught SIG" << sigabbrev_np(sigInfo.ssi_signo)
<< "; exiting" << std::endl;
std::println("Caught SIG{}; exiting",
sigabbrev_np(sigInfo.ssi_signo));
return 0;
}
else if (sz >= 0) {
std::cerr << "Caught signal but read unexpected number of "
"bytes from signalfd (" << sz << " != "
<< sizeof(sigInfo) << "); exiting\n";
std::println(std::cerr,
"Caught signal but read unexpected number of "
"bytes from signalfd ({} != {}); exiting",
sz, sizeof sigInfo);
return 1;
}
else {
reportError("read from signalfd", errno);
std::cerr << "Exiting\n";
std::println(std::cerr, "Exiting");
return 1;
}
}

for (const auto& pfd : pollFds) {
if ((pfd.revents & (POLLERR | POLLHUP)) != 0) {
std::cerr << "poll error for fd " << pfd.fd << ": "
<< pfd.revents << "; exiting\n";
std::println(std::cerr, "poll error for fd {}: {}; exiting",
pfd.fd, pfd.revents);
return 1;
}
}
Expand All @@ -191,10 +188,10 @@ int main()
char buf[8192];
const ssize_t sz = read(kmsgFd, buf, sizeof buf);
if (sz > 0) {
processEntry({buf, size_t(sz)});
processEntry({buf, std::size_t(sz)});
}
else if (sz == 0) {
std::cerr << "/dev/kmsg unexpectedly reached EOF\n";
std::println(std::cerr, "/dev/kmsg unexpectedly reached EOF");
return 1;
}
else if (errno == EAGAIN || errno == EPIPE) {
Expand Down

0 comments on commit 974b2b1

Please sign in to comment.