Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 1fb28a3

Browse files
committed
kill: add kill -a option
Adds the -a parameter to the kill runv command, allowing to kill all processes in the container. Signed-off-by: Antonios Motakis <antonios.motakis@huawei.com>
1 parent 7446ee7 commit 1fb28a3

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

kill.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ For example, if the container id is "ubuntu01" the following will send a "KILL"
6969
signal to the init process of the "ubuntu01" container:
7070
7171
# runv kill ubuntu01 KILL`,
72+
Flags: []cli.Flag{
73+
cli.BoolFlag{
74+
Name: "all, a",
75+
Usage: "send the signal to all processes in the container",
76+
},
77+
},
7278
Action: func(context *cli.Context) error {
7379
container := context.Args().First()
7480
if container == "" {
@@ -88,17 +94,50 @@ signal to the init process of the "ubuntu01" container:
8894
if err != nil {
8995
return cli.NewExitError(fmt.Sprintf("failed to get client: %v", err), -1)
9096
}
91-
if _, err = c.Signal(netcontext.Background(), &types.SignalRequest{
92-
Id: container,
93-
Pid: "init",
94-
Signal: uint32(signal),
95-
}); err != nil {
96-
return cli.NewExitError(fmt.Sprintf("kill signal failed, %v", err), -1)
97+
98+
plist := make([]string, 0)
99+
100+
if context.Bool("all") {
101+
if plist, err = getProcessList(c, container); err != nil {
102+
return cli.NewExitError(fmt.Sprintf("can't get process list, %v", err), -1)
103+
}
104+
} else {
105+
plist = append(plist, "init")
106+
}
107+
108+
for _, p := range plist {
109+
if _, err = c.Signal(netcontext.Background(), &types.SignalRequest{
110+
Id: container,
111+
Pid: p,
112+
Signal: uint32(signal),
113+
}); err != nil {
114+
return cli.NewExitError(fmt.Sprintf("kill signal failed, %v", err), -1)
115+
}
97116
}
98117
return nil
99118
},
100119
}
101120

121+
func getProcessList(c types.APIClient, container string) ([]string, error) {
122+
s, err := c.State(netcontext.Background(), &types.StateRequest{Id: container})
123+
if err != nil {
124+
return nil, fmt.Errorf("get container state failed, %v", err)
125+
}
126+
127+
for _, cc := range s.Containers {
128+
if cc.Id == container {
129+
plist := make([]string, 0)
130+
for _, p := range cc.Processes {
131+
plist = append(plist, p.Pid)
132+
}
133+
134+
return plist, nil
135+
}
136+
}
137+
138+
return nil, fmt.Errorf("container %s not found", container)
139+
}
140+
102141
func parseSignal(rawSignal string) (syscall.Signal, error) {
103142
s, err := strconv.Atoi(rawSignal)
104143
if err == nil {

supervisor/process.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ func (p *Process) signal(sig int) error {
9393
// TODO: change vm.KillContainer()
9494
return p.ownerCont.ownerPod.vm.KillContainer(p.ownerCont.Id, syscall.Signal(sig))
9595
} else {
96-
// TODO support it
97-
return fmt.Errorf("Kill to non-init process of container is unsupported")
96+
return p.ownerCont.ownerPod.vm.SignalProcess(p.ownerCont.Id, p.Id, syscall.Signal(sig))
9897
}
9998
}
10099

0 commit comments

Comments
 (0)