Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lxc
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
lxc
Commits
7ee5bb55
Commit
7ee5bb55
authored
Feb 24, 2010
by
Daniel Lezcano
Committed by
Daniel Lezcano
Feb 24, 2010
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use a mainloop for the console
Use the mainloop to manage io of the console. Signed-off-by:
Daniel Lezcano
<
dlezcano@fr.ibm.com
>
parent
6dae6815
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
68 deletions
+81
-68
lxc_console.c
src/lxc/lxc_console.c
+81
-68
No files found.
src/lxc/lxc_console.c
View file @
7ee5bb55
...
@@ -38,10 +38,10 @@
...
@@ -38,10 +38,10 @@
#include <sys/poll.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
#include <sys/ioctl.h>
#include
<lxc/error.h>
#include
"error.h"
#include
<lxc/lxc.h>
#include
"lxc.h"
#include
<lxc/log.h>
#include
"log.h"
#include "mainloop.h"
#include "arguments.h"
#include "arguments.h"
lxc_log_define
(
lxc_console_ui
,
lxc_console
);
lxc_log_define
(
lxc_console_ui
,
lxc_console
);
...
@@ -102,7 +102,7 @@ static void sigwinch(int sig)
...
@@ -102,7 +102,7 @@ static void sigwinch(int sig)
static
int
setup_tios
(
int
fd
,
struct
termios
*
newtios
,
struct
termios
*
oldtios
)
static
int
setup_tios
(
int
fd
,
struct
termios
*
newtios
,
struct
termios
*
oldtios
)
{
{
if
(
isatty
(
fd
))
{
if
(
!
isatty
(
fd
))
{
ERROR
(
"'%d' is not a tty"
,
fd
);
ERROR
(
"'%d' is not a tty"
,
fd
);
return
-
1
;
return
-
1
;
}
}
...
@@ -132,21 +132,68 @@ static int setup_tios(int fd, struct termios *newtios, struct termios *oldtios)
...
@@ -132,21 +132,68 @@ static int setup_tios(int fd, struct termios *newtios, struct termios *oldtios)
return
0
;
return
0
;
}
}
static
int
stdin_handler
(
int
fd
,
void
*
data
,
struct
lxc_epoll_descr
*
descr
)
{
static
int
wait4q
=
0
;
int
*
peer
=
(
int
*
)
data
;
char
c
;
if
(
read
(
0
,
&
c
,
1
)
<
0
)
{
SYSERROR
(
"failed to read"
);
return
1
;
}
/* we want to exit the console with Ctrl+a q */
if
(
c
==
my_args
.
escape
)
{
wait4q
=
!
wait4q
;
return
0
;
}
if
(
c
==
'q'
&&
wait4q
)
return
1
;
wait4q
=
0
;
if
(
write
(
*
peer
,
&
c
,
1
)
<
0
)
{
SYSERROR
(
"failed to write"
);
return
1
;
}
return
0
;
}
static
int
master_handler
(
int
fd
,
void
*
data
,
struct
lxc_epoll_descr
*
descr
)
{
char
buf
[
1024
];
int
*
peer
=
(
int
*
)
data
;
int
r
;
r
=
read
(
fd
,
buf
,
sizeof
(
buf
));
if
(
r
<
0
)
{
SYSERROR
(
"failed to read"
);
return
1
;
}
write
(
*
peer
,
buf
,
r
);
return
0
;
}
int
main
(
int
argc
,
char
*
argv
[])
int
main
(
int
argc
,
char
*
argv
[])
{
{
int
wait4q
=
0
;
int
err
,
std_in
=
1
;
int
er
r
;
struct
lxc_epoll_descr
desc
r
;
struct
termios
newtios
,
oldtios
;
struct
termios
newtios
,
oldtios
;
err
=
lxc_arguments_parse
(
&
my_args
,
argc
,
argv
);
err
=
lxc_arguments_parse
(
&
my_args
,
argc
,
argv
);
if
(
err
)
if
(
err
)
return
-
1
;
return
-
1
;
if
(
lxc_log_init
(
my_args
.
log_file
,
my_args
.
log_priority
,
err
=
lxc_log_init
(
my_args
.
log_file
,
my_args
.
log_priority
,
my_args
.
progname
,
my_args
.
quiet
))
my_args
.
progname
,
my_args
.
quiet
);
if
(
err
)
return
-
1
;
return
-
1
;
if
(
setup_tios
(
0
,
&
newtios
,
&
oldtios
))
{
err
=
setup_tios
(
0
,
&
newtios
,
&
oldtios
);
if
(
err
)
{
ERROR
(
"failed to setup tios"
);
ERROR
(
"failed to setup tios"
);
return
-
1
;
return
-
1
;
}
}
...
@@ -158,77 +205,47 @@ int main(int argc, char *argv[])
...
@@ -158,77 +205,47 @@ int main(int argc, char *argv[])
fprintf
(
stderr
,
"
\n
Type <Ctrl+%c q> to exit the console
\n
"
,
fprintf
(
stderr
,
"
\n
Type <Ctrl+%c q> to exit the console
\n
"
,
'a'
+
my_args
.
escape
-
1
);
'a'
+
my_args
.
escape
-
1
);
if
(
setsid
())
err
=
setsid
();
if
(
err
)
INFO
(
"already group leader"
);
INFO
(
"already group leader"
);
if
(
signal
(
SIGWINCH
,
sigwinch
)
==
SIG_ERR
)
{
if
(
signal
(
SIGWINCH
,
sigwinch
)
==
SIG_ERR
)
{
SYSERROR
(
"failed to set SIGWINCH handler"
);
SYSERROR
(
"failed to set SIGWINCH handler"
);
return
-
1
;
err
=
-
1
;
goto
out
;
}
}
winsz
();
winsz
();
err
=
0
;
err
=
lxc_mainloop_open
(
&
descr
);
if
(
err
)
{
/* let's proxy the tty */
ERROR
(
"failed to create mainloop"
);
for
(;;)
{
goto
out
;
struct
pollfd
pfd
[
2
]
=
{
{
.
fd
=
0
,
.
events
=
POLLIN
|
POLLPRI
,
.
revents
=
0
},
{
.
fd
=
master
,
.
events
=
POLLIN
|
POLLPRI
,
.
revents
=
0
},
};
if
(
poll
(
pfd
,
2
,
-
1
)
<
0
)
{
if
(
errno
==
EINTR
)
continue
;
SYSERROR
(
"failed to poll"
);
goto
out_err
;
}
}
/* read the "stdin" and write that to the master
err
=
lxc_mainloop_add_handler
(
&
descr
,
0
,
stdin_handler
,
&
master
);
*/
if
(
err
)
{
if
(
pfd
[
0
].
revents
&
POLLIN
)
{
ERROR
(
"failed to add handler for the stdin"
);
char
c
;
goto
out_mainloop_open
;
if
(
read
(
0
,
&
c
,
1
)
<
0
)
{
SYSERROR
(
"failed to read"
);
goto
out_err
;
}
}
/* we want to exit the console with Ctrl+a q */
err
=
lxc_mainloop_add_handler
(
&
descr
,
master
,
master_handler
,
&
std_in
);
if
(
c
==
my_args
.
escape
)
{
if
(
err
)
{
wait4q
=
!
wait4q
;
ERROR
(
"failed to add handler for the master"
)
;
continue
;
goto
out_mainloop_open
;
}
}
if
(
c
==
'q'
&&
wait4q
)
err
=
lxc_mainloop
(
&
descr
);
goto
out
;
if
(
err
)
{
ERROR
(
"mainloop returned an error"
);
wait4q
=
0
;
goto
out_mainloop_open
;
if
(
write
(
master
,
&
c
,
1
)
<
0
)
{
SYSERROR
(
"failed to write"
);
goto
out_err
;
}
}
}
/* other side has closed the connection */
err
=
0
;
if
(
pfd
[
1
].
revents
&
POLLHUP
)
goto
out
;
out_mainloop_open:
lxc_mainloop_close
(
&
descr
);
/* read the master and write to "stdout" */
if
(
pfd
[
1
].
revents
&
POLLIN
)
{
char
buf
[
1024
];
int
r
;
r
=
read
(
master
,
buf
,
sizeof
(
buf
));
if
(
r
<
0
)
{
SYSERROR
(
"failed to read"
);
goto
out_err
;
}
write
(
1
,
buf
,
r
);
}
}
out:
out:
/* Restore previous terminal parameter */
/* Restore previous terminal parameter */
tcsetattr
(
0
,
TCSAFLUSH
,
&
oldtios
);
tcsetattr
(
0
,
TCSAFLUSH
,
&
oldtios
);
...
@@ -239,8 +256,4 @@ out:
...
@@ -239,8 +256,4 @@ out:
close
(
master
);
close
(
master
);
return
err
;
return
err
;
out_err:
err
=
-
1
;
goto
out
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment