]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/terminal.h
8c7742f8102bfb841df466557bd44a47e77c4c9c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #ifndef __LXC_TERMINAL_H
4 #define __LXC_TERMINAL_H
16 struct lxc_async_descr
;
18 struct lxc_terminal_info
{
19 /* the path name of the pty side */
22 /* the file descriptor of the ptx */
25 /* the file descriptor of the pty */
28 /* whether the terminal is currently used */
32 struct lxc_terminal_state
{
38 /* Escape sequence to use for exiting the terminal. A single char can
39 * be specified. The terminal can then exited by doing: Ctrl +
40 * specified_char + q. This field is checked by
41 * lxc_terminal_stdin_cb(). Set to -1 to disable exiting the terminal
42 * via a escape sequence.
46 /* Used internally by lxc_terminal_stdin_cb() to check whether an
47 * escape sequence has been received.
51 /* File descriptor that accepts signals. If set to -1 no signal handler
52 * could be installed. This also means that the sigset_t oldmask member
65 struct lxc_terminal_info proxy
;
66 struct lxc_async_descr
*descr
;
70 struct lxc_terminal_state
*tty_state
;
72 struct /* lxc_terminal_log */ {
73 /* size of the log file */
76 /* path to the log file */
79 /* fd to the log file */
82 /* whether the log file will be rotated */
83 unsigned int log_rotate
;
86 struct /* lxc_terminal_ringbuf */ {
87 /* size of the ringbuffer */
90 /* the in-memory ringbuffer */
91 struct lxc_ringbuf ringbuf
;
96 * lxc_terminal_allocate: allocate the console or a tty
98 * @conf : the configuration of the container to allocate from
99 * @sockfd : the socket fd whose remote side when closed, will be an
100 * indication that the console or tty is no longer in use
101 * @ttyreq : the tty requested to be opened, -1 for any, 0 for the console
103 __hidden
extern int lxc_terminal_allocate(struct lxc_conf
*conf
, int sockfd
, int *ttynum
);
106 * Create a new terminal:
107 * - calls openpty() to allocate a ptx/pty pair
108 * - sets the FD_CLOEXEC flag on the ptx/pty fds
109 * - allocates either the current controlling terminal (default) or a user
110 * specified terminal as proxy for the newly created ptx/pty pair
111 * - sets up SIGWINCH handler, winsz, and new terminal settings
112 * (Handlers for SIGWINCH and I/O are not registered in a mainloop.)
114 __hidden
extern int lxc_terminal_create(const char *name
, const char *lxcpath
,
115 struct lxc_conf
*conf
,
116 struct lxc_terminal
*console
);
119 * lxc_terminal_setup: Create a new terminal.
120 * - In addition to lxc_terminal_create() also sets up logging.
122 __hidden
extern int lxc_terminal_setup(struct lxc_conf
*);
125 * Delete a terminal created via lxc_terminal_create() or lxc_terminal_setup():
126 * Note, registered handlers are not automatically deleted.
128 __hidden
extern void lxc_terminal_delete(struct lxc_terminal
*);
131 * lxc_terminal_free: mark the terminal as unallocated and free any resources
132 * allocated by lxc_terminal_allocate().
134 * @conf : the configuration of the container whose tty was closed
135 * @fd : the socket fd whose remote side was closed, which indicated
136 * the terminal is no longer in use. this is used to match
137 * which terminal is being freed.
139 __hidden
extern void lxc_terminal_free(struct lxc_conf
*conf
, int fd
);
142 * Register terminal event handlers in an open mainloop.
144 __hidden
extern int lxc_terminal_mainloop_add(struct lxc_async_descr
*, struct lxc_terminal
*);
147 * Handle SIGWINCH events on the allocated terminals.
149 __hidden
extern void lxc_terminal_sigwinch(int sig
);
152 * Connect to one of the ttys given to the container via lxc.tty.max.
153 * - allocates either the current controlling terminal (default) or a user specified
154 * terminal as proxy terminal for the containers tty
155 * - sets up SIGWINCH handler, winsz, and new terminal settings
157 * - registers SIGWINCH, I/O handlers in the mainloop
158 * - performs all necessary cleanup operations
160 __hidden
extern int lxc_console(struct lxc_container
*c
, int ttynum
, int stdinfd
, int stdoutfd
,
161 int stderrfd
, int escape
);
164 * Allocate one of the tty given to the container via lxc.tty.max. Returns an
165 * open fd to the allocated tty.
166 * Set ttynum to -1 to allocate the first available tty, or to a value within
167 * the range specified by lxc.tty.max to allocate a specific tty.
169 __hidden
extern int lxc_terminal_getfd(struct lxc_container
*c
, int *ttynum
, int *ptxfd
);
172 * Make fd a duplicate of the standard file descriptors. The fd is made a
173 * duplicate of a specific standard file descriptor iff the standard file
174 * descriptor refers to a terminal.
176 __hidden
extern int lxc_terminal_set_stdfds(int fd
);
179 * Handler for events on the stdin fd of the terminal. To be registered via the
180 * corresponding functions declared and defined in mainloop.{c,h} or
181 * lxc_terminal_mainloop_add().
182 * This function exits the loop cleanly when an EPOLLHUP event is received.
184 __hidden
extern int lxc_terminal_stdin_cb(int fd
, uint32_t events
, void *cbdata
,
185 struct lxc_async_descr
*descr
);
188 * Handler for events on the ptx fd of the terminal. To be registered via
189 * the corresponding functions declared and defined in mainloop.{c,h} or
190 * lxc_terminal_mainloop_add().
191 * This function exits the loop cleanly when an EPOLLHUP event is received.
193 __hidden
extern int lxc_terminal_ptx_cb(int fd
, uint32_t events
, void *cbdata
,
194 struct lxc_async_descr
*descr
);
197 * Setup new terminal properties. The old terminal settings are stored in
200 __hidden
extern int lxc_setup_tios(int fd
, struct termios
*oldtios
);
203 * lxc_terminal_winsz: propagate winsz from one terminal to another
206 * - terminal to get size from (typically a pty pty)
208 * - terminal to set size on (typically a ptx pty)
210 __hidden
extern void lxc_terminal_winsz(int srcfd
, int dstfd
);
213 * lxc_terminal_signal_init: install signal handler
216 * - src for winsz in SIGWINCH handler
218 * - dst for winsz in SIGWINCH handler
220 * Returns lxc_terminal_state structure on success or NULL on failure. The
221 * sigfd member of the returned lxc_terminal_state can be
222 * select()/poll()ed/epoll()ed on (i.e. added to a mainloop) for signals.
224 * Must be called with process_lock held to protect the lxc_ttys list, or from
225 * a non-threaded context.
227 * Note that the signal handler isn't installed as a classic asynchronous
228 * handler, rather signalfd(2) is used so that we can handle the signal when
229 * we're ready for it. This avoids deadlocks since a signal handler (ie
230 * lxc_terminal_sigwinch()) would need to take the thread mutex to prevent
231 * lxc_ttys list corruption, but using the fd we can provide the tty_state
232 * needed to the callback (lxc_terminal_signalfd_cb()).
234 * This function allocates memory. It is up to the caller to free it.
236 __hidden
extern struct lxc_terminal_state
*lxc_terminal_signal_init(int srcfd
, int dstfd
);
239 * Handler for signal events. To be registered via the corresponding functions
240 * declared and defined in mainloop.{c,h} or lxc_terminal_mainloop_add().
242 __hidden
extern int lxc_terminal_signalfd_cb(int fd
, uint32_t events
, void *cbdata
,
243 struct lxc_async_descr
*descr
);
245 __hidden
extern int lxc_terminal_write_ringbuffer(struct lxc_terminal
*terminal
);
246 __hidden
extern int lxc_terminal_create_log_file(struct lxc_terminal
*terminal
);
247 __hidden
extern int lxc_terminal_io_cb(int fd
, uint32_t events
, void *data
,
248 struct lxc_async_descr
*descr
);
250 __hidden
extern int lxc_make_controlling_terminal(int fd
);
251 __hidden
extern int lxc_terminal_prepare_login(int fd
);
252 __hidden
extern void lxc_terminal_conf_free(struct lxc_terminal
*terminal
);
253 __hidden
extern void lxc_terminal_info_init(struct lxc_terminal_info
*terminal
);
254 __hidden
extern void lxc_terminal_init(struct lxc_terminal
*terminal
);
255 __hidden
extern int lxc_terminal_signal_sigmask_safe_blocked(struct lxc_terminal
*terminal
);
256 __hidden
extern int lxc_devpts_terminal(int devpts_fd
, int *ret_ptx
,
257 int *ret_pty
, int *ret_pty_nr
,
258 bool require_tiocgptpeer
);
259 __hidden
extern int lxc_terminal_parent(struct lxc_conf
*conf
);
261 static inline bool wants_console(const struct lxc_terminal
*terminal
)
263 return !terminal
->path
|| !strequal(terminal
->path
, "none");
266 #endif /* __LXC_TERMINAL_H */