]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/console.c
typo in 488624016575d092d56211347b2bbe8367cd339a (V2)
[mirror_lxc.git] / src / lxc / console.c
CommitLineData
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
34lxc_log_define(lxc_console, lxc);
35
96fa1ff0
MN
36extern int lxc_console(const char *name, int ttynum, int *fd)
37{
d97b36f8 38 int ret, stopped = 0;
96fa1ff0
MN
39 struct lxc_command command = {
40 .request = { .type = LXC_COMMAND_TTY, .data = ttynum },
41 };
42
d97b36f8
DL
43 ret = lxc_command(name, &command, &stopped);
44 if (ret < 0 && stopped) {
45 ERROR("'%s' is stopped", name);
46 return -1;
47 }
48
96fa1ff0
MN
49 if (ret < 0) {
50 ERROR("failed to send command");
51 return -1;
52 }
53
54 if (!ret) {
55 ERROR("console denied by '%s'", name);
56 return -1;
57 }
58
3cc5de36
MN
59 if (command.answer.ret) {
60 ERROR("console access denied: %s",
61 strerror(-command.answer.ret));
62 return -1;
63 }
64
96fa1ff0
MN
65 *fd = command.answer.fd;
66 if (*fd <0) {
67 ERROR("unable to allocate fd for tty %d", ttynum);
68 return -1;
69 }
70
71 INFO("tty %d allocated", ttynum);
72 return 0;
73}
724e753c
MN
74
75/*----------------------------------------------------------------------------
76 * functions used by lxc-start mainloop
77 * to handle above command request.
78 *--------------------------------------------------------------------------*/
79extern void lxc_console_remove_fd(int fd, struct lxc_tty_info *tty_info)
80{
81 int i;
82
83 for (i = 0; i < tty_info->nbtty; i++) {
84
85 if (tty_info->pty_info[i].busy != fd)
86 continue;
87
88 tty_info->pty_info[i].busy = 0;
89 }
90
91 return;
92}
93
94extern int lxc_console_callback(int fd, struct lxc_request *request,
95 struct lxc_handler *handler)
96{
97 int ttynum = request->data;
571e6ec8 98 struct lxc_tty_info *tty_info = &handler->conf.tty_info;
724e753c
MN
99
100 if (ttynum > 0) {
101 if (ttynum > tty_info->nbtty)
102 goto out_close;
103
104 if (tty_info->pty_info[ttynum - 1].busy)
105 goto out_close;
106
107 goto out_send;
108 }
109
110 /* fixup index tty1 => [0] */
111 for (ttynum = 1;
112 ttynum <= tty_info->nbtty && tty_info->pty_info[ttynum - 1].busy;
113 ttynum++);
114
115 /* we didn't find any available slot for tty */
116 if (ttynum > tty_info->nbtty)
117 goto out_close;
118
119out_send:
120 if (lxc_af_unix_send_fd(fd, tty_info->pty_info[ttynum - 1].master,
121 &ttynum, sizeof(ttynum)) < 0) {
122 ERROR("failed to send tty to client");
123 goto out_close;
124 }
125
126 tty_info->pty_info[ttynum - 1].busy = fd;
127
128 return 0;
129
130out_close:
131 /* the close fd and related cleanup will be done by caller */
132 return 1;
133}
134