|
| 1 | +package criu |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/golang/protobuf/proto" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "rpc" |
| 10 | + "strconv" |
| 11 | + "syscall" |
| 12 | +) |
| 13 | + |
| 14 | +type Criu struct { |
| 15 | + swrk_cmd *exec.Cmd |
| 16 | + swrk_sk *os.File |
| 17 | +} |
| 18 | + |
| 19 | +func MakeCriu() *Criu { |
| 20 | + return &Criu{} |
| 21 | +} |
| 22 | + |
| 23 | +func (c *Criu) Prepare() error { |
| 24 | + fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_SEQPACKET, 0) |
| 25 | + if err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + |
| 29 | + cln := os.NewFile(uintptr(fds[0]), "criu-xprt-cln") |
| 30 | + syscall.CloseOnExec(fds[0]) |
| 31 | + srv := os.NewFile(uintptr(fds[1]), "criu-xprt-srv") |
| 32 | + defer srv.Close() |
| 33 | + |
| 34 | + args := []string{"swrk", strconv.Itoa(fds[1])} |
| 35 | + cmd := exec.Command("criu", args...) |
| 36 | + |
| 37 | + err = cmd.Start() |
| 38 | + if err != nil { |
| 39 | + cln.Close() |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + c.swrk_cmd = cmd |
| 44 | + c.swrk_sk = cln |
| 45 | + |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +func (c *Criu) Cleanup() { |
| 50 | + if c.swrk_cmd != nil { |
| 51 | + c.swrk_sk.Close() |
| 52 | + c.swrk_sk = nil |
| 53 | + c.swrk_cmd.Wait() |
| 54 | + c.swrk_cmd = nil |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (c *Criu) sendAndRecv(req_b []byte) ([]byte, int, error) { |
| 59 | + cln := c.swrk_sk |
| 60 | + _, err := cln.Write(req_b) |
| 61 | + if err != nil { |
| 62 | + return nil, 0, err |
| 63 | + } |
| 64 | + |
| 65 | + resp_b := make([]byte, 2*4096) |
| 66 | + n, err := cln.Read(resp_b) |
| 67 | + if err != nil { |
| 68 | + return nil, 0, err |
| 69 | + } |
| 70 | + |
| 71 | + return resp_b, n, nil |
| 72 | +} |
| 73 | + |
| 74 | +func (c *Criu) doSwrk(req_type rpc.CriuReqType, opts *rpc.CriuOpts, nfy CriuNotify) error { |
| 75 | + req := rpc.CriuReq{ |
| 76 | + Type: &req_type, |
| 77 | + Opts: opts, |
| 78 | + } |
| 79 | + |
| 80 | + if nfy != nil { |
| 81 | + opts.NotifyScripts = proto.Bool(true) |
| 82 | + } |
| 83 | + |
| 84 | + if c.swrk_cmd == nil { |
| 85 | + err := c.Prepare() |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + defer c.Cleanup() |
| 91 | + } |
| 92 | + |
| 93 | + for { |
| 94 | + req_b, err := proto.Marshal(&req) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + resp_b, resp_s, err := c.sendAndRecv(req_b) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + resp := &rpc.CriuResp{} |
| 105 | + err = proto.Unmarshal(resp_b[:resp_s], resp) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + if !resp.GetSuccess() { |
| 111 | + return fmt.Errorf("operation failed (msg:%s err:%d)", |
| 112 | + resp.GetCrErrmsg(), resp.GetCrErrno()) |
| 113 | + } |
| 114 | + |
| 115 | + resp_type := resp.GetType() |
| 116 | + if resp_type == req_type { |
| 117 | + break |
| 118 | + } |
| 119 | + if resp_type != rpc.CriuReqType_NOTIFY { |
| 120 | + return errors.New("unexpected responce") |
| 121 | + } |
| 122 | + if nfy == nil { |
| 123 | + return errors.New("unexpected notify") |
| 124 | + } |
| 125 | + |
| 126 | + notify := resp.GetNotify() |
| 127 | + switch notify.GetScript() { |
| 128 | + case "pre-dump": |
| 129 | + err = nfy.PreDump() |
| 130 | + case "post-dump": |
| 131 | + err = nfy.PostDump() |
| 132 | + case "pre-restore": |
| 133 | + err = nfy.PreRestore() |
| 134 | + case "post-restore": |
| 135 | + err = nfy.PostRestore(notify.GetPid()) |
| 136 | + case "network-lock": |
| 137 | + err = nfy.NetworkLock() |
| 138 | + case "network-unlock": |
| 139 | + err = nfy.NetworkUnlock() |
| 140 | + case "setup-namespaces": |
| 141 | + err = nfy.SetupNamespaces(notify.GetPid()) |
| 142 | + case "post-setup-namespaces": |
| 143 | + err = nfy.PostSetupNamespaces() |
| 144 | + case "post-resume": |
| 145 | + err = nfy.PostResume() |
| 146 | + default: |
| 147 | + err = nil |
| 148 | + } |
| 149 | + |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + req = rpc.CriuReq{ |
| 155 | + Type: &resp_type, |
| 156 | + NotifySuccess: proto.Bool(true), |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +func (c *Criu) Dump(opts rpc.CriuOpts, nfy CriuNotify) error { |
| 164 | + return c.doSwrk(rpc.CriuReqType_DUMP, &opts, nfy) |
| 165 | +} |
| 166 | + |
| 167 | +func (c *Criu) Restore(opts rpc.CriuOpts, nfy CriuNotify) error { |
| 168 | + return c.doSwrk(rpc.CriuReqType_RESTORE, &opts, nfy) |
| 169 | +} |
| 170 | + |
| 171 | +func (c *Criu) PreDump(opts rpc.CriuOpts, nfy CriuNotify) error { |
| 172 | + return c.doSwrk(rpc.CriuReqType_PRE_DUMP, &opts, nfy) |
| 173 | +} |
| 174 | + |
| 175 | +func (c *Criu) StartPageServer(opts rpc.CriuOpts) error { |
| 176 | + return c.doSwrk(rpc.CriuReqType_PAGE_SERVER, &opts, nil) |
| 177 | +} |
0 commit comments