~serge-hallyn/ubuntu/natty/lxc/lxc-fix-3bugs

« back to all changes in this revision

Viewing changes to src/lxc/console.c

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter, Stéphane Graber, Guido Trotter
  • Date: 2010-01-10 10:40:21 UTC
  • mfrom: (1.1.2 upstream) (3.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100110104021-z8rj5zw5mlvra08l
Tags: 0.6.4-1
[ Stéphane Graber ]
* Upgrade standards-version to 3.8.3
* Drop the copy of etc/* from rules as "etc" is no longer in the tarball

[ Guido Trotter ]
* New Upstream Version
* Update libcap2-dev dependency to libcap-dev
* Install upstream-built man pages via debian/lxc.manpages
* Drop unneeded docbook-utils build dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <sys/types.h>
28
28
#include <sys/un.h>
29
29
 
 
30
#include <lxc/log.h>
 
31
#include <lxc/conf.h>
 
32
#include <lxc/start.h>  /* for struct lxc_handler */
 
33
 
 
34
#include "commands.h"
30
35
#include "af_unix.h"
31
 
#include "error.h"
32
 
 
33
 
#include <lxc/log.h>
34
36
 
35
37
lxc_log_define(lxc_console, lxc);
36
38
 
37
39
extern int lxc_console(const char *name, int ttynum, int *fd)
38
40
{
39
 
        struct sockaddr_un addr = { 0 };
40
 
        int sock, ret = -1;
41
 
 
42
 
        snprintf(addr.sun_path, sizeof(addr.sun_path), "@%s", name);
43
 
        addr.sun_path[0] = '\0';
44
 
 
45
 
        sock = lxc_af_unix_connect(addr.sun_path);
46
 
        if (sock < 0) {
47
 
                ERROR("failed to connect to the tty service");
48
 
                goto out;
49
 
        }
50
 
 
51
 
        ret = lxc_af_unix_send_credential(sock, &ttynum, sizeof(ttynum));
52
 
        if (ret < 0) {
53
 
                SYSERROR("failed to send credentials");
54
 
                goto out_close;
55
 
        }
56
 
 
57
 
        ret = lxc_af_unix_recv_fd(sock, fd, &ttynum, sizeof(ttynum));
58
 
        if (ret < 0) {
59
 
                ERROR("failed to connect to the tty");
60
 
                goto out_close;
61
 
        }
62
 
 
63
 
        INFO("tty %d allocated", ttynum);
 
41
        int ret, stopped = 0;
 
42
        struct lxc_command command = {
 
43
                .request = { .type = LXC_COMMAND_TTY, .data = ttynum },
 
44
        };
 
45
 
 
46
        ret = lxc_command(name, &command, &stopped);
 
47
        if (ret < 0 && stopped) {
 
48
                ERROR("'%s' is stopped", name);
 
49
                return -1;
 
50
        }
 
51
 
 
52
        if (ret < 0) {
 
53
                ERROR("failed to send command");
 
54
                return -1;
 
55
        }
64
56
 
65
57
        if (!ret) {
66
58
                ERROR("console denied by '%s'", name);
67
 
                goto out_close;
68
 
        }
69
 
 
70
 
        ret = 0;
71
 
 
72
 
out:
73
 
        return ret;
 
59
                return -1;
 
60
        }
 
61
 
 
62
        if (command.answer.ret) {
 
63
                ERROR("console access denied: %s",
 
64
                        strerror(-command.answer.ret));
 
65
                return -1;
 
66
        }
 
67
 
 
68
        *fd = command.answer.fd;
 
69
        if (*fd <0) {
 
70
                ERROR("unable to allocate fd for tty %d", ttynum);
 
71
                return -1;
 
72
        }
 
73
 
 
74
        INFO("tty %d allocated", ttynum);
 
75
        return 0;
 
76
}
 
77
 
 
78
/*----------------------------------------------------------------------------
 
79
 * functions used by lxc-start mainloop
 
80
 * to handle above command request.
 
81
 *--------------------------------------------------------------------------*/
 
82
extern void lxc_console_remove_fd(int fd, struct lxc_tty_info *tty_info)
 
83
{
 
84
        int i;
 
85
 
 
86
        for (i = 0; i < tty_info->nbtty; i++) {
 
87
 
 
88
                if (tty_info->pty_info[i].busy != fd)
 
89
                        continue;
 
90
 
 
91
                tty_info->pty_info[i].busy = 0;
 
92
        }
 
93
 
 
94
        return;
 
95
}
 
96
 
 
97
extern int lxc_console_callback(int fd, struct lxc_request *request,
 
98
                        struct lxc_handler *handler)
 
99
{
 
100
        int ttynum = request->data;
 
101
        struct lxc_tty_info *tty_info = &handler->conf.tty_info;
 
102
 
 
103
        if (ttynum > 0) {
 
104
                if (ttynum > tty_info->nbtty)
 
105
                        goto out_close;
 
106
 
 
107
                if (tty_info->pty_info[ttynum - 1].busy)
 
108
                        goto out_close;
 
109
 
 
110
                goto out_send;
 
111
        }
 
112
 
 
113
        /* fixup index tty1 => [0] */
 
114
        for (ttynum = 1;
 
115
             ttynum <= tty_info->nbtty && tty_info->pty_info[ttynum - 1].busy;
 
116
             ttynum++);
 
117
 
 
118
        /* we didn't find any available slot for tty */
 
119
        if (ttynum > tty_info->nbtty)
 
120
                goto out_close;
 
121
 
 
122
out_send:
 
123
        if (lxc_af_unix_send_fd(fd, tty_info->pty_info[ttynum - 1].master,
 
124
                                &ttynum, sizeof(ttynum)) < 0) {
 
125
                ERROR("failed to send tty to client");
 
126
                goto out_close;
 
127
        }
 
128
 
 
129
        tty_info->pty_info[ttynum - 1].busy = fd;
 
130
 
 
131
        return 0;
 
132
 
74
133
out_close:
75
 
        close(sock);
76
 
        goto out;
 
134
        /* the close fd and related cleanup will be done by caller */
 
135
        return 1;
77
136
}
 
137