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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <xs.h>
#include "list.h"
#include "xenstore.h"
#include "apps.h"
/* ------------------------------------------------------------- */
#define BUFSIZE 64
#define PRE_STATE "%16s: "
struct dom {
int domid;
char name[BUFSIZE];
char tty[BUFSIZE];
int connected;
int destroyed;
struct list_head next;
};
static LIST_HEAD(doms);
static int domcnt;
static char *screenrc = "/etc/xen/xenscreenrc";
static char *screen_session = "xencon";
static char *screen_title = "watch";
/* ------------------------------------------------------------- */
static struct dom *find_dom(int domid)
{
struct dom *dom;
struct list_head *item;
list_for_each(item, &doms) {
dom = list_entry(item, struct dom, next);
if (dom->domid == domid)
return dom;
}
return NULL;
}
static struct dom *get_dom(int domid)
{
struct dom *dom;
dom = find_dom(domid);
if (!dom) {
dom = malloc(sizeof(*dom));
memset(dom,0,sizeof(*dom));
dom->domid = domid;
list_add_tail(&dom->next, &doms);
}
return dom;
}
/* ------------------------------------------------------------- */
static int termsig;
static void catchsig(int sig)
{
termsig = sig;
}
static void try_attach_screen(struct dom *dom, int boot)
{
int rc;
if (dom->connected)
return;
if (!strlen(dom->name))
return;
if (!strlen(dom->tty))
return;
fprintf(stderr, PRE_STATE "%s (%d) @ %s\n",
"connecting", dom->name, dom->domid, dom->tty);
if (0 != access(dom->tty, R_OK)) {
fprintf(stderr, " error: no access to tty %s\n", dom->tty);
return;
}
/* known-racy, but better than nothing ... */
rc = run_application(1, "fuser", "fuser", "-s", dom->tty, NULL);
if (0 == rc) {
fprintf(stderr," error: tty %s already in use\n", dom->tty);
return;
}
rc = run_application(1, "screen", "screen",
"-X", "-S", screen_session,
"screen", "-L", "-t", dom->name, dom->tty,
NULL);
#if 0
/*
* Hmm, not exactly the most elegant way to do this, has
* some ugly glitches too.
*
* Switches back from the new window to the previous one.
* Better would be to not switch in the first place,
* seems screen can't do that though :-(
*/
if (!boot)
rc = run_application(1, "screen", "screen",
"-X", "-S", screen_session,
"other",
NULL);
#endif
dom->connected = 1;
domcnt++;
}
static void try_release_domain(struct dom *dom)
{
if (!dom->destroyed)
return;
fprintf(stderr, PRE_STATE "%s (%d)%s\n", "disappeared",
dom->name, dom->domid, dom->connected ? " [conn]" : "");
if (dom->connected)
domcnt--;
list_del(&dom->next);
free(dom);
}
static void builtin_screen_setup(void)
{
static char *config[] = {
"sorendition =s wb",
"hardstatus lastline \"%{=b bw} xen |%{-} %-w%{= yb} %50>%n* %t %{-}%+w%<\"",
"logfile /var/log/xenscreen.%t",
NULL /* EOF */,
};
char cmdline[256];
int line,try,rc;
#if 1
/* FIXME: Hmm, doesn't work reliable without that one ... */
sleep(1);
#endif
for (line = 0; config[line] != NULL; line++) {
for (try = 0; try < 5; try++) {
snprintf(cmdline, sizeof(cmdline), "screen -X -S %s %s",
screen_session, config[line]);
rc = run_cmdline(1,cmdline);
if (0 == rc)
break;
sleep(1);
}
}
}
/* ------------------------------------------------------------- */
int main(int argc, char *argv[])
{
struct sigaction act,old;
fd_set set;
struct xs_handle *xenstore = NULL;
xs_transaction_t xst;
char **vec = NULL;
int domid;
char path[BUFSIZE], value[BUFSIZE];
unsigned int count, i, rc;
struct dom *dom;
time_t last_ctrl_c = 0;
if (!have_application("screen")) {
fprintf(stderr, "screen not installed, exiting\n");
exit(1);
}
if (NULL == getenv("STY") || NULL == strstr(getenv("STY"),screen_session)) {
/* not running inside screen, try to attach */
rc = run_application(1, "screen", "screen",
"-S", screen_session,
"-r", "-p", "=",
NULL);
if (0 == rc)
exit(0);
/* failing that, start a new screen session */
if (0 != access(screenrc, R_OK))
screenrc = "/dev/null";
execlp("screen", "screen",
"-S", screen_session,
"-c", screenrc,
"-t", screen_title, argv[0],
NULL);
perror("execlp(screen)");
exit(1);
} else if (0 != access(screenrc, R_OK)) {
/* no screenrc: do mini setup */
builtin_screen_setup();
}
/* setup signal handler */
memset(&act,0,sizeof(act));
sigemptyset(&act.sa_mask);
act.sa_handler = catchsig;
sigaction(SIGTERM,&act,&old);
sigaction(SIGINT,&act,&old);
fprintf(stderr,
"###\n"
"### Xen consoles in screen\n"
"### This is the watch process\n"
"###\n"
"\n");
/* connect to xenstore */
xenstore = xenstore_open(1,1,1,1);
if (NULL == xenstore) {
fprintf(stderr, "can't access xenstore, exiting\n");
exit(1);
}
xs_watch(xenstore, "/local/domain", "token");
/* look for running domains */
if (!(xst = xs_transaction_start(xenstore))) {
fprintf(stderr,"Oops, can't start xenstore transaction\n");
exit(1);
}
vec = xs_directory(xenstore, xst, "/local/domain", &count);
xs_transaction_end(xenstore, xst, 0);
fprintf(stderr,"looking for existing domains\n");
for (i = 0; i < count; i++) {
domid = atoi(vec[i]);
dom = get_dom(domid);
snprintf(path, sizeof(path), "/local/domain/%d/name", domid);
xenstore_read(xenstore, path, dom->name, sizeof(dom->name));
snprintf(path, sizeof(path), "/local/domain/%d/console/tty", domid);
xenstore_read(xenstore, path, dom->tty, sizeof(dom->tty));
try_attach_screen(dom, 1);
}
rc = run_application(1, "screen", "screen",
"-X", "-S", screen_session,
"select", "0",
NULL);
rc = run_application(1, "screen", "screen",
"-X", "-S", screen_session,
"windowlist", "-b",
NULL);
/* main loop */
fprintf(stderr,"ok, watching out for changes now\n");
for (;;) {
if (termsig) {
if (!domcnt)
break;
if (time(NULL) - last_ctrl_c < 3)
break;
fprintf(stderr,
"\n"
"Got ^C - still %d domain(s) active - not quitting.\n"
"\n"
"You should better use detach instead (^A d).\n"
"Or kill all windows (^A \\) if you don't want\n"
"keep screen hanging around.\n"
"\n"
"Hit ^C within 3 secs again to quit nevertheless.\n"
"\n",
domcnt);
last_ctrl_c = time(NULL);
termsig = 0;
}
FD_ZERO(&set);
FD_SET(xs_fileno(xenstore), &set);
switch (select(xs_fileno(xenstore)+1, &set, NULL, NULL, NULL)) {
case -1:
if (EINTR == errno)
continue; /* termsig check */
perror("select");
break;
case 0:
fprintf(stderr,"Huh, select() timeout?\n");
exit(1);
break;
default:
break;
}
if (vec)
free(vec);
vec = xs_read_watch(xenstore, &count);
if (NULL == vec) {
fprintf(stderr,"xs_read_watch() failed\n");
exit(1);
}
if (2 != sscanf(vec[XS_WATCH_PATH], "/local/domain/%d/%64s", &domid, path)) {
if (1 != sscanf(vec[XS_WATCH_PATH], "/local/domain/%d", &domid))
continue;
strcpy(path, "");
}
dom = get_dom(domid);
if (0 == strcmp(path,"")) {
rc = xenstore_read(xenstore, vec[XS_WATCH_PATH], value, sizeof(value));
if (0 != rc)
dom->destroyed = 1;
} else if (0 == strcmp(path, "console/tty")) {
rc = xenstore_read(xenstore, vec[XS_WATCH_PATH], value, sizeof(value));
if (0 != rc)
continue;
strcpy(dom->tty, value);
} else if (0 == strcmp(path, "name")) {
rc = xenstore_read(xenstore, vec[XS_WATCH_PATH], value, sizeof(value));
if (0 != rc)
continue;
strcpy(dom->name, value);
fprintf(stderr, PRE_STATE "%s (%d)\n", "new domain", dom->name, dom->domid);
} else {
continue;
}
try_attach_screen(dom, 0);
try_release_domain(dom);
}
return 0;
}
|