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
a8a0f65d
Commit
a8a0f65d
authored
Mar 04, 2016
by
Serge Hallyn
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #873 from brauner/bugfix_branch
lxc-attach: fix redirection - stderr
parents
6861fb22
8d1ea537
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
170 additions
and
1 deletion
+170
-1
lxc_attach.c
src/lxc/lxc_attach.c
+33
-1
Makefile.am
src/tests/Makefile.am
+2
-0
lxc-test-lxc-attach
src/tests/lxc-test-lxc-attach
+135
-0
No files found.
src/lxc/lxc_attach.c
View file @
a8a0f65d
...
...
@@ -273,7 +273,39 @@ static int get_pty_on_host(struct lxc_container *c, struct wrapargs *wrap, int *
if
(
lxc_console_create
(
conf
)
<
0
)
return
-
1
;
ts
=
conf
->
console
.
tty_state
;
ts
->
stdoutfd
=
STDOUT_FILENO
;
/*
* We need to make sure that the ouput that is produced inside the
* container is received on the host. Suppose we want to run
*
* lxc-attach -n a -- /bin/sh -c 'hostname >&2' > /dev/null
*
* This command produces output on stderr inside the container. On the
* host we close stdout by redirecting it to /dev/null. But stderr is,
* as expected, still connected to a tty. We receive the output produced
* on stderr in the container on stderr on the host.
*
* For the command
*
* lxc-attach -n a -- /bin/sh -c 'hostname >&2' 2> /dev/null
*
* the logic is analogous but because we now have closed stderr on the
* host no output will be received.
*
* Finally, imagine a more complicated case
*
* lxc-attach -n a -- /bin/sh -c 'echo OUT; echo ERR >&2' > /tmp/out 2> /tmp/err
*
* Here, we produce output in the container on stdout and stderr. On the
* host we redirect stdout and stderr to files. Because of that stdout
* and stderr are not dup2()ed. Thus, they are not connected to a pty
* and output on stdout and stderr is redirected to the corresponding
* files as expected.
*/
if
(
!
isatty
(
STDOUT_FILENO
)
&&
isatty
(
STDERR_FILENO
))
ts
->
stdoutfd
=
STDERR_FILENO
;
else
ts
->
stdoutfd
=
STDOUT_FILENO
;
conf
->
console
.
descr
=
&
descr
;
/* Shift ttys to container. */
...
...
src/tests/Makefile.am
View file @
a8a0f65d
...
...
@@ -52,6 +52,7 @@ bin_SCRIPTS = lxc-test-autostart lxc-test-cloneconfig lxc-test-createconfig
if
DISTRO_UBUNTU
bin_SCRIPTS
+=
\
lxc-test-lxc-attach
\
lxc-test-apparmor-mount
\
lxc-test-checkpoint-restore
\
lxc-test-snapdeps
\
...
...
@@ -77,6 +78,7 @@ EXTRA_DIST = \
list.c
\
locktests.c
\
lxcpath.c
\
lxc-test-lxc-attach
\
lxc-test-autostart
\
lxc-test-apparmor-mount
\
lxc-test-checkpoint-restore
\
...
...
src/tests/lxc-test-lxc-attach
0 → 100755
View file @
a8a0f65d
#!/bin/sh
# lxc: linux Container library
# Authors:
# Christian Brauner <christian.brauner@mailbox.org>
#
# This is a test script for the lxc-attach program. It tests whether I/O
# redirection works correctly.
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
set
-e
FAIL
()
{
echo
-n
"Failed "
>
&2
echo
"
$*
"
>
&2
lxc-destroy
-n
busy
-f
exit
1
}
# Create a container, start it and wait for it to be in running state
lxc-create
-t
busybox
-n
busy
||
FAIL
"creating busybox container"
lxc-start
-n
busy
-d
||
FAIL
"starting busybox container"
lxc-wait
-n
busy
-s
RUNNING
||
FAIL
"waiting for busybox container to run"
# Test if simple attach is working
# - stdin --> attached to pty
# - stdout --> attached to pty
# - stderr --> attached to pty
attach
=
$(
lxc-attach
-n
busy
--
hostname
)
# expect output
if
[
"
$attach
"
!=
"busy"
]
;
then
FAIL
" simple attach"
fi
# Test if we still receive output when stdin does not refer to a tty
# - stdin --> /dev/null
# - stdout --> attached to pty
# - stderr --> attached to pty
attach
=
$(
lxc-attach
-n
busy
--
hostname
< /dev/null
)
# expect output
if
[
"
$attach
"
!=
"busy"
]
;
then
FAIL
" with stdin redirection < /dev/null"
fi
# Test if we are silent when stdout does not refer to a tty
# - stdin --> attached to pty
# - stdout --> /dev/null
# - stderr --> attached to pty
attach
=
$(
lxc-attach
-n
busy
--
hostname
>
/dev/null
)
# expect no output
if
[
-n
"
$attach
"
]
;
then
FAIL
" with stdout redirection > /dev/null"
fi
# Test if we are silent when stdout and stdin do not refer to a tty
# - stdin --> /dev/null
# - stdout --> /dev/null
# - stderr --> attached to pty
attach
=
$(
lxc-attach
-n
busy
--
hostname
>
/dev/null < /dev/null
)
# expect no output
if
[
-n
"
$attach
"
]
;
then
FAIL
" with stdin and stdout redirection > /dev/null < /dev/null"
fi
# Test if we still receive output when stdin and stderr do not refer to a tty
# - stdin --> /dev/null
# - stdout --> attached to pty
# - stderr --> /dev/null
attach
=
$(
lxc-attach
-n
busy
--
hostname
2> /dev/null < /dev/null
)
# expect output
if
[
"
$attach
"
!=
"busy"
]
;
then
FAIL
" with stdin and stderr redirection 2> /dev/null < /dev/null"
fi
# - produce output on stderr in container
# - redirect stdout on host to /dev/null
# - output on host should be received on stderr
# - to capture the ouput on stderr on the host we redirect stderr on the host to
# stdout
# - stdin --> attached to pty
# - stdout --> /dev/null
# - stderr --> attached to pty
attach
=
$(
(
lxc-attach
-n
busy
--
sh
-c
'hostname >&2'
>
/dev/null
)
2>&1
)
if
[
"
$attach
"
!=
"busy"
]
;
then
FAIL
" bla sh -c 'hostname >&2' >/dev/null"
fi
# - produce output on stderr in container
# - redirect stderr on host to /dev/null
# - no output from container should be received on stderr on host
# - stdin --> attached to pty
# - stdout --> attached to pty
# - stderr --> /dev/null
attach
=
$(
(
lxc-attach
-n
busy
--
sh
-c
'hostname >&2'
2> /dev/null
)
2>&1
)
if
[
-n
"
$attach
"
]
;
then
FAIL
" bla sh -c 'hostname >&2' 2> /dev/null"
fi
# Test file redirection
# - stdin --> attached to pty
# - stdout --> /dev/null
# - stderr --> /dev/null
out
=
$(
mktemp
/tmp/out_XXXX
)
err
=
$(
mktemp
/tmp/err_XXXX
)
lxc-attach
-n
busy
--
sh
-c
'echo OUT; echo ERR >&2'
>
$out
2>
$err
outcontent
=
$(
cat
$out
)
errcontent
=
$(
cat
$err
)
if
[
"
$outcontent
"
!=
"OUT"
]
||
[
"
$errcontent
"
!=
"ERR"
]
;
then
FAIL
" bla sh -c 'echo OUT; echo ERR >&2' >
$out
2>
$err
"
fi
# Test stdin input
# - stdin --> $in
# - stdout --> attached to pty
# - stderr --> attached to pty
in
=
$(
mktemp
/tmp/in_XXXX
)
echo
'#!/bin/sh'
>
$in
echo hostname
>>
$in
attach
=
$(
lxc-attach
-n
busy
--
<
$in
)
if
[
"
$attach
"
!=
"busy"
]
;
then
FAIL
" <
$in
"
fi
lxc-destroy
-n
busy
-f
exit
0
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