• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	"../cause"
23)
24
25// MaxProcMemory is the maximum virtual memory per child process.
26// Note: This is not used on Windows, as there is no sensible way to limit
27// process memory.
28var MaxProcMemory uint64 = 6 * 1024 * 1024 * 1024 // 6 GiB
29
30// Shell runs the executable exe with the given arguments, in the working
31// directory wd.
32// If the process does not finish within timeout a errTimeout will be returned.
33func Shell(timeout time.Duration, exe, wd string, args ...string) error {
34	return Env(timeout, exe, wd, nil, args...)
35}
36
37// Env runs the executable exe with the given arguments, in the working
38// directory wd, with the custom env.
39// If the process does not finish within timeout a errTimeout will be returned.
40func Env(timeout time.Duration, exe, wd string, env []string, args ...string) error {
41	if out, err := Exec(timeout, exe, wd, env, args...); err != nil {
42		return cause.Wrap(err, "%s", out)
43	}
44	return nil
45}
46
47// ErrTimeout is the error returned when a process does not finish with its
48// permitted time.
49type ErrTimeout struct {
50	process string
51	timeout time.Duration
52}
53
54func (e ErrTimeout) Error() string {
55	return fmt.Sprintf("'%v' did not return after %v", e.process, e.timeout)
56}
57