]>
Commit | Line | Data |
---|---|---|
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 | ||
0d4137cc CB |
27 | #include "conf.h" |
28 | #include "list.h" | |
29 | ||
22926b39 CB |
30 | struct lxc_epoll_descr; /* defined in mainloop.h */ |
31 | struct lxc_container; /* defined in lxccontainer.h */ | |
0d4137cc CB |
32 | struct lxc_tty_state |
33 | { | |
34 | struct lxc_list node; | |
35 | int stdinfd; | |
36 | int stdoutfd; | |
37 | int masterfd; | |
22926b39 CB |
38 | /* Escape sequence to use for exiting the pty. A single char can be |
39 | * specified. The pty can then exited by doing: Ctrl + specified_char + q. | |
40 | * This field is checked by lxc_console_cb_tty_stdin(). Set to -1 to | |
41 | * disable exiting the pty via a escape sequence. */ | |
0d4137cc | 42 | int escape; |
22926b39 CB |
43 | /* Used internally by lxc_console_cb_tty_stdin() to check whether an |
44 | * escape sequence has been received. */ | |
0d4137cc | 45 | int saw_escape; |
22926b39 | 46 | /* Name of the container to forward the SIGWINCH event to. */ |
0d4137cc | 47 | const char *winch_proxy; |
22926b39 | 48 | /* Path of the container to forward the SIGWINCH event to. */ |
0d4137cc | 49 | const char *winch_proxy_lxcpath; |
341c2aed CB |
50 | /* File descriptor that accepts SIGWINCH signals. If set to -1 no |
51 | * SIGWINCH handler could be installed. This also means that | |
52 | * the sigset_t oldmask member is meaningless. */ | |
0d4137cc CB |
53 | int sigfd; |
54 | sigset_t oldmask; | |
55 | }; | |
b5159817 | 56 | |
22926b39 CB |
57 | /* |
58 | * lxc_console_allocate: allocate the console or a tty | |
59 | * | |
60 | * @conf : the configuration of the container to allocate from | |
61 | * @sockfd : the socket fd whose remote side when closed, will be an | |
62 | * indication that the console or tty is no longer in use | |
63 | * @ttyreq : the tty requested to be opened, -1 for any, 0 for the console | |
64 | */ | |
b5159817 | 65 | extern int lxc_console_allocate(struct lxc_conf *conf, int sockfd, int *ttynum); |
22926b39 CB |
66 | |
67 | /* | |
68 | * Create a new pty: | |
69 | * - calls openpty() to allocate a master/slave pty pair | |
70 | * - sets the FD_CLOEXEC flag on the master/slave fds | |
71 | * - allocates either the current controlling pty (default) or a user specified | |
72 | * pty as peer pty for the newly created master/slave pair | |
73 | * - sets up SIGWINCH handler, winsz, and new terminal settings | |
74 | * (Handlers for SIGWINCH and I/O are not registered in a mainloop.) | |
75 | * (For an unprivileged container the created pty on the host is not | |
76 | * automatically chowned to the uid/gid of the unprivileged user. For this | |
77 | * ttys_shift_ids() can be called.) | |
78 | */ | |
b5159817 | 79 | extern int lxc_console_create(struct lxc_conf *); |
22926b39 CB |
80 | |
81 | /* | |
82 | * Delete a pty created via lxc_console_create(): | |
83 | * - set old terminal settings | |
84 | * - memory allocated via lxc_console_create() is free()ed. | |
85 | * - close master/slave pty pair and allocated fd for the peer (usually | |
86 | * /dev/tty) | |
87 | * Registered handlers in a mainloop are not automatically deleted. | |
88 | */ | |
b5159817 | 89 | extern void lxc_console_delete(struct lxc_console *); |
22926b39 CB |
90 | |
91 | /* | |
92 | * lxc_console_free: mark the console or a tty as unallocated, free any | |
93 | * resources allocated by lxc_console_allocate(). | |
94 | * | |
95 | * @conf : the configuration of the container whose tty was closed | |
96 | * @fd : the socket fd whose remote side was closed, which indicated | |
97 | * the console or tty is no longer in use. this is used to match | |
98 | * which console/tty is being freed. | |
99 | */ | |
b5159817 DE |
100 | extern void lxc_console_free(struct lxc_conf *conf, int fd); |
101 | ||
22926b39 CB |
102 | /* |
103 | * Register pty event handlers in an open mainloop | |
104 | */ | |
da41561c | 105 | extern int lxc_console_mainloop_add(struct lxc_epoll_descr *, struct lxc_conf *); |
22926b39 CB |
106 | |
107 | /* | |
108 | * Handle SIGWINCH events on the allocated ptys. | |
109 | */ | |
b5159817 | 110 | extern void lxc_console_sigwinch(int sig); |
22926b39 CB |
111 | |
112 | /* | |
113 | * Connect to one of the ptys given to the container via lxc.tty. | |
114 | * - allocates either the current controlling pty (default) or a user specified | |
115 | * pty as peer pty for the containers tty | |
116 | * - sets up SIGWINCH handler, winsz, and new terminal settings | |
117 | * - opens mainloop | |
118 | * - registers SIGWINCH, I/O handlers in the mainloop | |
119 | * - performs all necessary cleanup operations | |
120 | */ | |
b5159817 DE |
121 | extern int lxc_console(struct lxc_container *c, int ttynum, |
122 | int stdinfd, int stdoutfd, int stderrfd, | |
123 | int escape); | |
22926b39 CB |
124 | |
125 | /* | |
126 | * Allocate one of the ptys given to the container via lxc.tty. Returns an open | |
127 | * fd to the allocated pty. | |
128 | * Set ttynum to -1 to allocate the first available pty, or to a value within | |
129 | * the range specified by lxc.tty to allocate a specific pty. | |
130 | */ | |
b5159817 DE |
131 | extern int lxc_console_getfd(struct lxc_container *c, int *ttynum, |
132 | int *masterfd); | |
22926b39 CB |
133 | |
134 | /* | |
135 | * Make fd a duplicate of the standard file descriptors: | |
136 | * fd is made a duplicate of a specific standard file descriptor iff the | |
137 | * standard file descriptor refers to a pty. | |
138 | */ | |
39a78bbe | 139 | extern int lxc_console_set_stdfds(int fd); |
22926b39 CB |
140 | |
141 | /* | |
142 | * Handler for events on the stdin fd of the pty. To be registered via the | |
143 | * corresponding functions declared and defined in mainloop.{c,h} or | |
144 | * lxc_console_mainloop_add(). | |
145 | * This function exits the loop cleanly when an EPOLLHUP event is received. | |
146 | */ | |
0d4137cc CB |
147 | extern int lxc_console_cb_tty_stdin(int fd, uint32_t events, void *cbdata, |
148 | struct lxc_epoll_descr *descr); | |
22926b39 CB |
149 | |
150 | /* | |
151 | * Handler for events on the master fd of the pty. To be registered via the | |
152 | * corresponding functions declared and defined in mainloop.{c,h} or | |
153 | * lxc_console_mainloop_add(). | |
154 | * This function exits the loop cleanly when an EPOLLHUP event is received. | |
155 | */ | |
0d4137cc CB |
156 | extern int lxc_console_cb_tty_master(int fd, uint32_t events, void *cbdata, |
157 | struct lxc_epoll_descr *descr); | |
22926b39 CB |
158 | |
159 | /* | |
160 | * Setup new terminal properties. The old terminal settings are stored in | |
161 | * oldtios. | |
162 | */ | |
0d4137cc | 163 | extern int lxc_setup_tios(int fd, struct termios *oldtios); |
22926b39 CB |
164 | |
165 | ||
166 | /* | |
167 | * lxc_console_winsz: propagte winsz from one terminal to another | |
168 | * | |
169 | * @srcfd : terminal to get size from (typically a slave pty) | |
170 | * @dstfd : terminal to set size on (typically a master pty) | |
171 | */ | |
0d4137cc | 172 | extern void lxc_console_winsz(int srcfd, int dstfd); |
22926b39 CB |
173 | |
174 | /* | |
175 | * lxc_console_sigwinch_init: install SIGWINCH handler | |
176 | * | |
177 | * @srcfd : src for winsz in SIGWINCH handler | |
178 | * @dstfd : dst for winsz in SIGWINCH handler | |
179 | * | |
180 | * Returns lxc_tty_state structure on success or NULL on failure. The sigfd | |
181 | * member of the returned lxc_tty_state can be select()/poll()ed/epoll()ed | |
182 | * on (ie added to a mainloop) for SIGWINCH. | |
183 | * | |
184 | * Must be called with process_lock held to protect the lxc_ttys list, or | |
185 | * from a non-threaded context. | |
186 | * | |
187 | * Note that SIGWINCH isn't installed as a classic asychronous handler, | |
188 | * rather signalfd(2) is used so that we can handle the signal when we're | |
189 | * ready for it. This avoids deadlocks since a signal handler | |
190 | * (ie lxc_console_sigwinch()) would need to take the thread mutex to | |
191 | * prevent lxc_ttys list corruption, but using the fd we can provide the | |
192 | * tty_state needed to the callback (lxc_console_cb_sigwinch_fd()). | |
193 | * | |
194 | * This function allocates memory. It is up to the caller to free it. | |
195 | */ | |
196 | extern struct lxc_tty_state *lxc_console_sigwinch_init(int srcfd, int dstfd); | |
197 | ||
198 | /* | |
199 | * Handler for SIGWINCH events. To be registered via the corresponding functions | |
200 | * declared and defined in mainloop.{c,h} or lxc_console_mainloop_add(). | |
201 | */ | |
0d4137cc CB |
202 | extern int lxc_console_cb_sigwinch_fd(int fd, uint32_t events, void *cbdata, |
203 | struct lxc_epoll_descr *descr); | |
22926b39 CB |
204 | |
205 | /* | |
206 | * lxc_console_sigwinch_fini: uninstall SIGWINCH handler | |
207 | * | |
208 | * @ts : the lxc_tty_state returned by lxc_console_sigwinch_init | |
209 | * | |
210 | * Restore the saved signal handler that was in effect at the time | |
211 | * lxc_console_sigwinch_init() was called. | |
212 | * | |
213 | * Must be called with process_lock held to protect the lxc_ttys list, or | |
214 | * from a non-threaded context. | |
215 | */ | |
0d4137cc | 216 | extern void lxc_console_sigwinch_fini(struct lxc_tty_state *ts); |
f1a4a029 ÇO |
217 | |
218 | #endif |