1*03ce13f7SAndroid Build Coastguard Worker// Copyright 2019 The SwiftShader Authors. All Rights Reserved. 2*03ce13f7SAndroid Build Coastguard Worker// 3*03ce13f7SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*03ce13f7SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*03ce13f7SAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*03ce13f7SAndroid Build Coastguard Worker// 7*03ce13f7SAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*03ce13f7SAndroid Build Coastguard Worker// 9*03ce13f7SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*03ce13f7SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*03ce13f7SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*03ce13f7SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*03ce13f7SAndroid Build Coastguard Worker// limitations under the License. 14*03ce13f7SAndroid Build Coastguard Worker 15*03ce13f7SAndroid Build Coastguard Worker// Package shell provides functions for running sub-processes. 16*03ce13f7SAndroid Build Coastguard Workerpackage shell 17*03ce13f7SAndroid Build Coastguard Worker 18*03ce13f7SAndroid Build Coastguard Workerimport ( 19*03ce13f7SAndroid Build Coastguard Worker "fmt" 20*03ce13f7SAndroid Build Coastguard Worker "time" 21*03ce13f7SAndroid Build Coastguard Worker) 22*03ce13f7SAndroid Build Coastguard Worker 23*03ce13f7SAndroid Build Coastguard Worker// MaxProcMemory is the maximum virtual memory per child process. 24*03ce13f7SAndroid Build Coastguard Worker// Note: This is not used on Windows, as there is no sensible way to limit 25*03ce13f7SAndroid Build Coastguard Worker// process memory. 26*03ce13f7SAndroid Build Coastguard Workervar MaxProcMemory uint64 = 6 * 1024 * 1024 * 1024 // 6 GiB 27*03ce13f7SAndroid Build Coastguard Worker 28*03ce13f7SAndroid Build Coastguard Worker// Shell runs the executable exe with the given arguments, in the working 29*03ce13f7SAndroid Build Coastguard Worker// directory wd. 30*03ce13f7SAndroid Build Coastguard Worker// If the process does not finish within timeout a errTimeout will be returned. 31*03ce13f7SAndroid Build Coastguard Workerfunc Shell(timeout time.Duration, exe, wd string, args ...string) error { 32*03ce13f7SAndroid Build Coastguard Worker return Env(timeout, exe, wd, nil, args...) 33*03ce13f7SAndroid Build Coastguard Worker} 34*03ce13f7SAndroid Build Coastguard Worker 35*03ce13f7SAndroid Build Coastguard Worker// Env runs the executable exe with the given arguments, in the working 36*03ce13f7SAndroid Build Coastguard Worker// directory wd, with the custom env. 37*03ce13f7SAndroid Build Coastguard Worker// If the process does not finish within timeout a errTimeout will be returned. 38*03ce13f7SAndroid Build Coastguard Workerfunc Env(timeout time.Duration, exe, wd string, env []string, args ...string) error { 39*03ce13f7SAndroid Build Coastguard Worker if out, err := Exec(timeout, exe, wd, env, "", args...); err != nil { 40*03ce13f7SAndroid Build Coastguard Worker return fmt.Errorf("%s\n%w", out, err) 41*03ce13f7SAndroid Build Coastguard Worker } 42*03ce13f7SAndroid Build Coastguard Worker return nil 43*03ce13f7SAndroid Build Coastguard Worker} 44*03ce13f7SAndroid Build Coastguard Worker 45*03ce13f7SAndroid Build Coastguard Worker// ErrTimeout is the error returned when a process does not finish with its 46*03ce13f7SAndroid Build Coastguard Worker// permitted time. 47*03ce13f7SAndroid Build Coastguard Workertype ErrTimeout struct { 48*03ce13f7SAndroid Build Coastguard Worker process string 49*03ce13f7SAndroid Build Coastguard Worker timeout time.Duration 50*03ce13f7SAndroid Build Coastguard Worker} 51*03ce13f7SAndroid Build Coastguard Worker 52*03ce13f7SAndroid Build Coastguard Workerfunc (e ErrTimeout) Error() string { 53*03ce13f7SAndroid Build Coastguard Worker return fmt.Sprintf("'%v' did not return after %v", e.process, e.timeout) 54*03ce13f7SAndroid Build Coastguard Worker} 55