github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/shim/proc/utils.go (about)

     1  // Copyright 2018 The containerd Authors.
     2  // Copyright 2018 The gVisor Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     https://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package proc
    17  
    18  import (
    19  	"encoding/json"
    20  	"io"
    21  	"os"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/SagerNet/gvisor/pkg/shim/runsc"
    26  )
    27  
    28  const (
    29  	internalErrorCode = 128
    30  	bufferSize        = 32
    31  )
    32  
    33  // ExitCh is the exit events channel for containers and exec processes
    34  // inside the sandbox.
    35  var ExitCh = make(chan Exit, bufferSize)
    36  
    37  // TODO(mlaventure): move to runc package?
    38  func getLastRuntimeError(r *runsc.Runsc) (string, error) {
    39  	if r.Log == "" {
    40  		return "", nil
    41  	}
    42  
    43  	f, err := os.OpenFile(r.Log, os.O_RDONLY, 0400)
    44  	if err != nil {
    45  		return "", err
    46  	}
    47  
    48  	var (
    49  		errMsg string
    50  		log    struct {
    51  			Level string
    52  			Msg   string
    53  			Time  time.Time
    54  		}
    55  	)
    56  
    57  	dec := json.NewDecoder(f)
    58  	for err = nil; err == nil; {
    59  		if err = dec.Decode(&log); err != nil && err != io.EOF {
    60  			return "", err
    61  		}
    62  		if log.Level == "error" {
    63  			errMsg = strings.TrimSpace(log.Msg)
    64  		}
    65  	}
    66  
    67  	return errMsg, nil
    68  }
    69  
    70  func hasNoIO(r *CreateConfig) bool {
    71  	return r.Stdin == "" && r.Stdout == "" && r.Stderr == ""
    72  }