make bulk export commands gracefully exit on SIGPIPE (for example, if piped to head)

This commit is contained in:
Doug Hoyte
2023-07-25 00:37:56 -04:00
parent 80915f969d
commit 0fe929ffcb
6 changed files with 15 additions and 1 deletions

View File

@ -1,5 +1,6 @@
#include <arpa/inet.h>
#include <stdio.h>
#include <signal.h>
#include <algorithm>
#include <string>
@ -109,3 +110,10 @@ std::string padBytes(std::string_view str, size_t n, char padChar) {
if (str.size() > n) throw herr("unable to pad, string longer than expected");
return std::string(str) + std::string(n - str.size(), padChar);
}
void exitOnSigPipe() {
struct sigaction act;
memset(&act, 0, sizeof act);
act.sa_sigaction = [](int, siginfo_t*, void*){ ::exit(1); };
if (sigaction(SIGPIPE, &act, nullptr)) throw herr("couldn't run sigaction(): ", strerror(errno));
}