1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package unix
import (
"fmt"
"net"
"os"
"path/filepath"
"runtime"
"syscall"
)
// Listen wraps net.Listen to handle 108 characters issue
func Listen(path string) (net.Listener, error) {
socket := path
if len(path) >= 108 {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get current working directory: %s", err)
}
defer os.Chdir(cwd)
dir := filepath.Dir(path)
socket = filepath.Base(path)
if err := os.Chdir(dir); err != nil {
return nil, fmt.Errorf("failed to go into %s: %s", dir, err)
}
}
return net.Listen("unix", socket)
}
// Dial wraps net.Dial to handle 108 characters issue
func Dial(path string) (net.Conn, error) {
socket := path
if len(path) >= 108 {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get current working directory: %s", err)
}
defer os.Chdir(cwd)
dir := filepath.Dir(path)
socket = filepath.Base(path)
if err := os.Chdir(dir); err != nil {
return nil, fmt.Errorf("failed to go into %s: %s", dir, err)
}
}
return net.Dial("unix", socket)
}
// CreateSocket creates an unix socket and returns connection listener.
func CreateSocket(path string) (net.Listener, error) {
oldmask := syscall.Umask(0o177)
defer syscall.Umask(oldmask)
return Listen(path)
}
// WriteSocket writes data over unix socket
func WriteSocket(path string, data []byte) error {
c, err := Dial(path)
if err != nil {
return fmt.Errorf("failed to connect to %s socket: %s", path, err)
}
defer c.Close()
if _, err := c.Write(data); err != nil {
return fmt.Errorf("failed to send data over socket: %s", err)
}
return nil
}
|