Browse code

Store the master ptys in order to close them when the process dies (#228)

Guillaume J. Charmes authored on 2013/03/31 01:05:53
Showing 1 changed files
... ...
@@ -40,6 +40,10 @@ type Container struct {
40 40
 	stdin       io.ReadCloser
41 41
 	stdinPipe   io.WriteCloser
42 42
 
43
+	ptyStdinMaster  io.Closer
44
+	ptyStdoutMaster io.Closer
45
+	ptyStderrMaster io.Closer
46
+
43 47
 	runtime *Runtime
44 48
 }
45 49
 
... ...
@@ -151,12 +155,14 @@ func (container *Container) startPty() error {
151 151
 	if err != nil {
152 152
 		return err
153 153
 	}
154
+	container.ptyStdoutMaster = stdoutMaster
154 155
 	container.cmd.Stdout = stdoutSlave
155 156
 
156 157
 	stderrMaster, stderrSlave, err := pty.Open()
157 158
 	if err != nil {
158 159
 		return err
159 160
 	}
161
+	container.ptyStderrMaster = stderrMaster
160 162
 	container.cmd.Stderr = stderrSlave
161 163
 
162 164
 	// Copy the PTYs to our broadcasters
... ...
@@ -181,6 +187,7 @@ func (container *Container) startPty() error {
181 181
 		if err != nil {
182 182
 			return err
183 183
 		}
184
+		container.ptyStdinMaster = stdinMaster
184 185
 		container.cmd.Stdin = stdinSlave
185 186
 		// FIXME: The following appears to be broken.
186 187
 		// "cannot set terminal process group (-1): Inappropriate ioctl for device"
... ...
@@ -380,6 +387,23 @@ func (container *Container) monitor() {
380 380
 	if err := container.stderr.Close(); err != nil {
381 381
 		Debugf("%s: Error close stderr: %s", container.Id, err)
382 382
 	}
383
+
384
+	if container.ptyStdinMaster != nil {
385
+		if err := container.ptyStdinMaster.Close(); err != nil {
386
+			Debugf("%s: Error close pty stdin master: %s", container.Id, err)
387
+		}
388
+	}
389
+	if container.ptyStdoutMaster != nil {
390
+		if err := container.ptyStdoutMaster.Close(); err != nil {
391
+			Debugf("%s: Error close pty stdout master: %s", container.Id, err)
392
+		}
393
+	}
394
+	if container.ptyStderrMaster != nil {
395
+		if err := container.ptyStderrMaster.Close(); err != nil {
396
+			Debugf("%s: Error close pty stderr master: %s", container.Id, err)
397
+		}
398
+	}
399
+
383 400
 	if err := container.Unmount(); err != nil {
384 401
 		log.Printf("%v: Failed to umount filesystem: %v", container.Id, err)
385 402
 	}