-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwmblocks.c
executable file
·310 lines (277 loc) · 10.3 KB
/
dwmblocks.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#define NILL INT_MIN
#define LOCKFILE "/var/local/dwmblocks/dwmblocks.pid"
#define LENGTH(X) (sizeof X / sizeof X[0])
#define DELIMITERLENGTH (sizeof delimiter)
#define STATUSLENGTH (LENGTH(blocks) * (CMDOUTLENGTH + 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");
static void buttonhandler(int sig, siginfo_t *info, void *ucontext);
static void cleanup();
static void setupsignals();
static void sighandler(int sig, siginfo_t *si, void *ucontext);
static void statusloop();
static void termhandler(int sig);
static void updateblock(Block *block, int sigval);
static void updatestatus();
static void writepid();
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->pathu; block++)
if (block->signal == sig)
switch (fork()) {
case -1:
perror("buttonhandler - fork");
break;
case 0:
{
char button[] = { '0' + (info->si_value.sival_int & 0xff), '\0' };
char *arg[] = { block->pathc, button, NULL };
close(ConnectionNumber(dpy));
setsid();
execv(arg[0], arg);
perror("buttonhandler - child - execv");
_exit(127);
}
}
}
void
cleanup()
{
unlink(LOCKFILE);
XStoreName(dpy, DefaultRootWindow(dpy), "");
XCloseDisplay(dpy);
}
void
setupsignals()
{
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->pathu; 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_DFL;
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->pathu; 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->pathu; block++)
if (block->signal == sig)
updateblock(block, info->si_value.sival_int);
updatestatus();
}
void
statusloop()
{
int i;
struct timespec t;
sigprocmask(SIG_BLOCK, &blocksigmask, NULL);
for (Block *block = blocks; block->pathu; block++)
if (block->interval >= 0)
updateblock(block, NILL);
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->pathu; block++)
if (block->interval > 0 && i % block->interval == 0)
updateblock(block, NILL);
}
}
void
termhandler(int sig)
{
cleanup();
exit(0);
}
void
updateblock(Block *block, int sigval)
{
int fd[2];
if (pipe(fd) == -1) {
perror("updateblock - pipe");
cleanup();
exit(1);
}
switch (fork()) {
case -1:
perror("updateblock - fork");
cleanup();
exit(1);
case 0:
close(ConnectionNumber(dpy));
close(fd[0]);
if (fd[1] != STDOUT_FILENO) {
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
perror("updateblock - child - dup2");
exit(1);
}
close(fd[1]);
}
if (sigval == NILL) {
char *arg[] = { block->pathu, NULL };
execv(arg[0], arg);
} else {
char buf[12];
char *arg[] = { block->pathu, buf, NULL };
snprintf(buf, sizeof buf, "%d", sigval);
execv(arg[0], arg);
}
perror("updateblock - child - execv");
_exit(127);
default:
{
size_t trd = 0;
ssize_t rd;
close(fd[1]);
do
rd = read(fd[0], block->curtext + trd, CMDOUTLENGTH - trd);
while (rd > 0 && (trd += rd) < CMDOUTLENGTH);
if (rd == -1) {
perror("updateblock - read");
cleanup();
exit(1);
}
close(fd[0]);
block->curtext[trd] = '\0';
if (memcmp(block->curtext, block->prvtext, trd + 1) != 0) {
memcpy(block->prvtext, block->curtext, trd + 1);
if (!dirtyblock || block < dirtyblock)
dirtyblock = block;
}
if (trd == 0)
block->length = 0;
else {
if (block->curtext[trd - 1] == '\n')
trd--;
if (block->pathc)
block->curtext[trd++] = block->signal;
memcpy(block->curtext + trd, delimiter, DELIMITERLENGTH);
block->length = trd + DELIMITERLENGTH;
}
}
}
}
void
updatestatus()
{
static char statustext[STATUSLENGTH];
char *s = statustext;
Block *block;
if (!dirtyblock)
return;
for (block = blocks; block < dirtyblock; block++)
s += block->length;
for (; block->pathu; 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()
{
int fd;
struct flock fl;
if ((fd = open(LOCKFILE, O_RDWR | O_CREAT, 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) {
fputs("Error: another instance of dwmblocks is already running.\n", stderr);
exit(2);
}
perror("writepid - fcntl");
exit(1);
}
if (ftruncate(fd, 0) == -1) {
perror("writepid - ftruncate");
exit(1);
}
if (dprintf(fd, "%ld", (long)getpid()) < 0) {
perror("writepid - dprintf");
exit(1);
}
}
int
main(int argc, char *argv[])
{
writepid();
if (!(dpy = XOpenDisplay(NULL))) {
fputs("Error: could not open display.\n", stderr);
unlink(LOCKFILE);
return 1;
}
setupsignals();
statusloop();
cleanup();
return 0;
}