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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
diff -Nurp util-vserver-0.30.210.orig/ensc_wrappers/wrappers.h util-vserver-0.30.210.vlogin/ensc_wrappers/wrappers.h
--- util-vserver-0.30.210.orig/ensc_wrappers/wrappers.h 2006-01-22 12:18:14.000000000 +0100
+++ util-vserver-0.30.210.vlogin/ensc_wrappers/wrappers.h 2006-04-04 04:18:38.000000000 +0200
@@ -111,6 +111,10 @@
# include "wrappers-stat.hc"
#endif
+#ifdef ENSC_WRAPPERS_TERMIOS
+# include "wrappers-termios.hc"
+#endif
+
#undef ENSC_DETAIL2
#undef ENSC_DETAIL1
#undef ENSC_DOQUOTE_COND
diff -Nurp util-vserver-0.30.210.orig/ensc_wrappers/wrappers-termios.hc util-vserver-0.30.210.vlogin/ensc_wrappers/wrappers-termios.hc
--- util-vserver-0.30.210.orig/ensc_wrappers/wrappers-termios.hc 1970-01-01 01:00:00.000000000 +0100
+++ util-vserver-0.30.210.vlogin/ensc_wrappers/wrappers-termios.hc 2006-04-04 04:18:38.000000000 +0200
@@ -0,0 +1,32 @@
+// $Id$ --*- c -*--
+
+// Copyright (C) 2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//
+// 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; version 2 of the License.
+//
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+#ifndef H_ENSC_IN_WRAPPERS_H
+# error wrappers-termios.hc can not be used in this way
+#endif
+
+inline static WRAPPER_DECL void
+Etcgetattr(int fd, struct termios *termios_p)
+{
+ FatalErrnoError(tcgetattr(fd, termios_p)==-1, "tcgetattr()");
+}
+
+inline static WRAPPER_DECL void
+Etcsetattr(int fd, int optional_actions, struct termios *termios_p)
+{
+ FatalErrnoError(tcsetattr(fd, optional_actions, termios_p)==-1, "tcsetattr()");
+}
diff -Nurp util-vserver-0.30.210.orig/Makefile.in util-vserver-0.30.210.vlogin/Makefile.in
--- util-vserver-0.30.210.orig/Makefile.in 2006-01-22 20:33:21.000000000 +0100
+++ util-vserver-0.30.210.vlogin/Makefile.in 2006-04-04 05:00:11.000000000 +0200
@@ -965,7 +965,7 @@ src_testsuite_vunify_functest_DEPENDENCI
am_src_vattribute_OBJECTS = src/vattribute.$(OBJEXT)
src_vattribute_OBJECTS = $(am_src_vattribute_OBJECTS)
src_vattribute_DEPENDENCIES = $(am__DEPENDENCIES_11)
-am_src_vcontext_OBJECTS = src/vcontext.$(OBJEXT)
+am_src_vcontext_OBJECTS = src/vcontext.$(OBJEXT) src/vlogin.$(OBJEXT)
src_vcontext_OBJECTS = $(am_src_vcontext_OBJECTS)
src_vcontext_DEPENDENCIES = $(am__DEPENDENCIES_11) \
$(am__DEPENDENCIES_9)
@@ -1746,7 +1746,7 @@ src_chbind_LDFLAGS = $(VSERVER_LDFLGS)
src_chcontext_compat_SOURCES = src/chcontext.c
src_chcontext_compat_LDADD = $(VSERVER_LDADDS) $(LIBINTERNAL)
src_chcontext_compat_LDFLAGS = $(VSERVER_LDFLGS)
-src_vcontext_SOURCES = src/vcontext.c
+src_vcontext_SOURCES = src/vcontext.c src/vlogin.c
src_vcontext_LDADD = $(VSERVER_LDADDS) $(LIBINTERNAL)
src_vcontext_LDFLAGS = $(VSERVER_LDFLGS)
src_vsched_SOURCES = src/vsched.c
@@ -3489,6 +3489,8 @@ src/vattribute$(EXEEXT): $(src_vattribut
$(LINK) $(src_vattribute_LDFLAGS) $(src_vattribute_OBJECTS) $(src_vattribute_LDADD) $(LIBS)
src/vcontext.$(OBJEXT): src/$(am__dirstamp) \
src/$(DEPDIR)/$(am__dirstamp)
+src/vlogin.$(OBJEXT): src/$(am__dirstamp) \
+ src/$(DEPDIR)/$(am__dirstamp)
src/vcontext$(EXEEXT): $(src_vcontext_OBJECTS) $(src_vcontext_DEPENDENCIES) src/$(am__dirstamp)
@rm -f src/vcontext$(EXEEXT)
$(LINK) $(src_vcontext_LDFLAGS) $(src_vcontext_OBJECTS) $(src_vcontext_LDADD) $(LIBS)
@@ -4222,6 +4224,7 @@ mostlyclean-compile:
-rm -f src/vdu.$(OBJEXT)
-rm -f src/vhashify.$(OBJEXT)
-rm -f src/vlimit.$(OBJEXT)
+ -rm -f src/vlogin.$(OBJEXT)
-rm -f src/vnamespace.$(OBJEXT)
-rm -f src/vps.$(OBJEXT)
-rm -f src/vreboot.$(OBJEXT)
@@ -4593,6 +4596,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/vdu.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/vhashify.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/vlimit.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/vlogin.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/vnamespace.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/vps.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/vreboot.Po@am__quote@
diff -Nurp util-vserver-0.30.210.orig/scripts/vserver.functions util-vserver-0.30.210.vlogin/scripts/vserver.functions
--- util-vserver-0.30.210.orig/scripts/vserver.functions 2006-01-22 13:24:25.000000000 +0100
+++ util-vserver-0.30.210.vlogin/scripts/vserver.functions 2006-04-04 04:18:38.000000000 +0200
@@ -39,7 +39,7 @@ declare -a ENTER_SHELL=()
declare -a OPTS_VCONTEXT_CREATE=()
declare -a OPTS_VCONTEXT_MIGRATE=()
-declare -a OPTS_VCONTEXT_ENTER=()
+declare -a OPTS_VCONTEXT_ENTER=( --vlogin )
declare -a OPTS_VATTRIBUTE=( --flag fakeinit )
declare -a OPTS_VSCHED=()
diff -Nurp util-vserver-0.30.210.orig/src/Makefile-files util-vserver-0.30.210.vlogin/src/Makefile-files
--- util-vserver-0.30.210.orig/src/Makefile-files 2006-01-22 12:28:00.000000000 +0100
+++ util-vserver-0.30.210.vlogin/src/Makefile-files 2006-04-04 04:18:38.000000000 +0200
@@ -167,7 +167,7 @@ src_chcontext_compat_SOURCES = src/chcon
src_chcontext_compat_LDADD = $(VSERVER_LDADDS) $(LIBINTERNAL)
src_chcontext_compat_LDFLAGS = $(VSERVER_LDFLGS)
-src_vcontext_SOURCES = src/vcontext.c
+src_vcontext_SOURCES = src/vcontext.c src/vlogin.c
src_vcontext_LDADD = $(VSERVER_LDADDS) $(LIBINTERNAL)
src_vcontext_LDFLAGS = $(VSERVER_LDFLGS)
diff -Nurp util-vserver-0.30.210.orig/src/vcontext.c util-vserver-0.30.210.vlogin/src/vcontext.c
--- util-vserver-0.30.210.orig/src/vcontext.c 2005-10-30 00:38:36.000000000 +0200
+++ util-vserver-0.30.210.vlogin/src/vcontext.c 2006-04-04 04:18:38.000000000 +0200
@@ -62,6 +62,7 @@
#define CMD_NAMESPACE 0x400d
#define CMD_PERSTYPE 0x400e
#define CMD_PERSFLAG 0x400f
+#define CMD_VLOGIN 0x4010
struct option const
@@ -85,6 +86,7 @@ CMDLINE_OPTIONS[] = {
{ "syncmsg", required_argument, 0, CMD_SYNCMSG },
{ "personality-type", required_argument, 0, CMD_PERSTYPE },
{ "personality-flags", required_argument, 0, CMD_PERSFLAG },
+ { "vlogin", no_argument, 0, CMD_VLOGIN },
#if 1
{ "fakeinit", no_argument, 0, CMD_INITPID }, // compatibility
#endif
@@ -100,6 +102,7 @@ struct Arguments {
bool is_initpid;
bool is_silentexist;
bool set_namespace;
+ bool do_vlogin;
uint_least32_t personality_flags;
uint_least32_t personality_type;
int verbosity;
@@ -112,6 +115,8 @@ struct Arguments {
int wrapper_exit_code = 255;
+void do_vlogin(char *argv[]);
+
static void
showHelp(int fd, char const *cmd, int res)
{
@@ -145,6 +150,7 @@ showHelp(int fd, char const *cmd, int re
" --syncmsg <message>\n"
" ... use <message> as synchronization message; by\n"
" default, 'ok' will be used\n"
+ " --vlogin ... enable terminal proxy\n"
"\n"
"'vcontext --create' exits with code 254 iff the context exists already.\n"
"\n"
@@ -287,12 +293,15 @@ doit(struct Arguments const *args, char
sys_personality(args->personality_type | args->personality_flags)==-1) {
perror(ENSC_WRAPPERS_PREFIX "personality()");
exit(wrapper_exit_code);
- }
+ }
doExternalSync(ext_sync_fd, args->sync_msg);
doSyncStage1(p, args->do_disconnect);
DPRINTF("doit: pid=%u, ppid=%u\n", getpid(), getppid());
- execvp (argv[optind],argv+optind);
+ if (!args->do_vlogin)
+ execvp (argv[optind],argv+optind);
+ else
+ do_vlogin(argv+optind);
doSyncStage2(p, args->do_disconnect);
PERROR_Q(ENSC_WRAPPERS_PREFIX "execvp", argv[optind]);
@@ -341,6 +350,7 @@ int main (int argc, char *argv[])
.do_migrateself = false,
.do_disconnect = false,
.do_endsetup = false,
+ .do_vlogin = false,
.is_initpid = false,
.is_silentexist = false,
.set_namespace = false,
@@ -363,6 +373,7 @@ int main (int argc, char *argv[])
case CMD_MIGRATE : args.do_migrate = true; break;
case CMD_DISCONNECT : args.do_disconnect = true; break;
case CMD_ENDSETUP : args.do_endsetup = true; break;
+ case CMD_VLOGIN : args.do_vlogin = true; break;
case CMD_INITPID : args.is_initpid = true; break;
case CMD_CHROOT : args.do_chroot = true; break;
case CMD_NAMESPACE : args.set_namespace = true; break;
diff -Nurp util-vserver-0.30.210.orig/src/vlogin.c util-vserver-0.30.210.vlogin/src/vlogin.c
--- util-vserver-0.30.210.orig/src/vlogin.c 1970-01-01 01:00:00.000000000 +0100
+++ util-vserver-0.30.210.vlogin/src/vlogin.c 2006-04-04 06:48:12.000000000 +0200
@@ -0,0 +1,318 @@
+// $Id$
+
+// Copyright (C) 2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+// Based on vserver-utils' vlogin program.
+//
+// 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; version 2 of the License.
+//
+// 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "util.h"
+#include <lib/vserver.h>
+#include <lib/fmt.h>
+
+#include <stdlib.h>
+#include <getopt.h>
+#include <stdint.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/wait.h>
+#include <sys/socket.h>
+#include <termios.h>
+#include <signal.h>
+#include <pty.h>
+#include <fcntl.h>
+
+#define ENSC_WRAPPERS_PREFIX "vlogin: "
+#define ENSC_WRAPPERS_IOCTL 1
+#define ENSC_WRAPPERS_UNISTD 1
+#define ENSC_WRAPPERS_SOCKET 1
+#define ENSC_WRAPPERS_IO 1
+#define ENSC_WRAPPERS_TERMIOS 1
+#define ENSC_WRAPPERS_FCNTL 1
+#include <wrappers.h>
+
+struct terminal {
+ int fd; /* terminal file descriptor */
+ struct termios term; /* terminal settings */
+ struct winsize ws; /* terminal size */
+ pid_t pid; /* terminal process id */
+ struct termios termo; /* original terminal settings */
+ enum { TS_RESET, TS_RAW } state; /* terminal state */
+};
+
+static struct terminal t;
+extern int wrapper_exit_code;
+
+/* set terminal to raw mode */
+static void
+terminal_raw(void)
+{
+ struct termios buf;
+
+ /* save original terminal settings */
+ Etcgetattr(STDIN_FILENO, &t.termo);
+
+ buf = t.termo;
+
+ /* echo off
+ ** canonical mode off
+ ** extended input processing off
+ ** signal chars off */
+ buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
+
+ /* no SIGINT on BREAK
+ ** CR-to-NL off
+ ** input parity check off
+ ** don't strip 8th bit on input
+ ** output flow control off */
+ buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
+
+ /* clear size bits
+ ** parity checking off */
+ buf.c_cflag &= ~(CSIZE | PARENB);
+
+ /* set 8 bits/char */
+ buf.c_cflag |= CS8;
+
+ /* output processing off */
+ buf.c_oflag &= ~(OPOST);
+
+ /* 1 byte at a time
+ ** no timer */
+ buf.c_cc[VMIN] = 1;
+ buf.c_cc[VTIME] = 0;
+
+ Etcsetattr(STDIN_FILENO, TCSAFLUSH, &buf);
+
+ t.state = TS_RAW;
+}
+
+/* reset terminal to original state */
+static void
+terminal_reset(void)
+{
+ if (t.state != TS_RAW)
+ return;
+
+ Etcsetattr(STDIN_FILENO, TCSAFLUSH, &t.termo);
+
+ t.state = TS_RESET;
+}
+
+/* exit handler */
+static void
+terminal_atexit(void)
+{
+ terminal_reset();
+}
+
+/* send signal to terminal */
+static void
+terminal_kill(int sig)
+{
+ pid_t pgrp = -1;
+
+ /* try to get process group leader */
+ if (ioctl(t.fd, TIOCGPGRP, &pgrp) >= 0 &&
+ pgrp != -1 &&
+ kill(-pgrp, sig) != -1)
+ return;
+
+ /* fallback using terminal pid */
+ kill(-t.pid, sig);
+}
+
+/* redraw the terminal screen */
+static void
+terminal_redraw(void)
+{
+ /* get winsize from stdin */
+ if (ioctl(STDIN_FILENO, TIOCGWINSZ, &t.ws) == -1)
+ return;
+
+ /* set winsize in terminal */
+ ioctl(t.fd, TIOCSWINSZ, &t.ws);
+
+ /* set winsize change signal to terminal */
+ terminal_kill(SIGWINCH);
+}
+
+/* copy terminal activity to user */
+static void
+terminal_activity(void)
+{
+ char buf[64];
+ size_t len;
+
+ /* read terminal activity */
+ len = Eread(t.fd, buf, sizeof(buf));
+
+ /* get the current terminal settings */
+ Etcgetattr(t.fd, &t.term);
+
+ /* set the current terminal settings */
+ Etcsetattr(STDIN_FILENO, TCSANOW, &t.term);
+
+ /* write activity to user */
+ EwriteAll(STDOUT_FILENO, buf, len);
+}
+
+/* copy user activity to terminal */
+static void
+user_activity(void)
+{
+ char buf[64];
+ size_t len;
+
+ len = Eread(STDIN_FILENO, buf, sizeof(buf));
+ EwriteAll(t.fd, buf, len);
+}
+
+/* shuffle all output, and reset the terminal */
+static void
+terminal_end(void)
+{
+ char buf[64];
+ ssize_t len;
+ long options;
+
+ options = Efcntl(t.fd, F_GETFL, 0) | O_NONBLOCK;
+ Efcntl(t.fd, F_SETFL, options);
+ for (;;) {
+ len = read(t.fd, buf, sizeof(buf));
+ if (len == 0 || len == -1)
+ break;
+ EwriteAll(STDOUT_FILENO, buf, len);
+ }
+
+ /* in case atexit hasn't been setup yet */
+ terminal_reset();
+}
+
+/* catch signals */
+static void
+signal_handler(int sig)
+{
+ int status;
+
+ switch(sig) {
+ /* catch interrupt */
+ case SIGINT:
+ terminal_kill(sig);
+ break;
+
+ /* terminal died */
+ case SIGCHLD:
+ terminal_end();
+ wait(&status);
+ exit(WEXITSTATUS(status));
+ break;
+
+ /* window size has changed */
+ case SIGWINCH:
+ terminal_redraw();
+ break;
+
+ default:
+ exit(0);
+ }
+
+}
+
+void do_vlogin(char *argv[])
+{
+ int slave;
+ pid_t pid;
+ int n;
+ fd_set rfds;
+
+ if (!isatty(0) || !isatty(1)) {
+ execvp(argv[0], argv);
+ return;
+ }
+
+ /* set terminal to raw mode */
+ terminal_raw();
+
+ /* fork new pseudo terminal */
+ if (openpty(&t.fd, &slave, NULL, NULL, NULL) == -1) {
+ perror(ENSC_WRAPPERS_PREFIX "openpty()");
+ exit(EXIT_FAILURE);
+ }
+
+ /* setup SIGCHLD here, so we're sure to get the signal */
+ signal(SIGCHLD, signal_handler);
+
+ pid = Efork();
+
+ if (pid == 0) {
+ /* we don't need the master side of the terminal */
+ close(t.fd);
+
+ /* login_tty() stupid dietlibc doesn't have it */
+ Esetsid();
+
+ Eioctl(slave, TIOCSCTTY, NULL);
+
+ Edup2(slave, 0);
+ Edup2(slave, 1);
+ Edup2(slave, 2);
+
+ if (slave > 2)
+ close(slave);
+
+ Eexecvp(argv[0], argv);
+ }
+
+ /* setup SIGINT and SIGWINCH here, as they can cause loops in the child */
+ signal(SIGWINCH, signal_handler);
+ signal(SIGINT, signal_handler);
+
+ /* save terminals pid */
+ t.pid = pid;
+
+ /* we want a redraw */
+ atexit(terminal_atexit);
+ terminal_redraw();
+
+ /* main loop */
+ for (;;) {
+ /* init file descriptors for select */
+ FD_ZERO(&rfds);
+ FD_SET(STDIN_FILENO, &rfds);
+ FD_SET(t.fd, &rfds);
+ n = t.fd;
+
+ /* wait for something to happen */
+ while (select(n + 1, &rfds, NULL, NULL, NULL) == -1) {
+ if (errno == EINTR || errno == EAGAIN)
+ continue;
+ perror(ENSC_WRAPPERS_PREFIX "select()");
+ exit(wrapper_exit_code);
+ }
+
+ if (FD_ISSET(STDIN_FILENO, &rfds))
+ user_activity();
+
+ if (FD_ISSET(t.fd, &rfds))
+ terminal_activity();
+ }
+
+ /* never get here, signal handler exits */
+}
|