go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butlerlib/streamclient/protocol_unix.go (about)

     1  // Copyright 2017 The LUCI Authors.
     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  //go:build unix
    16  // +build unix
    17  
    18  package streamclient
    19  
    20  import (
    21  	"io"
    22  	"net"
    23  	"os"
    24  
    25  	"go.chromium.org/luci/common/errors"
    26  	"go.chromium.org/luci/logdog/client/butlerlib/streamproto"
    27  )
    28  
    29  type unixDialer struct {
    30  	path string
    31  }
    32  
    33  var _ dialer = (*unixDialer)(nil)
    34  
    35  func (u *unixDialer) conn(f streamproto.Flags) (*net.UnixConn, error) {
    36  	conn, err := net.DialUnix("unix", nil, &net.UnixAddr{Net: "unix", Name: u.path})
    37  	if err != nil {
    38  		return nil, errors.Annotate(err, "opening socket %q", u.path).Err()
    39  	}
    40  	if err = f.WriteHandshake(conn); err != nil {
    41  		conn.Close()
    42  		return nil, errors.Annotate(err, "writing handshake").Err()
    43  	}
    44  	return conn, nil
    45  }
    46  
    47  func (u *unixDialer) DialStream(forProcess bool, f streamproto.Flags) (io.WriteCloser, error) {
    48  	conn, err := u.conn(f)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	if !forProcess {
    54  		return conn, nil
    55  	}
    56  
    57  	fd, err := conn.File()
    58  	// either File dup'd our connection, or it failed; either way conn must be
    59  	// closed here.
    60  	conn.Close()
    61  	return fd, errors.Annotate(err, "converting to os.File").Err()
    62  }
    63  
    64  func (u *unixDialer) DialDgramStream(f streamproto.Flags) (DatagramStream, error) {
    65  	conn, err := u.conn(f)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	return &datagramStreamWriter{conn}, nil
    70  }
    71  
    72  func init() {
    73  	protocolRegistry["unix"] = func(address string) (dialer, error) {
    74  		// Ensure that the supplied address exists and is a named pipe.
    75  		info, err := os.Lstat(address)
    76  		if err != nil {
    77  			return nil, errors.Reason("failed to stat file [%s]: %s", address, err).Err()
    78  		}
    79  		if info.Mode()&os.ModeSocket == 0 {
    80  			return nil, errors.Reason("not a named pipe: [%s]", address).Err()
    81  		}
    82  		return &unixDialer{address}, nil
    83  	}
    84  }