forked from vivien/i3blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.c
299 lines (247 loc) · 7.39 KB
/
block.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
/*
* block.c - update of a single status line block
* Copyright (C) 2014 Vivien Didelot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "block.h"
#include "click.h"
#include "log.h"
static void
child_setenv(struct block *block, const char *name, const char *value)
{
if (setenv(name, value, 1) == -1) {
berrorx(block, "setenv(%s=%s)", name, value);
_exit(EXIT_ERR_INTERNAL);
}
}
static void
child_setup_env(struct block *block, struct click *click)
{
child_setenv(block, "BLOCK_NAME", NAME(block));
child_setenv(block, "BLOCK_INSTANCE", INSTANCE(block));
child_setenv(block, "BLOCK_BUTTON", click ? click->button : "");
child_setenv(block, "BLOCK_X", click ? click->x : "");
child_setenv(block, "BLOCK_Y", click ? click->y : "");
}
static void
child_reset_signals(struct block *block)
{
sigset_t set;
if (sigfillset(&set) == -1) {
berrorx(block, "sigfillset");
_exit(EXIT_ERR_INTERNAL);
}
/* It should be safe to assume that all signals are unblocked by default */
if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) {
berrorx(block, "sigprocmask");
_exit(EXIT_ERR_INTERNAL);
}
}
static void
child_redirect_write(struct block *block, int pipe[2], int fd)
{
if (close(pipe[0]) == -1) {
berrorx(block, "close pipe read end");
_exit(EXIT_ERR_INTERNAL);
}
/* Defensive check */
if (pipe[1] == fd)
return;
if (dup2(pipe[1], fd) == -1) {
berrorx(block, "dup pipe write end");
_exit(EXIT_ERR_INTERNAL);
}
if (close(pipe[1]) == -1) {
berrorx(block, "close pipe write end");
_exit(EXIT_ERR_INTERNAL);
}
}
static void
child_exec(struct block *block)
{
static const char * const shell = "/bin/sh";
execl(shell, shell, "-c", COMMAND(block), (char *) NULL);
/* Unlikely to reach this point */
berrorx(block, "exec(%s -c %s)", shell, COMMAND(block));
_exit(EXIT_ERR_INTERNAL);
}
static void
linecpy(char **lines, char *dest, unsigned int size)
{
char *newline = strchr(*lines, '\n');
/* split if there's a newline */
if (newline)
*newline = '\0';
/* if text in non-empty, copy it */
if (**lines) {
strncpy(dest, *lines, size);
*lines += strlen(dest);
}
/* increment if next char is non-null */
if (*(*lines + 1))
*lines += 1;
}
static void
mark_as_failed(struct block *block, const char *reason)
{
if (log_level < LOG_WARN)
return;
struct properties *props = &block->updated_props;
memset(props, 0, sizeof(struct properties));
snprintf(props->full_text, sizeof(props->full_text), "[%s] %s", NAME(block), reason);
snprintf(props->short_text, sizeof(props->short_text), "[%s] ERROR", NAME(block));
strcpy(props->color, "#FF0000");
strcpy(props->urgent, "true");
}
void
block_spawn(struct block *block, struct click *click)
{
const unsigned long now = time(NULL);
int out[2], err[2];
if (!*COMMAND(block)) {
bdebug(block, "no command, skipping");
return;
}
if (block->pid > 0) {
bdebug(block, "process already spawned");
return;
}
if (pipe(out) == -1 || pipe(err) == -1) {
berrorx(block, "pipe");
return mark_as_failed(block, strerror(errno));
}
block->pid = fork();
if (block->pid == -1) {
berrorx(block, "fork");
return mark_as_failed(block, strerror(errno));
}
/* Child? */
if (block->pid == 0) {
/* Error messages are merged into the parent's stderr... */
child_setup_env(block, click);
child_reset_signals(block);
child_redirect_write(block, out, STDOUT_FILENO);
child_redirect_write(block, err, STDERR_FILENO);
/* ... until here */
child_exec(block);
}
/*
* Note: no need to set the pipe read end as non-blocking, since it is
* meant to be read once the child has exited (and thus the write end is
* closed and read is available).
*/
/* Parent */
if (close(out[1]) == -1)
berrorx(block, "close stdout");
if (close(err[1]) == -1)
berrorx(block, "close stderr");
block->out = out[0];
block->err = err[0];
if (!click)
block->timestamp = now;
bdebug(block, "forked child %d at %ld", block->pid, now);
}
void
block_reap(struct block *block)
{
struct properties *props = &block->updated_props;
char buf[2048] = { 0 };
char *lines = buf;
int status, code;
if (block->pid <= 0) {
bdebug(block, "not spawned yet");
return;
}
if (waitpid(block->pid, &status, 0) == -1) {
berrorx(block, "waitpid(%d)", block->pid);
mark_as_failed(block, strerror(errno));
goto close;
}
code = WEXITSTATUS(status);
bdebug(block, "process %d exited with %d", block->pid, code);
/* Note read(2) returns 0 for end-of-pipe */
if (read(block->err, buf, sizeof(buf)) == -1) {
berrorx(block, "read stderr");
mark_as_failed(block, strerror(errno));
goto close;
}
if (*buf) {
bdebug(block, "stderr:\n{\n%s\n}", buf);
memset(buf, 0, sizeof(buf));
}
if (code != 0 && code != EXIT_URGENT) {
char reason[32];
if (code == EXIT_ERR_INTERNAL)
sprintf(reason, "internal error");
else
sprintf(reason, "bad exit code %d", code);
berror(block, "%s", reason);
mark_as_failed(block, reason);
goto close;
}
/* Note read(2) returns 0 for end-of-pipe */
if (read(block->out, buf, sizeof(buf)) == -1) {
berrorx(block, "read stdout");
mark_as_failed(block, strerror(errno));
goto close;
}
/* The update went ok, so reset the defaults and merge the output */
memcpy(props, &block->default_props, sizeof(struct properties));
strncpy(props->urgent, code == EXIT_URGENT ? "true" : "false", sizeof(props->urgent) - 1);
linecpy(&lines, props->full_text, sizeof(props->full_text) - 1);
linecpy(&lines, props->short_text, sizeof(props->short_text) - 1);
linecpy(&lines, props->color, sizeof(props->color) - 1);
if (*FULL_TEXT(block) && *LABEL(block)) {
static const size_t size = sizeof(props->full_text);
char concat[size];
snprintf(concat, size, "%s %s", LABEL(block), FULL_TEXT(block));
strcpy(props->full_text, concat);
}
bdebug(block, "updated successfully");
close:
if (close(block->out) == -1)
berrorx(block, "close stdout");
if (close(block->err) == -1)
berrorx(block, "close stderr");
block->pid = 0;
}
void block_setup(struct block *block)
{
struct properties *defaults = &block->default_props;
struct properties *updated = &block->updated_props;
/* Convenient shortcuts */
if (strcmp(defaults->interval, "once") == 0)
block->interval = INTER_ONCE;
else if (strcmp(defaults->interval, "repeat") == 0)
block->interval = INTER_REPEAT;
else
block->interval = atoi(defaults->interval);
block->signal = atoi(defaults->signal);
/* First update (for static blocks and loading labels) */
memcpy(updated, defaults, sizeof(struct properties));
#define PLACEHOLDERS(_name, _size, _flags) " %s: \"%s\"\n"
#define ARGS(_name, _size, _flags) #_name, updated->_name,
debug("\n{\n" PROPERTIES(PLACEHOLDERS) "%s", PROPERTIES(ARGS) "}");
#undef ARGS
#undef PLACEHOLDERS
}