aboutsummaryrefslogtreecommitdiff
blob: 5a1e45abdf8a89f925a3bfdb289c161b17e53981 (plain)
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
/*
 * lxc: linux Container library
 *
 * (C) Copyright IBM Corp. 2007, 2008
 *
 * Authors:
 * Daniel Lezcano <dlezcano at fr.ibm.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <libgen.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <sys/ioctl.h>

#include "error.h"
#include "lxc.h"
#include "log.h"
#include "mainloop.h"
#include "arguments.h"

lxc_log_define(lxc_console_ui, lxc_console);

static char etoc(const char *expr)
{
	/* returns "control code" of given expression */
	char c = expr[0] == '^' ? expr[1] : expr[0];
	return 1 + ((c > 'Z') ? (c - 'a') : (c - 'Z'));
}

static int my_parser(struct lxc_arguments* args, int c, char* arg)
{
	switch (c) {
	case 't': args->ttynum = atoi(arg); break;
	case 'e': args->escape = etoc(arg); break;
	}
	return 0;
}

static const struct option my_longopts[] = {
	{"tty", required_argument, 0, 't'},
	{"escape", required_argument, 0, 'e'},
	LXC_COMMON_OPTIONS
};

static struct lxc_arguments my_args = {
	.progname = "lxc-console",
	.help     = "\
--name=NAME [--tty NUMBER]\n\
\n\
lxc-console logs on the container with the identifier NAME\n\
\n\
Options :\n\
  -n, --name=NAME      NAME for name of the container\n\
  -t, --tty=NUMBER     console tty number\n\
  -e, --escape=PREFIX  prefix for escape command\n",
	.options  = my_longopts,
	.parser   = my_parser,
	.checker  = NULL,
	.ttynum = -1,
	.escape = 1,
};

static int master = -1;

static void winsz(void)
{
	struct winsize wsz;
	if (ioctl(0, TIOCGWINSZ, &wsz) == 0)
		ioctl(master, TIOCSWINSZ, &wsz);
}

static void sigwinch(int sig)
{
	winsz();
}

static int setup_tios(int fd, struct termios *newtios, struct termios *oldtios)
{
	if (!isatty(fd)) {
		ERROR("'%d' is not a tty", fd);
		return -1;
	}

	/* Get current termios */
	if (tcgetattr(0, oldtios)) {
		SYSERROR("failed to get current terminal settings");
		return -1;
	}

	*newtios = *oldtios;

	/* Remove the echo characters and signal reception, the echo
	 * will be done below with master proxying */
	newtios->c_iflag &= ~IGNBRK;
	newtios->c_iflag &= BRKINT;
	newtios->c_lflag &= ~(ECHO|ICANON|ISIG);
	newtios->c_cc[VMIN] = 1;
	newtios->c_cc[VTIME] = 0;

	/* Set new attributes */
	if (tcsetattr(0, TCSAFLUSH, newtios)) {
		ERROR("failed to set new terminal settings");
		return -1;
	}

	return 0;
}

static int stdin_handler(int fd, void *data, struct lxc_epoll_descr *descr)
{
	static int wait4q = 0;
	int *peer = (int *)data;
	char c;

	if (read(0, &c, 1) < 0) {
		SYSERROR("failed to read");
		return 1;
	}

	/* we want to exit the console with Ctrl+a q */
	if (c == my_args.escape) {
		wait4q = !wait4q;
		return 0;
	}

	if (c == 'q' && wait4q)
		return 1;

	wait4q = 0;
	if (write(*peer, &c, 1) < 0) {
		SYSERROR("failed to write");
		return 1;
	}

	return 0;
}

static int master_handler(int fd, void *data, struct lxc_epoll_descr *descr)
{
	char buf[1024];
	int *peer = (int *)data;
	int r;

	r = read(fd, buf, sizeof(buf));
	if (r < 0) {
		SYSERROR("failed to read");
		return 1;
	}
	r = write(*peer, buf, r);

	return 0;
}

int main(int argc, char *argv[])
{
	int err, std_in = 1;
	struct lxc_epoll_descr descr;
	struct termios newtios, oldtios;

	err = lxc_arguments_parse(&my_args, argc, argv);
	if (err)
		return -1;

	err = lxc_log_init(my_args.log_file, my_args.log_priority,
			   my_args.progname, my_args.quiet);
	if (err)
		return -1;

	err = setup_tios(0, &newtios, &oldtios);
	if (err) {
		ERROR("failed to setup tios");
		return -1;
	}

	err = lxc_console(my_args.name, my_args.ttynum, &master);
	if (err)
		goto out;

	fprintf(stderr, "\nType <Ctrl+%c q> to exit the console\n",
                'a' + my_args.escape - 1);

	err = setsid();
	if (err)
		INFO("already group leader");

	if (signal(SIGWINCH, sigwinch) == SIG_ERR) {
		SYSERROR("failed to set SIGWINCH handler");
		err = -1;
		goto out;
	}

	winsz();

	err = lxc_mainloop_open(&descr);
	if (err) {
		ERROR("failed to create mainloop");
		goto out;
	}

	err = lxc_mainloop_add_handler(&descr, 0, stdin_handler, &master);
	if (err) {
		ERROR("failed to add handler for the stdin");
		goto out_mainloop_open;
	}

	err = lxc_mainloop_add_handler(&descr, master, master_handler, &std_in);
	if (err) {
		ERROR("failed to add handler for the master");
		goto out_mainloop_open;
	}

	err = lxc_mainloop(&descr);
	if (err) {
		ERROR("mainloop returned an error");
		goto out_mainloop_open;
	}

	err =  0;

out_mainloop_open:
	lxc_mainloop_close(&descr);

out:
	/* Restore previous terminal parameter */
	tcsetattr(0, TCSAFLUSH, &oldtios);
	
	/* Return to line it is */
	printf("\n");

	close(master);

	return err;
}