func()

in google_guest_agent/events/sshtrustedca/sshtrustedca_linux.go [81:142]


func (mp *Watcher) Run(ctx context.Context, evType string) (bool, interface{}, error) {
	var canceled atomic.Bool

	for mp.isWaitingWrite() {
		time.Sleep(10 * time.Millisecond)
	}

	// Channel used to cancel the context cancelation go routine.
	// Used when the Watcher is returning to the event manager.
	cancelContext := make(chan bool)
	defer close(cancelContext)

	// Cancelation handling code.
	go func() {
		select {
		case <-cancelContext:
			break
		case <-ctx.Done():
			canceled.Store(true)

			// Open the pipe as O_RDONLY to release the blocking open O_WRONLY.
			pipeFile, err := os.OpenFile(mp.pipePath, os.O_RDONLY, 0644)
			if err != nil {
				logger.Errorf("Failed to open readonly pipe: %+v", err)
				return
			}

			defer func() {
				if err := pipeFile.Close(); err != nil {
					logger.Errorf("Failed to close readonly pipe: %+v", err)
				}
			}()
		}
	}()

	// If the configured named pipe doesn't exists we create it before emitting events
	// from it.
	if err := createNamedPipe(ctx, mp.pipePath); err != nil {
		return true, nil, err
	}

	// Open the pipe as writeonly, it will block until a read is performed from the
	// other end of the pipe.
	pipeFile, err := os.OpenFile(mp.pipePath, os.O_WRONLY, 0644)
	if err != nil {
		return true, nil, err
	}

	// Have we got a ctx.Done()? if so lets just return from here and unregister
	// the watcher.
	if canceled.Load() {
		if err := pipeFile.Close(); err != nil {
			logger.Errorf("Failed to close readonly pipe: %+v", err)
		}
		return false, nil, nil
	}

	cancelContext <- true
	mp.setWaitingWrite(true)

	return true, &PipeData{File: pipeFile, Finished: mp.finishedCb}, nil
}