]>
Commit | Line | Data |
---|---|---|
b0a33c1e | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <dlezcano at fr.ibm.com> | |
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 | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | */ | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <unistd.h> | |
26 | #include <errno.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/un.h> | |
29 | ||
724e753c | 30 | #include <lxc/lxc.h> |
96fa1ff0 | 31 | #include "commands.h" |
724e753c | 32 | #include "af_unix.h" |
36eb9bde CLG |
33 | |
34 | lxc_log_define(lxc_console, lxc); | |
35 | ||
96fa1ff0 MN |
36 | extern int lxc_console(const char *name, int ttynum, int *fd) |
37 | { | |
38 | int ret; | |
39 | struct lxc_command command = { | |
40 | .request = { .type = LXC_COMMAND_TTY, .data = ttynum }, | |
41 | }; | |
42 | ||
724e753c | 43 | ret = lxc_command(name, &command); |
96fa1ff0 MN |
44 | if (ret < 0) { |
45 | ERROR("failed to send command"); | |
46 | return -1; | |
47 | } | |
48 | ||
49 | if (!ret) { | |
50 | ERROR("console denied by '%s'", name); | |
51 | return -1; | |
52 | } | |
53 | ||
54 | *fd = command.answer.fd; | |
55 | if (*fd <0) { | |
56 | ERROR("unable to allocate fd for tty %d", ttynum); | |
57 | return -1; | |
58 | } | |
59 | ||
60 | INFO("tty %d allocated", ttynum); | |
61 | return 0; | |
62 | } | |
724e753c MN |
63 | |
64 | /*---------------------------------------------------------------------------- | |
65 | * functions used by lxc-start mainloop | |
66 | * to handle above command request. | |
67 | *--------------------------------------------------------------------------*/ | |
68 | extern void lxc_console_remove_fd(int fd, struct lxc_tty_info *tty_info) | |
69 | { | |
70 | int i; | |
71 | ||
72 | for (i = 0; i < tty_info->nbtty; i++) { | |
73 | ||
74 | if (tty_info->pty_info[i].busy != fd) | |
75 | continue; | |
76 | ||
77 | tty_info->pty_info[i].busy = 0; | |
78 | } | |
79 | ||
80 | return; | |
81 | } | |
82 | ||
83 | extern int lxc_console_callback(int fd, struct lxc_request *request, | |
84 | struct lxc_handler *handler) | |
85 | { | |
86 | int ttynum = request->data; | |
87 | struct lxc_tty_info *tty_info = &handler->tty_info; | |
88 | ||
89 | if (ttynum > 0) { | |
90 | if (ttynum > tty_info->nbtty) | |
91 | goto out_close; | |
92 | ||
93 | if (tty_info->pty_info[ttynum - 1].busy) | |
94 | goto out_close; | |
95 | ||
96 | goto out_send; | |
97 | } | |
98 | ||
99 | /* fixup index tty1 => [0] */ | |
100 | for (ttynum = 1; | |
101 | ttynum <= tty_info->nbtty && tty_info->pty_info[ttynum - 1].busy; | |
102 | ttynum++); | |
103 | ||
104 | /* we didn't find any available slot for tty */ | |
105 | if (ttynum > tty_info->nbtty) | |
106 | goto out_close; | |
107 | ||
108 | out_send: | |
109 | if (lxc_af_unix_send_fd(fd, tty_info->pty_info[ttynum - 1].master, | |
110 | &ttynum, sizeof(ttynum)) < 0) { | |
111 | ERROR("failed to send tty to client"); | |
112 | goto out_close; | |
113 | } | |
114 | ||
115 | tty_info->pty_info[ttynum - 1].busy = fd; | |
116 | ||
117 | return 0; | |
118 | ||
119 | out_close: | |
120 | /* the close fd and related cleanup will be done by caller */ | |
121 | return 1; | |
122 | } | |
123 |