Skip to content

Commit

Permalink
Read max 1MB data from panic.log
Browse files Browse the repository at this point in the history
Panic.log file can get very large if there are other log statements writing to
stderr. Avoid reading the entire file

Signed-off-by: Amit Barve <[email protected]>
  • Loading branch information
ambarve committed May 18, 2021
1 parent 3b82d41 commit e174824
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions cmd/containerd-shim-runhcs-v1/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
gcontext "context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
Expand All @@ -13,11 +11,34 @@ import (
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/containerd/containerd/runtime/v2/task"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"go.opencensus.io/trace"
)

// LimitedRead reads at max `readLimitBytes` bytes from the file at path `filePath`. If the file has
// more than `readLimitBytes` bytes of data then first `readLimitBytes` will be returned.
func limitedRead(filePath string, readLimitBytes int64) ([]byte, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, errors.Wrapf(err, "limited read failed to open file: %s", filePath)
}
defer f.Close()
if fi, err := f.Stat(); err == nil {
if fi.Size() < readLimitBytes {
readLimitBytes = fi.Size()
}
buf := make([]byte, readLimitBytes)
_, err := f.Read(buf)
if err != nil {
return []byte{}, errors.Wrapf(err, "limited read failed during file read: %s", filePath)
}
return buf, nil
}
return []byte{}, errors.Wrapf(err, "limited read failed during file stat: %s", filePath)
}

var deleteCommand = cli.Command{
Name: "delete",
Usage: `
Expand All @@ -44,9 +65,16 @@ The delete command will be executed in the container's bundle as its cwd.
// log those messages (if any) on stderr so that it shows up in containerd's log.
// This should be done as the first thing so that we don't miss any panic logs even if
// something goes wrong during delete op.
logs, err := ioutil.ReadFile(filepath.Join(bundleFlag, "panic.log"))
if err == nil && len(logs) > 0 {
logrus.WithField("log", string(logs)).Error("found shim panic logs during delete")
// The file can be very large so read only first 1MB of data.
readLimit := int64(1024 * 1024) // 1MB
logBytes, err := limitedRead(filepath.Join(bundleFlag, "panic.log"), readLimit)
if err == nil {
if int64(len(logBytes)) == readLimit {
logrus.Warnf("shim panic log file %s is larger than 1MB, logging only first 1MB", filepath.Join(bundleFlag, "panic.log"))
}
logrus.WithField("log", string(logBytes)).Warn("found shim panic logs during delete")
} else if !errors.As(err, &os.ErrNotExist) {
logrus.WithError(err).Warn("failed to open shim panic log")
}

// Attempt to find the hcssystem for this bundle and terminate it.
Expand Down

0 comments on commit e174824

Please sign in to comment.