1// Copyright 2019 The SwiftShader Authors. All Rights Reserved. 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// Package shell provides functions for running sub-processes. 16package shell 17 18import ( 19 "fmt" 20 "time" 21) 22 23// MaxProcMemory is the maximum virtual memory per child process. 24// Note: This is not used on Windows, as there is no sensible way to limit 25// process memory. 26var MaxProcMemory uint64 = 6 * 1024 * 1024 * 1024 // 6 GiB 27 28// Shell runs the executable exe with the given arguments, in the working 29// directory wd. 30// If the process does not finish within timeout a errTimeout will be returned. 31func Shell(timeout time.Duration, exe, wd string, args ...string) error { 32 return Env(timeout, exe, wd, nil, args...) 33} 34 35// Env runs the executable exe with the given arguments, in the working 36// directory wd, with the custom env. 37// If the process does not finish within timeout a errTimeout will be returned. 38func Env(timeout time.Duration, exe, wd string, env []string, args ...string) error { 39 if out, err := Exec(timeout, exe, wd, env, "", args...); err != nil { 40 return fmt.Errorf("%s\n%w", out, err) 41 } 42 return nil 43} 44 45// ErrTimeout is the error returned when a process does not finish with its 46// permitted time. 47type ErrTimeout struct { 48 process string 49 timeout time.Duration 50} 51 52func (e ErrTimeout) Error() string { 53 return fmt.Sprintf("'%v' did not return after %v", e.process, e.timeout) 54} 55