How to use runscCmd method of gvisor Package

Best Syzkaller code snippet using gvisor.runscCmd

gvisor.go

Source:gvisor.go Github

copy

Full Screen

...111 name: fmt.Sprintf("%v-%v", pool.env.Name, index),112 merger: merger,113 }114 // Kill the previous instance in case it's still running.115 osutil.Run(time.Minute, inst.runscCmd("delete", "-force", inst.name))116 time.Sleep(3 * time.Second)117 cmd := inst.runscCmd("run", "-bundle", bundleDir, inst.name)118 cmd.Stdout = wpipe119 cmd.Stderr = wpipe120 if err := cmd.Start(); err != nil {121 wpipe.Close()122 merger.Wait()123 return nil, err124 }125 inst.cmd = cmd126 wpipe.Close()127 if err := inst.waitBoot(); err != nil {128 inst.Close()129 return nil, err130 }131 return inst, nil132}133func (inst *instance) waitBoot() error {134 errorMsg := []byte("FATAL ERROR:")135 bootedMsg := []byte(initStartMsg)136 timeout := time.NewTimer(time.Minute)137 defer timeout.Stop()138 var output []byte139 for {140 select {141 case out := <-inst.merger.Output:142 output = append(output, out...)143 if pos := bytes.Index(output, errorMsg); pos != -1 {144 end := bytes.IndexByte(output[pos:], '\n')145 if end == -1 {146 end = len(output)147 } else {148 end += pos149 }150 return vmimpl.BootError{151 Title: string(output[pos:end]),152 Output: output,153 }154 }155 if bytes.Contains(output, bootedMsg) {156 return nil157 }158 case err := <-inst.merger.Err:159 return vmimpl.BootError{160 Title: fmt.Sprintf("runsc failed: %v", err),161 Output: output,162 }163 case <-timeout.C:164 return vmimpl.BootError{165 Title: "init process did not start",166 Output: output,167 }168 }169 }170}171func (inst *instance) runscCmd(add ...string) *exec.Cmd {172 args := []string{173 "-root", inst.rootDir,174 "-watchdog-action=panic",175 "-network=none",176 "-debug",177 "-alsologtostderr",178 }179 if inst.cfg.RunscArgs != "" {180 args = append(args, strings.Split(inst.cfg.RunscArgs, " ")...)181 }182 args = append(args, add...)183 cmd := osutil.Command(inst.image, args...)184 cmd.Env = []string{185 "GOTRACEBACK=all",186 "GORACE=halt_on_error=1",187 }188 return cmd189}190func (inst *instance) Close() {191 time.Sleep(3 * time.Second)192 osutil.Run(time.Minute, inst.runscCmd("delete", "-force", inst.name))193 inst.cmd.Process.Kill()194 inst.merger.Wait()195 inst.cmd.Wait()196 osutil.Run(time.Minute, inst.runscCmd("delete", "-force", inst.name))197 time.Sleep(3 * time.Second)198}199func (inst *instance) Forward(port int) (string, error) {200 if inst.port != 0 {201 return "", fmt.Errorf("forward port is already setup")202 }203 inst.port = port204 return "stdin", nil205}206func (inst *instance) Copy(hostSrc string) (string, error) {207 fname := filepath.Base(hostSrc)208 if err := osutil.CopyFile(hostSrc, filepath.Join(inst.imageDir, fname)); err != nil {209 return "", err210 }211 if err := os.Chmod(inst.imageDir, 0777); err != nil {212 return "", err213 }214 return filepath.Join("/", fname), nil215}216func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command string) (217 <-chan []byte, <-chan error, error) {218 args := []string{"exec", "-user=0:0"}219 for _, c := range sandboxCaps {220 args = append(args, "-cap", c)221 }222 args = append(args, inst.name)223 args = append(args, strings.Split(command, " ")...)224 cmd := inst.runscCmd(args...)225 rpipe, wpipe, err := osutil.LongPipe()226 if err != nil {227 return nil, nil, err228 }229 defer wpipe.Close()230 inst.merger.Add("cmd", rpipe)231 cmd.Stdout = wpipe232 cmd.Stderr = wpipe233 guestSock, err := inst.guestProxy()234 if err != nil {235 return nil, nil, err236 }237 if guestSock != nil {238 defer guestSock.Close()239 cmd.Stdin = guestSock240 }241 if err := cmd.Start(); err != nil {242 return nil, nil, err243 }244 errc := make(chan error, 1)245 signal := func(err error) {246 select {247 case errc <- err:248 default:249 }250 }251 go func() {252 select {253 case <-time.After(timeout):254 signal(vmimpl.ErrTimeout)255 case <-stop:256 signal(vmimpl.ErrTimeout)257 case err := <-inst.merger.Err:258 cmd.Process.Kill()259 if cmdErr := cmd.Wait(); cmdErr == nil {260 // If the command exited successfully, we got EOF error from merger.261 // But in this case no error has happened and the EOF is expected.262 err = nil263 }264 signal(err)265 return266 }267 cmd.Process.Kill()268 cmd.Wait()269 }()270 return inst.merger.Output, errc, nil271}272func (inst *instance) guestProxy() (*os.File, error) {273 if inst.port == 0 {274 return nil, nil275 }276 // One does not simply let gvisor guest connect to host tcp port.277 // We create a unix socket, pass it to guest in stdin.278 // Guest will use it instead of dialing manager directly.279 // On host we connect to manager tcp port and proxy between the tcp and unix connections.280 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)281 if err != nil {282 return nil, err283 }284 hostSock := os.NewFile(uintptr(socks[0]), "host unix proxy")285 guestSock := os.NewFile(uintptr(socks[1]), "guest unix proxy")286 conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%v", inst.port))287 if err != nil {288 hostSock.Close()289 guestSock.Close()290 return nil, err291 }292 go func() {293 io.Copy(hostSock, conn)294 hostSock.Close()295 }()296 go func() {297 io.Copy(conn, hostSock)298 conn.Close()299 }()300 return guestSock, nil301}302func (inst *instance) Diagnose() ([]byte, bool) {303 b, err := osutil.Run(time.Minute, inst.runscCmd("debug", "-stacks", inst.name))304 if err != nil {305 b = append(b, []byte(fmt.Sprintf("\n\nError collecting stacks: %v", err))...)306 }307 return b, false308}309func init() {310 if os.Getenv("SYZ_GVISOR_PROXY") != "" {311 fmt.Fprint(os.Stderr, initStartMsg)312 select {}313 }314}315const initStartMsg = "SYZKALLER INIT STARTED\n"316const configTempl = `317{...

