]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/console.h
console: lxc_terminal_setup()
[mirror_lxc.git] / src / lxc / console.h
CommitLineData
63376d7d
DL
1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2010
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
63376d7d
DL
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
63376d7d
DL
22 */
23
f1a4a029
ÇO
24#ifndef __LXC_CONSOLE_H
25#define __LXC_CONSOLE_H
26
0519b5cc
CB
27#include <signal.h>
28#include <stdio.h>
29
0d4137cc
CB
30#include "conf.h"
31#include "list.h"
32
22926b39
CB
33struct lxc_epoll_descr; /* defined in mainloop.h */
34struct lxc_container; /* defined in lxccontainer.h */
0d4137cc
CB
35struct lxc_tty_state
36{
37 struct lxc_list node;
38 int stdinfd;
39 int stdoutfd;
40 int masterfd;
22926b39
CB
41 /* Escape sequence to use for exiting the pty. A single char can be
42 * specified. The pty can then exited by doing: Ctrl + specified_char + q.
52f9292f 43 * This field is checked by lxc_terminal_stdin_cb(). Set to -1 to
0519b5cc
CB
44 * disable exiting the pty via a escape sequence.
45 */
0d4137cc 46 int escape;
52f9292f 47 /* Used internally by lxc_terminal_stdin_cb() to check whether an
0519b5cc
CB
48 * escape sequence has been received.
49 */
0d4137cc 50 int saw_escape;
22926b39 51 /* Name of the container to forward the SIGWINCH event to. */
0d4137cc 52 const char *winch_proxy;
22926b39 53 /* Path of the container to forward the SIGWINCH event to. */
0d4137cc 54 const char *winch_proxy_lxcpath;
0519b5cc
CB
55 /* File descriptor that accepts signals. If set to -1 no signal handler
56 * could be installed. This also means that the sigset_t oldmask member
57 * is meaningless.
58 */
0d4137cc
CB
59 int sigfd;
60 sigset_t oldmask;
61};
b5159817 62
22926b39 63/*
c1ee47cd 64 * lxc_terminal_allocate: allocate the console or a tty
22926b39
CB
65 *
66 * @conf : the configuration of the container to allocate from
67 * @sockfd : the socket fd whose remote side when closed, will be an
68 * indication that the console or tty is no longer in use
69 * @ttyreq : the tty requested to be opened, -1 for any, 0 for the console
70 */
c1ee47cd 71extern int lxc_terminal_allocate(struct lxc_conf *conf, int sockfd, int *ttynum);
22926b39
CB
72
73/*
74 * Create a new pty:
75 * - calls openpty() to allocate a master/slave pty pair
76 * - sets the FD_CLOEXEC flag on the master/slave fds
77 * - allocates either the current controlling pty (default) or a user specified
78 * pty as peer pty for the newly created master/slave pair
79 * - sets up SIGWINCH handler, winsz, and new terminal settings
80 * (Handlers for SIGWINCH and I/O are not registered in a mainloop.)
81 * (For an unprivileged container the created pty on the host is not
82 * automatically chowned to the uid/gid of the unprivileged user. For this
83 * ttys_shift_ids() can be called.)
84 */
9785b539 85extern int lxc_pty_create(struct lxc_pty *console);
5777fe90
CB
86
87/**
564e31c4 88 * lxc_terminal_setup: Create a new pty.
5777fe90
CB
89 * - In addition to lxc_pty_create() also sets up all pty logs.
90 */
564e31c4 91extern int lxc_terminal_setup(struct lxc_conf *);
22926b39
CB
92
93/*
564e31c4 94 * Delete a pty created via lxc_terminal_setup():
22926b39 95 * - set old terminal settings
564e31c4 96 * - memory allocated via lxc_terminal_setup() is free()ed.
22926b39
CB
97 * - close master/slave pty pair and allocated fd for the peer (usually
98 * /dev/tty)
99 * Registered handlers in a mainloop are not automatically deleted.
100 */
2aac071b 101extern void lxc_terminal_delete(struct lxc_pty *);
22926b39
CB
102
103/*
3dfe6f8d 104 * lxc_terminal_free: mark the console or a tty as unallocated, free any
c1ee47cd 105 * resources allocated by lxc_terminal_allocate().
22926b39
CB
106 *
107 * @conf : the configuration of the container whose tty was closed
108 * @fd : the socket fd whose remote side was closed, which indicated
109 * the console or tty is no longer in use. this is used to match
110 * which console/tty is being freed.
111 */
3dfe6f8d 112extern void lxc_terminal_free(struct lxc_conf *conf, int fd);
b5159817 113
22926b39
CB
114/*
115 * Register pty event handlers in an open mainloop
116 */
093bce5f 117extern int lxc_terminal_mainloop_add(struct lxc_epoll_descr *, struct lxc_pty *);
22926b39
CB
118
119/*
120 * Handle SIGWINCH events on the allocated ptys.
121 */
dad4a039 122extern void lxc_terminal_sigwinch(int sig);
22926b39
CB
123
124/*
fe1c5887 125 * Connect to one of the ptys given to the container via lxc.tty.max.
22926b39
CB
126 * - allocates either the current controlling pty (default) or a user specified
127 * pty as peer pty for the containers tty
128 * - sets up SIGWINCH handler, winsz, and new terminal settings
129 * - opens mainloop
130 * - registers SIGWINCH, I/O handlers in the mainloop
131 * - performs all necessary cleanup operations
132 */
b5159817
DE
133extern int lxc_console(struct lxc_container *c, int ttynum,
134 int stdinfd, int stdoutfd, int stderrfd,
135 int escape);
22926b39
CB
136
137/*
fe1c5887
CB
138 * Allocate one of the ptys given to the container via lxc.tty.max. Returns an
139 * open fd to the allocated pty.
22926b39 140 * Set ttynum to -1 to allocate the first available pty, or to a value within
fe1c5887 141 * the range specified by lxc.tty.max to allocate a specific pty.
22926b39 142 */
c86e2584 143extern int lxc_terminal_getfd(struct lxc_container *c, int *ttynum,
b5159817 144 int *masterfd);
22926b39
CB
145
146/*
147 * Make fd a duplicate of the standard file descriptors:
148 * fd is made a duplicate of a specific standard file descriptor iff the
149 * standard file descriptor refers to a pty.
150 */
ae6d3913 151extern int lxc_terminal_set_stdfds(int fd);
22926b39
CB
152
153/*
154 * Handler for events on the stdin fd of the pty. To be registered via the
155 * corresponding functions declared and defined in mainloop.{c,h} or
093bce5f 156 * lxc_terminal_mainloop_add().
22926b39
CB
157 * This function exits the loop cleanly when an EPOLLHUP event is received.
158 */
52f9292f
CB
159extern int lxc_terminal_stdin_cb(int fd, uint32_t events, void *cbdata,
160 struct lxc_epoll_descr *descr);
22926b39
CB
161
162/*
163 * Handler for events on the master fd of the pty. To be registered via the
164 * corresponding functions declared and defined in mainloop.{c,h} or
093bce5f 165 * lxc_terminal_mainloop_add().
22926b39
CB
166 * This function exits the loop cleanly when an EPOLLHUP event is received.
167 */
ee9102ff
CB
168extern int lxc_terminal_master_cb(int fd, uint32_t events, void *cbdata,
169 struct lxc_epoll_descr *descr);
22926b39
CB
170
171/*
172 * Setup new terminal properties. The old terminal settings are stored in
173 * oldtios.
174 */
0d4137cc 175extern int lxc_setup_tios(int fd, struct termios *oldtios);
22926b39
CB
176
177
178/*
4e9c0330 179 * lxc_terminal_winsz: propagate winsz from one terminal to another
22926b39
CB
180 *
181 * @srcfd : terminal to get size from (typically a slave pty)
182 * @dstfd : terminal to set size on (typically a master pty)
183 */
4e9c0330 184extern void lxc_terminal_winsz(int srcfd, int dstfd);
22926b39
CB
185
186/*
dc8c7883 187 * lxc_terminal_signal_init: install signal handler
22926b39
CB
188 *
189 * @srcfd : src for winsz in SIGWINCH handler
190 * @dstfd : dst for winsz in SIGWINCH handler
191 *
192 * Returns lxc_tty_state structure on success or NULL on failure. The sigfd
193 * member of the returned lxc_tty_state can be select()/poll()ed/epoll()ed
0519b5cc 194 * on (ie added to a mainloop) for signals.
22926b39
CB
195 *
196 * Must be called with process_lock held to protect the lxc_ttys list, or
197 * from a non-threaded context.
198 *
0519b5cc
CB
199 * Note that the signal handler isn't installed as a classic asychronous
200 * handler, rather signalfd(2) is used so that we can handle the signal when
201 * we're ready for it. This avoids deadlocks since a signal handler (ie
dad4a039 202 * lxc_terminal_sigwinch()) would need to take the thread mutex to prevent
0519b5cc 203 * lxc_ttys list corruption, but using the fd we can provide the tty_state
9bafc8cb 204 * needed to the callback (lxc_terminal_signalfd_cb()).
22926b39
CB
205 *
206 * This function allocates memory. It is up to the caller to free it.
207 */
dc8c7883 208extern struct lxc_tty_state *lxc_terminal_signal_init(int srcfd, int dstfd);
22926b39
CB
209
210/*
0519b5cc 211 * Handler for signal events. To be registered via the corresponding functions
093bce5f 212 * declared and defined in mainloop.{c,h} or lxc_terminal_mainloop_add().
22926b39 213 */
9bafc8cb 214extern int lxc_terminal_signalfd_cb(int fd, uint32_t events, void *cbdata,
0519b5cc 215 struct lxc_epoll_descr *descr);
22926b39
CB
216
217/*
a8867251 218 * lxc_terminal_signal_fini: uninstall signal handler
22926b39 219 *
dc8c7883 220 * @ts : the lxc_tty_state returned by lxc_terminal_signal_init
22926b39
CB
221 *
222 * Restore the saved signal handler that was in effect at the time
dc8c7883 223 * lxc_terminal_signal_init() was called.
22926b39
CB
224 *
225 * Must be called with process_lock held to protect the lxc_ttys list, or
226 * from a non-threaded context.
227 */
a8867251 228extern void lxc_terminal_signal_fini(struct lxc_tty_state *ts);
f1a4a029 229
7ef7427e 230extern int lxc_terminal_write_ringbuffer(struct lxc_pty *console);
43366ca2 231extern int lxc_terminal_create_log_file(struct lxc_pty *console);
de708fb7 232extern int lxc_terminal_io_cb(int fd, uint32_t events, void *data,
a529bc25 233 struct lxc_epoll_descr *descr);
63b74cda 234
e98affda
CB
235extern int lxc_make_controlling_pty(int fd);
236extern int lxc_login_pty(int fd);
9785b539 237extern void lxc_pty_conf_free(struct lxc_pty *console);
e98affda 238extern void lxc_pty_info_init(struct lxc_pty_info *pty);
9785b539
CB
239extern void lxc_pty_init(struct lxc_pty *pty);
240extern int lxc_pty_map_ids(struct lxc_conf *c, struct lxc_pty *pty);
e98affda 241
f1a4a029 242#endif