...

Source file src/github.com/chaos-mesh/chaos-mesh/cmd/watchmaker/main_linux.go

Documentation: github.com/chaos-mesh/chaos-mesh/cmd/watchmaker

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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