-
Notifications
You must be signed in to change notification settings - Fork 13
/
dsblocks.c
263 lines (232 loc) · 7.83 KB
/
dsblocks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include "shared.h"
#define LOCKFILE "/var/local/dsblocks/dsblocks.pid"
#define DELIMITERLENGTH (sizeof delimiter)
#define STATUSLENGTH (LENGTH(blocks) * (BLOCKLENGTH + DELIMITERLENGTH) + 1)
#include "config.h"
_Static_assert(INTERVALs >= 0, "INTERVALs must be greater than or equal to 0");
_Static_assert(INTERVALn >= 0 && INTERVALn <= 999999999, "INTERVALn must be between 0 and 999999999");
void cleanup(void);
static void buttonhandler(int sig, siginfo_t *info, void *ucontext);
static void setupsignals(void);
static void sighandler(int sig, siginfo_t *info, void *ucontext);
static void statusloop(void);
static void termhandler(int sig);
static void updateblock(Block *block, int sigval);
static void updatestatus(void);
static void writepid(void);
pid_t pid;
static Block *dirtyblock;
static Display *dpy;
static sigset_t blocksigmask;
void
buttonhandler(int sig, siginfo_t *info, void *ucontext)
{
sig = info->si_value.sival_int >> 8;
for (Block *block = blocks; block->funcu; block++)
if (block->signal == sig)
switch (fork()) {
case -1:
perror("buttonhandler - fork");
break;
case 0:
block->funcc(info->si_value.sival_int & 0xff);
exit(0);
}
}
void
cleanup(void)
{
unlink(LOCKFILE);
XStoreName(dpy, DefaultRootWindow(dpy), "");
XCloseDisplay(dpy);
}
void
setupsignals(void)
{
struct sigaction sa;
/* populate blocksigmask and check validity of signals */
sigemptyset(&blocksigmask);
sigaddset(&blocksigmask, SIGHUP);
sigaddset(&blocksigmask, SIGINT);
sigaddset(&blocksigmask, SIGTERM);
for (Block *block = blocks; block->funcu; block++) {
if (block->signal <= 0)
continue;
if (block->signal > SIGRTMAX - SIGRTMIN) {
fprintf(stderr, "Error: SIGRTMIN + %d exceeds SIGRTMAX.\n", block->signal);
unlink(LOCKFILE);
XCloseDisplay(dpy);
exit(2);
}
sigaddset(&blocksigmask, SIGRTMIN + block->signal);
}
/* setup signal handlers */
/* to handle HUP, INT and TERM */
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sa.sa_handler = termhandler;
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
/* to ignore unused realtime signals */
// sa.sa_flags = SA_RESTART;
// sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
for (int i = SIGRTMIN + 1; i <= SIGRTMAX; i++)
sigaction(i, &sa, NULL);
/* to prevent forked children from becoming zombies */
sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART;
// sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &sa, NULL);
/* to handle signals generated by dwm on click events */
sa.sa_flags = SA_RESTART | SA_SIGINFO;
// sigemptyset(&sa.sa_mask);
sa.sa_sigaction = buttonhandler;
sigaction(SIGRTMIN, &sa, NULL);
/* to handle update signals for individual blocks */
sa.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
sa.sa_mask = blocksigmask;
sa.sa_sigaction = sighandler;
for (Block *block = blocks; block->funcu; block++)
if (block->signal > 0)
sigaction(SIGRTMIN + block->signal, &sa, NULL);
}
void
sighandler(int sig, siginfo_t *info, void *ucontext)
{
sig -= SIGRTMIN;
for (Block *block = blocks; block->funcu; block++)
if (block->signal == sig)
updateblock(block, info->si_value.sival_int);
updatestatus();
}
void
statusloop(void)
{
int i;
struct timespec t;
sigprocmask(SIG_BLOCK, &blocksigmask, NULL);
for (Block *block = blocks; block->funcu; block++)
if (block->interval >= 0)
updateblock(block, STRT);
for (i = 1; ; i++) {
updatestatus();
sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL);
t.tv_sec = INTERVALs, t.tv_nsec = INTERVALn;
while (nanosleep(&t, &t) == -1);
sigprocmask(SIG_BLOCK, &blocksigmask, NULL);
for (Block *block = blocks; block->funcu; block++)
if (block->interval > 0 && i % block->interval == 0)
updateblock(block, RTNE);
}
}
void
termhandler(int sig)
{
cleanup();
exit(0);
}
void
updateblock(Block *block, int sigval)
{
size_t l;
if (!(l = block->funcu(block->curtext, sigval)))
return;
if (memcmp(block->curtext, block->prvtext, l) != 0) {
memcpy(block->prvtext, block->curtext, l);
if (!dirtyblock || block < dirtyblock)
dirtyblock = block;
}
if (l == 1)
block->length = 0;
else {
if (block->funcc)
block->curtext[l - 1] = block->signal;
else
l--;
memcpy(block->curtext + l, delimiter, DELIMITERLENGTH);
block->length = l + DELIMITERLENGTH;
}
}
void
updatestatus(void)
{
static char statustext[STATUSLENGTH];
char *s = statustext;
Block *block;
if (!dirtyblock)
return;
for (block = blocks; block < dirtyblock; block++)
s += block->length;
for (; block->funcu; block++) {
memcpy(s, block->curtext, block->length);
s += block->length;
}
s[s == statustext ? 0 : -DELIMITERLENGTH] = '\0';
dirtyblock = NULL;
XStoreName(dpy, DefaultRootWindow(dpy), statustext);
XSync(dpy, False);
}
void
writepid(void)
{
int fd;
struct flock fl;
if ((fd = open(LOCKFILE, O_RDWR | O_CREAT | O_CLOEXEC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
perror("writepid - open");
exit(1);
}
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
if (fcntl(fd, F_SETLK, &fl) == -1) {
if (errno == EACCES || errno == EAGAIN) {
close(fd);
fputs("Error: another instance of dsblocks is already running.\n", stderr);
exit(2);
}
perror("writepid - fcntl");
close(fd);
exit(1);
}
if (ftruncate(fd, 0) == -1) {
perror("writepid - ftruncate");
close(fd);
exit(1);
}
if (dprintf(fd, "%ld", (long)pid) < 0) {
perror("writepid - dprintf");
close(fd);
exit(1);
}
}
int
main(int argc, char *argv[])
{
int xfd;
pid = getpid();
writepid();
if (!(dpy = XOpenDisplay(NULL))) {
fputs("Error: could not open display.\n", stderr);
unlink(LOCKFILE);
return 1;
}
xfd = ConnectionNumber(dpy);
fcntl(xfd, F_SETFD, fcntl(xfd, F_GETFD) | FD_CLOEXEC);
setupsignals();
statusloop();
cleanup();
return 0;
}