Skip to content

Commit b19b8a9

Browse files
author
Mrunal Patel
committed
Merge pull request docker-archive#407 from LK4D4/move_console_to_process
Move tty configuration to Process
2 parents f659381 + 8d0b062 commit b19b8a9

9 files changed

+41
-34
lines changed

configs/config.go

-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ type Config struct {
4949
// Hostname optionally sets the container's hostname if provided
5050
Hostname string `json:"hostname"`
5151

52-
// Console is the path to the console allocated to the container.
53-
Console string `json:"console"`
54-
5552
// Namespaces specifies the container's namespaces that it should setup when cloning the init process
5653
// If a namespace is not provided that namespace is shared from the container's parent process
5754
Namespaces Namespaces `json:"namespaces"`

console_linux.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
"github.com/docker/libcontainer/label"
1313
)
1414

15-
// NewConsole returns an initalized console that can be used within a container by copying bytes
15+
// newConsole returns an initalized console that can be used within a container by copying bytes
1616
// from the master side to the slave that is attached as the tty for the container's init process.
17-
func NewConsole(uid, gid int) (Console, error) {
17+
func newConsole(uid, gid int) (Console, error) {
1818
master, err := os.OpenFile("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0)
1919
if err != nil {
2020
return nil, err

container_linux.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,12 @@ func (c *linuxContainer) newSetnsProcess(p *Process, cmd *exec.Cmd, parentPipe,
181181

182182
func (c *linuxContainer) newInitConfig(process *Process) *initConfig {
183183
return &initConfig{
184-
Config: c.config,
185-
Args: process.Args,
186-
Env: process.Env,
187-
User: process.User,
188-
Cwd: process.Cwd,
184+
Config: c.config,
185+
Args: process.Args,
186+
Env: process.Env,
187+
User: process.User,
188+
Cwd: process.Cwd,
189+
Console: process.consolePath,
189190
}
190191
}
191192

init_linux.go

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type initConfig struct {
4545
Cwd string `json:"cwd"`
4646
User string `json:"user"`
4747
Config *configs.Config `json:"config"`
48+
Console string `json:"console"`
4849
Networks []*network `json:"network"`
4950
}
5051

nsinit/exec.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,11 @@ func execAction(context *cli.Context) {
4141
if err != nil {
4242
fatal(err)
4343
}
44-
rootuid, err := config.HostUID()
45-
if err != nil {
46-
fatal(err)
47-
}
48-
tty, err := newTty(context, rootuid)
49-
if err != nil {
50-
fatal(err)
51-
}
5244
created := false
5345
container, err := factory.Load(context.String("id"))
5446
if err != nil {
55-
if tty.console != nil {
56-
config.Console = tty.console.Path()
57-
}
5847
created = true
5948
if container, err = factory.Create(context.String("id"), config); err != nil {
60-
tty.Close()
6149
fatal(err)
6250
}
6351
}
@@ -70,6 +58,14 @@ func execAction(context *cli.Context) {
7058
Stdout: os.Stdout,
7159
Stderr: os.Stderr,
7260
}
61+
rootuid, err := config.HostUID()
62+
if err != nil {
63+
fatal(err)
64+
}
65+
tty, err := newTty(context, process, rootuid)
66+
if err != nil {
67+
fatal(err)
68+
}
7369
if err := tty.attach(process); err != nil {
7470
fatal(err)
7571
}

nsinit/tty.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"github.com/docker/libcontainer"
1010
)
1111

12-
func newTty(context *cli.Context, rootuid int) (*tty, error) {
12+
func newTty(context *cli.Context, p *libcontainer.Process, rootuid int) (*tty, error) {
1313
if context.Bool("tty") {
14-
console, err := libcontainer.NewConsole(rootuid, rootuid)
14+
console, err := p.NewConsole(rootuid)
1515
if err != nil {
1616
return nil, err
1717
}

process.go

+13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type Process struct {
3636
// Stderr is a pointer to a writer which receives the standard error stream.
3737
Stderr io.Writer
3838

39+
// consolePath is the path to the console allocated to the container.
40+
consolePath string
41+
3942
ops processOperations
4043
}
4144

@@ -63,3 +66,13 @@ func (p Process) Signal(sig os.Signal) error {
6366
}
6467
return p.ops.signal(sig)
6568
}
69+
70+
// NewConsole creates new console for process and returns it
71+
func (p *Process) NewConsole(rootuid int) (Console, error) {
72+
console, err := newConsole(rootuid, rootuid)
73+
if err != nil {
74+
return nil, err
75+
}
76+
p.consolePath = console.Path()
77+
return console, nil
78+
}

rootfs_linux.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var baseMounts = []*configs.Mount{
4141

4242
// setupRootfs sets up the devices, mount points, and filesystems for use inside a
4343
// new mount namespace.
44-
func setupRootfs(config *configs.Config) (err error) {
44+
func setupRootfs(config *configs.Config, console *linuxConsole) (err error) {
4545
if err := prepareRoot(config); err != nil {
4646
return newSystemError(err)
4747
}
@@ -53,7 +53,7 @@ func setupRootfs(config *configs.Config) (err error) {
5353
if err := createDevices(config); err != nil {
5454
return newSystemError(err)
5555
}
56-
if err := setupPtmx(config); err != nil {
56+
if err := setupPtmx(config, console); err != nil {
5757
return newSystemError(err)
5858
}
5959
// stdin, stdout and stderr could be pointing to /dev/null from parent namespace.
@@ -255,16 +255,15 @@ func setReadonly() error {
255255
return syscall.Mount("/", "/", "bind", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC, "")
256256
}
257257

258-
func setupPtmx(config *configs.Config) error {
258+
func setupPtmx(config *configs.Config, console *linuxConsole) error {
259259
ptmx := filepath.Join(config.Rootfs, "dev/ptmx")
260260
if err := os.Remove(ptmx); err != nil && !os.IsNotExist(err) {
261261
return err
262262
}
263263
if err := os.Symlink("pts/ptmx", ptmx); err != nil {
264264
return fmt.Errorf("symlink dev ptmx %s", err)
265265
}
266-
if config.Console != "" {
267-
console := newConsoleFromPath(config.Console)
266+
if console != nil {
268267
return console.mount(config.Rootfs, config.MountLabel, 0, 0)
269268
}
270269
return nil

standard_init_linux.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ func (l *linuxStandardInit) Init() error {
2020
if err := joinExistingNamespaces(l.config.Config.Namespaces); err != nil {
2121
return err
2222
}
23-
consolePath := l.config.Config.Console
24-
if consolePath != "" {
25-
console := newConsoleFromPath(consolePath)
23+
var console *linuxConsole
24+
if l.config.Console != "" {
25+
console = newConsoleFromPath(l.config.Console)
2626
if err := console.dupStdio(); err != nil {
2727
return err
2828
}
2929
}
3030
if _, err := syscall.Setsid(); err != nil {
3131
return err
3232
}
33-
if consolePath != "" {
33+
if console != nil {
3434
if err := system.Setctty(); err != nil {
3535
return err
3636
}
@@ -47,7 +47,7 @@ func (l *linuxStandardInit) Init() error {
4747
label.Init()
4848
// InitializeMountNamespace() can be executed only for a new mount namespace
4949
if l.config.Config.Namespaces.Contains(configs.NEWNS) {
50-
if err := setupRootfs(l.config.Config); err != nil {
50+
if err := setupRootfs(l.config.Config, console); err != nil {
5151
return err
5252
}
5353
}

0 commit comments

Comments
 (0)