每当网络状态发生变化时,我都需要观察网络变化并设置一些路由规则(例如,如果接口重启,我需要再次设置ip路由表b / c src是动态的,所以我不能硬编码它)。 为了达到这个目的,我试图使用Simple-query.php包但WatchState恐慌。知道为什么吗?
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: 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
编辑:该计划似乎对网络管理员的任何方法都感到恐慌。 GetState恐慌如下。
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()
答案 0 :(得分:0)
我自己没有使用过该套餐,但是在检查the source code时,您似乎应该在endp.Dial
之前致电networkmanager.New
,否则endpoint.proxy
会保持未初始化状态
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)