Replaced signal handlers with signalfd + pool
This commit is contained in:
parent
30fe169632
commit
c3fef3b7ee
1 changed files with 56 additions and 55 deletions
109
dwmblocks.c
109
dwmblocks.c
|
@ -6,6 +6,8 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
|
#include <sys/signalfd.h>
|
||||||
|
#include <poll.h>
|
||||||
#define LENGTH(X) (sizeof(X) / sizeof (X[0]))
|
#define LENGTH(X) (sizeof(X) / sizeof (X[0]))
|
||||||
#define CMDLENGTH 50
|
#define CMDLENGTH 50
|
||||||
|
|
||||||
|
@ -15,16 +17,13 @@ typedef struct {
|
||||||
unsigned int interval;
|
unsigned int interval;
|
||||||
unsigned int signal;
|
unsigned int signal;
|
||||||
} Block;
|
} Block;
|
||||||
void sighandler(int num);
|
void sighandler();
|
||||||
void buttonhandler(int sig, siginfo_t *si, void *ucontext);
|
void buttonhandler(int ssi_int);
|
||||||
void replace(char *str, char old, char new);
|
void replace(char *str, char old, char new);
|
||||||
void remove_all(char *str, char to_remove);
|
void remove_all(char *str, char to_remove);
|
||||||
void getcmds(int time);
|
void getcmds(int time);
|
||||||
#ifndef __OpenBSD__
|
|
||||||
void getsigcmds(int signal);
|
void getsigcmds(int signal);
|
||||||
void setupsignals();
|
void setupsignals();
|
||||||
void sighandler(int signum);
|
|
||||||
#endif
|
|
||||||
int getstatus(char *str, char *last);
|
int getstatus(char *str, char *last);
|
||||||
void setroot();
|
void setroot();
|
||||||
void statusloop();
|
void statusloop();
|
||||||
|
@ -39,6 +38,8 @@ static Window root;
|
||||||
static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
|
static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
|
||||||
static char statusstr[2][256];
|
static char statusstr[2][256];
|
||||||
static int statusContinue = 1;
|
static int statusContinue = 1;
|
||||||
|
static int signalFD;
|
||||||
|
static int timerInterval = -1;
|
||||||
static void (*writestatus) () = setroot;
|
static void (*writestatus) () = setroot;
|
||||||
|
|
||||||
void replace(char *str, char old, char new)
|
void replace(char *str, char old, char new)
|
||||||
|
@ -128,7 +129,6 @@ void getcmds(int time)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef __OpenBSD__
|
|
||||||
void getsigcmds(int signal)
|
void getsigcmds(int signal)
|
||||||
{
|
{
|
||||||
const Block *current;
|
const Block *current;
|
||||||
|
@ -143,22 +143,20 @@ void getsigcmds(int signal)
|
||||||
|
|
||||||
void setupsignals()
|
void setupsignals()
|
||||||
{
|
{
|
||||||
struct sigaction sa;
|
sigset_t signals;
|
||||||
|
sigemptyset(&signals);
|
||||||
for(int i = SIGRTMIN; i <= SIGRTMAX; i++)
|
sigaddset(&signals, SIGALRM); // Timer events
|
||||||
signal(i, SIG_IGN);
|
sigaddset(&signals, SIGUSR1); // Button events
|
||||||
|
// All signals assigned to blocks
|
||||||
for(int i = 0; i < LENGTH(blocks); i++)
|
for (size_t i = 0; i < LENGTH(blocks); i++)
|
||||||
{
|
|
||||||
if (blocks[i].signal > 0)
|
if (blocks[i].signal > 0)
|
||||||
{
|
sigaddset(&signals, SIGRTMIN + blocks[i].signal);
|
||||||
signal(SIGRTMIN+blocks[i].signal, sighandler);
|
// Create signal file descriptor for pooling
|
||||||
sigaddset(&sa.sa_mask, SIGRTMIN+blocks[i].signal);
|
signalFD = signalfd(-1, &signals, 0);
|
||||||
}
|
// Block all real-time signals
|
||||||
}
|
for (int i = SIGRTMIN; i <= SIGRTMAX; i++) sigaddset(&signals, i);
|
||||||
sa.sa_sigaction = buttonhandler;
|
sigprocmask(SIG_BLOCK, &signals, NULL);
|
||||||
sa.sa_flags = SA_SIGINFO;
|
// Do not transform children into zombies
|
||||||
sigaction(SIGUSR1, &sa, NULL);
|
|
||||||
struct sigaction sigchld_action = {
|
struct sigaction sigchld_action = {
|
||||||
.sa_handler = SIG_DFL,
|
.sa_handler = SIG_DFL,
|
||||||
.sa_flags = SA_NOCLDWAIT
|
.sa_flags = SA_NOCLDWAIT
|
||||||
|
@ -166,7 +164,6 @@ void setupsignals()
|
||||||
sigaction(SIGCHLD, &sigchld_action, NULL);
|
sigaction(SIGCHLD, &sigchld_action, NULL);
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
int getstatus(char *str, char *last)
|
int getstatus(char *str, char *last)
|
||||||
{
|
{
|
||||||
|
@ -206,52 +203,57 @@ void pstdout()
|
||||||
|
|
||||||
void statusloop()
|
void statusloop()
|
||||||
{
|
{
|
||||||
#ifndef __OpenBSD__
|
|
||||||
setupsignals();
|
setupsignals();
|
||||||
#endif
|
|
||||||
// first figure out the default wait interval by finding the
|
// first figure out the default wait interval by finding the
|
||||||
// greatest common denominator of the intervals
|
// greatest common denominator of the intervals
|
||||||
unsigned int interval = -1;
|
|
||||||
for(int i = 0; i < LENGTH(blocks); i++){
|
for(int i = 0; i < LENGTH(blocks); i++){
|
||||||
if(blocks[i].interval){
|
if(blocks[i].interval){
|
||||||
interval = gcd(blocks[i].interval, interval);
|
timerInterval = gcd(blocks[i].interval, timerInterval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsigned int i = 0;
|
getcmds(-1); // Fist time run all commands
|
||||||
int interrupted = 0;
|
raise(SIGALRM); // Schedule first timer event
|
||||||
const struct timespec sleeptime = {interval, 0};
|
int ret;
|
||||||
struct timespec tosleep = sleeptime;
|
struct pollfd pfd[] = {{.fd = signalFD, .events = POLLIN}};
|
||||||
getcmds(-1);
|
while (statusContinue) {
|
||||||
while(statusContinue)
|
// Wait for new signal
|
||||||
{
|
ret = poll(pfd, sizeof(pfd) / sizeof(pfd[0]), -1);
|
||||||
// sleep for tosleep (should be a sleeptime of interval seconds) and put what was left if interrupted back into tosleep
|
if (ret < 0 || !(pfd[0].revents & POLLIN)) break;
|
||||||
interrupted = nanosleep(&tosleep, &tosleep);
|
sighandler(); // Handle signal
|
||||||
// if interrupted then just go sleep again for the remaining time
|
|
||||||
if(interrupted == -1){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// if not interrupted then do the calling and writing
|
|
||||||
getcmds(i);
|
|
||||||
writestatus();
|
|
||||||
// then increment since its actually been a second (plus the time it took the commands to run)
|
|
||||||
i += interval;
|
|
||||||
// set the time to sleep back to the sleeptime of 1s
|
|
||||||
tosleep = sleeptime;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef __OpenBSD__
|
void sighandler()
|
||||||
void sighandler(int signum)
|
|
||||||
{
|
{
|
||||||
getsigcmds(signum-SIGRTMIN);
|
static int time = 0;
|
||||||
|
struct signalfd_siginfo si;
|
||||||
|
int ret = read(signalFD, &si, sizeof(si));
|
||||||
|
if (ret < 0) return;
|
||||||
|
int signal = si.ssi_signo;
|
||||||
|
switch (signal) {
|
||||||
|
case SIGALRM:
|
||||||
|
// Execute blocks and schedule the next timer event
|
||||||
|
getcmds(time);
|
||||||
|
alarm(timerInterval);
|
||||||
|
time += timerInterval;
|
||||||
|
break;
|
||||||
|
case SIGUSR1:
|
||||||
|
// Handle buttons
|
||||||
|
buttonhandler(si.ssi_int);
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
// Execute the block that has the given signal
|
||||||
|
getsigcmds(signal - SIGRTMIN);
|
||||||
|
break;
|
||||||
|
}
|
||||||
writestatus();
|
writestatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
void buttonhandler(int sig, siginfo_t *si, void *ucontext)
|
void buttonhandler(int ssi_int)
|
||||||
{
|
{
|
||||||
char button[2] = {'0' + si->si_value.sival_int & 0xff, '\0'};
|
char button[2] = {'0' + ssi_int & 0xff, '\0'};
|
||||||
pid_t process_id = getpid();
|
pid_t process_id = getpid();
|
||||||
sig = si->si_value.sival_int >> 8;
|
int sig = ssi_int >> 8;
|
||||||
if (fork() == 0)
|
if (fork() == 0)
|
||||||
{
|
{
|
||||||
const Block *current;
|
const Block *current;
|
||||||
|
@ -271,12 +273,10 @@ void buttonhandler(int sig, siginfo_t *si, void *ucontext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void termhandler(int signum)
|
void termhandler(int signum)
|
||||||
{
|
{
|
||||||
statusContinue = 0;
|
statusContinue = 0;
|
||||||
exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
|
@ -291,4 +291,5 @@ int main(int argc, char** argv)
|
||||||
signal(SIGTERM, termhandler);
|
signal(SIGTERM, termhandler);
|
||||||
signal(SIGINT, termhandler);
|
signal(SIGINT, termhandler);
|
||||||
statusloop();
|
statusloop();
|
||||||
|
close(signalFD);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue