github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/client/socketservice/socketservice_windows.go (about)

     1  // Copyright 2017 Google Inc.
     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  //     https://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 windows
    16  
    17  package socketservice
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"os"
    23  	"path/filepath"
    24  
    25  	log "github.com/golang/glog"
    26  
    27  	"github.com/google/fleetspeak/fleetspeak/src/client/socketservice/checks"
    28  	"github.com/google/fleetspeak/fleetspeak/src/windows/wnixsocket"
    29  	"github.com/hectane/go-acl"
    30  )
    31  
    32  func listen(socketPath string) (net.Listener, error) {
    33  	parent := filepath.Dir(socketPath)
    34  
    35  	// Ensure that the parent directory exists. wnixsocket.Listen ensures the
    36  	// socket file exists and is truncated.
    37  	stat, err := os.Lstat(parent)
    38  	if err != nil && !os.IsNotExist(err) {
    39  		return nil, fmt.Errorf("os.Lstat failed: %v", err)
    40  	}
    41  	if err == nil {
    42  		log.Infof("wnix socket parent %v already exists (IsDir: %v)", parent, stat.Mode().IsDir())
    43  	}
    44  
    45  	if err := os.MkdirAll(parent, 0); err != nil {
    46  		return nil, fmt.Errorf("os.MkdirAll failed: %v", err)
    47  	}
    48  
    49  	// MkdirAll doesn't set mode as expected on Windows, so we make
    50  	// sure with Chmod. Note that os.Chmod also doesn't work as expected, so
    51  	// we use go-acl.
    52  	if err := acl.Chmod(parent, 0700); err != nil {
    53  		return nil, fmt.Errorf("failed to chmod a Wnix domain listener's parent directory: %v", err)
    54  	}
    55  
    56  	l, err := wnixsocket.Listen(socketPath)
    57  	if err != nil {
    58  		return nil, fmt.Errorf("failed to create a Wnix domain listener: %v", err)
    59  	}
    60  
    61  	if err := checks.CheckSocketFile(socketPath); err != nil {
    62  		return nil, fmt.Errorf("CheckSocketFile(...): %v", err)
    63  	}
    64  
    65  	return l, nil
    66  }