Full Screen

Full Screen

runscCmd

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if len(args) < 1 {4 fmt.Println("Usage: runscCmd <command> [args]")5 os.Exit(1)6 }7 cmd := exec.Command("runsc", args...)8 cmd.SysProcAttr = &syscall.SysProcAttr{9 }10 if err := cmd.Run(); err != nil {11 fmt.Println("Error:", err)12 os.Exit(1)13 }14}15import (16func main() {17 if len(args) < 1 {18 fmt.Println("Usage: runscCmd <command> [args]")19 os.Exit(1)20 }21 cmd := exec.Command("runsc", args...)22 cmd.SysProcAttr = &syscall.SysProcAttr{23 }24 if err := cmd.Run(); err != nil {25 fmt.Println("Error:", err)26 os.Exit(1)27 }28}29import (30func main() {31 if len(args) < 1 {32 fmt.Println("Usage: runscCmd <command> [args]")33 os.Exit(1)34 }35 cmd := exec.Command("runsc", args...)36 cmd.SysProcAttr = &syscall.SysProcAttr{

Full Screen

Full Screen

runscCmd

Using AI Code Generation

copy

Full Screen

1import (2type gvisor struct {3}4func (gv *gvisor) runscCmd(cmd string) (string, error) {5 args = strings.Split(cmd, " ")6 out, err := exec.Command("/usr/local/bin/runsc", args...).CombinedOutput()7 if err != nil {8 }9 return string(out), nil10}11func main() {12 gv := gvisor{}13 out, err := gv.runscCmd("runsc --help")14 if err != nil {15 fmt.Println(err)16 os.Exit(1)17 }18 fmt.Println(out)19}20 kill Kill sends the specified signal (default: SIGTERM) to the container's init process21 stop Stops a running container with a grace period (default: 10s)

Full Screen

Full Screen

runscCmd

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 g := gvisor.New("/var/run/gvisor.sock")4 if err := g.RunscCmd("runsc", "run", "test"); err != nil {5 fmt.Println(err)6 }7}

Full Screen

Full Screen

runscCmd

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 gv := gvisor{}4 gv.runscCmd(image, diffPath)5}6import (7func (g *gvisor) runscCmd(image string, diffPath string) {8 cmd := exec.Command("runsc", "checkpoint", "-image-path", diffPath, image)9 file, err := os.Create(diffPath + "/gvisor.txt")10 if err != nil {11 fmt.Println(err)12 }13 defer file.Close()14 writer := bufio.NewWriter(file)15 err = cmd.Run()16 if err != nil {17 fmt.Println(err)18 }19 writer.Flush()20 output, err := exec.Command("cat", diffPath+"/gvisor.txt").Output()21 if err != nil {22 fmt.Println(err)23 }24 fmt.Println(string(output))25}

Full Screen

Full Screen

runscCmd

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 g := gvisor{}4 cmd := exec.Command("ls")5 err := g.runscCmd(cmd)6 if err != nil {7 fmt.Println(err)8 }9}10import (11func main() {12 g := gvisor{}13 cmd := exec.Command("ls")14 err := g.runscCmd(cmd)15 if err != nil {16 fmt.Println(err)17 }18}19import (20func main() {21 g := gvisor{}22 cmd := exec.Command("ls")23 err := g.runscCmd(cmd)24 if err != nil {25 fmt.Println(err)26 }27}28import (29func main() {30 g := gvisor{}31 cmd := exec.Command("ls")32 err := g.runscCmd(cmd)33 if err != nil {34 fmt.Println(err)35 }36}37import (38func main() {39 g := gvisor{}40 cmd := exec.Command("ls")41 err := g.runscCmd(cmd)42 if err != nil {43 fmt.Println(err)44 }45}46import (47func main()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful