Skip to content

Commit a435f28

Browse files
committed
separate proc interfaces into standalone package
Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
1 parent 00d4910 commit a435f28

File tree

7 files changed

+109
-84
lines changed

7 files changed

+109
-84
lines changed

runtime/linux/proc/deleted_state.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"context"
2323

2424
"github.com/containerd/console"
25+
"github.com/containerd/containerd/runtime/proc"
2526
google_protobuf "github.com/gogo/protobuf/types"
2627
"github.com/pkg/errors"
2728
)
@@ -65,6 +66,6 @@ func (s *deletedState) SetExited(status int) {
6566
// no op
6667
}
6768

68-
func (s *deletedState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) {
69+
func (s *deletedState) Exec(ctx context.Context, path string, r *ExecConfig) (proc.Process, error) {
6970
return nil, errors.Errorf("cannot exec in a deleted state")
7071
}

runtime/linux/proc/exec.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"golang.org/x/sys/unix"
3232

3333
"github.com/containerd/console"
34+
"github.com/containerd/containerd/runtime/proc"
3435
"github.com/containerd/fifo"
3536
runc "github.com/containerd/go-runc"
3637
specs "github.com/opencontainers/runtime-spec/specs-go"
@@ -40,7 +41,7 @@ import (
4041
type execProcess struct {
4142
wg sync.WaitGroup
4243

43-
State
44+
proc.State
4445

4546
mu sync.Mutex
4647
id string
@@ -51,7 +52,7 @@ type execProcess struct {
5152
pid int
5253
closers []io.Closer
5354
stdin io.Closer
54-
stdio Stdio
55+
stdio proc.Stdio
5556
path string
5657
spec specs.Process
5758

@@ -127,7 +128,7 @@ func (e *execProcess) Stdin() io.Closer {
127128
return e.stdin
128129
}
129130

130-
func (e *execProcess) Stdio() Stdio {
131+
func (e *execProcess) Stdio() proc.Stdio {
131132
return e.stdio
132133
}
133134

runtime/linux/proc/init.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/containerd/containerd/log"
3535
"github.com/containerd/containerd/mount"
3636
"github.com/containerd/containerd/runtime/linux/runctypes"
37+
"github.com/containerd/containerd/runtime/proc"
3738
"github.com/containerd/fifo"
3839
runc "github.com/containerd/go-runc"
3940
"github.com/containerd/typeurl"
@@ -63,15 +64,15 @@ type Init struct {
6364
id string
6465
bundle string
6566
console console.Console
66-
platform Platform
67+
platform proc.Platform
6768
io runc.IO
6869
runtime *runc.Runc
6970
status int
7071
exited time.Time
7172
pid int
7273
closers []io.Closer
7374
stdin io.Closer
74-
stdio Stdio
75+
stdio proc.Stdio
7576
rootfs string
7677
IoUID int
7778
IoGID int
@@ -94,7 +95,7 @@ func NewRunc(root, path, namespace, runtime, criu string, systemd bool) *runc.Ru
9495
}
9596

9697
// New returns a new init process
97-
func New(context context.Context, path, workDir, runtimeRoot, namespace, criu string, systemdCgroup bool, platform Platform, r *CreateConfig) (*Init, error) {
98+
func New(context context.Context, path, workDir, runtimeRoot, namespace, criu string, systemdCgroup bool, platform proc.Platform, r *CreateConfig) (*Init, error) {
9899
var success bool
99100

100101
var options runctypes.CreateOptions
@@ -134,7 +135,7 @@ func New(context context.Context, path, workDir, runtimeRoot, namespace, criu st
134135
bundle: r.Bundle,
135136
runtime: runtime,
136137
platform: platform,
137-
stdio: Stdio{
138+
stdio: proc.Stdio{
138139
Stdin: r.Stdin,
139140
Stdout: r.Stdout,
140141
Stderr: r.Stderr,
@@ -363,7 +364,7 @@ func (p *Init) Runtime() *runc.Runc {
363364
}
364365

365366
// exec returns a new exec'd process
366-
func (p *Init) exec(context context.Context, path string, r *ExecConfig) (Process, error) {
367+
func (p *Init) exec(context context.Context, path string, r *ExecConfig) (proc.Process, error) {
367368
// process exec request
368369
var spec specs.Process
369370
if err := json.Unmarshal(r.Spec.Value, &spec); err != nil {
@@ -376,7 +377,7 @@ func (p *Init) exec(context context.Context, path string, r *ExecConfig) (Proces
376377
path: path,
377378
parent: p,
378379
spec: spec,
379-
stdio: Stdio{
380+
stdio: proc.Stdio{
380381
Stdin: r.Stdin,
381382
Stdout: r.Stdout,
382383
Stderr: r.Stderr,
@@ -430,7 +431,7 @@ func (p *Init) update(context context.Context, r *google_protobuf.Any) error {
430431
}
431432

432433
// Stdio of the process
433-
func (p *Init) Stdio() Stdio {
434+
func (p *Init) Stdio() proc.Stdio {
434435
return p.stdio
435436
}
436437

runtime/linux/proc/init_state.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,21 @@ import (
2525

2626
"github.com/containerd/console"
2727
"github.com/containerd/containerd/errdefs"
28+
"github.com/containerd/containerd/runtime/proc"
2829
"github.com/containerd/fifo"
2930
runc "github.com/containerd/go-runc"
3031
google_protobuf "github.com/gogo/protobuf/types"
3132
"github.com/pkg/errors"
3233
)
3334

3435
type initState interface {
35-
State
36+
proc.State
3637

3738
Pause(context.Context) error
3839
Resume(context.Context) error
3940
Update(context.Context, *google_protobuf.Any) error
4041
Checkpoint(context.Context, *CheckpointConfig) error
41-
Exec(context.Context, string, *ExecConfig) (Process, error)
42+
Exec(context.Context, string, *ExecConfig) (proc.Process, error)
4243
}
4344

4445
type createdState struct {
@@ -130,7 +131,7 @@ func (s *createdState) SetExited(status int) {
130131
}
131132
}
132133

133-
func (s *createdState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) {
134+
func (s *createdState) Exec(ctx context.Context, path string, r *ExecConfig) (proc.Process, error) {
134135
s.p.mu.Lock()
135136
defer s.p.mu.Unlock()
136137
return s.p.exec(ctx, path, r)
@@ -272,7 +273,7 @@ func (s *createdCheckpointState) SetExited(status int) {
272273
}
273274
}
274275

275-
func (s *createdCheckpointState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) {
276+
func (s *createdCheckpointState) Exec(ctx context.Context, path string, r *ExecConfig) (proc.Process, error) {
276277
s.p.mu.Lock()
277278
defer s.p.mu.Unlock()
278279

@@ -364,7 +365,7 @@ func (s *runningState) SetExited(status int) {
364365
}
365366
}
366367

367-
func (s *runningState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) {
368+
func (s *runningState) Exec(ctx context.Context, path string, r *ExecConfig) (proc.Process, error) {
368369
s.p.mu.Lock()
369370
defer s.p.mu.Unlock()
370371
return s.p.exec(ctx, path, r)
@@ -456,7 +457,7 @@ func (s *pausedState) SetExited(status int) {
456457
}
457458
}
458459

459-
func (s *pausedState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) {
460+
func (s *pausedState) Exec(ctx context.Context, path string, r *ExecConfig) (proc.Process, error) {
460461
s.p.mu.Lock()
461462
defer s.p.mu.Unlock()
462463

@@ -536,7 +537,7 @@ func (s *stoppedState) SetExited(status int) {
536537
// no op
537538
}
538539

539-
func (s *stoppedState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) {
540+
func (s *stoppedState) Exec(ctx context.Context, path string, r *ExecConfig) (proc.Process, error) {
540541
s.p.mu.Lock()
541542
defer s.p.mu.Unlock()
542543

runtime/linux/proc/process.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,12 @@
1919
package proc
2020

2121
import (
22-
"context"
23-
"io"
24-
"sync"
25-
"time"
26-
27-
"github.com/containerd/console"
2822
"github.com/pkg/errors"
2923
)
3024

3125
// RuncRoot is the path to the root runc state directory
3226
const RuncRoot = "/run/containerd/runc"
3327

34-
// Stdio of a process
35-
type Stdio struct {
36-
Stdin string
37-
Stdout string
38-
Stderr string
39-
Terminal bool
40-
}
41-
42-
// IsNull returns true if the stdio is not defined
43-
func (s Stdio) IsNull() bool {
44-
return s.Stdin == "" && s.Stdout == "" && s.Stderr == ""
45-
}
46-
47-
// Process on a linux system
48-
type Process interface {
49-
State
50-
// ID returns the id for the process
51-
ID() string
52-
// Pid returns the pid for the process
53-
Pid() int
54-
// ExitStatus returns the exit status
55-
ExitStatus() int
56-
// ExitedAt is the time the process exited
57-
ExitedAt() time.Time
58-
// Stdin returns the process STDIN
59-
Stdin() io.Closer
60-
// Stdio returns io information for the container
61-
Stdio() Stdio
62-
// Status returns the process status
63-
Status(context.Context) (string, error)
64-
// Wait blocks until the process has exited
65-
Wait()
66-
}
67-
68-
// State of a process
69-
type State interface {
70-
// Resize resizes the process console
71-
Resize(ws console.WinSize) error
72-
// Start execution of the process
73-
Start(context.Context) error
74-
// Delete deletes the process and its resourcess
75-
Delete(context.Context) error
76-
// Kill kills the process
77-
Kill(context.Context, uint32, bool) error
78-
// SetExited sets the exit status for the process
79-
SetExited(status int)
80-
}
81-
8228
func stateName(v interface{}) string {
8329
switch v.(type) {
8430
case *runningState, *execRunningState:
@@ -94,12 +40,3 @@ func stateName(v interface{}) string {
9440
}
9541
panic(errors.Errorf("invalid state %v", v))
9642
}
97-
98-
// Platform handles platform-specific behavior that may differs across
99-
// platform implementations
100-
type Platform interface {
101-
CopyConsole(ctx context.Context, console console.Console, stdin, stdout, stderr string,
102-
wg, cwg *sync.WaitGroup) (console.Console, error)
103-
ShutdownConsole(ctx context.Context, console console.Console) error
104-
Close() error
105-
}

runtime/proc/proc.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package proc
18+
19+
import (
20+
"context"
21+
"io"
22+
"sync"
23+
"time"
24+
25+
"github.com/containerd/console"
26+
)
27+
28+
// Stdio of a process
29+
type Stdio struct {
30+
Stdin string
31+
Stdout string
32+
Stderr string
33+
Terminal bool
34+
}
35+
36+
// IsNull returns true if the stdio is not defined
37+
func (s Stdio) IsNull() bool {
38+
return s.Stdin == "" && s.Stdout == "" && s.Stderr == ""
39+
}
40+
41+
// Process on a system
42+
type Process interface {
43+
State
44+
// ID returns the id for the process
45+
ID() string
46+
// Pid returns the pid for the process
47+
Pid() int
48+
// ExitStatus returns the exit status
49+
ExitStatus() int
50+
// ExitedAt is the time the process exited
51+
ExitedAt() time.Time
52+
// Stdin returns the process STDIN
53+
Stdin() io.Closer
54+
// Stdio returns io information for the container
55+
Stdio() Stdio
56+
// Status returns the process status
57+
Status(context.Context) (string, error)
58+
// Wait blocks until the process has exited
59+
Wait()
60+
}
61+
62+
// State of a process
63+
type State interface {
64+
// Resize resizes the process console
65+
Resize(ws console.WinSize) error
66+
// Start execution of the process
67+
Start(context.Context) error
68+
// Delete deletes the process and its resourcess
69+
Delete(context.Context) error
70+
// Kill kills the process
71+
Kill(context.Context, uint32, bool) error
72+
// SetExited sets the exit status for the process
73+
SetExited(status int)
74+
}
75+
76+
// Platform handles platform-specific behavior that may differs across
77+
// platform implementations
78+
type Platform interface {
79+
CopyConsole(ctx context.Context, console console.Console, stdin, stdout, stderr string,
80+
wg, cwg *sync.WaitGroup) (console.Console, error)
81+
ShutdownConsole(ctx context.Context, console console.Console) error
82+
Close() error
83+
}

runtime/shim/service.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/containerd/containerd/runtime"
3535
"github.com/containerd/containerd/runtime/linux/proc"
3636
"github.com/containerd/containerd/runtime/linux/runctypes"
37+
rproc "github.com/containerd/containerd/runtime/proc"
3738
shimapi "github.com/containerd/containerd/runtime/shim/v1"
3839
runc "github.com/containerd/go-runc"
3940
"github.com/containerd/typeurl"
@@ -78,7 +79,7 @@ func NewService(config Config, publisher events.Publisher) (*Service, error) {
7879
s := &Service{
7980
config: config,
8081
context: ctx,
81-
processes: make(map[string]proc.Process),
82+
processes: make(map[string]rproc.Process),
8283
events: make(chan interface{}, 128),
8384
ec: Default.Subscribe(),
8485
}
@@ -96,9 +97,9 @@ type Service struct {
9697

9798
config Config
9899
context context.Context
99-
processes map[string]proc.Process
100+
processes map[string]rproc.Process
100101
events chan interface{}
101-
platform proc.Platform
102+
platform rproc.Platform
102103
ec chan runc.Exit
103104

104105
// Filled by Create()

0 commit comments

Comments
 (0)