duane2364 2018-06-25 13:20
浏览 129
已采纳

拨号unixgram:没有此类文件或目录错误

I've written a small library that talks to wpa_supplicant. I've verified it works with a test application but I wanted to add a unit test as well. My unit test is not able to connect to the unix socket. I get the error 'no such file or directory' but both socket files get created.

lib.go

package libwpa

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "net"
    "os"
    "strconv"
    "strings"
)

type Conn struct {
    uconn     *net.UnixConn //Linux specific
    localSock *os.File
}

func Connect(usock string) (*Conn, error) {
    var (
        uc  *Conn
        err error
    )
    uc = &Conn{}

    if uc.localSock, err = ioutil.TempFile("/tmp", "wpa_supplicant"); err != nil {
        return uc, err
    }
    os.Remove(uc.localSock.Name())

    uc.uconn, err = net.DialUnix("unixgram",
        &net.UnixAddr{Name: uc.localSock.Name(), Net: "unixgram"},
        &net.UnixAddr{Name: usock, Net: "unixgram"})
    if err != nil {
        return uc, err
    }

    return uc, nil
}

lib_test.go

package libwpa

import (
    "fmt"
    "io/ioutil"
    "net"
    "os"
    "testing"
)

func listen(reply chan<- []byte) {
    conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: "/tmp/foobar", Net: "unixgram"})
    if err != nil {
        fmt.Printf("failed to listen: %v
", err)
        panic(err)
    }
    defer os.Remove("/tmp/foobar")

    f, _ := ioutil.ReadDir("/tmp")
    for _, d := range f {
        fmt.Printf("%v
", d.Name())
    }

    buf := make([]byte, 2048)
    n, uaddr, err := conn.ReadFromUnix(buf)
    if err != nil {
        fmt.Printf("LISTEN: Error: %v
", err)
    } else {
        fmt.Printf("LISTEN: received %v bytes from %+v
", n, uaddr)
        fmt.Printf("LISTEN: %v
", string(buf))
    }

    conn.Close()
    reply <- buf
}

func Test_Connect(t *testing.T) {
    reply := make(chan []byte, 2)
    go listen(reply)
    _, err := Connect("/tmp/foobar")
    if err != nil {
        t.Fatalf("Failed to connect: %v", err)
    }
}

Running it I get

$ go test                                                                                                                                                                                                                                                    
--- FAIL: Test_Connect (0.00s)
        lib_test.go:42: Failed to connect: dial unixgram /tmp/wpa_supplicant208023735->/tmp/foobar: connect: no such file or directory
FAIL
exit status 1
FAIL    _/home/code/apps/go/src/crown/libwpa 0.006s

$ ls /tmp/{foobar,wpa*}                                                                                                                                                                                                                                        
/tmp/foobar=  /tmp/wpa_supplicant923064975=
  • 写回答

2条回答 默认 最新

  • dongzhilian0188 2018-07-12 15:22
    关注

    My issue turned out to be a race condition in lib_test.go. I had a goroutine to create my listen socket and then immediately called Connect() from the library, but the listen socket wasn't ready yet. Adding a small delay fixed the issue.

    Original:

    go listen(reply)
    _, err := Connect("/tmp/foobar")
    

    Fix:

    go listen(reply)
    time.Sleep(time.Second * 1)
    _, err := Connect("/tmp/foobar")
    

    I know there are better long term solutions but this at least got me rolling again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?