func openLog(ctx context.Context, id string) (io.Writer, error) {
	ns, err := namespaces.NamespaceRequired(ctx)
	if err != nil {
		return nil, err
	}
	dswl := &deferredShimWriteLogger{
		ctx:    ctx,
		buffer: newBlockingBuffer(64 * 1024),  
	}
	l, err := winio.ListenPipe(fmt.Sprintf("\\\\.\\pipe\\containerd-shim-%s-%s-log", ns, id), nil)
	if err != nil {
		return nil, err
	}
	dswl.l = l
	go dswl.beginAccept()
	return dswl, nil
}
openLog on Windows acts as the server of the log pipe. This allows the
containerd daemon to independently restart and reconnect to the logs.
package shim
import (
	"bytes"
	"context"
	"fmt"
	"io"
	"net"
	"os"
	"sync"
	"unsafe"
	winio "github.com/Microsoft/go-winio"
	"github.com/containerd/containerd/namespaces"
	"github.com/containerd/ttrpc"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"golang.org/x/sys/windows"
)
func setupSignals(config Config) (chan os.Signal, error) {
	signals := make(chan os.Signal, 32)
	return signals, nil
}
func newServer() (*ttrpc.Server, error) {
	return ttrpc.NewServer()
}
func subreaper() error {
	return nil
}
type fakeSignal struct {
}
func (fs *fakeSignal) String() string {
	return ""
}
func (fs *fakeSignal) Signal() {
}
func setupDumpStacks(dump chan<- os.Signal) {
	event := "Global\\containerd-shim-runhcs-v1-" + fmt.Sprint(os.Getpid())
	ev, _ := windows.UTF16PtrFromString(event)
	sd, err := winio.SddlToSecurityDescriptor("D:P(A;;GA;;;BA)(A;;GA;;;SY)")
	if err != nil {
		logrus.Errorf("failed to get security descriptor for debug stackdump event %s: %s", event, err.Error())
		return
	}
	var sa windows.SecurityAttributes
	sa.Length = uint32(unsafe.Sizeof(sa))
	sa.InheritHandle = 1
	sa.SecurityDescriptor = uintptr(unsafe.Pointer(&sd[0]))
	h, err := windows.CreateEvent(&sa, 0, 0, ev)
	if h == 0 || err != nil {
		logrus.Errorf("failed to create debug stackdump event %s: %s", event, err.Error())
		return
	}
	go func() {
		logrus.Debugf("Stackdump - waiting signal at %s", event)
		for {
			windows.WaitForSingleObject(h, windows.INFINITE)
			dump <- new(fakeSignal)
		}
	}()
}
func serveListener(path string) (net.Listener, error) {
	if path == "" {
		return nil, errors.New("'socket' must be npipe path")
	}
	l, err := winio.ListenPipe(path, nil)
	if err != nil {
		return nil, err
	}
	logrus.WithField("socket", path).Debug("serving api on npipe socket")
	return l, nil
}
func handleSignals(ctx context.Context, logger *logrus.Entry, signals chan os.Signal) error {
	logger.Info("starting signal loop")
	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		case s := <-signals:
			switch s {
			case os.Interrupt:
				return nil
			}
		}
	}
}
var _ = (io.WriterTo)(&blockingBuffer{})
var _ = (io.Writer)(&blockingBuffer{})
type blockingBuffer struct {
	c *sync.Cond
	capacity int
	buffer bytes.Buffer
}
func newBlockingBuffer(capacity int) *blockingBuffer {
	return &blockingBuffer{
		c:        sync.NewCond(&sync.Mutex{}),
		capacity: capacity,
	}
}
func (bb *blockingBuffer) Len() int {
	bb.c.L.Lock()
	defer bb.c.L.Unlock()
	return bb.buffer.Len()
}
func (bb *blockingBuffer) Write(p []byte) (int, error) {
	if len(p) > bb.capacity {
		return 0, errors.Errorf("len(p) (%d) too large for capacity (%d)", len(p), bb.capacity)
	}
	bb.c.L.Lock()
	for bb.buffer.Len()+len(p) > bb.capacity {
		bb.c.Wait()
	}
	defer bb.c.L.Unlock()
	return bb.buffer.Write(p)
}
func (bb *blockingBuffer) WriteTo(w io.Writer) (int64, error) {
	bb.c.L.Lock()
	defer bb.c.L.Unlock()
	defer bb.c.Signal()
	return bb.buffer.WriteTo(w)
}
type deferredShimWriteLogger struct {
	mu sync.Mutex
	ctx context.Context
	connected bool
	aborted   bool
	buffer *blockingBuffer
	l      net.Listener
	c      net.Conn
	conerr error
}
func (dswl *deferredShimWriteLogger) beginAccept() {
	dswl.mu.Lock()
	if dswl.connected {
		return
	}
	dswl.mu.Unlock()
	c, err := dswl.l.Accept()
	if err == winio.ErrPipeListenerClosed {
		dswl.mu.Lock()
		dswl.aborted = true
		dswl.l.Close()
		dswl.conerr = errors.New("connection closed")
		dswl.mu.Unlock()
		return
	}
	dswl.mu.Lock()
	dswl.connected = true
	dswl.c = c
	if dswl.buffer.Len() > 0 {
		_, err := dswl.buffer.WriteTo(dswl.c)
		if err != nil {
			dswl.connected = false
			dswl.c.Close()
			go dswl.beginAccept()
		}
	}
	dswl.mu.Unlock()
}
func (dswl *deferredShimWriteLogger) Write(p []byte) (int, error) {
	dswl.mu.Lock()
	defer dswl.mu.Unlock()
	if dswl.aborted {
		return 0, dswl.conerr
	}
	if dswl.connected {
		written, err := dswl.c.Write(p)
		if err != nil {
			dswl.connected = false
			dswl.c.Close()
			go dswl.beginAccept()
			if written != len(p) {
				w, err := dswl.buffer.Write(p[written:])
				if err != nil {
					return written + w, err
				}
				written += w
			}
		}
		return written, nil
	}
	return dswl.buffer.Write(p)
}