-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx-agent.c
executable file
·338 lines (291 loc) · 9.71 KB
/
x-agent.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* x-agent.c --- Simple session agent for Xorg.
*
* Copyright (C) 2005,2016 Zajcev Evgeny <zevlg@yandex.ru>
*
** Author: Zajcev Evgeny <zevlg@yandex.ru>
** Created: Sun Feb 13 01:38:54 MSK 2005
** Keywords: Xorg
*
** Comentary:
*
* x-agent is handy when developing window manager, or in situations
* when using unstable window manager. It allows restarting window
* manager without restarting Xorg.
*
* When x-agent starts it install magic keys (C-Sh-F6 and C-Sh-F11)
* by default and starts <WM>. Whenever you want to restart
* <WM> - press one of the magic keys. You can also force
* <WM> restart by killing x-agent with SIGHUP signal.
*
* C-Sh-F6 - Kill <WM> with SIGABORT signal, this will cause
* <WM> to dump core for futher investigation.
* C-Sh-F9 - Kill <WM> with SIGKILL signal, this will cause
* <WM> to always exit.
* C-Sh-F11 - Kill <WM> with SIGTERM signal, this will cause
* <WM> to terminate normally.
* C-Sh-ESC - Exit x-agent.
*
** Usage:
*
* Put x-agent in PATH
*
* Add into your ~/.xinitrc:
*
* exec x-agent -f <log-file> <yourwm>
*
*/
#include <X11/X.h>
#include <X11/Xlib.h>
#define XK_MISCELLANY
#include <X11/keysymdef.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <time.h>
#include <signal.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sysexits.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
Display *xdpy;
pid_t wmpid = -1; /* <WM> pid */
enum {
VERBOSE_NONE,
VERBOSE_STDOUT,
VERBOSE_X,
VERBOSE_BOTH
};
int verbose = VERBOSE_BOTH;
int autodetect = 1;
char *wm;
char **wm_argv;
char *outfile = NULL;
enum {
STATE_WAITING,
STATE_RUNNING,
STATE_KILLING
};
static int state = STATE_WAITING; /* x-agent current state */
/*
* Evil hackery do display verbose logs
*/
int
xverbose(const char *fmt, ...)
{
#define VSTRLEN 256
static char vstr[VSTRLEN+1];
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsnprintf(vstr, VSTRLEN, fmt, ap);
va_end(ap);
if (verbose == VERBOSE_STDOUT || verbose == VERBOSE_BOTH)
printf("%s\n", vstr);
if (verbose == VERBOSE_X || verbose == VERBOSE_BOTH) {
static int y = 13;
XDrawImageString(xdpy, RootWindow(xdpy, DefaultScreen(xdpy)),
DefaultGC(xdpy, DefaultScreen(xdpy)),
0, y, vstr, strlen(vstr));
y += 13;
if (y > 600) {
XClearWindow(xdpy, RootWindow(xdpy, DefaultScreen(xdpy)));
y = 13;
}
XFlush(xdpy);
}
return ret;
}
pid_t
start_wm()
{
if (wmpid > 0) {
/* Already running WM */
xverbose(" - %s already running, not starting", wm);
return -1;
}
if ((wmpid = fork()) == 0) {
/* Direct <WM>'s stdout/stderr to file */
if (outfile) {
int fd = open(outfile, O_CREAT|O_WRONLY|O_APPEND,
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (fd > 0) {
time_t clock = time(NULL);
char *ts = ctime(&clock);
write(fd, "------------[ ", 14);
write(fd, ts, strlen(ts));
close(STDOUT_FILENO);
close(STDERR_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
}
}
/* Remove signal handlers */
signal(SIGCHLD, SIG_DFL);
signal(SIGHUP, SIG_DFL);
execvp(wm, wm_argv);
/* Execve failed :( */
fprintf(stderr, " - execve failed: %s\n", strerror(errno));
exit(EX_UNAVAILABLE);
/* NOT REACHED */
}
state = STATE_RUNNING;
xverbose(" + Starting %s pid=%d ..", wm, wmpid);
return wmpid;
}
void
restart_wm(int sig)
{
xverbose("+ Restarting %s ..", wm);
/* Kill wm */
if (wmpid > 0) {
xverbose(" + Killing %s pid=%d sig=%d ..", wm, wmpid, sig);
state = STATE_KILLING;
kill(wmpid, sig);
} else {
start_wm();
}
}
void
wm_rip(int sig)
{
pid_t pid;
xverbose("+ SIGCHLD received ..");
while ((pid = waitpid(wmpid, NULL, WNOHANG)) > 0) {
xverbose(" + Riping %s pid=%d ..", wm, pid);
if (pid == wmpid)
wmpid = -1;
}
XSetInputFocus(xdpy, PointerRoot, RevertToPointerRoot, CurrentTime);
xverbose(" + InputFocus set to PointerRoot ..");
if ((state == STATE_KILLING) || autodetect) {
if (start_wm() < 0)
state = STATE_WAITING;
} else
state = STATE_WAITING;
}
void
wm_hup(int sig)
{
xverbose("+ SIGHUP received ..");
restart_wm(SIGTERM);
}
void
usage(const char *prog)
{
printf("usage: %s [-on] [-f <outfile>] [-v X|stdout|both|none] "
"-- <WM> [wm arguments]\n"
"\t-o\tOmit starting <WM> on start.\n"
"\t-n\tDo not autostart new <WM> when old exits.\n"
"\t-f\tSpecify output file."
" <WM> will direct its output to this file.\n"
"\t-v\tEnable/disable verbosity type. Default is 'both'\n",
prog);
exit(EX_USAGE);
}
int
main(int argc, char **argv)
{
const char *prog = argv[0];
int o_flag = 0;
int ch;
KeyCode exit_kc, abort_kc, term_kc, kill_kc;
while ((ch = getopt(argc, argv, "onv:f:")) != -1) {
switch (ch) {
case 'o':
o_flag = 1;
break;
case 'v':
if (!strcmp(optarg, "X"))
verbose = VERBOSE_X;
else if (!strcmp(optarg, "stdout"))
verbose = VERBOSE_STDOUT;
else if (!strcmp(optarg, "both"))
verbose = VERBOSE_BOTH;
else if (!strcmp(optarg, "none"))
verbose = VERBOSE_NONE;
else
usage(prog);
break;
case 'n':
autodetect = 0;
break;
case 'f':
outfile = optarg;
break;
case '?':
default:
usage(prog);
}
}
argc -= optind;
argv += optind;
if (argc < 1)
usage(prog);
/* Setup wm arguments */
wm = argv[0];
wm_argv = argv;
xdpy = XOpenDisplay(NULL);
if (xdpy == NULL) {
fprintf(stderr, "- Can't open display\n");
exit(EX_NOINPUT);
}
/* Install signal handler to rip childs */
signal(SIGCHLD, wm_rip);
signal(SIGHUP, wm_hup);
xverbose("+ X Initialisation ..");
XSetInputFocus(xdpy, RootWindow(xdpy, DefaultScreen(xdpy)),
RevertToPointerRoot, CurrentTime);
xverbose(" + InputFocus set to root window ..");
/* Grab magic key */
exit_kc = XKeysymToKeycode(xdpy, XK_Escape);
XGrabKey(xdpy, exit_kc, ShiftMask|ControlMask,
RootWindow(xdpy, DefaultScreen(xdpy)), True,
GrabModeAsync, GrabModeAsync);
abort_kc = XKeysymToKeycode(xdpy, XK_F6);
XGrabKey(xdpy, abort_kc, ShiftMask|ControlMask,
RootWindow(xdpy, DefaultScreen(xdpy)), True,
GrabModeAsync, GrabModeAsync);
term_kc = XKeysymToKeycode(xdpy, XK_F11);
XGrabKey(xdpy, term_kc, ShiftMask|ControlMask,
RootWindow(xdpy, DefaultScreen(xdpy)), True,
GrabModeAsync, GrabModeAsync);
kill_kc = XKeysymToKeycode(xdpy, XK_F9);
XGrabKey(xdpy, kill_kc, ShiftMask|ControlMask,
RootWindow(xdpy, DefaultScreen(xdpy)), True,
GrabModeAsync, GrabModeAsync);
XFlush(xdpy);
xverbose(" + Magic keys at C-Sh-ESC(exit), C-Sh-F6(SIGABORT),"
" C-Sh-F9(SIGKILL) and C-Sh-F11(SIGTERM) ..");
state = STATE_WAITING;
if (o_flag == 0)
start_wm();
/* Event loop */
while (1) {
XEvent xev;
XNextEvent(xdpy, &xev);
switch (xev.type) {
case KeyPress:
xverbose("+ Magic KeyPress %s pid=%d ..", wm, wmpid);
if (xev.xkey.keycode == exit_kc) {
xverbose(" + Exiting ..");
XCloseDisplay(xdpy);
exit(EX_OK);
} else if (xev.xkey.keycode == abort_kc)
restart_wm(SIGABRT);
else if (xev.xkey.keycode == term_kc)
restart_wm(SIGTERM);
else if (xev.xkey.keycode == kill_kc)
restart_wm(SIGKILL);
else
xverbose(" - Unrecognized keycode 0x%X ..",
xev.xkey.keycode);
break;
}
}
return 0;
}