github.com/tri-adam/singularity@v3.1.1+incompatible/internal/app/singularity/oci_pause_linux.go (about) 1 // Copyright (c) 2018-2019, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package singularity 7 8 import ( 9 "encoding/json" 10 "fmt" 11 "io" 12 13 "github.com/sylabs/singularity/pkg/ociruntime" 14 "github.com/sylabs/singularity/pkg/util/unix" 15 ) 16 17 // OciPauseResume pauses/resumes processes in a container 18 func OciPauseResume(containerID string, pause bool) error { 19 state, err := getState(containerID) 20 if err != nil { 21 return err 22 } 23 24 if state.ControlSocket == "" { 25 return fmt.Errorf("can't find control socket") 26 } 27 28 if pause && state.Status != ociruntime.Running { 29 return fmt.Errorf("container %s is not running", containerID) 30 } else if !pause && state.Status != ociruntime.Paused { 31 return fmt.Errorf("container %s is not paused", containerID) 32 } 33 34 ctrl := &ociruntime.Control{} 35 if pause { 36 ctrl.Pause = true 37 } else { 38 ctrl.Resume = true 39 } 40 41 c, err := unix.Dial(state.ControlSocket) 42 if err != nil { 43 return fmt.Errorf("failed to connect to control socket") 44 } 45 defer c.Close() 46 47 enc := json.NewEncoder(c) 48 if err != nil { 49 return err 50 } 51 52 if err := enc.Encode(ctrl); err != nil { 53 return err 54 } 55 56 // wait runtime close socket connection for ACK 57 d := make([]byte, 1) 58 if _, err := c.Read(d); err != io.EOF { 59 return err 60 } 61 62 // check status 63 state, err = getState(containerID) 64 if err != nil { 65 return err 66 } 67 if pause && state.Status != ociruntime.Paused { 68 return fmt.Errorf("bad status %s returned instead of paused", state.Status) 69 } else if !pause && state.Status != ociruntime.Running { 70 return fmt.Errorf("bad status %s returned instead of running", state.Status) 71 } 72 73 return nil 74 }