运行目标程序
(ctx context.Context, session *JudgeSession, rst *commonStructs.TestCaseResult, isChecker bool)
| 66 | |
| 67 | // 运行目标程序 |
| 68 | func runAsync(ctx context.Context, session *JudgeSession, rst *commonStructs.TestCaseResult, isChecker bool) (*ProcessInfo, error) { |
| 69 | var err error |
| 70 | |
| 71 | runSuccess := make(chan bool, 1) |
| 72 | pid := 0 |
| 73 | pinfo := ProcessInfo{} |
| 74 | |
| 75 | go func() { |
| 76 | var pstate *cmd.ProcessState |
| 77 | var pArgs *PArgs |
| 78 | var proc *cmd.Process |
| 79 | // Get process options |
| 80 | pArgs, err = getProcessOptions(session, rst, isChecker, false, nil) |
| 81 | if err != nil { |
| 82 | runSuccess <- false |
| 83 | return |
| 84 | } |
| 85 | // Start process |
| 86 | proc, err = cmd.StartProcess(pArgs.Name, pArgs.Args, pArgs.Attr) |
| 87 | if err != nil { |
| 88 | runSuccess <- false |
| 89 | return |
| 90 | } |
| 91 | // Collect process info |
| 92 | pinfo.Process = proc |
| 93 | pinfo.Pid = proc.Pid |
| 94 | pid = proc.Pid |
| 95 | //log.Printf("Start process (%d)...\n", pinfo.Pid) |
| 96 | // Wait for exit. |
| 97 | pstate, err = proc.Wait() |
| 98 | if err != nil { |
| 99 | runSuccess <- false |
| 100 | return |
| 101 | } |
| 102 | //log.Printf("Process (%d) exited.\n", pinfo.Pid) |
| 103 | pinfo.Status = pstate.Sys().(syscall.WaitStatus) |
| 104 | pinfo.Rusage = pstate.SysUsage().(*syscall.Rusage) |
| 105 | if pinfo.Rusage == nil { |
| 106 | err = errors.Errorf("get rusage failed") |
| 107 | runSuccess <- false |
| 108 | return |
| 109 | } |
| 110 | closeFiles(pArgs.Attr.Files) |
| 111 | runSuccess <- true |
| 112 | }() |
| 113 | for { |
| 114 | select { |
| 115 | case <-runSuccess: |
| 116 | goto finish |
| 117 | case <-ctx.Done(): // 触发超时 |
| 118 | log.Println("Child process timeout!") |
| 119 | err = errors.Errorf("Child process timeout!") |
| 120 | goto doClean |
| 121 | } |
| 122 | } |
| 123 | doClean: |
| 124 | if pid > 0 { |
| 125 | _ = syscall.Kill(pid, syscall.SIGKILL) |
no test coverage detected