gpt4 book ai didi

ubuntu - 为什么 networkmanager.WatchState 出现 panic ?

转载 作者:IT王子 更新时间:2023-10-29 02:23:54 30 4
gpt4 key购买 nike

我需要观察网络变化并在网络状态发生变化时设置一些路由规则(例如,如果接口(interface)重新启动我需要再次设置 ip 路由表 b/c src 是动态的所以我不能硬编码它) . 为此,我尝试使用 networkmanager包,但 WatchState panic 。知道为什么吗?

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 踪迹

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

编辑: 程序似乎对来自网络管理器的任何方法都发生了 panic 。 GetState panic 如下。

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()

最佳答案

我自己没有使用过这个包,但是通过检查 the source code ,似乎您应该在 networkmanager.New 之前调用 endp.Dial,否则 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)

关于ubuntu - 为什么 networkmanager.WatchState 出现 panic ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31204482/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com