...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package main
17
18 import (
19 "flag"
20 "fmt"
21 "os"
22 "strings"
23
24 "github.com/go-logr/zapr"
25 "go.uber.org/zap"
26
27 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/tasks"
28 "github.com/chaos-mesh/chaos-mesh/pkg/time"
29 "github.com/chaos-mesh/chaos-mesh/pkg/time/utils"
30 "github.com/chaos-mesh/chaos-mesh/pkg/version"
31 )
32
33 var (
34 pid int
35 secDelta int64
36 nsecDelta int64
37 printVersion bool
38 clockIdsSlice string
39 )
40
41 func initFlag() {
42 flag.IntVar(&pid, "pid", 0, "pid of target program")
43 flag.Int64Var(&secDelta, "sec_delta", 0, "delta time of sec field")
44 flag.Int64Var(&nsecDelta, "nsec_delta", 0, "delta time of nsec field")
45 flag.StringVar(&clockIdsSlice, "clk_ids", "CLOCK_REALTIME", "all affected clock ids split with \",\"")
46 flag.BoolVar(&printVersion, "version", false, "print version information and exit")
47
48 flag.Parse()
49 }
50
51 func main() {
52 fmt.Println("Watchmaker will not support recovery function in future," +
53 " please use time attack in chaosd.")
54 initFlag()
55
56 version.PrintVersionInfo("watchmaker")
57
58 if printVersion {
59 os.Exit(0)
60 }
61
62 zapLog, err := zap.NewDevelopment()
63 if err != nil {
64 panic(fmt.Sprintf("error while creating zap logger: %v", err))
65 }
66 log := zapr.NewLogger(zapLog)
67
68 clkIds := strings.Split(clockIdsSlice, ",")
69 mask, err := utils.EncodeClkIds(clkIds)
70 if err != nil {
71 log.Error(err, "error while converting clock ids to mask")
72 os.Exit(1)
73 }
74 log.Info("get clock ids mask", "mask", mask)
75
76 s, err := time.GetSkew(log, time.NewConfig(secDelta, nsecDelta, mask))
77 if err != nil {
78 log.Error(err, "error while GetSkew")
79 os.Exit(1)
80 }
81 err = s.Inject(tasks.SysPID(pid))
82
83 if err != nil {
84 log.Error(err, "error while modifying time", "pid", pid, "secDelta", secDelta, "nsecDelta", nsecDelta, "mask", mask)
85 }
86 }
87