@@ -69,6 +69,12 @@ For example, if the container id is "ubuntu01" the following will send a "KILL"
69
69
signal to the init process of the "ubuntu01" container:
70
70
71
71
# 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
+ },
72
78
Action : func (context * cli.Context ) error {
73
79
container := context .Args ().First ()
74
80
if container == "" {
@@ -88,17 +94,50 @@ signal to the init process of the "ubuntu01" container:
88
94
if err != nil {
89
95
return cli .NewExitError (fmt .Sprintf ("failed to get client: %v" , err ), - 1 )
90
96
}
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
+ }
97
116
}
98
117
return nil
99
118
},
100
119
}
101
120
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
+
102
141
func parseSignal (rawSignal string ) (syscall.Signal , error ) {
103
142
s , err := strconv .Atoi (rawSignal )
104
143
if err == nil {
0 commit comments