forked from chenhy97/chaosblade-exec-os
/
kernel_error.go
169 lines (147 loc) · 4.91 KB
/
kernel_error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package exec
import (
"context"
"fmt"
"path"
"strings"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
)
const StraceErrorBin = "chaos_straceerror"
type StraceErrorActionSpec struct {
spec.BaseExpActionCommandSpec
}
func NewStraceErrorActionSpec() spec.ExpActionCommandSpec {
return &StraceErrorActionSpec{
spec.BaseExpActionCommandSpec{
ActionMatchers: []spec.ExpFlagSpec{
&spec.ExpFlag{
Name: "pid",
Desc: "The Pid of the target process",
Required: true,
},
},
ActionFlags: []spec.ExpFlagSpec{
&spec.ExpFlag{
Name: "syscall-name",
Desc: "The target syscall which will be injected",
Required: true,
},
&spec.ExpFlag{
Name: "return-value",
Desc: "the return-value the syscall will return",
Required: true,
},
&spec.ExpFlag{
Name: "first",
Desc: "if the flag is true, the fault will be injected to the first met syscall",
},
&spec.ExpFlag{
Name: "end",
Desc: "if the flag is true, the fault will be injected to the last met syscall",
},
&spec.ExpFlag{
Name: "step",
Desc: "the fault will be injected intervally",
},
},
ActionExecutor: &StraceErrorActionExecutor{},
ActionExample: `
# Create a strace error experiment to the process
blade create strace error --pid 1 --syscall-name mmap --return-value XX`,
ActionPrograms: []string{StraceErrorBin},
},
}
}
func (*StraceErrorActionSpec) Name() string {
return "error"
}
func (*StraceErrorActionSpec) Aliases() []string {
return []string{}
}
func (*StraceErrorActionSpec) ShortDesc() string {
return "change the syscall's return value of the target pid"
}
func (f *StraceErrorActionSpec) LongDesc() string {
if f.ActionLongDesc != "" {
return f.ActionLongDesc
}
return "change the syscall's return value of the specified process, if the process exists"
}
type StraceErrorActionExecutor struct {
channel spec.Channel
}
func (dae *StraceErrorActionExecutor) SetChannel(channel spec.Channel) {
dae.channel = channel
}
func (*StraceErrorActionExecutor) Name() string {
return "error"
}
func (dae *StraceErrorActionExecutor) Exec(uid string, ctx context.Context, model *spec.ExpModel) *spec.Response{
if dae.channel == nil {
return spec.ReturnFail(spec.Code[spec.ServerError],"channel is nil")
}
var pidList string
var first_flag string
var end_flag string
var step string
pidStr := model.ActionFlags["pid"]
if pidStr != ""{
pids, err := util.ParseIntegerListToStringSlice(pidStr)
if err != nil {
return spec.ReturnFail(spec.Code[spec.IllegalParameters],
fmt.Sprintf("parse %s flag err, %v", "pid",err))
}
pidList = strings.Join(pids, ",")
}
return_value := model.ActionFlags["return-value"]
if return_value == "" {
return spec.ReturnFail(spec.Code[spec.IllegalParameters], "must specify --return-value flag")
}
syscallName := model.ActionFlags["syscall-name"]
if syscallName == "" {
return spec.ReturnFail(spec.Code[spec.IllegalParameters], "must specify --syscall-name flag")
}
first_flag = model.ActionFlags["first"]
end_flag = model.ActionFlags["end"]
step = model.ActionFlags["step"]
if _, ok := spec.IsDestroy(ctx); ok{
return dae.stop(ctx, pidList, syscallName)
}
// fmt.Printf("%s,%s,%s",first_flag,end_flag,step)
return dae.start(ctx, pidList, return_value, syscallName,first_flag,end_flag,step)
}
//start strace Error
func (dae *StraceErrorActionExecutor) start(ctx context.Context, pidList string,return_value string, syscallName string,first_flag string, end_flag string, step string) *spec.Response{
args := fmt.Sprintf("--start --pid %s --return-value %s --syscall-name %s ",pidList, return_value, syscallName)
if first_flag != ""{
args = fmt.Sprintf("%s --first %s", args, first_flag)
}
if end_flag != ""{
args = fmt.Sprintf("%s --end %s",args,end_flag)
}
if step != ""{
args = fmt.Sprintf("%s --step %s",args,step)
}
// fmt.Println(args)
return dae.channel.Run(ctx,path.Join(dae.channel.GetScriptPath(), StraceErrorBin), args)
}
func (dae *StraceErrorActionExecutor) stop(ctx context.Context, pidList string, syscallName string) *spec.Response{
args := fmt.Sprintf("--stop --pid %s --syscall-name %s",pidList,syscallName)
return dae.channel.Run(ctx, path.Join(dae.channel.GetScriptPath(), StraceErrorBin),args)
}