dongyijing2353
2015-07-03 10:24
浏览 92

为什么会出现networkmanager.WatchState恐慌?

I need to watch network changes and set some routing rules whenever the network status changes(e.g. if an interface is rebooted I need to set the ip routing tables again b/c the src is dynamic so I can't hardcode it). To achieve this I'm trying to use networkmanager package but WatchState panics. Any idea why?

package main

import(
    "flag"
    "os" 
    "launchpad.net/ubuntu-push/bus"
    "launchpad.net/ubuntu-push/bus/networkmanager"
    "launchpad.net/ubuntu-push/logger"

)

func main(){
    flag.Parse()

    lg := logger.NewSimpleLogger(os.Stderr, "info")
    endp := bus.SystemBus.Endpoint(networkmanager.BusAddress, lg)
    nm := networkmanager.New(endp, lg)
    watchState, cancel, err := nm.WatchState()
    if err !=nil{
        log.Printf("error %v", err)
        return
    }
    defer cancel.Cancel()
    for state := range watchState{
        log.Printf.("state is %v", state)
    }
}

Panic trace

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x488788]

    goroutine 1 [running]:
    launchpad.net/go-dbus/v1.(*ObjectProxy).WatchSignal(0x0, 0x616cd0, 0x1e, 0x5f0af0, 0xc, 0x7f9733d0e478, 0x0, 0x0)
        /root/go/src/launchpad.net/go-dbus/v1/dbus.go:97 +0x198
    launchpad.net/ubuntu-push/bus.(*endpoint).WatchSignal(0xc20803e0c0, 0x5f0af0, 0xc, 0xc20800a0e0, 0xc20802a530, 0x0, 0x0, 0x0, 0x0)
        /root/go/src/launchpad.net/ubuntu-push/bus/endpoint.go:133 +0x91
    launchpad.net/ubuntu-push/bus/networkmanager.(*networkManager).WatchState(0xc20800a0c0, 0x5bce80, 0x0, 0x0, 0x0, 0x0)
        /root/go/src/launchpad.net/ubuntu-push/bus/networkmanager/networkmanager.go:108 +0x1cf
    main.main()
        /root/go/src/github.com/vinkoo/net/main.go:16 +0x1f9

    goroutine 2 [runnable]:
    runtime.forcegchelper()
        /usr/local/go/src/runtime/proc.go:90
    runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1

    goroutine 3 [runnable]:
    runtime.bgsweep()
        /usr/local/go/src/runtime/mgc0.go:82
    runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1

    goroutine 4 [runnable]:
    runtime.runfinq()
        /usr/local/go/src/runtime/malloc.go:712
    runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
    exit status 2

Edit: It seems the program panics on any method from the networkmanager. GetState panics as follows.

goroutine 1 [running]:
launchpad.net/go-dbus/v1.(*ObjectProxy).Call(0x0, 0x616c90, 0x1f, 0x5e26d0, 0x3, 0xc20801d5a0, 0x2, 0x2, 0x7f13a95d0b50, 0x0, ...)
    /root/go/src/launchpad.net/go-dbus/v1/dbus.go:77 +0x1d1
launchpad.net/ubuntu-push/bus.(*endpoint).GetProperty(0xc20803e0c0, 0x5eac50, 0x5, 0x0, 0x0, 0x0, 0x0)
    /root/go/src/launchpad.net/ubuntu-push/bus/endpoint.go:169 +0x1cf
launchpad.net/ubuntu-push/bus/networkmanager.(*networkManager).GetState(0xc20800a0c0, 0x5bce80)
    /root/go/src/launchpad.net/ubuntu-push/bus/networkmanager/networkmanager.go:79 +0x62
main.main()
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsv73806 2015-07-03 11:03
    已采纳

    I haven't used the package myself, but from inspecting the source code, it seems like you should call endp.Dial before networkmanager.New because otherwise, endpoint.proxy stays uninitialised.

    endp := bus.SystemBus.Endpoint(networkmanager.BusAddress, lg)
    err := endp.Dial()
    if err != nil{
        log.Printf("dial error %v", err)
        return
    }
    defer endp.Close()
    nm := networkmanager.New(endp, lg)
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报